4 Process primitives: fork () function, getpid () function, getppid () function, getuid () function, getgid () function, vfork ()

Source: Internet
Author: User
Zookeeper

1 fork () function

The sub-process copies the 0-3g space of the parent process and the PCB in the parent process kernel, but the ID number is different. A fork call returns two times at a time, which has the following features:

A: The child process ID is returned from the parent process.

B: The sub-process returns 0.

C: shared during read and copy during write

2 fork ()-dependent header file

# Include <unistd. h>

3 fork () function description:

Pid_tfork (void );

Use this function to create a sub-process

4 case study:

Summary:

A because of read sharing and write replication, the child process will copy the code of the parent process, but the execution starts from fork. However, when the parent and child processes define the variables used by the parent and child processes before fork, the Parent and Child processes do not operate the variables at the same time, but are independent of each other, that is, the Child processes specify their own N, does not operate on N of the parent process.

5. getpid/getppid

Header file on which a depends

# Include <unistd. h>

# Include <sys/types. h>

B function declaration

Uid_t getuid (void); // return the actual user ID

Uid_t geteuid (void); // return a valid user ID

C: Case Study

6. getuid () function

Header file on which a depends

# Include <unistd. h>

# Include <sys/types. h>

B function declaration

Uid_t getuid (void); // obtain the user ID

Uid_t geteuid (void); // obtain a valid user ID

Function Description:

Use the getuid () and geteuid () functions to obtain the user ID.

Case study:

Running result:

7. getgid () function

A: header file of dependency

# Include <unistd. h>

# Include <sys/types. h>

B function declaration

Gid_t getgid (void );

Gid_t getegid (void );

Function Description:

Use this function to obtain the group ID.

C Case study:

Running result:

 

Vfork

Call the exec function immediately after fork is used.

The Parent and Child processes share the same address space. If the child process does not immediately exec, it modifies the variable value obtained by the parent process. This modification takes effect in the parent process.

Designed to improve system efficiency and reduce unnecessary overhead

Now, fork has a mechanism to share data during read and write, and vfork is gradually deprecated.

 

 

 

 

 

 

 

 

4 Process primitives: fork () function, getpid () function, getppid () function, getuid () function, getgid () function, vfork ()

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.