Denis,

You have a race condition.  If the parent process gets scheduled first and
executes the pause() before the child runs and sends the SIGUSR1 signal, you
get the behavior you're expecting.  However, if the child gets scheduled
first and sends the signal before the parent has executed pause(), when the
parent eventually gets scheduled it will execute the pause() and block
waiting for some signal (after the signal send by the child has already been
received and handled).

Andy

On Thu, Jul 30, 2009 at 3:18 PM, Denis Kirjanov <[email protected]> wrote:

> Good times.
> Look at the code. What do you think will be the result?
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <signal.h>
>
> void trap1(int sig)
> {
>        fprintf(stderr, "signal\n");
> }
> int main()
> {
>        int stat;
>
>        signal(SIGUSR1, trap1);
>        if (fork()) {
>                pause();
>                wait(&stat);
>                exit(0);
>        }
>        kill(getppid(), SIGUSR1);
>        exit(0);
> }
>
> Both processes must be completed.
> The result is that the parent process sleeps.
>
> ./test
> signal
> (zzzzzz)
>
> At a different operating system, everything works as expected
> (tested on OpenBSD 4.6/hppa, OpenBSD/sparc64)
>
> I would like to know what you think about this.
>
> --
> Regards,
> Denis
>

Reply via email to