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
[email protected]:/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
%
------------------------------------------------------------------