Linux process detailed

Source: Internet
Author: User
Tags assert terminates

This article is actually a reading note for "Advanced Programming for UNIX environments." So many details have not been stated, and the students who want to get to the top are advised to look at the original book. The reason why the reading notes posted on the blog, for two purposes: 1. To deepen their learning effect. 2.provides a quick way to browse.
The techniques mentioned in this article are actually verified in the following environment: 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 on disk, and 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 Identifiers
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).
#include <unistd.h>pid_t getpid ();//Process idpid_t getppid ();//Parent Process ID
the process of PID 0 is usually the dispatch process, also known as the switching process (swapper). It is a system process.A process with a PID of 1 is usually the init process, which is called by the kernel at the end of the bootstrap process. Responsible for booting the system.It is an ordinary user process that runs with superuser privileges.
Process Start
The C program is always executed from the main function, and the prototype of the main function is:int main (int argc, char *argv[]);The kernel passes the command-line arguments of the START process to the process through the parameters of the main function. In addition, the kernel passes the environment variable environ. 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 a C-string pointer array. 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, which sets the last environment variable pointer to NULL to indicate that the environment table is closed.
A process can call the fork function to create a new process. #include <unistd.h>pid_t fork ();//The child process returns 0, the parent process returns the child process ID, and an error returns-1. Typically, fork It is indeterminate whether the parent process executes first or the child process first.
Parent-child processes share body segments, but do not share data segments.
the fork function causes the child process to copy the data segments and stacks of the current parent process.The vfork function can also create a new process, but it does not replicate 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 Termination
There are eight ways to make the process terminate, and they are: 1. Returns from the main function. 2. Call Exit.3. Call _exit or _EXIT4. The last thread returns from its startup routine. 5. The last thread calls Pthread_exit.6. Call abort. It produces a SIGABRT signal. 7. A signal is received and terminated. 8. The last thread corresponds to the cancellation request. 1-5 of which is normal exit, and 6-8 is an abnormal termination.
There are three functions for gracefully exiting a process.#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 to execute each termination handler, closes all standard IO streams, and so on) before entering the kernel.Status is the terminating state of the process.
In the following cases, the terminating state of the process is undefined. 1. These functions are called without a terminating state. 2. Main executes a return.3 with no return value. Main does not declare the return type to be shaped. If the return type of main is shaped, and the last statement does not provide a return value, the process's terminating state is 0. The exit (0) in the main function is equivalent to return 0;
Regardless of how the process terminates, the same piece of code in the kernel is executed at the end. This code closes all open descriptors for the corresponding process, releasing the registers it uses.
termination of child processes
When the process terminates, the kernel sends a SIGCHLD signal to its parent process.
This signaling system defaults to ignoring it. The programmer can register a signal processing function for this signal to capture the 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 terminating state to the parent process.If the process is terminated abnormally, the kernel generates a terminating state that indicates its cause.
#include <sys/wait.h>pid_t wait (int *status);p id_t waitpid (pid_t pid, int *status, int options), Success returns the process ID, error returns-1;
These conditions can occur when you call the wait or Waitpid function: 1. If all of its child processes are still running, it is blocked. 2. If a child process has been terminated (a zombie process), the terminating state of the child process is returned. 3. If it does not have any child processes, an immediate error is returned, and if the process calls wait after receiving the SIGCHLD signal, wait is expected to return immediately. At other times, the process is likely to block.
For processes that the parent process has terminated, their parent process will be changed to the init process.
Waitid, WAIT3, WAIT4 These functions can also be used to get the terminating state of a child process.
Zombie Process
When a child process terminates, the kernel retains a certain amount of information for the terminated child process if its parent process is still running. The parent process can know the child process based on this information. The child process is not completely terminated until the parent process has disposed of it. During this time, the subprocess becomes a zombie process, which still occupies a certain amount of resources.
User Termination Handler
as specified in ISO C, a process can register up to 32 functions, which are automatically called by exit.These functions are called termination handlers. Use the Atexit function to register these functions.
#include <stdlib.h>int atexit (void (*func) (void));//Successful return 0, error return not 0
The order in which exit calls these functions is the reverse of the enlistment order. The same function is called multiple times if it is registered multiple times. To determine the maximum number of termination handlers that a platform supports, you can use the sysconf function.
process Execution and termination process


