make the every thread to read the line %4 + i ( 1<= i <=4) (for exaple the first thread the lines 1,5,9. The second thread the lines 2,6,10 etc):
#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>
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) {
code
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]);
code
}
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);
code
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);
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 * lines_per_thread;
thread_data[i].end_line = (i + 1) * lines_per_thread - 1;
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;
}
Assistant
To make every thread read the line %4 + i (1 <= i <= 4), you can modify the reader_thread function as follows:
c
void *reader_thread(void *arg) {
thread_data_t *data = (thread_data_t *)arg;
FILE *fp = data->fp;
int local_sum = 0;
int num;
int count = 0;
int current_line = 0;
printf("1 At reading: line %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;
pthread_mutex_unlock(&mutex);
local_sum = 0;
}
}
return NULL;
}
Here, the thread will read the lines starting from line 0 up to line data->end_line, with a buffer of 25 lines in memory (NUMBERS_PER_LINE). The current_line variable is used to keep track of the current line being processed, and the count variable is used to keep track of the number of numbers read from the file. When the buffer is full, the thread will process the numbers and update the global_sum2 array accordingly.