Parent-child processes

Source: Internet
Author: User

Inheritance of child processes to parent processes after fork

(1) Why do I need to create a child process?

Each program needs a process to run, and creating a sub-process can achieve a macro-parallel

(2) When do I need to create a child process?

Call Fork to create a new process when the parent process executes to a stage or receives an event and needs to create a separate process to assist it in completing the task

(3) The child process inherits from the parent process

1. Why inherit the related resources of the parent process, refer to http://blog.csdn.net/xlsernt_sina_com/article/details/31350829

Two words were picked:

The parent process creates a child process for it to be able to assist the parent process in doing something, so the parent process must share some of its own resources with the child process so that the parent-child process can complete the task together

Child processes are created to assist the parent process in completing related tasks, resources that are not related to this purpose, child processes do not have to inherit, inherit from the memory resources will be wasted

2. Inherited resources, refer to http://blog.chinaunix.net/uid-24517549-id-4117366.html

    • User number UIDs and user group number Gids
    • Environment variables
    • Stack
    • Shared memory
    • Opening a file descriptor
    • Off (CLOSE-ON-EXEC) flag on execution
    • Signal (Signal) control settings
    • Nice value, which represents the priority of the process , the smaller the value, the higher the priority
    • Process Scheduling category (Scheduler Class)
    • Process Group number
    • Conversation ID (session ID) (refers to the session ID that the process belongs to, and a conversation period consists of one or more process groups, see Advanced Programming Section 9.5 for more detailed instructions)
    • Current working directory
    • root directory
    • File mode to create a masking word
    • Resource limits
    • Control Terminal

3. Resources that are not inherited

    • Process number PID
    • Different parent Process Number (Translator Note: The parent process number of the child Process is the parent of the parent process . process number is different, parent Process number can be obtained by getppid function)
    • Copy of the child process's own file descriptor and directory stream
    • asynchronous inputs and outputs
    • File locks, pending alarms and pending signals
    • Timer created by the Timer_create function
    • Block signal set initialized to empty
    • Resource Usage (resource utilizations) set to 0
    • System time in the TMS fabric
    • Child The process does not inherit the parent process 's process body (text), data, and other locked memory (locks) ( Translator Note: Locked memory refers to the locked virtual memory page, locked, the kernel will not allow it to swap out when necessary (page out), detailed instructions see "The GNU C Library Reference Manual" version 2.2, 1999, 3.4.2)

4. Other needs to be aware of

1. Parent and child processes have separate address space and PID parameters

2. After fork (), the parent and child processes have the same content of code snippets, data segments, and user stacks (shared memory)

3. Just like the parent process, clone yourself again. In fact, the parent process replicates only its own PCB block. The code snippet, the data segment, and the user stack memory space are not copied, but are shared with the child process. Interrupts are generated only when a child process has a write operation in the run, and the child process allocates memory space. Because the parent process is the same as the PCB and the child process, the parent process that is recorded in the PCB outage occupies a resource that is also shared with the child process. The word "sharing" here means "competition"

(4) return value of fork

The fork function call will return 2 times, first return PID = 0, the return value equals 0 is the child process, and then return the PID is greater than 0, the return value is greater than 0 is the parent process, and this return value is the child process ID created by this fork

(5) The parent process recycles the child process

1. Orphan process : When the parent process ends first, the system changes the init process (process 1) to the parent of the child process

2. Zombie Process : child process before the end of the parent process, the parent process has not yet had time to recover it (the system simply recycles the memory and IO consumed by the process, and does not reclaim the memory that the process itself occupies, 8KB, mainly task_struct and stack memory))

3. Recovery function

Wait: block, when the child process ends, the system sends a sigchild signal to its parent process pid_t wait (int *status);

Reference http://blog.csdn.net/qq_18973645/article/details/52648812

Waitpid: Can recycle the specified PID sub-process, can be blocked or non-blocking two modes of operation

pid_t waitpid (PID, &status, 0); 0 default indicates blocking

pid_t waitpid (Pid,&status,wnohang)//= Non-blocking

  

Parent-child processes

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.