Use and verification of signal (sigchld, sig_ign );

Source: Internet
Author: User
# Include <stdio. h> # include <unistd. h> # include <sys/types. h> # include <stdlib. h> # include <signal. h> int main (INT argc, char ** argv) {sigchld (sig_ign); int ID; id = fork (); If (ID <0) {printf ("fork error \ n");} else if (ID = 0) {printf ("I'm in child process % d parent process % d \ n ", getpid (), getppid (); exit (0);} else if (ID> 0) {printf ("I'm in parent process % d \ n ", getpid (); sleep (60);} // signal (sigchld, Sig_ign); Return 0;} [[email protected] desktop] # GCC zombie process. c [[email protected] desktop] #. /. outi'm in parent process8072i'm in child process8073 parent process8072 // The second line [[email protected] desktop] # blocks the output in the second line for about 1 minute, run the following command [[email protected] desktop] # ps aux | grep 'Z' user PID % CPU % mem vsz RSS tty stat start time commandroot 8076 0.0 0.0 4340 740 pts/1 S + grep Z can find no zombie process, it's strange! This is because the INIT process calls wait to clear the resources such as the Child process progress table! Why does a child process become a child process of init instead of a zombie process? Strange! Because signal (sigchld, sig_ign); this line of code, the sub-process becomes the init sub-process! Be sure to pay attention to the position of signal (sigchld, sig_ign);, because the code is executed from above, because when we execute those commands, this line of code has not been executed, however, the child process has become a zombie process! See the following. If the position of this line of code is at the end, the results will be different. The following is the execution result: [[email protected] desktop] # ps aux | grep-W 'Z' root 8734 0.0 0.0 0 0 pts/0 z + [. out] <defunct> root 8737 0.0 0.0 4336 792 pts/1 S + grep-w z [[email protected] desktop] # It can be seen that a zombie process has occurred! We can use PS -- ppid 1 to view the process whose parent process is 1! (Which process number can be used) You can also use PS -- PID 1 to view the process with process number 1 [[email protected] desktop] # ps -- PID 1 PID tty time cmd 1? 00:00:04 init, but we must remember that when using these commands, we must remember to get the results only when the program is running! Once the program ends, it cannot be seen! The following is an example. In a window, [[email protected] desktop] #. /. outi'm in parent process8853i'm in child process8854 parent process8853 // it takes about one minute to pause, [email protected] desktop] # [email protected] [email protected] desktop] # ps -- PID 8854 PID tty time cmd 8854 pts/0 00:00:00. out [[email protected] desktop] # ps -- ppid 8853 PID tty time cmd 8854 pts/0 00:00:00. out [[email protected] desktop] # ps-ppid 8853 PID tty tim E cmd // note that no result is output here, because the program execution has ended [[email protected] desktop] # Remember to check it when the program is running, including viewing zombie processes, because once the program ends, the zombie process is probably invisible! Remember...

 

Use and verification of signal (sigchld, sig_ign );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.