What is the problem?

Can someone tell me what is the problem?
What I want to do is to jump from signalhandler into signalhandler but just doesnt seem to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int A, B;
int sighandler(int sig)
{
    if (sig==SIGUSR1) //its A called
    {
        printf("A: %i. \n",getpid());
        sleep(1);
        kill(B,SIGUSR2);
    }
    else if(sig==SIGUSR2) //its B called
    {
        printf("B: %i. \n",getpid());
        sleep(1);
        kill(A,SIGUSR1);
    }
}
int main(int argc, char** argv) {

    signal(SIGUSR1,sighandler);
    signal(SIGUSR2,sighandler);
    A=getpid();
    B=fork();

    if (getpid()==A)
    {
        kill(B,SIGUSR2);
        while(1) pause();

    }

    else if (getpid()==B)
    {
        while(1) pause();
    }
    return (EXIT_SUCCESS);
}


And the output is:

B: 17345.
A: 17343.
...And here its endless loop, nothing happens.
why are't you returning from the function

int sighandler(int sig)


also with why are't you using switch case , statement .

1
2
3
4
5
6
7
8
9
10
11
12
int sighandler(int sig)
{
switch( sig ) 
{
case SIGUSR1:
                           .....
                         ....
case SIGUSR2:
                  ...
                  ...
}
    


why use while(1) pause .
Last edited on
Sorry, the sighandler is not supposed to return int is should be void. But still my problem remains unsolved.
I dont think switch case is any better here (I just dont like it thats all), and while(1) pause makes the process wait until it gets a signal from somewhere.
Topic archived. No new replies allowed.