I want to write a program which will create N children, using fork() function. Each child will wait from 0 to 3 seconds and then it'll send it's parent exactly one signal SIGUSR1. Parent handles all of these signals.
The problem is that my program is not always handling all signals from it's children. How to repair it?
Second question: I know that I shouldn't use printf inside handler because something bad might happen. How can I replace this instruction?
main.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void error(char *s){
printf("%s\n",s);
perror("Program execution failed.");
exit(EXIT_FAILURE);
}
volatile int N=8; //final number of children
volatile int k=0; //number of received SIGUSR1 signals
void childRequestHandler(int signo, siginfo_t* info, void* context){
k++;
printf("%d,Father received request from child: %d\n",k,info->si_pid);
}
int main() {
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = childRequestHandler;
if(sigaction(SIGUSR1,&act,NULL) == -1) printf("ERROR OCCURED");
for(int i=0;i<N;i++){
pid_t pid = fork();
if(pid == 0) {
execl("./child", "./child", NULL);
error("Fork error happened\n");
}
}
while (1){
sleep(1);
}
}
child.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
int main() {
time_t t;
srand((unsigned int) getpid()+ time(NULL));
int length_of_sleeping = rand() % 4 ;
printf("I live: %d, sleeps %ds \n",getpid(),length_of_sleeping);
sleep(length_of_sleeping);
kill(getppid(),SIGUSR1);
while(1){
}
}
output:
I live: 4195, sleeps 3s
I live: 4196, sleeps 3s
I live: 4197, sleeps 1s
I live: 4202, sleeps 3s
I live: 4198, sleeps 0s
I live: 4201, sleeps 2s
I live: 4199, sleeps 0s
I live: 4200, sleeps 3s
1,Father received request from child: 4198
2,Father received request from child: 4197
3,Father received request from child: 4201
4,Father received request from child: 4195