Introduction
Let's look at the following two semaphores:
Sigpipe when the pipe reads off and then writes to the pipe, the sigpipe signal is emitted.
SIGCHLD Child process exit will send a SIGCHLD signal to the parent process, the system default processing is to ignore the signal
Code
/************************************************************************* > File name:my_fork.c > Author: Krischou > Mail:[email protected] > Created Time:mon-10:42:26 AM CST *************************** *********************************************/#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<sys/wait.h>#include<signal.h>voidHandlerintnum) {pid_t pid; printf ("Sig_num:%d \ n", num); PID=Wait (NULL); printf ("wait:%u \ n", PID);}intMainintargcChar*argv[]) { intfds[2];//Fds[0] R Fds[1] Wpipe (FDS); Charbuf[1024x768]; Signal (sigpipe, handler); Signal (SIGCHLD, handler); if(fork () = =0) {Close (fds[1]); while(Memset (BUF,0,1024x768), read (fds[0], buf,1024x768) {Write (1, buf, strlen (BUF)); } printf ("Child over ! \ n"); Exit (0); } if(fork () = =0) {exit (0); } if(fork () = =0) {exit (0); } Close (fds[0]) ; while(Memset (BUF,0,1024x768), Read (0, BUF,1024x768) {write (fds[1], buf, strlen (BUF)); } //while (1); return 0 ;}
The results of the operation are as follows:
[[email protected] review]$./a. Out of15693 15692hellohelloworld! World! !
When the program runs, first two sub-processes exit successively, send SIGCHLD signal to the parent process, then execute handler function, reclaim two child process resources. The program then executes a while loop to the parent process, because read is a blocking function, and the time slice switches back and forth between the first child and parent process before we press ENTER or Ctrl+d. If the input character presses ENTER, then the parent process writes it to the pipe, and the child process takes it out of the pipe and displays it on the screen.
If you press CTRL+D, the parent process exits the while loop and exits the program. At this point the first child process becomes an orphan process, adopted by INIT, and when the child process exits, the SIGCHLD signal is sent to Init and the resource is reclaimed by init. (Note that Init is very fierce, it has already registered the SIGCHLD, its signal processing function has realized the recycling of resources)
Dry
We write the wait function that reclaims the child process resources in the signal handler because wait is the blocking function. If the parent process is blocked, it cannot handle its own work.
As we have mentioned before, when a child process terminates, it sends a SIGCHLD signal to the parent process, although the default processing action of the signal is ignored, but the parent process can customize the processing function of the SIGCHLD signal so that the parent process only needs to concentrate on its own work and not care about the child process. The parent process is notified when the child process terminates, and the parent process calls the Wait cleanup child process resource in the signal handler function.
Normally the server will never quit, so we can simulate the server by adding a while (1) before return 0 in the previous program.
Attention
If the parent process exits before the child process, the child process becomes an orphan process , which is automatically taken over by the PID 1 process (that is, init). After the orphan process exits, its cleanup work is automatically processed by the ancestor process init. However, before the INIT process cleans up the child process, it consumes the resources of the system, so try to avoid it.
If the child process exits first, the system does not automatically clean up the child process's environment, and the parent process must call the wait or Waitpid function to complete the cleanup, and if the parent process does not clean up, the child process that has exited will become a zombie process (defunct) , if there are too many zombie (zombie) processes in the system, it will affect the performance of the system, so the zombie process must be processed.
Linux Signal Processing 2