Skip to content

Commit d131017

Browse files
author
Vladimir Kotal
committed
add SIGCHLD example
1 parent 1e6ebd0 commit d131017

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

signals/Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ PROGS=alarm \
1414
signal-vs-sigaction \
1515
sigsegv \
1616
sigwait \
17-
sigpending
17+
sigpending \
18+
sigchld
1819

1920
all: $(PROGS)
2021

@@ -72,6 +73,9 @@ sigwait: sigwait.o
7273
sigpending: sigpending.o
7374
$(CC) -o $@ $^
7475

76+
sigchld: sigchld.o
77+
$(CC) -o $@ $^
78+
7579
clean:
7680
rm -f *.o a.out $(PROGS)
7781

signals/sigchld.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* The SIGCHLD signal is always delivered to the parent process except
3+
* in case it is ignored.
4+
*/
5+
6+
#include <stdio.h>
7+
#include <signal.h>
8+
#include <unistd.h>
9+
#include <sys/types.h>
10+
#include <sys/wait.h>
11+
12+
void
13+
handler(int s)
14+
{
15+
write(1, "got it\n", 7);
16+
}
17+
18+
int
19+
main(int argc, char *argv[])
20+
{
21+
pid_t pid;
22+
23+
#ifdef HANDLE_SIGCHLD
24+
struct sigaction act;
25+
// act.sa_handler = handler;
26+
act.sa_handler = SIG_IGN;
27+
sigemptyset(&act.sa_mask);
28+
act.sa_flags = 0;
29+
sigaction(SIGCHLD, &act, NULL);
30+
#endif
31+
32+
if ((pid = fork()) == 0) {
33+
sleep(1);
34+
} else {
35+
int status;
36+
printf("forked %d\n", pid);
37+
// wait(&status);
38+
waitpid(pid, &status, 0);
39+
}
40+
41+
return (0);
42+
}

0 commit comments

Comments
 (0)