fix this i need every thread to read diffirent line#include <stdio.h> #include…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
fix this i need every thread to read diffirent line#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <fcntl.h> #include <sys/wait.h> #include <semaphore.h> #include <signal.h> #include <time.h>
#define NUM_THREADS 4 #define NUM_LINES 100 #define NUMBERS_PER_LINE 50
sem_t *semaphore; int global_sum = 0; int global_sum_per_line[NUM_LINES]; int global_sum_per_line2[NUM_LINES]; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct { FILE *fp; int start_line; int end_line; } ThreadArgs;
void generate_random_data(const char *filename) { FILE *fp = fopen(filename, "w"); if (!fp) { perror("File opening failed"); exit(EXIT_FAILURE); } int allthenumberstogether = 0; for (int i = 0; i < NUM_LINES; i++) { int tempsum = 0; for (int j = 0; j < NUMBERS_PER_LINE; j++) { int randomnumber = rand() % 101; fprintf(fp, "%d ", randomnumber); allthenumberstogether += randomnumber; tempsum += randomnumber; } global_sum_per_line[i] = tempsum; fprintf(fp, "\n"); } for (int i = 0; i < NUM_LINES; i++) { printf("Line %d sum: %d\n", i + 1, global_sum_per_line[i]); } printf("Total sum: %d\n", allthenumberstogether); fclose(fp); }
void *reader_thread(void *arg) { ThreadArgs *thread_args = (ThreadArgs *)arg; FILE *fp = thread_args->fp; int local_sum = 0; int num, count = 0;
for (int line = thread_args->start_line; line <= thread_args->end_line; line++) {
fseek(fp, line * (NUMBERS_PER_LINE + 1), SEEK_SET);
for (int j = 0; j < NUMBERS_PER_LINE; j++) {
if (fscanf(fp, "%d", &num) == 1) {
local_sum += num;
count++;
}
}
if (count == NUMBERS_PER_LINE) {
pthread_mutex_lock(&mutex);
global_sum_per_line2[line - 1] = local_sum;
global_sum += local_sum;
pthread_mutex_unlock(&mutex);
local_sum = 0;
count = 0;
}
}
for(int i = 0; i < NUM_LINES; i++) {
printf("2 Line %d sum: %d\n", i + 1, global_sum_per_line2[i]);
}
free(thread_args);
return NULL;
}
void signal_handler(int signum) { char response; printf("Received signal %d. Do you really want to terminate (y/n)? ", signum); scanf(" %c", &response);
if (response == 'y' || response == 'Y') {
// Clean-up resources
sem_close(semaphore);
sem_unlink("/mysemaphore");
exit(signum);
}
}
int main() { srand(time(NULL)); // Initialize semaphore semaphore = sem_open("/mysemaphore", O_CREAT, 0644, 0); if (semaphore == SEM_FAILED) { perror("sem_open failed"); exit(EXIT_FAILURE); }
// Install signal handlers
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
exit(EXIT_FAILURE);
}
if (pid > 0) { // Parent process - writer
generate_random_data("data.txt");
sem_post(semaphore); // Signal the child process that writing is done
wait(NULL); // Wait for the child process to finish
sem_close(semaphore);
sem_unlink("/mysemaphore");
} else { // Child process - reader
sem_wait(semaphore); // Wait for the parent process to finish writing
FILE *fp = fopen("data.txt", "r");
if (!fp) {
perror("File opening failed");
exit(EXIT_FAILURE);
}
pthread_t threads[NUM_THREADS];
ThreadArgs *thread_args_array[NUM_THREADS];
int line_range = NUM_LINES / NUM_THREADS;
for (int i = 0; i < NUM_THREADS; i++) {
ThreadArgs *thread_args = malloc(sizeof(ThreadArgs));
thread_args->fp = fp;
thread_args->start_line = i * line_range + 1;
thread_args->end_line = (i == NUM_THREADS - 1) ? NUM_LINES : (i + 1) * line_range;
thread_args_array[i] = thread_args;
if (pthread_create(&threads[i], NULL, reader_thread, (void *)thread_args) != 0) {
perror("Failed to create thread");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
fclose(fp);
printf("Total sum: %d\n", global_sum);
exit(EXIT_SUCCESS);
}
return 0;
}
The main issue with the code is that each thread is reading all the lines of the file instead of just their assigned portion. This is causing incorrect results and potential data races. To fix this, you need to update the reading loop in the reader_thread() function to only read the assigned lines for each thread.
Here's the updated reader_thread() function:
void *reader_thread(void *arg) {
ThreadArgs *thread_args = (ThreadArgs *)arg;
FILE *fp = thread_args->fp;
int local_sum = 0;
int num, count = 0;
for (int line = thread_args->start_line; line <= thread_args->end_line; line++) {
if (fseek(fp, line * (NUMBERS_PER_LINE + 1), SEEK_SET) != 0) {
perror("fseek failed");
continue;
}
count = 0;
for (int j = 0; j < NUMBERS_PER_LINE; j++) {
if (fscanf(fp, "%d", &num) == 1) {
local_sum += num;
count++;
}
}
if (count == NUMBERS_PER_LINE) {
pthread_mutex_lock(&mutex);
global_sum_per_line2[line - 1] = local_sum;
global_sum += local_sum;
pthread_mutex_unlock(&mutex);
local_sum = 0;
}
}
free(thread_args);
return NULL;
}
This change ensures that each thread is only reading its assigned lines from the file and updating the global sum accordingly.
Additionally, you might want to consider removing the print statements inside the reader_thread() function to avoid outputting the sum of each line multiple times. Instead, you can move the print statements for the global_sum_per_line2 array to the main() function, after all the threads have completed execution.
Here's the updated part of the main() function:
// ...
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
fclose(fp);
for(int i = 0; i < NUM_LINES; i++) {
printf("2 Line %d sum: %d\n", i + 1, global_sum_per_line2[i]);
}
printf("Total sum: %d\n", global_sum);
exit(EXIT_SUCCESS);
// ...
This change ensures that the output is consistent and not duplicated for each line sum.