Linux Basic Learning Series: Learn about fork () Functions and Process Creation

Source: Internet
Author: User

Fork () function

 

: Generate a process from the current process.


# Include <sys/types. h>

# Include <unistdh>




Pid_t fork (void );

Return Value: The value is 0 in the child process, the value is the child process ID in the parent process, and the error value is-1.

If the value is greater than 0, it indicates that the job is running in the parent process, and if the value is equal to 0, it indicates that the job is running in the child process.

 

/*************************************** *******************************/

Note: Obtain the process ID

 

# Include <sys/types. h>

# Include <unistd. h>

Pid_t getpid (void); // return process ID of the called Process

Pid_tgetppid (void); // return the parent process ID of the calling process.

/*************************************** *******************************/

 

A new process created by fork is called a child process ).


This function is called once, but returns twice. The difference between the two responses is that the return value of the child process is 0, while that of the parent process is the ID of the child process.
Bind sub-Processes
I d

The reason returned to the parent process is: Because there can be more than one sub-process of a process, there is no function so that a process can obtain all of its sub-processes.
I d

.
F o r k

Obtain the returned value from the sub-process.
0

The reason is: A process has only one parent process, so sub-processes can always call
G e t p I d

To obtain the process of its parent process
I d (

Process
Id 0

It is always used by the exchange process, so the process of a sub-process
I d

Impossible
0)

.


Generally, after the fork () function is run, it is not clear whether the parent process is executed first or the child process is executed first. This depends on the scheduling algorithm used by the kernel.

 

The child process obtained by using the fork () function inherits the address space of the entire process from the parent process, including: process context, process stack, memory information, open file descriptor, signal control settings, process priority, process group number, current working directory, root directory, resource limit, control terminal, etc.

 

F o r k
There are two usage methods:

 

(1)

A parent process needs to copy itself so that the parent and child processes can execute different code segments at the same time. This is common in network service processes-the parent process waits for the service request of the delegate. When such a request arrives, the parent process calls
F o r k

To make the sub-process the request. The parent process continues to wait for the next service request.

 

(2)

A process needs to execute a different program. This pair
S H E L

Is a common situation. In this case, the sub-process
F o r k

Call immediately after return
E x e c

.

 

Differences between parent and child processes:

 

1. Return Value of Fork () function

2. process ID

3. Set tms_utime, tms_stime, tms_cutime, and tms_ustime to 0, that is, the time-related parameters in the process control block (PCB) of the child process to 0.

4. The lock set by the parent process. The child process does not inherit

5. Pending alarms of sub-processes are cleared

6. Set the pending model set of the sub-process to an empty set.

 

Routine:

 

# Include <sys/types. h> <br/> # include <unistd. h> <br/> # include <stdlib. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> pid_t reuslt; <br/> result = fork (); <br/> If (result =-1) <br/>{< br/> perror ("fork"); <br/> exit; <br/>}< br/> else if (result = 0) <br/>{< br/> printf ("the return value is % d/Nin child process! /Nmy PID is % d/N ", result, getpid ()); <br/>}< br/> else <br/> {<br/> printf ("the return value is % d/Nin father process! /Nmy PID is % d/N ", result, getpid (); <br/>}< br/>}



Running result:




The return value is 9479
In father process!
My PID is 9478
The return value is 0
In child process!
My PID is 9479

 

......................................

 

Vfork () function

 

Generally, the operating system running on the same operating system is not the same as that running on the same operating system.
, The latter
With
There is a MMU
.


The vfork () function is for uClinux
. Why the vfork () function is used, because it has optimized the memory Application
. First, the parameters and return values are the same as those of Fork (), but the two running processes are slightly different. Only the main content of the parent process is copied, instead of completely copying the data segment and stack segment of the parent process, the copy-on-write (COW) technology is used to copy and back up data only when the data segment is modified.

 

 

........................................ ............

 

Exec () function

 

The child process from fork is exactly the same as the parent process. It makes no sense. In this case, the exec function family is used (a series of functions are represented here ). That is to say, fork () generates a sub-process and only provides a runtime space. The specific program to run is not determined yet. Finally, exec () is used to execute another program, it is a bit similar to the feeling of "golden Chan shelling. Two steps to establish a process are arranged to allow the child process to process its file descriptor after fork but before exex, this allows you to redirect standard input, standard output, and standard errors.


When a process calls an exec () function, the process is completely replaced by a new program, and the new program is executed from its main () function. Because exec () is called and no new process is created, the process ID does not change. Exec only replaces the current process body, program, heap, and stack segment with another program.

 

Int execl (char * pathname, char * arg0, arg1 ,..., argn, null); <br/> int execle (char * pathname, char * arg0, arg1 ,..., argn, null, char * envp []); <br/> int execlp (char * pathname, char * arg0, arg1 ,.., null); <br/> int execple (char * pathname, char * arg0, arg1 ,..., null, <br/> char * envp []); <br/> int execv (char * pathname, char * argv []); <br/> int execve (char * pathname, char * argv [], char * envp []); <br/> int execvp (char * pathname, char * argv []); <br/> int execvpe (char * pathname, char * argv [], char * envp []);

 

In the above function name: l represents the linked list, V represents the vector, and E represents the environment variable of the table. P represents the automatic search for the relevant system path (PATH)

 

# Include <unistd. h> <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> int main (void) <br/>{< br/> If (Fork () = 0) <br/> {<br/> If (execlp ("Ps", "Ps", "-Ef", null) <0) <br/>{< br/> perror ("execlp error! "); <Br/>}< br/> return 0; <br/> // For the sake of omission, no error debugging statements added <br/>}

 

 

 

Repost my blog article solemnly states: the famous author of a technical website can be reprinted, and a commercial website can be reprinted only with my consent; otherwise, the website will be held accountable --

** Pang123hui blog:

**




** Csdnhttp: // blog.csdn.net/pang123hui/













 

 

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.