LINUX Process Control (1)

Source: Internet
Author: User
Article Title: LINUX Process Control (1 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

1. Linux Process Overview

A process is an execution instance of a program. It is the basic scheduling unit of Linux. A process consists of the following elements:

The current context of the program, that is, the current execution status of the program;

Current execution directory of the program

Files And Directories accessed by the program

Program access permissions, such as its file mode and ownership

Memory and other system resources allocated to processes

The kernel uses processes to control the access to CPU and other system resources, and uses processes to determine which program runs on the CPU, how long it runs, and what features it uses to run it. The kernel scheduler is responsible for allocating CPU execution time among all processes, known as time slice. It takes turns to obtain control from the process after each process has used up the time slice.

1.1. process mark 1.2. Recognition

The OS assigns a unique integer ID to each process as the process ID (pid ). In addition to its own ID, a process also has a parent process ID (ppid). The ancestor processes of all processes are the same process. It is called the init process and the ID is 1, the init process is a startup process after kernel self-lifting. The init process is responsible for guiding the system, starting the daemon (background) process, and running necessary programs.

The pid and ppid of a process can be obtained by using the getpid () and getppid () functions.

Example:

# Include

# Include

# Include

Int main ()

{

Printf ("pid: % d ppid: % d \ n", getpid (), getppid ());

Return 0;

}

1.3. process user ID and group ID (process running identity)

In the running process, a process must have a user identity for process permission control. By default, which login user runs the program, the program process has the identity of this user. For example, if the currently logged-on user is gotter and the user runs the ls program, the user has the gotter identity during the running process, the user ID and group ID of the ls process are the group to which the gotter and gotter belong. This type of ID is called the real user ID and real group ID of the process. Real user IDs and real group IDs can be obtained through the getuid () and getgid () functions.

Corresponds to the real ID. The process also has valid user ID and valid group ID attributes. When the kernel checks the process access permissions, it checks the valid user ID and valid group ID of the process, instead of the real user ID and real group ID. By default, the user (valid user ID and valid group ID) and (real user ID and real group ID) are the same. Valid user IDs and valid group IDs are obtained through the geteuid () and getegid () functions.

Example

# Include

# Include

# Include

Int main ()

{

Printf ("uid: % d gid: % d euid: % d egid: % d \ n ",

Getuid (), getgid (), geteuid (), getegid ());

Return 0;

}

The logon user is ghaha (uid is 500) at the beginning ):

Shell> id

Uid = 500 (ghaha) gid = 500 (ghaha) groups = 500 (ghaha)

Compile and generate an executable file a. out. The properties of the program file may be:

-Rwxrwxr-x 1 ghaha 12132 Oct 7 a. out

The execution result may be:

Shell> a. out

Uid: 500 gid: 500 euid: 500 egid: 500

Change the executable attribute of the owner of a. out to s.

Shell> chmod u + s a. out

Shell> ll

-Rwsrwxr-x 1 ghaha 12132 Oct 7 a. out

At this time, change the other user gotter to log on and run the program a. out.

Shell> id

Uid = 502 (gotter) gid = 502 (gotter) groups = 502 (gotter)

Shell> a. out

Uid: 502 gid: 502 euid: 500 egid: 502

We can see that the valid user identity of the process is changed to ghaha, not gotter, because the file. the owner of the out access permission can execute. out,. the valid user identity of the out process is no longer running. out user, but. the owner of the out file.

The most common example of s permission is

/Usr/bin/passwd program. Its permission bit is

Shell> ll/usr/bin/passwd

-R-s -- x 1 root 16336 Feb 13 2003/usr/bin/passwd

This means that when a user runs the program, the valid identity of the program will be root, so that the passwd program has the permission to read the information of the/etc/passwd file.

2. Process Creation

There are four types of sub-process creation functions in Linux:

System (), fork (), exec * (), popen ()

2.1. system functions

Prototype:

# Include

Int system (const char * string );

The system function executes the command specified by string by calling the shell program/bin/sh-c. This function internally calls execve ("/bin/sh ",..) function. After a sub-process is created through system, the original process and sub-process run separately, with less association between them. If the system call is successful, 0 is returned.

Example:

# Include

# Include

Int main ()

{

System ("ls-l ");

Return 0;

}

Example:

# Include

# Include

Int main ()

{

System ("ls-l ");

Return 0;

}

2.2. fork Function

Prototype:

# Include

Pid_t fork (void );

The fork function creates a sub-process. When a fork is called by a process, the system copies a new sub-process based on the features of the existing process, the copied content includes the code, variables, objects, and other memory states of the current memory space of the original process, real and valid uid and gid, environment, resource restrictions, and opened files. After a sub-process is created through this replication method, both the original process and the sub-process are returned from the fork function, and each of them continues to run. However, the fork return value of the original process is different from the fork return value of the sub-process, in the original process, fork returns the pid of the sub-process, while in the process, fork returns 0. If fork returns a negative value, the sub-process creation fails.

Example:

// TestFork. cpp {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

# Include

# Include

Int main ()

{

Printf ("Parent process id: % d \ n", getpid ());

Pid_t iRet = fork ();

If (iRet <0 ){

Printf ("Create child process fail! \ N ");

} Else if (iRet = 0 ){

Printf ("I'm child process, and id: % d ppid: % d \ n ",

Getpid (), getppid ());

} Else {

Printf ("Create child process success, child id: % d \ n", iRet );

}

Return 0;

}

// TestFork. cpp }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

Shell> g ++ TestFork... cpp

Shell> a. out

Parent process id: 6059

I'm child process, and id: 6060 ppid: 6059

Create child process success, child id: 6060

[1] [2] Next page

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.