(3) learning the Unix creation process fork getpid getppid together with cainiao

Source: Internet
Author: User

(1) Basic Process Environment

A process is an execution of a program. It is a program that runs in its own virtual address space and has independent functions. Processes are the basic unit for allocating and releasing resources. When a program is executed, the system creates processes and allocates memory and CPU resources. When the system ends, the system recycles these resources.

 

(2) process concept

A process is usually composed of three parts: a program, data, and a process control block (PCB. The program section describes the functions to be completed by the process, the data section provides the stack and private data required for the process to run, and the process control block contains the process description and control information, reflecting the dynamic characteristics of processes in a centralized manner is the basis for System Identification and Control of processes.

 

(3) process definition:

A process is a function program with a certain degree of independence.

 

(4) Process status and its conversion
1. Basic Process status
(1) Readiness
When a process has been allocated to all necessary resources except the processor, it can be executed immediately as long as the processor is obtained.
(2) Execution status
Execution status refers to the state in which a process has obtained a processor and its program is being executed.
(3) blocking status
If a running process is temporarily unable to continue execution due to a certain time, it will discard the processor and be paused.
(5) Parent and Child Processes

Processes are managed in a tree structure. When a process starts another process, the started process is a child process, and the original process is a parent process. In UNIX, the fork creation process is called by the system. Fork copies the data, stack, and process environment of the parent process. Therefore, the child process inherits some of the parent's environments and shares the code segment of the parent process. However, child processes also have their own environments. Parent and Child processes execute different branches of the same program in parallel.

UNIX contains a process init with the identifier 1, which is the parent process of all processes except sysproc in the computer.

Man fork:

 

Fork (2) Linux programmer's Manual fork (2)

Name
Fork-create a child process // create a child process

Synopsis // header file
# Include <sys/types. h>
# Include <unistd. h>

Pid_t fork (void );

Return Value
On success, the PID of the child process is returned in the parent's
Thread of execution, and a 0 is returned in the child's thread of Exe-
Cution. On failure, A-1 will be returned in the parent's context, no
Child process will be created, and errno will be set appropriately.

Function: Creates a sub-process.

Returned value: the child process returns 0, the parent process returns the child process ID, and the error returns-1;

 

Getpid:
# Include <sys/types. h>
# Include <unistd. h>
Pid_t getpid (void );
Function: gets the ID of the current process.
Return process ID
Getppid:
# Include <sys/types. h>
# Include <unistd. h>
Pid_t getppid (void );
Function: gets the parent process ID.
Return: parent process ID

Example:

1 # include <sys/types. h>
2 # include <stdio. h>
3 # include <unistd. h>
4 # include <stdlib. h>
5 Int main (void)
6 {
7 pid_t PID;
8 pid = fork ();
9 switch (PID)
10 {
11 case-1:
12 perror ("error :");
13 exit (1 );
14 break;
15 case 0:
16 printf ("current process ID % d, parent process ID % d \ n", getpid (), getppid ());
17 break;
18 default:
19 printf ("current process ID % d, parent process ID % d \ n", getpid (), getppid ());
20}
21}
 

 

 

 

 

 

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.