0

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();
   }
}
GamerGirl
  • 55
  • 8
  • IIRC the signal handler is un-set when it's called, which means it needs to re-set itself. – user253751 Nov 09 '16 at 00:43
  • I'm sorry but aren't all 3 signal handlers supposed to be defined before the main? I mean for the first time(for the signal SIGINT) the parent works perfectly, the problem is the second time. – GamerGirl Nov 09 '16 at 00:51
  • Possible duplicate of [What is the difference between sigaction and signal?](http://stackoverflow.com/questions/231912/what-is-the-difference-between-sigaction-and-signal) – Zan Lynx Nov 09 '16 at 00:53
  • Hah! And also, just noticed that the variable you named `son` is actually the parent! Your naming confused me. – Zan Lynx Nov 09 '16 at 00:56
  • @j.DOH What I just said has nothing to do with which order they're defined in the program. – user253751 Nov 09 '16 at 00:58
  • Thank you @immibis i understand my mistake now, i need to rearm the signal handler after it's called, thank you. – GamerGirl Nov 09 '16 at 01:07

1 Answers1

0

Your naming confused me. It appears that your problem is your variable named son. That isn't the son aka child process. That's the parent.

The fork call returns twice. Once in the parent and once in the child. In the parent it returns the child's PID. It returns 0 in the child. You have them reversed!

You are using signal instead of sigaction. I recommend using sigaction. It is much more complicated but gives many more options as well.

The problem you're having with signal is that after the handler is called it resets the signal to default behavior. But that isn't guaranteed either. The GNU C library has it behave differently depending on if _BSD_SOURCE, _GNU_SOURCE or nothing is defined. That is to simulate how it behaved on BSD UNIX or SYSV UNIX. And that's why sigaction is a better choice, it acts the same on every POSIX system.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • I'm sorry but isn't calling getpid() on the parent return the pid of the son and calling it on the son return 0? That's why I called getpid() on the else in main.(Sorry if it is a dumb question) – GamerGirl Nov 09 '16 at 01:02
  • @j.DOH: It is easy to see for yourself. Print out the values of `pid` and `son`. – Zan Lynx Nov 09 '16 at 01:10
  • @ Zan Lynx Ok thank you very much for the help, I really appreciate it. – GamerGirl Nov 09 '16 at 01:11