Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Short SIGCHLD sample. For details, visit:
- // <https://www.ipa.go.jp/security/awareness/vendor/programmingv1/b07_04.html>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void setup_SIGCHLD(void);
- void catch_SIGCHLD(int signo);
- int main(void)
- {
- int i;
- puts("main start.");
- setup_SIGCHLD();
- for (i = 0; i < 3; i++)
- {
- pid_t child_pid = fork();
- if(child_pid == -1) {
- puts("fork error.");
- }
- else if(child_pid == 0) {
- sleep(1);
- exit(0);
- }
- printf("child %d spawned.\n", child_pid);
- sleep(1);
- }
- puts("Press Ctrl+C to exit.");
- while(1) {
- sleep(1);
- }
- }
- void setup_SIGCHLD(void)
- {
- struct sigaction act;
- memset(&act, 0, sizeof(act));
- act.sa_handler = catch_SIGCHLD;
- sigemptyset(&act.sa_mask);
- act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
- sigaction(SIGCHLD, &act, NULL);
- }
- void catch_SIGCHLD(int signo)
- {
- pid_t child_pid = 0;
- puts("SIGCHLD trapped.");
- #if 1
- do {
- int child_ret;
- child_pid = waitpid(-1, &child_ret, WNOHANG);
- printf("waitpid => %d, %d\n", child_pid, child_ret);
- } while(child_pid > 0);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement