Linux Process Detailed __linux

Source: Internet
Author: User
Tags assert terminates
This article is actually a reading note for "UNIX Environment Advanced Programming". So many details are not expressed, want to ask the students to look at the original book.  The reason why I posted my reading notes on my blog is for two purposes: 1. Deepen your learning effect. 2. Provide a quick way to browse.
The techniques mentioned in this article are actually validated in the following environments: Linux version 2.6.18-164.el5 x86_64 gnu/linux (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46))
Procedures and processes
A program is an executable file on a disk that the kernel uses the EXEC function to read the program into memory and make it execute. The execution instance of a program is called a process. A program can start several processes.
Process identifier
The kernel uses a unique, non-negative integer to identify each process, called the process ID. When the process terminates, the PID can be used again (after a period of time will not be used).
#include <unistd.h> pid_t getpid ()//process ID pid_t getppid ();//Parent Process ID
The process of PID 0 is usually the scheduling process, also known as the Exchange process (Swapper). It is a system process. A process with PID 1 is usually the init process, which is called by the kernel at the end of the bootstrap process.  Responsible for starting the system. It is an ordinary user process that runs with superuser privileges.
Process Start
The C program always starts with the main function, and the main function's prototype is: int main (int argc, char *argv[]); The kernel passes the command line arguments of the startup process to the process through the arguments of the main function. In addition, the kernel passes the environment variables environ through the variable. Typically, a C program does not use Environ directly, but instead uses the GETENV function to take the value of an environment variable.
The parameter table, like the environment table, is an array of C-string pointers. The difference is that the number of parameters in the parameter table is represented by a parameter. The environment table does not provide the number of environment variables, and it sets the last environment variable pointer to NULL to indicate the end of the environment table.
A process can call the fork function to create a new process. #include <unistd.h> pid_t Fork ()//child process returns 0, the parent process returns the Subprocess ID, and error returns-1. In general, it is uncertain whether the parent process executes first or fork after the execution of the child.
A parent-child process shares body segments, but does not share data segments.
The fork function allows the child process to replicate the data segments and stacks of the current parent process. The Vfork function can also create a new process, but it does not copy the data segments and stacks of the parent process. Also, the parent process is blocked after calling Vfork until the child process calls exec or exit;
Process terminated
There are eight ways to terminate the process, which are: 1. Returns from the main function. 2. Call exit. 3. Call _exit or _exit 4. The last thread returns from its startup routine. 5. The last thread invokes Pthread_exit. 6. Call Abort. It generates SIGABRT signals. 7. Receive a signal and terminate it. 8. The last thread makes a corresponding cancellation request. 1-5 of them are normal exits and 6-8 are terminated abnormally.
There are three functions to exit a process normally. #include <stdlib.h> void exit (int status); void _exit (int status); #include <unistd.h> void _exit (int status);
_exit and _exit immediately enter the kernel, exit performs some cleanup (calls execute each termination handler, closes all standard IO streams, etc.), and then enters the kernel. Status is the terminating state of the process.
The termination status of a process is undefined in the following situations. 1. Call these functions with no terminating state. 2. Main performs a return with no returned value. 3. Main does not declare the return type to be cosmetic. If the return type of main is an integer, and the last statement does not provide a return value, the end state of the process is 0. Exit (0) in the main function is equivalent to return 0;
The same piece of code in the kernel is executed at the end, regardless of how the process terminates. This code closes all open descriptors for the corresponding process and releases the registers it uses.
Termination of child processes
When a process terminates, the kernel sends a SIGCHLD signal to its parent process.
This signaling system defaults to ignoring it. Programmers can register a signal processing function for this signal to capture this signal.
The parent process can use the wait or Waitpid function to get the terminating state of the child process.
Exit, _exit, _exit three functions pass the termination state to the parent process. If the process is terminated abnormally, the kernel produces a termination state that indicates its cause.
#include <sys/wait.h> pid_t Wait (int *status); pid_t waitpid (pid_t pid, int *status, int options); Success returns the process ID, error returns-1;
These conditions can occur when you invoke the wait or Waitpid function: 1. If all of its child processes are still running, block. 2. If a child process has been terminated (zombie process), returns the terminating state of the subprocess. 3. If it does not have any child processes, immediately error return; If the process receives a SIGCHLD signal and calls wait, you can expect that wait to return immediately. At other times, the process is likely to block.
For processes that have been terminated by the parent process, their parent process will be changed to the init process.
Waitid, WAIT3, WAIT4 These functions can also be used to get the termination state of a child process.
Zombie Process
When a child process terminates, if its parent process is still running, the kernel retains a certain amount of information for the aborted child process. The parent process can know the status of the child process based on this information. The child process will terminate completely until the parent process has treated it. During this time, this subprocess will become a zombie process, it still occupies a certain resources.
User Termination Handler
As stipulated in ISO C, a process can register up to 32 functions that will be automatically invoked by exit. These functions are called termination handlers. Use the Atexit function to enlist these functions.
#include <stdlib.h> int atexit (void (*func) (void);//Success returns 0, error returns non 0
The order in which exit calls these functions is the reverse of the registration order. The same function is called multiple times if it is registered more than once. To determine the maximum number of termination handlers supported by a platform, you can use the sysconf function.
Process execution and termination process


