SIGCHLD and libpthread.so

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

SIGCHLD and libpthread.so

Jun KAWAI
Hi,

I see that SIGCHLD which was sent before calling sigaction() for
SIGCHLD is delivered to the handler when program is linked to
libpthread.so.  However, this behavior doesn't occur when program
isn't linked to libpthread.so.

I feel the behavior of SIGCHLD when linked to libpthread.so
is strange, but I don't know whether the behavior is correct
or not.

Can anyone tell me what happened?


Best Regards,
Jun Kawai

------------------------------------------------------------------
% dmesg | head -2
OpenBSD 4.6-stable (GENERIC.MP) #0: Sat Dec  5 10:35:10 JST 2009
    [hidden email]:/usr/src/sys/arch/amd64/compile/GENERIC.MP
% cat SIGCHLD-test.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

void trap(int);
void puts_curtime(void);

int
main(int argc, char *argv[])
{
        pid_t pid;
        struct sigaction act, oact;

        puts_curtime();

        pid = fork();
        if (pid == 0) {
                sleep(1);
                _exit(0);
        } else if (pid == -1) {
                exit(1);
        } else {
                int status;
                waitpid(pid, &status, 0);
        }

        puts_curtime();
        sleep(5);
        puts_curtime();

        sigemptyset(&act.sa_mask);
        act.sa_handler = trap;
        act.sa_flags = 0;
        if (sigaction(SIGCHLD, &act, &oact) != 0) {
                exit(1);
        }

        sleep(5);

        if (sigaction(SIGCHLD, &oact, (struct sigaction *) 0) != 0) {
                exit(1);
        }

        puts_curtime();

        exit(0);
}

void
trap(int sig)
{
        printf("Received SIGCHLD - ");
        puts_curtime();

        return;
}

void
puts_curtime(void)
{
        time_t tloc;

        time(&tloc);
        puts(ctime(&tloc));

        return;
}

% cc -Wall -o SIGCHLD-test SIGCHLD-test.c
% cc -Wall -pthread -o SIGCHLD-test-thread SIGCHLD-test.c
% ldd ./SIGCHLD-test ./SIGCHLD-test-thread
./SIGCHLD-test:
        Start            End              Type Open Ref GrpRef Name
        0000000000400000 0000000000802000 exe  1    0   0      ./SIGCHLD-test
        000000020d2b6000 000000020d795000 rlib 0    1   0      /usr/lib/libc.so.51.0
        0000000201c00000 0000000201c00000 rtld 0    1   0      /usr/libexec/ld.so
./SIGCHLD-test-thread:
        Start            End              Type Open Ref GrpRef Name
        0000000000400000 0000000000802000 exe  1    0   0      ./SIGCHLD-test-thread
        000000020a66d000 000000020aa91000 rlib 0    1   0      /usr/lib/libpthread.so.11.1
        00000002036a5000 0000000203b84000 rlib 0    1   0      /usr/lib/libc.so.51.0
        0000000201400000 0000000201400000 rtld 0    1   0      /usr/libexec/ld.so
% ./SIGCHLD-test
Sat Dec  5 12:06:34 2009

Sat Dec  5 12:06:35 2009

Sat Dec  5 12:06:40 2009

Sat Dec  5 12:06:45 2009

% echo $?
0
% ./SIGCHLD-test-thread
Sat Dec  5 12:06:58 2009

Sat Dec  5 12:06:59 2009

Sat Dec  5 12:07:04 2009

Received SIGCHLD - Sat Dec  5 12:07:09 2009

Sat Dec  5 12:07:09 2009

% echo $?
0
%
------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: SIGCHLD and libpthread.so

Philip Guenther-2
On Fri, Dec 4, 2009 at 7:42 PM, Jun KAWAI <[hidden email]> wrote:
> I see that SIGCHLD which was sent before calling sigaction() for
> SIGCHLD is delivered to the handler when program is linked to
> libpthread.so.  However, this behavior doesn't occur when program
> isn't linked to libpthread.so.
>
> I feel the behavior of SIGCHLD when linked to libpthread.so
> is strange, but I don't know whether the behavior is correct
> or not.

It's a bug, due to how libpthread uses SIGCHLD to detect when a thread
blocked in wait*() should be unblocked.  The fix isn't entirely
trivial and I'm not going to have a chance to look at it closely for
quite a while, so please file a bug with "sendbug" so we don't lose
track of this if I forget about it.


Philip Guenther

Reply | Threaded
Open this post in threaded view
|

Re: SIGCHLD and libpthread.so

Jun KAWAI
> It's a bug, due to how libpthread uses SIGCHLD to detect when a thread
> blocked in wait*() should be unblocked.  The fix isn't entirely
> trivial and I'm not going to have a chance to look at it closely for
> quite a while, so please file a bug with "sendbug" so we don't lose
> track of this if I forget about it.

Thank you for your reply.  I'll file a bug report with sendbug.


Regards,
Jun Kawai