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;
}
This issue seems to be caused by the way you're determining the start and end lines for each thread. You're using the fseek function to jump to a specific line in the file, but you're basing the offset on the number of numbers per line, not the actual number of bytes. This won't work because each number is likely to take up more than one byte. Additionally, newline characters also take up a byte.
Moreover, the way you're calculating the start and end lines for each thread is not correct. You're adding 1 to the start line but not to the end line, which means that, except for the first thread, each thread will start reading from the line after its intended start line.
Here is a revised version of your reader_thread function and the main function's thread creation section:
void *reader_thread(void *arg) {
ThreadArgs *thread_args = (ThreadArgs *)arg;
FILE *fp = thread_args->fp;
int local_sum = 0;
int num;
char *line = NULL;
size_t len = 0;
ssize_t read;
// Move to start line
for (int i = 0; i < thread_args->start_line; i++) {
if((read = getline(&line, &len, fp)) == -1) {
printf("Error reading line\n");
break;
}
}
// Read lines
for (int line_num = thread_args->start_line; line_num <= thread_args->end_line; line_num++) {
char *ptr = line;
for (int j = 0; j < NUMBERS_PER_LINE; j++) {
num = strtol(ptr, &ptr, 10);
local_sum += num;
}
pthread_mutex_lock(&mutex);
global_sum_per_line2[line_num] = local_sum;
global_sum += local_sum;
pthread_mutex_unlock(&mutex);
local_sum = 0;
if(line_num != thread_args->end_line) {
if((read = getline(&line, &len, fp)) == -1) {
printf("Error reading line\n");
break;
}
}
}
free(line);
free(thread_args);
return NULL;
}
And the thread creation section:
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;
thread_args->end_line = (i == NUM_THREADS - 1) ? NUM_LINES - 1 : (i + 1) * line_range - 1;
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);
}
}
This code uses getline to read each line and strtol to parse each number in the line. Note that this code assumes that each line in the file contains exactly NUMBERS_PER_LINE numbers.