executing a new program in a process
Fork is used to create a child process, and the EXEC function is used to execute another program. When the process calls exec, a completely new program replaces the current process's body, data, and stack segments. The new program starts with its main function. The PID of the new program does not change, in addition, Other features of the original process are also preserved.
There are six different exec functions: execl, EXECV, Execle, Execve, EXECLP, EXECVP;
Process Security
The permissions for a process are determined by the user and group ID of the initiating process, and sometimes for security reasons, only the process is least privileged.Programs can dynamically modify the user and group IDs that the process belongs to by using some functions that restrict the permissions of the process, 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), successful return 0, error return-1
uid_t getuid ();//user iduid_t geteuid ();//valid User idgid_t Getgid ();//actual Group idgid_t getegid ();//valid group ID
Process Environment
C program's storage space layout:Body Segment. Consists of machine instructions executed by the CPU. Typically, body segments are shared by multiple processes and are read-only.Initializing data Segments. Often called a data segment, contains all variables that are declared outside the function and explicitly assigned an initial value.non-initialized data segment. Also known as BSS (blocks starting with symbols) segment.The kernel initializes the data in this segment to a 0 or null pointer before the program begins execution.any variable declared outside the function (but not explicitly assigned to the initial value) is stored in a non-initialized data segment.Stack. The information that needs to be saved when automatic variables and function calls are placed in the stack.Heap. Dynamic memory allocations are made in the heap. Because of historical conventions, heaps are located between non-initialized data segments and stacks.

In addition to the above paragraph, there are several other types of segments. For example: a segment containing a symbol table, a segment containing debugging information, and a segment containing a dynamic shared library link table. These sections are not loaded into the program image that the process executes.
The size directive can display the length of the program's body segment, data segment, and BSS segment:
The four and fifth columns represent the total length of three segments, respectively, in decimal and hexadecimal.
Resource Limits
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 must be greater than or equal to its soft limit value. This reduction is irreversible for ordinary users. 3. Only the superuser process can increase the hard limit value. Constant rlim_infinity indicates unlimited.
Some resource types are listed below: The maximum number of bytes in the rlimit_as process available storage. Affects the SBRK function and the mmap function. The maximum number of bytes for the Rlimit_core core file, and 0 prevents the creation of the core file.
RLIMIT_CPU the maximum amount of CPU time (in seconds), the process receives a SIGXCPU signal when it exceeds this soft limit.
The maximum byte length of the Rlimit_data data segment. Includes initialization data segments, non-initialized data segments, and the sum of heaps.
Rlimit_fsize the maximum byte length of the file created. When this soft limit is exceeded, the process receives a SIGXFSZ signal.
Rlimit_locks the maximum number of file locks that can be held. This number includes the number of file leases that are unique to Linux.
The Rlimit_memlock process uses Mlock to lock the maximum byte length in the register. Rlimit_nofile the maximum number of files that can be opened. Affects the return value of the sysconf function in the parameter _sc_open_max.
Rlimit_nproc the maximum number of child processes that each actual user ID can have. Affects the return value of the sysconf function in the parameter _sc_child_max.
Rlimit_rss the maximum memory set (RSS) byte length. If the physical memory is in short supply, the kernel will retrieve more than the RSS portion from the process.
Rlimit_stackmaximum byte length of the stack.

Resource restrictions 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 the 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_LIM IT (rlimit_fsize) print_limit (rlimit_locks) print_limit (rlimit_memlock) print_limit (rlimit_nofile) PRINT_LIMIT ( Rlimit_nproc) Print_limit (Rlimit_rss) print_limit (Rlimit_stack) return 0;}


executing a 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");//deposit date into file.
Because the system calls fork, exec, and waitpid in its implementation, there are three return values. 1. If the fork fails or Waitpid returns an error other than EINTR, 1 is returned and the fault type value is set in errno. 2. If Exec fails (indicating that the shell cannot be executed), its 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 shell's terminating state.
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 various signals from the same terminal. Each process group has a unique process group ID. #include <unistd.h>pid_t getpgrp ();//The process group of the current process idpid_t Getpgid (pid_t pid);//returns the corresponding process group ID according to the PID.
Each process group has a leader process. The process ID equals the process group ID, which is the leader process.
Session
A session is a collection of one or more process groups.

Linux process detailed

Related Article

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.