Several thoughts and conclusions about the father and son processes under Linux

  
 

1. The parent process can use wait()/waitpid() to wait for the end of the child process to avoid the occurrence of a zombie child process. Of course, it can also wait for the end of all child processes by waiting for wait()/watipid(); The best use is that at the end of the child process, the SIGCHLD signal will be sent to the parent process, and the parent process responds to the end of the child process by signal()/sigaction(). For specific examples, refer to TestFork4.c and the key code is as follows: :signal(SIGCHLD, sigchld_handler);void sigchld_handler(int sig){pid_t pid;int status;for(; (pid =waitpid(-1, &status, WNOHANG)) >0;){printf("child %d died:%d\ ", pid, WEXITSTATUS(status));//pid, status);}//while((pid =waitpid(-1, &status, WNOHANG))>0){ }return;}

2 . When the parent process ends, the child process that has not ended will become an orphan process, and the system will treat the init process as its parent process; thus, the child process will be after the parent process ends. Continue to run.

3. When the parent and child processes coexist, when the relevant signal such as SIGINT is generated, the parent and child processes can receive this signal, but only the parent process responds first. Then the parent process passes this signal to the child process; but the point to note is: (1) If the parent process does not customize the processing of the signal process, the parent and child processes accept the default processing of the signal, eg: there is no SIGINT When the process is custom processing, this signal is generated, and the parent and child processes are immediately aborted; (2) if the parent process customizes the signal processing method, the child process accepts the signal and its signal processing method; at this time, if the child process becomes orphaned Process, then the child process will no longer accept this signal and its signal processing method. For specific examples, please refer to TestFork6.c, the key code is as follows: signal(SIGINT, sig_handler); for(i =0; i<5; i++ ) {if(fork() ==0){printf("child %d\ ", getpid());sleep(5);//At this moment, respond to the SIGINT signal, and wake up the process printf after the signal is processed ( "after sleep1:child %d\ ", getpid());sleep(5);//At this point, the child process becomes an orphan process, so it no longer responds to the SIGINT signal printf("after sleep2:child %d\\ n", getpid());exit(0);}}

4 . When creating a child process, the kernel will be the parent process Multi-content copy to the child process, at the same time after calling fork (), the parent and child processes share the body part (actually equivalent to copy); pay attention to copy the two words, so the modified Dongdong has no effect on the parent process, unless the father and son are used Process communication method; therefore, pay special attention to the code writing of the child process, such as the release of dynamic space, memory signal modification; specific examples can refer to: TestFork1.c, the key code is as follows: gets(buf); pid =fork() ;if(pid ==0){printf("child:");strcpy(buf, "xiaofeng");//buf here (a copy of buf in the main process) and else (main process) The buf in the ) has nothing to do, because its modification does not affect the contents of the buf in the main process}else{printf("parent:");sleep(5);}printf("%s\ ", buf) ;//The output here depends on which process (parent /child process) the statement is located: (hello is the gets(buf) statement) hellochild:xiaofengparent:hello


5 . Several conclusions about the signal are added: (1) There are three modes of signal processing, one is the default processing mode; the other is the signal Slightly mode; three are custom signal processing methods; (2) When a process sleeps in interruptible priority, the signal captured by the process will interrupt the sleep of the process

//, add multithreaded Linux The characteristics of the multi-threaded model:

The thread under Linux is essentially a light weighted process. When the thread is generated, the corresponding process control structure is generated, but the process control structure of the structure and the parent thread. Shared the same process memory space. At the same time, the process control structure of the new thread will copy the same process information from the parent thread (process), such as opening the file list and signal blocking mask. Since we modified the signal blocking mask after the child thread is generated, the child thread uses the original process information of the main thread at this moment, so the child thread still reacts to the SIGINT and SIGTERM signals, so when we use Ctrl+C When the SIGINT signal is used, the main process does not process the signal, and the child process (thread) will perform the default processing, that is, exit. When the child process exits, it sends a SIGCHLD signal to the parent process (thread), indicating that the child process exits. Since the signal is not blocked, the main process (thread) also exits immediately, and the foregoing operation occurs. Therefore, one solution to this problem is to set the signal before the child thread is generated, or to set the signal inside the child thread. Since the child thread is often a transaction function, I recommend using the former in a simple case. If the signal to be processed is more complicated, then the latter method must be used.


-------------------------------------- ----Supplement: Zombie Process

Unix: Zombie Process

The process has several states in its life cycle: sleep, runnable, stopped, running, and dead state. . The so-called zombie process means that a process has exited, its memory and related resources have been released by the kernel, but the process entry is still in the process table, so that its parent process gets its exit status. . When a process exits, its parent process receives a SIGCHLD signal. In general, the handle of this signal usually executes the wait system call, so that the process in the dead state will be deleted. If the parent process does not do this, what is the result? There is no doubt that the process will be in a state of zombie. In fact, the zombie process does not hurt the system much, at most its process number (PID) and the process item system in the process table cannot be used.

A parent process forks a child process, and then both of them execute their own code. At some point, the child process exits. But at this time, the child process still has a certain amount of space and is not recycled by the operating system
. The parent process is required to recycle the child process using the system call of the wait series, so that the child process can completely disappear from the system. So the zombie process is the time before the child process exits before being recycled by the parent process.

After the child process dies, it sends a SIGCHLD signal to the parent process.

Copyright © Windows knowledge All Rights Reserved