Process-related operations under Linux

Source: Internet
Author: User
Tags session id

I. Definition and understandingNarrowly defined: A process is an instance of a running program. Generalized definition: A process is a running activity of a program with certain independent functions about a data set. The concept of the process is mainly two points: first, the process is an entity.each process has its own address space, which, in general, includes the text area, data region, and stack area. The text area stores the code executed by the processor, the data region stores variables and the dynamically allocated memory used during process execution, and the stack area stores the instructions and local variables for the active procedure call. Second, the process is an "executing procedure". A program is an inanimate entity that can become an active entity when the processor is given the program's life, which we call a process. Properties of the process
Process identifier: Process ID, kernel allocation, the flag printable negative, range 0~32767
Parent process and parent process ID (PPID)
The user ID (UID) of the initiating process and the group (GID) to which it belongs;
Valid user ID and valid group ID for the process
Process group ID for a process: a process can belong to a process group.
Session ID of the process: each process belongs to a unique session.
Process state: The state is divided into running R, hibernation s, blocking Z;
The priority of the process execution; the name of the terminal to which the process is connected;
Process resource consumption: such as resource size (memory, CPU usage); II. related operations of the process (1) Get Process properties and change propertiesGet:
Getpid (void) Gets the current process ID
Getppid (void) Gets the parent process ID of the current process
Getpgrp (void) Gets the process group ID of the current process
Getpgid (pid_t pid) gets the process group idgetuid (void) Gets the user ID of the current process
Geteuid (void) Gets the valid user ID for the current process
Getgid (void) Gets the user group ID of the current process
Getegid (void) Gets the valid user group ID for the current process
GetSID (pid_t PID) gets the session ID setting for the current process: int setpgid (pid_t pid,pid_t pgid)
Setpgid () sets the parameter PID to the group ID that the process belongs to, and sets the group identifier specified by the parameter pgid. If the parameter PID is 0, it is used to set the current process's group ID, and if the parameter pgid is 0, the
The process ID of the current process to replace.
int setpgrp (void)
SETPGRP () sets the group ID to which the current process belongs to the process ID of the current process. This function is equivalent to calling Setpgid (0,0).
pid_t setsid (void) Setsid function, the process that invokes the function will create a new session, session, and process group ID for the calling process, which will be set as the process ID of the calling process, and return the process ID
int setuid (uid_t uid) sets the user ID of the process
int Setreuid (uid_t ruid, uid_t euid) sets the actual user ID of the process to Ruid, and the valid user ID is set to Euid
int Seteuid (uid_t uid) sets the valid user ID of the process to UID
int Setgid (gid_t gid) sets the group ID of the process
int Setregid (gid_t rgid, gid_t egid) sets the actual group ID of the process to Rgid, the valid group ID is set to Egid
int Setegid (gid_t gid) sets the effective group ID process priority for the process: the Linux system runs concurrently for multiple processes, and Linux uses the time-slice rotation process scheduling method. The priority of the process defines the priority of the process being dispatched, and the lower the priority value, the higher the priority.
Linux is used with nice system to modify the priority of the process, by default, the priority of the process is 0, the system allows the priority range is: -20~2
int Nice (int inc)
Nice () is used to change the process execution priority of the process.  The higher the parameter Inc value, the higher the precedence order, which means that the process execution is slower. Only the superuser can use the negative Inc value, which precedes the order of precedence and the process executes more quickly. The return value returns 0 if the execution succeeds, otherwise returns 1, and the reason for the failure is stored in errno. Error code eperm General user attempts to switch to negative parameter Inc value to change process precedence. Nice system calls can only be used to modify the priority of the process itself.
SetPriority (Set program Process execution priority) defines the function int setpriority (int which,int who, int prio);
The function Description SetPriority () can be used to set process execution priorities for processes, process groups, and users. parameter which has three kinds of values, and the WHO is defined according to which value which the meaning of the WHO representative prio_process who is the process ID
Prio_pgrp who is the group ID for the process
Prio_user Who as User ID
The parameter prio is between 20 and 20. On behalf of process execution priority, the lower the number represents a higher priority, and execution will be more frequent.
This priority is 0 by default, and only Superuser (root) allows this value to be lowered. The return value returns 0 if the execution succeeds, or 1 if there is an error, and the reason for the error is stored in errno. Esrch parameter which or who may be wrong, and cannot find a conforming process einval parameter which value error. Eperm Not enough permissions to complete Setup eacces
The general user cannot lower the priority int getpriority (int which,int who);
The function Description GetPriority () can be used to get process execution precedence for processes, process groups, and users. parameter which has three kinds of values, and who has different definitions according to which value
which the meaning of the WHO representative prio_process who is the process ID
Prio_pgrp who is the group ID for the process
Prio_user Who as User ID
The value returned by this function is between 20 and 20, representing the priority of the process execution, and the lower the value, the higher the precedence, and the more frequent the execution. The return value returns the Process execution priority, or 1 if an error occurs, and the reason for the error is stored in errno. Additional instructions because the return value may be-1, you should also check whether the errno has the wrong reason. It is best to clear the errno variable before calling the secondary function. Error code esrch parameter which or who may be wrong and cannot find a compliant process. EINVAL parameter which value error. (2) Create a process1, int fork (void); return value: 0 in child process, parent process returns child process ID, error returns-1
Function Description: An existing process can call the fork function to create a new process. A new process created by Fork is called a subprocess (child process).
The fork function is called once but returns two times. The only difference of two returns is that the child process ID is returned in the parent process with a value of 0.
A child process is a copy of the parent process that obtains a copy of the parent process's data space, heap, stack, and so on.
Note that the child process holds a "copy" of the above storage space, which means that the storage space is not shared between parent and child processes, and that only the code snippets are shared between them. In-depth understanding can be found in: Linux fork () functions in detail http://os.chinaunix.net/a2012/0203/1306/000001306508.shtml2, int vfork (void); Return value: 0 is returned in the child process, the parent process returns the child process ID, and the error returns-1 vfork is roughly the same as the fork, the difference is as follows:
1) Fork to copy the data segment of the parent process, while Vfork does not need to completely copy the data segment of the parent process, and the child process shares the data segment with the parent process before the child process calls exec and exit
2) fork does not restrict the execution order of the parent-child process; In the vfork call, the child process runs first and the parent process hangs until the child process calls exec or exit, and the order of the parent-child process is no longer restricted (3) EXEC function familyusing the EXEC function family in a sub-process after fork, you can load and run other programs (the child process replaces the original process, and the parent process does something different). Fork creates a new process that generates a new pid,exec to start a new program, replacing the original process, so the PID of the new exec-executed process does not change (as with the PID of the exec process). For use, see: Linux Process Control--exec function family http://www.cnblogs.com/hnrainll/archive/2011/07/23/2114854.html (4) Process wait1, pid_t Wait (int * status);
Function Description:the Wait () function works by first judging whether a child process exists, that is, whether a child process was successfully created. If the creation fails, it exits directly and prompts the relevant error message, and returns-1;If the creation succeeds, wait () suspends the parent process until the child process ends and returns the state and PID at the end of the child process.If there are child processes, the end state at exit (status) has the following two types:1) The child process ends normally: Call exit (0). 2) The signal causes the child process to end: such as Call Kill (pid_t pid, int sig);
The parameter status can be set to NULL if the end state value is not the intention.2, pid_t waitpid (pid_t pid,int * status,int options);
Return value: Returns the child process identifier (PID) if execution succeeds, or 1 if an error occurs. The reason for failure is stored in errno.
Function Description: Waitpid () temporarily stops execution of the current process until a signal arrives or the child process ends. If the child process has ended when you call Wait (), then wait () returns the child process end status value immediately.
The end state value of the child process is returned by the parameter status, and the process identification code of the child process is returned quickly. The parameter status can be set to NULL if the end state value is not the intention. Parameter PID is to wait for the child process identification code, the other numerical significance is as follows:
Pid<-1 waits for the process group identifier to be the PID absolute value of any child processes.
Pid=-1 waits for any child process, equivalent to wait ().
Pid=0 waits for the process group identifier to be the same as the current process for any child processes.
Pid>0 waits for any child process identification code to be a child of PID.
The parameter option can be 0 or the following or combination:
Wnohang If there are no completed sub-processes then return immediately, not to wait.
wuntraced If a child process enters a paused execution, it returns immediately, but the end state is ignored.
The end state of the child process is returned and stored in status, with a few macros at the bottom to determine the end condition:
Wifexited (status) is a non-0 value if the child process ends normally.
Wexitstatus (status) Gets the end code returned by the child process exit (), typically using wifexited to determine whether the macro ends properly before it can be used.
wifsignaled (status) This macro value is true if the child process ends because of a signal.
Wtermsig (status) Gets the signal code that the child process aborts because of the signal, generally uses wifsignaled to judge before using this macro.
wifstopped (status) This macro value is true if the child process is in a paused execution condition. This is generally the case only if you are using wuntraced.
Wstopsig (status) Gets the signal code that causes the child process to pause, usually using wifstopped to judge before the macro is used. (5) Process communicationNow, the way Linux uses inter-process communication:
1. Pipeline (pipe) and well-known pipe (FIFO) 1) pipeline (pipe) Introduction A. The pipe is half-duplex, the data can only flow in one direction; need to establish two pipelines when both sides are required to communicateB. Can only be used between parent-child processes or sibling processes (affinity processes);C. A separate file system: A pipeline is a file for the process at both ends of the pipe, but it is not a normal file, it is not a file system, it is a separate file system, and only exists in memory. D. Read and Write data: a process that writes to the pipeline is read out by the process at the other end of the pipeline. The content that is written is added to the end of the pipe buffer every time, and the data is read from the head of the buffer each time. 2) pipeline creation Create a nameless pipe that can use system callspipe (). It takes a parameter, which is an array that consists of two integers. If the system call succeeds, this array will include the two file descriptors used by the pipeline. After you create a pipeline, the process typically produces a new process. System calls:pipe ();prototype:int pipe (int fd[2]); return value: Returns 0 if the system call succeeds . If the system call fails , return-1: errno=emfile (file descriptor with no empty pro)   Emfile (System File Table full)       &NBSP;&NBSP;&NBSP;  efault ( Note: fd[0] for reading pipelines, FD[1] is used to write to the pipeline. 3) Introduction of famous pipelinesNameless pipes, because there is no name, can only be used for inter-process communication of affinity. To overcome this shortcoming, a famous pipeline (FIFO) is proposed. FIFO differs from nameless pipeis that it provides a path name associated with it, in a FIFO file form in the file system, so that even if there is no affinity with the FIFO creation process, as long as the path can be accessed to each other through the FIFO to communicate with each other, therefore, Processes that are not related to FIFO can also exchange data. The value of the note is that FIFO strictly adheres to first-in-a-go (initial-out), and the pipeline and FIFO reads always return data from the beginning, and their writes add data to the end. They do not support file location operations such as Lseek (). Note: The name of a named pipe exists in the file system, and the contents are stored in memory

