Hi what I'm trying to do here is a program that starts by registing it's pid in the file server.lock then it should do a fork(). The son should say it started working than do a passive wait and arm the signal SIGUSR2, and when it receives it, it should terminate it's execution and write "goodbye cruel world". The parent should write it began and do a passive wait, and then arm the signals SIGUSR1 and SIGINT. If it receives SIGUSR1 it himself should send the signal SIGUSR2 to the son, and if it receives the signal SIGINT if it's the first time it receives it it should say it got the signal, if it's the second time it should send the signal SIGUSR2 to the son so it terminates the son's and it's execution. Now my problem is with the parent, I don't know why but the second time it receives the signal it says the error "User defined signal 2" and leaves, if somebody could explain to me what I'm doing wrong here I would very much appreciate. Here is the code. Thank you.
int p = 1;
int son;
void handle_SIGINT(int signal){
if ( p==1 ) {
p = 2;
printf("The authentication module received the signal SIGINT\n");
}else{
kill( son, SIGUSR2);
exit(0);
}
}
void handle_SIGUSR1(int signal){
kill( son, SIGUSR2);
}
void handle_SIGUSR2(int signal){
printf("Goodbye cruel world\n");
exit(0);
}
void main(){
int pid = getpid();
FILE *f = fopen("server.lock", "w");
fprintf( f, "%d", pid );
fclose(f);
int n = fork();
if ( n==0 ) {
printf("The message handling module has started\n");
signal(SIGUSR2, handle_SIGUSR2);
while(1)
pause();
}else{
printf("The authentication module has started\n");
son = getpid();
signal(SIGUSR1, handle_SIGUSR1);
signal(SIGINT, handle_SIGINT);
while(1)
pause();
}
}