In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Under linux, we can call the fork function to create a child process, which will get the data space, stack and stack of the parent process. Copy (using write-time copy mechanism), the child process will inherit the parent process's signal mask, signal processing method, current working directory, session id, group id. The parent process should obtain the exit state of the child process in time when the child process exits, otherwise, if the parent process is running all the time, the exit state of the child process will be kept in memory until the parent process exits.
We can use the following methods to avoid zombie processes:
1. Call the wait/waitpid function after fork to get the exit status of the child process.
two。 Call fork twice (the first call produces a child process, the second call fork is called in the first child process, and the parent process exits (the first child process exits), and the parent process id of the second child process is the init process id (note: the new version of Ubuntu is not the process id of init).
3. The ignore SIGCHLD signal is displayed in the program (a SIGCHLD signal is generated when the child process exits, and we show that we can ignore this signal).
4. Capture the SIGCHLD signal and call the wait/waitpid function in the capture program.
Method 1:
# include ".. / common/common.h" int main (void) {pid_t pid; if ((pid = fork ()
< 0) { perror("fork error"); return EXIT_FAILURE; } else if (0 == pid) { printf("[%ld] child process is running...\n", (long)getpid()); _exit(0); } //sleep(15); if (waitpid(pid, NULL, 0) < 0) { perror("waitpid error"); return EXIT_FAILURE; } for (; ;) { pause(); } return EXIT_SUCCESS;} 方法二: #include #include "../common/common.h"int main(void){ pid_t pid; if ((pid = fork()) < 0) { perror("fork error"); return EXIT_FAILURE; } else if (0 == pid) { printf("first child is running..\n"); /**在第一个子进程中再次fork***/ if ((pid = fork()) < 0) { perror("fork error"); return EXIT_FAILURE; } else if (pid >0) {/ * * parent process exits * * / printf ("[% ld] first child is exit...\ n", (long) getpid ()); _ exit (0);} sleep (2); / * * make sure the parent process runs * * / printf ("second process pid:% ld, second process's parent pid:% ld\ n", (long) getpid (), (long) getppid ()); / / sleep (15) Printf ("[% ld] is exit..\ n", (long) getpid ()); _ exit (0);} / * obtain the exit status of the first child process * * / if (waitpid (pid, NULL, 0) < 0) {perror ("waitpid error"); return EXIT_FAILURE;} for (;) pause (); return EXIT_SUCCESS;}
Method 3:
# include # include ".. / common/common.h" int main (void) {/ * shows ignoring SIGCHLD signal * * / if (signal (SIGCHLD, SIG_IGN) = = SIG_ERR) {perror ("signal error"); return EXIT_SUCCESS;} pid_t pid; int i; / * * produces 10 child processes * * / for (iS0; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Exchange 2013 (VII) High availability of Mailbox servers
© 2024 shulou.com SLNews company. All rights reserved.