CTRL + c --> 2) SIGINT
CTRL + \ --> 3) sigquit
13) when the read end of the sigpipe pipeline is closed and then writes to the pipeline, the sigpipe signal is sent.
17) The sigchld signal is sent to the parent process when the sigchld sub-process exits. The system ignores the signal by default.
#include<stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <sys/wait.h>#include <signal.h>void handler(int num){ pid_t pid ; printf("sig_num: %d \n", num); pid = wait(NULL); printf("wait : %u \n", pid);}int main(int argc, char* argv[]){ int fds[2];// fds[0] r fds[1] w pipe(fds); char buf[1024]; signal(SIGPIPE, handler); signal(SIGCHLD, handler); if(fork() == 0) { close(fds[1]); while(memset(buf, 0, 1024), read(fds[0], buf, 1024)) { 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, 1024), read(0, buf, 1024)) { write(fds[1], buf, strlen(buf)); } //while(1) ; return 0 ;}
When the program runs, the two sub-processes exit successively, send the sigchld signal to the parent process, and then execute the handler function to recycle the resources of the two sub-processes. Then the program executes the while loop to the parent process. Because read is a blocking function, the time slice will switch back and forth between the first child process and the parent process before we press ENTER or Ctrl + D. If you press enter, the parent process writes it to the MPs queue, and the child process extracts it from the MPs queue and displays it on the screen.
If you press Ctrl + D, the parent process exits the while loop and exits the program. At this time, the first child process becomes an orphan process and is adopted by init. When the child process exits, it will send a sigchld signal to init and the init will recycle resources.
We write the wait function that recycles sub-process resources in the signal processing function because wait is a blocking function. If the parent process is blocked, it cannot process its own work.
We have mentioned earlier that when a child process is terminated, it will send a sigchld signal to the parent process, although the default processing of this signal is ignored, however, the parent process can customize the sigchld signal processing function, so that the parent process only needs to focus on its own work and does not have to worry about the child process. When the child process ends, it will notify the parent process, the parent process calls wait in the signal processing function to clear sub-process resources.
Generally, the server will never exit, so we can add a while (1) before return 0 in the previous program to simulate the server.
If the parent process exits before the child process, the child process becomesOrphan ProcessIn this case, it is automatically taken over by the process (init) with PID 1. After the orphan process exits, its cleanup is automatically handled by the ancestor process init. However, before the INIT process clears sub-processes, it consumes system resources, so avoid it as much as possible.
If the child process exits first, the system will not automatically clean up the environment of the child process, but the parent process must call the wait or waitpid function to complete the cleaning. If the parent process does not clean up, then the child process that has exited will becomeBotnets(Defunct)If too many zombie processes exist in the system, the system performance will be affected. Therefore, the zombie process must be processed.
Basic signal operations