using the semaphores make one thread at a time to read the file not all together because it makes problems:
#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_sum1[100];
int global_sum2[100];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct {
FILE *fp;
int start_line;
int end_line;
} thread_data_t;
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 local_sum = 0;
for (int j = 0; j < NUMBERS_PER_LINE; j++) {
int randomnumber = rand() % 101;
fprintf(fp, "%d ", randomnumber);
local_sum += randomnumber;
// allthenumberstogether+=randomnumber;
}
global_sum1[i]=local_sum;
allthenumberstogether+=local_sum;
fprintf(fp, "\n");
//print "global sum at line" for each line
printf("1 At writing: global sum at line %d: %d\n", i, global_sum1[i]);
}
printf("REALL Total sum: %d\n", allthenumberstogether);
fclose(fp);
}
// every thread read 25 lines the first starts from 0-24 the second from 25-49 the third from 50-74 the fourth from 75-99
void *reader_thread(void *arg) {
thread_data_t *data = (thread_data_t *)arg;
FILE *fp = data->fp;
int local_sum = 0;
int num, count = 0;
int current_line = 0;
printf(" ad %d\n", data->start_line);
// Move the file pointer to the start line
for (int i = 0; i < data->start_line * NUMBERS_PER_LINE; i++) {
fscanf(fp, "%d", &num);
}
printf("2 thread %d\n", data->start_line);
while (fscanf(fp, "%d", &num) == 1) {
local_sum += num;
count++;
if (count == NUMBERS_PER_LINE) {
fgetc(fp); // Read the newline character to prepare for the next line.
count = 0;
current_line++;
if (current_line > data->end_line) {
break;
}
pthread_mutex_lock(&mutex);
global_sum2[current_line] += local_sum;
printf("2 At reading: global sum at line %d: %d\n", current_line, global_sum2[current_line]);
global_sum += local_sum;
pthread_mutex_unlock(&mutex);
local_sum = 0;
}
}
return NULL;
}
//void *reader_thread(void *arg) {
// FILE *fp = (FILE *)arg;
// int local_sum = 0;
// int num, count = 0;
//
// while (fscanf(fp, "%d", &num) == 1) {
// local_sum += num;
// count++;
// if (count == NUMBERS_PER_LINE) {
// fgetc(fp); // Read the newline character to prepare for the next line.
// count = 0;
// pthread_mutex_lock(&mutex);
// global_sum += local_sum;
// pthread_mutex_unlock(&mutex);
// local_sum = 0;
// }
// }
//
// return NULL;
//}
void signal_handler(int signum) {
char response;
printf("\nReceived 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);
}
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
exit(EXIT_FAILURE);
}
if (pid > 0) { // Parent process - writer
// Install signal handlers
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
printf("starting to write\n");
generate_random_data("data.txt");
sem_post(semaphore); // Signal the child process that writing is done
printf("posting semaphore\n");
wait(NULL); // Wait for the child process to finish
sem_close(semaphore);
sem_unlink("/mysemaphore");
printf("success\n");
exit(EXIT_SUCCESS);
} else { // Child process - reader
// Install signal handlers
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
printf("child process\n");
sem_wait(semaphore); // Wait for the parent process to finish writing
printf("starting to read\n");
FILE *fp = fopen("data.txt", "r");
if (!fp) {
perror("File opening failed");
exit(EXIT_FAILURE);
}
pthread_t threads[NUM_THREADS];
thread_data_t thread_data[NUM_THREADS];
int lines_per_thread = NUM_LINES / NUM_THREADS;
for (int i = 0; i < NUM_THREADS; i++) {
thread_data[i].fp = fp;
thread_data[i].start_line = i;
if (pthread_create(&threads[i], NULL, reader_thread, (void *)&thread_data[i]) != 0) {
perror("Failed to create thread");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// sum1 vs sum 2
fclose(fp);
printf("Total sum: %d\n", global_sum);
exit(EXIT_SUCCESS);
}
return 0;
}