4) famous pipeline creation

#include <sys/types.h>

#include <sys/stat.h>

int Mkfifo (const char *pathname,mode_t mode);

Returns: 0 if successful , 1 If error is returned

Once you have created a FIFO with Mkfifo , you can open it by opening it. Indeed, general file I/O functions (Close,read,write,unlink, etc.) are available for FIFO. When a FIFO is opened , the non-blocking flag (O_nonblock) has the following effects:

A, in the general case (without stating O_nonblock), read-only open to block to some other process to open this FIFO for write. Similarly, opening a FIFO for writing to block to some other process opens it for reading.

B, if it refers to o_nonblock, then read-only open to return immediately. However, if no process has opened a fifo for reading, then only write-Open will return an error, its errno is enxio. Similar to a pipeline, if you write a fifo that has not yet been read by the process, the signal sigpipe is generated. If a fifo the last write process to close the fifo, then the fifo produces a file end flag.

FIFO related error message: eacces (no access) eexist (The specified file does not exist)
Enametoolong (path name is too long)
ENOENT (contains a directory that does not exist)
ENOSPC (Insufficient file system space)
Enotdir (Invalid file path)
Erofs (The specified file exists in the read-only file system)

a pipeline is a method of communicating based on a file descriptor. (so some functions about file operations are used) Pipe Understanding can be found in http://blog.chinaunix.net/uid-26833883-id-3227144.htmlUse the method to seethe road to codingThe http://blog.sina.com.cn/s/blog_8713d5b20100tmod.html
2. The signal (signal) can be found in detail in the following Daniel's blog: http://blog.sina.com.cn/s/blog_8713d5b20100tnes.htmlhttp://blog.sina.com.cn/s/blog_ 8713d5b20100tng1.html
3. Message Queuing http://blog.sina.com.cn/s/blog_8713d5b20100tolj.html
4. Shared Memory http://blog.csdn.net/ljianhui/article/details/10253345
5. Signal Volume http://blog.csdn.net/ljianhui/article/details/10243617
6 socket (socket) http://blog.csdn.net/ljianhui/article/details/10477427http://blog.csdn.net/ljianhui/article/details/10697935 This article refers to the content:1. Baidu Encyclopedia http://baike.baidu.com/link?url=DS_T9YE1tj5vUSag30iXUrKILrWrRqgXkRH8wVMr-9lM7_U96qMyy3VwkW0ucN_ Bdfhe5fdia7psqw6m3bzx3k#2_22. JK1860 's column http://blog.csdn.net/jk1860/article/details/61467393, HTTP://BLOG.CHINAUNIX.NET/UID-26833883-ID-3227144.HTML4, Http://blog.sina.com.cn/s/blog_ 8713D5B20100TMOD.HTML5, http://blog.csdn.net/ljianhui/article/details/10697935

Process-related operations under Linux

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.