Linux Process understanding and practice (2) botnets & orphan processes and file sharing

Source: Internet
Author: User

Linux Process understanding and practice (2) zombie and orphan processes and file sharing
Orphan process and zombie process orphan process: if the parent process exits first and the child process has not exited, the parent process of the child process will become the init process. (Note: Any process must have a parent process)

# Include <stdio. h> # include <stdlib. h> # include <errno. h> # include <unistd. h> int main () {pid_t pid; // create a process pid = fork (); // creation failure if (pid <0) {perror ("fork error: "); exit (1);} // sub-process if (pid = 0) {printf (" I am the child process. \ n "); // output process ID and parent process IDprintf (" pid: % d \ tppid: % d \ n ", getpid (), getppid ()); printf ("I will sleep five seconds. \ n "); // sleep for 5 s, ensure that the parent process exits sleep (5) First; printf (" pid: % d \ tppid: % d \ n ", getpid (), getppid (); printf ("child process is exited. \ n ");} // The parent process else {printf (" I am father process. \ n "); // The parent process sleeps for 1 s to ensure that the child process output process idsleep (1); printf (" father process isexited. \ n ") ;}return 0 ;}

 

Zombie process: if the child process exits first and the parent process has not exited, the child process must wait until the parent process captures the exit status of the child process, otherwise, the child process becomes a zombie process.
# Include <stdio. h> # include <unistd. h> # include <errno. h> # include <stdlib. h> int main () {pid_t pid; pid = fork (); if (pid <0) {perror ("fork error:"); exit (1 );} else if (pid = 0) {printf ("I am child process. I am exiting. \ n "); exit (0);} printf (" I am father process. I will sleep two seconds \ n "); // wait for the sub-process to exit sleep first (2); // output process information system (" ps-o pid, ppid, state, tty, command "); printf (" father process is exiting. \ n "); return 0 ;}

 

<Defunct> the zombie process orphan process is processed by init, which does not cause any harm. However, a large number of zombie processes occupy resources such as PID, which may lead to the system failure to generate new processes. Any sub-process (except init) does not disappear immediately after exit (), but leaves a data structure called Zombie, waiting for processing by the parent process. This is the stage that every sub-process will go through at the end. If the parent process does not have time to process the child process after exit (), run the ps command to check that the child process is in the "Z" status ". If the parent process can be processed in a timely manner, you may not be able to see the zombie state of the child process using the ps command, but this does not mean that the child process does not pass through the zombie state. If the parent process exits before the child process ends, the child process will be taken over by init. Init processes sub-processes in the botnet state as the parent process to prevent them from generating sub-processes to process certain processes through signal mechanisms, especially when requests arrive. If the parent process does not wait for the child process to end, the child process will become a zombie process, occupying system resources. If the parent process waits for the child process to end, it will increase the burden on the parent process and affect the concurrent performance of the server process. In Linux, you can set the SIGCHLD signal operation to SIG_IGN. That is to say, ignoring SIGCHLD signals is a technique commonly used in the performance of concurrent servers. Because the concurrent server often fork many sub-processes, after the sub-process ends, the server process needs to clean up resources through wait. If the signal processing method is set to ignore, the kernel can forward the zombie sub-process to the init process for processing, eliminating the need for a large number of zombie processes to occupy system resources. (Linux Only) the test program is as follows:
// Example: Avoid zombie process int main (int argc, char * argv []) {signal (SIGCHLD, SIG_IGN); pid_t pid = fork (); if (pid <0) err_exit ("fork error"); else if (pid = 0) exit (0); else {sleep (50) ;}exit (0 );}

 

All file descriptors of the file sharing parent process are copied to the child process, as if the dup function is called, each of the same open file descriptor of the Parent and Child processes shares a file table item (therefore, the Parent and Child processes share the same file offset );
// According to: Understand the demo int main (int argc, char * argv []) {signal (SIGCHLD, SIG_IGN); int fd = open ("test.txt ", o_WRONLY | O_CREAT | O_TRUNC, 0666); if (fd =-1) err_exit ("file open error"); cout <"We Don't flash memory \ n "; char buf [BUFSIZ]; bzero (buf, sizeof (buf); pid_t pid = fork (); if (pid <0) err_exit ("fork error "); else if (pid> 0) {strcpy (buf, "Parent... "); write (fd, buf, strlen (buf); close (fd); cout <" fd = "<fd <endl; exit (0 );} else if (pid = 0) {strcpy (buf, "Child... "); write (fd, buf, strlen (buf); close (fd); cout <" fd = "<fd <endl; exit (0 );}}

 

The result is that the value of fd (file descriptor) is equal. This indicates that the parent and child processes share the same file descriptor. Of course, the file offset and other information are also shared. Difference between fork and vfork 1. fork sub-processes copy the data segment of the parent process (but now the write-time replication technology is provided, only when the sub-process really needs to write the memory, to copy a copy of the memory). Therefore, modifications made to global variables in the parent process/child process do not affect the data content of the child process/parent process. the vfork sub-process shares the data segment with the parent process. Therefore, the Parent and Child processes synchronize data updates. 2. the execution sequence of fork Parent and Child processes is unknown, depending on the scheduling algorithm vfork of the Operating System: The child process runs first, and the parent process runs later; 3. if you create a sub-process to call exec to execute a new program, you should use vfork. However, it is very dangerous to execute other statements after vfork, it is easy to conflict with the parent process.
#include <unistd.h>#include <stdio.h>int main(void){pid_t pid;int count = 0;pid=vfork();count++;printf("count=%d\n",count);return 0;}

 

When learning linux Process programming, I encountered a problem. After using the vfork () function, I thought the following would print 1 and 2, but the result was unexpected. The printed result is: count = 1 count = 1 Segmentation fault has a segment error. After verification, it is found that after vfork () is successfully created, return is prohibited, only exit () or exec functions can be called. Otherwise, the consequences are unpredictable. In the main function, return and exit () functions have the same effect as before: vfork is not called. (If there is nothing at return, a segment error will also occur. The result is as follows: count = 1 count = 9 Segmentation fault)

Related Article

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.