here if i say no in the signal the child doesnt resumes corectly can u fix it using semapthores?
#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>
#include <string.h>
#include <errno.h>
#define NUM_THREADS 4
#define NUM_LINES 100
#define NUMBERS_PER_LINE 50
#define MAX_LINE_LENGTH 256
pid_t process_pid = -1;
sem_t *semaphore;
int global_sum = 0;
int lines_read_by_thread[NUM_THREADS] = {0}; // Array to keep track of lines read by each thread
char lines[NUM_LINES][MAX_LINE_LENGTH]; // Assuming each line won't exceed MAX_LINE_LENGTH characters
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct {
FILE *fp;
int start_line;
int end_line;
} thread_data_t;
volatile sig_atomic_t ask_to_terminate = 0; // Used to handle signal-based termination
void signal_handler(int signum) {
char response[4];
// Pause the child process
kill(process_pid, SIGSTOP);
// Prompt the user
printf("\nDo you want to terminate the program? (yes/no): ");
if (fgets(response, sizeof(response), stdin) && strncmp(response, "yes", 3) == 0) {
printf("Terminating process...\n");
kill(process_pid, SIGTERM); // Terminate the child process
exit(EXIT_SUCCESS); // Terminate the parent process
} else {
printf("Resuming child process...\n");
kill(process_pid, SIGCONT); // Resume the child process
}
}
void setup_signal_handling() {
struct sigaction sa;
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
}
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+=local_sum;
fprintf(fp, "\n");
// printf("1 At writing: global sum at line %d: %d\n", i, global_sum1[i]);
}
printf("REALL Total sum: %d\n", allthenumberstogether);
fclose(fp);
}
void *reader_thread(void *arg) {
int thread_id = (int)arg;
char line_copy[MAX_LINE_LENGTH]; // Local buffer for tokenizing
for (int i = thread_id; i < NUM_LINES; i += NUM_THREADS) {
strncpy(line_copy, lines[i], MAX_LINE_LENGTH); // Copy the line to avoid modifying shared data
line_copy[MAX_LINE_LENGTH - 1] = '\0'; // Ensure null-termination
int local_sum = 0, num;
char *saveptr; // For strtok_r's state
char *token = strtok_r(line_copy, " ", &saveptr);
while (token != NULL) {
num = atoi(token);
local_sum += num;
token = strtok_r(NULL, " ", &saveptr);
}
pthread_mutex_lock(&mutex);
global_sum += local_sum;
lines_read_by_thread[thread_id]++;
// printf("Thread %d processed line %d, sum: %d\n", thread_id, i, local_sum);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main() {
setup_signal_handling(); // Setup signal handling
srand(time(NULL));
// Initialize semaphore
semaphore = sem_open("/mysemaphore", O_CREAT, 0644, 0);
if (semaphore == SEM_FAILED) {
perror("sem_open failed");
exit(EXIT_FAILURE);
}
process_pid = fork();
if (process_pid < 0) {
perror("fork failed");
exit(EXIT_FAILURE);
}
if (process_pid > 0) { // Parent process - writer
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 parent\n");
exit(EXIT_SUCCESS);
} else { // Child process - reader
printf("child process\n");
sem_wait(semaphore); // Wait for the parent process to finish writing
printf("starting to read\n");
// Read the file into memory
FILE *fp = fopen("data.txt", "r");
if (!fp) {
perror("Unable to open file");
exit(EXIT_FAILURE);
}
int line_count = 0;
while (fgets(lines[line_count], MAX_LINE_LENGTH, fp) && line_count < NUM_LINES) {
line_count++;
}
fclose(fp);
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
// Create threads
for (int i = 0; i < NUM_THREADS; i++) {
thread_ids[i] = i;
if (pthread_create(&threads[i], NULL, reader_thread, (void *) &thread_ids[i]) != 0) {
perror("Failed to create thread");
exit(EXIT_FAILURE);
}
}
// Wait for threads to finish
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// printf lines read by each thread
for (int i = 0; i < NUM_THREADS; i++) {
printf("Thread %d read %d lines\n", i, lines_read_by_thread[i]);
}
printf("Total sum: %d\n", global_sum);
printf("success child\n");
exit(EXIT_SUCCESS);
}
return 0;
}