Process Control (i)

Source: Internet
Author: User

1. Process identifiers

The process identifier type is pid_t, which is essentially an unsigned integer.

There are 6 important IDs for each process:

Process ID, parent process ID, valid user ID, valid group ID, actual user ID, actual group ID

The Getpid (), Getppid (), Geteuid (), Getegid (), Getuid (), Getgid () are used respectively.

2. Process operations

(1) the fork () function creates a new process, and the fork () function calls back two times at a time

The fork () function returns a value of three cases:

For the parent process, fork () returns the ID of the child process, and only the kernel's process is 0, so the child process ID can only be greater than 0, so the child process and parent process can be distinguished by the return value. If the return value is 0, the description is run in the child process, and if the return value is greater than 0, the description is run in the parent process. If the return value is-1, there is an error in the fork () function. The following examples illustrate:

pid=Fork (); if 0 ) {       perror ("fail to fork");        Exit (1);} Else 0 ){       ...} Else {     ...}    

The top three programs are the usual judgment after creating a new process with the fork () function, which is the best understanding for the fork () function to return two times a call. The return value of the fork () function is in total three cases (the PID below represents the return value of the fork () function)

1. When PID < 0, the return value is less than 0 for the creation process to fail, at which time the operation of the process will return an error message;

2. When PID = 0, the return value equals 0 means that the process is a child process at this time, the operation of the process is in the child process, which is the essence of the fork () function, in the child process as in the parent process can make function calls and so on.

3. When PID > 0, the return value greater than 0 means that the process at this time is the parent process.

First look at the fork () function when you do not understand, always feel very strange, and then through the code, the whole to understand the program, see the debugging results only slowly understand the fork () function, from the beginning to learn the process management to now, knocked through the fork () function to create a new process of the program at least 20, This 20 times the continuous writing, and constantly understand to let me for the process and the fork () function to understand more thoroughly, the back of the study will also touch the sibling process, if the fork () function of the understanding is not clear, the brothers process will be confused, resulting in learning brother process through the pipeline communication between the time of error, Basic knowledge of learning is interlocking, these basic things understand clearly, for the future study will not be dragged behind, learning these basic things, key or knock code, hard to knock code, a program to do not understand, knock 20 times must understand, and remember, if the side knock thinking, It should be back to simple applications after 20 times.

(2) The difference between the fork () function and the Vfork function

The fork () function creates a new process, allocates the process space for the new process, and copies the contents of the parent process space into the child process space, including the data segment and the stack segment, and the parent process space shared code snippet. The child process's modifications to the data segment and the stack segment do not affect the parent process.

The vfork () function breaks into a new process, and the child process fully shares the address space with the parent process, including code snippets, data segments, and stack segments, and the changes that the child processes make to these shared resources can affect the parent process.

(3) Exit () function check for process error message

The exit state of the exit () function, if exited normally, has a parameter of 0, and if the exception exits, the parameter is not 0.

(4) Set process Owner

Each process has two user IDs, the actual user ID and the valid ID, both of which are generally equal and whose value is the user ID of the process owner. In some cases, a valid user ID for the process needs to be modified.

#include <unistd.h>int setuid (uid_t uid);

The parameters of the setuid () function indicate the actual user ID and valid user ID of the new user after the change, successfully returning 0, and 1 failure.

#include <unistd.h>int seteuid (uid_t uid);

The parameters of the Seteuid () function represent a valid user ID for the changed new user (only the valid user ID is changed), a successful return of 0, and a failure return-1.

Process Control (i)

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.