I'm trying to have it execute in a loop where the parent randomly picks between SIGUSR1 and SIGUSR2 and send it to the child process to receive and write to a file
My problem is the signal will only send in the first loop and after that it stops
int main(int argc, char* argv[], char *envp[]){
time_t start, finish; //for example purposes, to save the time
struct sigaction sact; //signal action structure
sact.sa_handler = &handler;
sact.sa_handler = &handler2;
sigset_t new_set, old_set; //signal mask data-types
FILE *file = fopen("received_signal.txt", "w");
fprintf(file,"%s\t %s\t %s\n", "Signal Type",
"Signal Time", "thread ID");
fclose(file);
int pid;
int cpid;
pid = fork();
if(pid == 0){//recieves
//sigaction(SIGUSR1, &sact, NULL);
while(1){
signal(SIGUSR1, handler);
signal(SIGUSR2, handler2);
sleep(1);
}
} else{ //generates
while(1){
sleep(1); // give child time to spawn
printf("hello\n");
parent_func(0);
//wait(NULL);
usleep(((rand() % 5) + 1) * 10000);
}
}
return 0;
}
void parent_func(int child_pid){
srand(time(NULL));
int rnd = rand();
int result = (rnd & 1) ? 2 : 1;
struct timeval t;
gettimeofday(&t, NULL);
unsigned long time = 1000000 * t.tv_sec + t.tv_usec;
printf("result: %d\n", result);
printf("time: %ld\n", time);
if(result == 1){
//sigaction(SIGUSR1, &sact, NULL);
kill(child_pid, SIGUSR1);
log(SIGUSR1);
} else{
//sigaction(SIGUSR2, &sact, NULL);
kill(child_pid, SIGUSR2);
log(SIGUSR2);
}
}
void handler(int sig){
if (sig == SIGUSR1){
puts("child received SIGUSR1");
}
}
void handler2(int sig){
if (sig == SIGUSR2){
puts("child received SIGUSR2");
}
}
Tried throwing the child in a while loop to get it to repeat but no such luck