7. Linux Process Learning

Source: Internet
Author: User

I feel that the basic knowledge about Linux is becoming increasingly vague. Some things know what is going on. But it's hard to say clearly. Now, sort it out.

I. process ID

1) What is a process ID?

Each process has a unique non-negative integer ID, which is called a process ID. Because it is unique, it is often used as part of other identifiers to ensure uniqueness.

2) Special process ID

L 0 scheduling process ID, also known as the exchange process, which is part of the kernel. It is also called a system process.

L 1 inid process, called by the kernel at the end of the Self-lifting process; I n I t usually reads system-related Initialization Files (/e t
C/r c * file) and direct the system to a State (for example, multiple users ),

L 2. The pagedaemon process ). This process is responsible for page operations that support the virtual storage system. Like the switch process, the page genie process is also a kernel process.

3) related functions

# Include <sys/types. h>

# Include <unistd. h>

Pid_t getpid (void); Return: process I d that calls the process

Pid_t getppid (void); Return: the parent process I d of the calling Process

Uid_t getuid (void); Return: actual user I d of the calling Process

Uid_t geteuid (void); Return: valid user I d of the calling Process

Gid_t getgid (void); Return: actual group I d of the calling Process

Gid_t getegid (void); Return: valid group I d of the calling Process

L there are six or more I D associated with a process, such

L actual user I d and actual group I d identify who we are. These two fields are taken from the logon entry in the password file during logon. These values do not change during a logon session, but the superuser process has a way to change them.

L valid user I d, valid group I d, and add group I d determine the access to our files.

L saved settings-user-I d and settings-group-
When executing a program, I d contains a copy of valid user I d and valid group I D.

Ii. Fork Functions

Fork is the only way to create a new process in the Linux kernel. A new process created by fork is called a sub-process .. This function is called once, but returns twice. The difference between the two responses is that the return value of the sub-process is 0, and the return value of the parent process is the process I of the new sub-process.
D

1) Why does fork return twice?

L fork is a copy, including two kernel pages with process descriptors and other resources, as well as a pointer to the external Virtual Memory Page. After fork, the content of the Parent and Child processes is exactly the same, therefore, different return values are needed to distinguish them. Of course, there are still many parameters in fork to control page copy to improve efficiency.

L fork returns the sub-process I d to the parent process for the following reason: because there can be more than one sub-process of a process, therefore, no function allows a process to obtain the process I of all its sub-processes.
D.

L fork: the reason why a sub-process returns 0 is that a process has only one parent process, so the sub-process can always call g e t p
I d to obtain the process I d of the parent process (process ID 0 is always used by the exchange process, so the process I D of a sub-process cannot be 0)

2) which data and resources will the child process copy and inherit from the parent process?

L child processes are copies of parent processes. For example, a child process obtains copies of the data space, heap, and stack of the parent process. Note that this is a copy of the sub-process. Parent and Child processes do not share these buckets. If the body segment is read-only, the Parent and Child processes share the body segment. Currently, many implementations do not completely copy the data segment of the parent process and the heap, because
O r k is often followed by e x e c. As an alternative, the replication technology (c o p y-o n-write, cow) is used during write.

L when f o r k is run, all the descriptors opened by the parent process are copied to the child process.

L other inheritance include actual user I d, actual group I d, valid user I d, and valid Group I
D. Add group I D. Process Group I D. Dialog period I D. Control terminal. Set-user-
I d flag and settings-group-I d flag

L shared storage segments and resource restrictions for signal shielding and arrangement, environment, and connection

3) under what circumstances will fork fail

L there are already too many processes in the system (which usually means that something is wrong)

L The total number of processes of the actual user I d exceeds the system limit. C h I l D _ m a x specifies the actual user I
D. Maximum number of processes that can be created at any time.

Iii. Exit Function

1) There are three methods for normal Process Termination and two methods for abnormal Process Termination.

A. Normal termination:

L execute the r e t u r n statement in the m a I n function. This is equivalent to calling e x I t.

L call the e x I t function. This function is defined by ansi c. Its operations include calling each termination handler (terminating the handler when calling
T e x I t function login), and then close all standard I/O streams. Because ansi c does not process file descriptors, multi-process (parent, sub-process), and job control, this definition is incomplete for the u n I x system.

L call the _ e x I t system call function. This function is called by e x I t, which processes the details Of u n I x.

A. Termination of exceptions:

L call a B o r t. It generates the S I g a B RT signal, so it is a special case of Abnormal Termination.

L when a process receives a signal. Processes themselves (such as calling the B o r t function), other processes and kernels can generate signals sent to a process. For example, if a process accesses a storage unit out of its address space or is divided by 0, the kernel generates a signal for the process.

B. No matter how the process is terminated, the same piece of code in the kernel will be executed. This code disables all open descriptors for the corresponding process and releases the memory used by it.

2) What is a zombie process?

L a terminated child process that has not been processed properly by its parent process (obtain information about the child process and release the resources it still occupies) is called a zombie process (z o m B I e)

Iv. Wait and waitpid

We have discussed various termination conditions of the process, and process termination is an asynchronous event. How can we obtain the termination status of a child process as a parent process? In Linux. When a process is normal or ends abnormally, A sigchld signal is sent. The parent process can ignore this signal, you can also provide a function (Signal Processing Program) that is called when the signal occurs. functions that can capture the signal include wait and waitpid. The following describes the two functions.

1) function prototype

# Include <sys/types. h>

# Include <sys/Wait. H>

Pid_t wait (int * statloc );

Pid_t waitpid (pid_t PID, int * statloc, int options );

Two functions return: process I d if the operation succeeds, and-1 if an error occurs.

2) what may happen to the process that calls wait and waitpid?

L blocking (if all its sub-processes are still running ).

L The termination status of the process is returned immediately (if a child process has been terminated, it is waiting for the parent process to access its termination status ).

L return immediately if an error occurs (if it does not have any sub-process ).

3) What is the difference between wait and waitpid?

L wait blocks callers before a sub-process is terminated, while waitpid has an option to prevent callers from blocking.

L waitpid does not wait for the first child process to terminate-it has several options to control the process it is waiting.

4) how to check the status returned by the sub-process

Both functions have an int * statloc parameter. If * statloc is not empty. Then the returned status of the sub-process is saved in the unit to which it points. How can this status be checked? You have used the following macros to check whether * statloc is set to null when you do not care about the termination status of the sub-process)

L wifexited (Status). If the status is returned by the normal process, it is true. In this case, you can execute wifexited (Status) and send the child process to the Lower 8 bits of the exit or _ exit parameter.

L wifsignaled (Status). If the status returned by the child process ends with an exception, it is true (a non-capturing signal is received ). In this case, run wtermsig (Status) to obtain the signal number that causes the sub-process to terminate.

L wifstopped (Status). If it is the status returned by the current pause sub-process, it is true. In this case, run wstopsig (Status) to obtain the signal number that suspends the sub-process.

(Unfinished)

Link to the original: http://blog.chinaunix.net/space.php? Uid = 8698570 & Do = Blog & id = 1763150

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.