Executing a new program in a process
Fork is used to create a subprocess that is used by the EXEC function to execute another program. When the process calls exec, a completely new program replaces the body, data, and stack segments of the current process. The new program starts with its main function. The PID of the new program does not change, and other features of the original process are preserved.
There are six different exec functions: execl, EXECV, Execle, Execve, EXECLP, EXECVP;
Process Security
The permissions of a process are determined by the user and group IDs of the startup process, sometimes with minimal privileges for security reasons. The program can modify the permissions of the process by modifying the user and group IDs to which the process belongs by dynamically changing the functions including:
#include <unistd.h> int setuid (uid_t uid); int Setgid (gid_t gid); int Setreuid (uid_t ruid, uid_t euid); int Setregid (gid_t rgid, gid_t egid); Successfully returned 0, error return-1
uid_t getuid ()//user ID uid_t geteuid ()//valid user ID gid_t getgid ();//actual group ID gid_t getegid ();//valid group ID
Process Environment
Storage space layout of C Program: Body section. Composed of machine instructions executed by the CPU. Typically, the body segment is shared by multiple processes and is read-only. Initializes a segment of data. Typically called a data segment, contains all variables declared outside the function and explicitly assigned an initial value. Uninitialized data segment.  Also known as BSS (The block from which the symbol begins). Before the program starts executing, the kernel initializes the data in this segment to 0 or null pointers. Any variable declared outside the function (but not explicitly assigned to the initial value) is stored in the uninitialized data segment.  Stacks. The information that needs to be saved when automatic variables and function calls are placed on the stack. Heap. Dynamic memory allocation in the heap. Because of historical conventions, the heap is between uninitialized data segments and stacks.

In addition to the above paragraph, there are several other types of segments. Examples include segments that contain symbol tables, sections that contain debugging information, and segments that contain linked tables for dynamic shared libraries, and so on. These parts are not loaded into the program image of the process execution.
The size instruction can display the body section of the program, the length of the data segment and the BSS segment:
The four and fifth columns represent the total length of three segments in decimal and hexadecimal respectively.
Resource constraints
Each process has a set of resource constraints, some of which can be queried and changed with the Getrlimit and Setrlimit functions.


When you change resource limits, you need to follow these rules: 1. Any process can change a soft limit value to less than or equal to its hard limit value. 2. Any process can reduce its hard limit value, but it must be greater than or equal to its soft limit value. This reduction is not reversible for ordinary users. 3. Only Superuser processes can increase hard limit values. Constant rlim_infinity indicates infinity.
Some resource types are listed below: The maximum number of bytes in the rlimit_as process's available storage. Affects the SBRK function and the mmap function. The maximum number of bytes Rlimit_core core files, 0 blocks the creation of core files.
RLIMIT_CPU the maximum amount of CPU time (in seconds), the process receives a SIGXCPU signal when the soft limit is exceeded.
The maximum byte length of the Rlimit_data data segment. Includes initialization data segments, uninitialized data segments, and the sum of the heaps.
The maximum byte length of the file created by Rlimit_fsize. When this soft limit is exceeded, the process receives a SIGXFSZ signal.
Maximum number of file locks that can be held by Rlimit_locks. This number includes the number of file leases that are unique to Linux.
The maximum byte length that the Rlimit_memlock process uses Mlock to lock in registers. The maximum number of files that Rlimit_nofile can open. Affects the return value of the sysconf function in the parameter _sc_open_max.
Rlimit_nproc the maximum number of child processes that can be owned by each actual user ID. Affects the return value of the sysconf function in the parameter _sc_child_max.
Rlimit_rss the byte length of the maximum in-memory set (RSS). If physical storage is in short supply, the kernel will retrieve more than the RSS portion from the process.
The maximum byte length of the Rlimit_stack stack.

Resource limits are inherited by the quilt process. We typically use the shell's limit command to set resource limits before the process starts. The following code is used to view resource limits for a process:
#include <iostream> #include <sys/resource.h> #include <assert.h>
#define PRINT_LIMIT (name) \ Assert (Getrlimit (name, &limit) ==0); \ printf ("%s:soft LIMIT =%d, hard LIMIT =%d\ n ", \ #NAME, limit.rlim_cur, Limit.rlim_max);
int main () {printf ("rlim_infinity =%d\n", rlim_infinity);     Rlimit limit; Print_limit (rlimit_as) print_limit (rlimit_core) print_limit (RLIMIT_CPU) print_limit (rlimit_data) PRINT _limit (rlimit_fsize) print_limit (rlimit_locks) print_limit (rlimit_memlock) print_limit (RLIMIT_NOFILE) PRIN T_limit (Rlimit_nproc) print_limit (Rlimit_rss) print_limit (Rlimit_stack) return 0; }


Execute command string
ISO C defines the system function, which makes it easy to execute a command string. #include <stdlib.h> int system (const char *cmdstring);
For example: System ("date > File");//save date to file.
Because system calls fork, exec, and waitpid in its implementation, there are three return values. 1. If fork fails or waitpid returns an error other than EINTR, it returns-1 and sets the wrong type value in errno. 2. If Exec fails (indicating that the shell cannot be executed), the return value is the same as if the shell executed exit (127). 3. If fork, exec, Waitpid are executed successfully, the return value is the end state of the shell.
Process Group
Each process belongs to a process group. A process group is a collection of one or more processes. Usually they are associated with the same job and can receive signals from the same terminal.  Each process group has a unique process group ID. #include <unistd.h> pid_t getpgrp ()//The process group ID of the current process pid_t GETPG

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.