Zombie process and process concepts and processes orphans

1, the concept of process

Process is an independent function of the program run activities, but also the system is the smallest unit of resource allocation and scheduling.
In short program, a process that is being executed or instance of a program to be executed

2, describes the process

Information in the process data structure, called a process control block. And a control system to be described by the program concurrently PCB (Program Control Block). The so-called creation process is to create a process image of the PCB, and the revocation process is to cancel the PCB.

3, the difference between processes and procedures

1> dynamic: the process is dynamic, with creation, activity, suspension, termination and other processes, there is a certain life cycle, the process can not be migrated between computers. And the program is a collection of static instruction, the corresponding file can be copied and pasted. Dynamic process is the process of the most basic features.
2> Concurrency : introduction process purpose is to enable programs to the program concurrent with other processes carried out in order to improve memory utilization. Entities with multiple processes can be stored in the memory, running at the same time. Concurrency is the most important characteristic of the process, but also the most important features of the operating system.
3> Independence : the entity is a process can be run independently, the independent distribution system and the basic unit of resource scheduling. But not those who are not able to independently build PCB program participation run.
4> Structural : From the structural point of view, by the process described and PCB to control it. Mainly by the program segment, the data segment, the process control of three parts segments.

Create 4, the process

A parent process calls fork () function to copy after copy of themselves, and this copy out of the new process is the child process, successfully completed the creation process.
The main provider ** fork in Linux (), vfork (), clone () ** three process creation method, where the first summarize fork ()
fork when a process is created, the child process only complete copy of the parent's resources, copy out of the child process has its own task_struct structure and pid, but copy all of the other resources of the parent process.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
   int ret=fork();
   if(ret<0)
   {
       perror("Fail");
       return -1;
   }
   else
   {
       printf("hello proc:%d\n!,getpid(),ret);
   }
       sleep(1);
       return 0;
   }

5, zombie process

Why would a zombie process? Because the child takes precedence over parent process to exit, in order to save and exit reasons, so its resources are not fully released. When the child process exits, the operating system will notify the parent, so the parent process to obtain exit reason the child process, and then release all the resources of the child process, if the parent does not exit the state of attention the child, that is not called wait / waitpid, then the child process will become a zombie process.
It is noteworthy that zombie process is a process of long-dead, but it still occupies a position in the process table.

6, zombie process solutions

(1) wait for the end of the parent process child process via wait / waitpid and other functions, which can cause the parent process to hang. The child will immediately put it in the process table of data returned to the parent after the termination of the system will immediately remove the node.
(2) If the parent is busy, you can install a signal function SIGHLD handler. At the end of the child, the parent will receive the signal, you can call wait function in the handler
(3) If the parent does not care about the process of child when to stop, you can tell the kernel, the end of his sub-processes are not interested in using signal (SIGCLD, SIG_IGN) or signal (SIGCHLD, SIG_IGN), then after the end of the child process, the kernel will recover, does not give the parent process sends a signal .
(4) fork twice, the parent process fork a child process, and then continue to work, fork child process exits after a grandchild, the grandchild was taken over by init, Sun after the end of the process, init will recover. But the recovery of the child process need to do ourselves.

7, orphaned

A parent process creates multiple sub-processes when the parent process exits, its one or more sub-process is still running, then the child will become orphaned. Orphan adoption process will be init process, by the init process for their completion status of the collection.
Zombie process will lead to a waste of resources, but the process does not orphans

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
   pid id=fork();
   if(id < 0)
   {
      perror("fork ");
      return 1;
   }
   else if(id==0)
   {//子进程
     	printf("I am child,pid:%d\n",getpid());
     	sleep(10);
   }
   else
   {//父进程
       printf("I am parent,pid:%d\n",getpid());
       sleep(3);
       exit(0);
   }
   return 0;
}

Result output for
the I parent AM, PID: 4416
the I AM Child, PID: 4417

Published 33 original articles · won praise 13 · views 1068

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/102755426