Linux Process Management <1> obtain process information functions
A process, also known as a task, is an application that dynamically uses system resources and is active.Program.
Process Management consists of process control blocks (PCB), process scheduling, interrupt management, and task queue. It is the basis of Linux File System, storage management, device management, and drivers.
The process control block PCB contains all information about the process, including the process PID, memory area occupied by the process, file descriptor, and process environment.
He uses the data structure of task_struct, which exists in include/Linux/sch. h.
Process status and conversion
# Define task_running 0 // running status # define task_interruptible 1 // waiting status (can be interrupted) # define task_uninterruptible 2 // waiting status (cannot be interrupted) # define task_stopped 4 // stop status # define task_zombie 8 // sleep status # define task_dead 16 // dead state
Basic Process operations:
1. process information retrieval function: Read the information in the PCB of the Process Control Block.
(1) getpid ()
Function: used to obtain the process ID of the current process.
Define functions:Pid_t getpid (void)
Returned value: the process identification number of the current process.
Header file: # include <unistd. h>
(2) getppid ()
Function: gets the ID of the parent process of the current process.
Define functions:Pid_t getppid (void)
Returned value: the identification number of the parent process of the current process.
Header file: # include <unistd. h>
(3) getpgid ()
Function: gets the group identification number of the PID command process. If the parameter is 0, the group identification code of the current process is returned.
Define functions:Pid_t getpgid (pid_t PID)
Returned value: if the execution is successful, the correct group ID is returned. If there is an error,-1 is returned. The error cause exists in errno.
Header file: # include <unistd. h>
(4) getpgrp ()
Function: used to obtain the group identification number of the current process. It is equivalent to getpgid (0 ).
Define functions:Pid_t getpgrp (void)
Returned value: if the execution is successful, the correct group ID is returned.
Header file: # include <unistd. h>
(5) getpriotity (void)
Function: obtains the priority of processes, Process Groups, and users.
Define functions:Int getpriority (INT which, int who)
Parameter description:
Which:
Prio_process who is the process identifier
Prio_pgrp who is the Group Identifier of the process
Prio_user who is the user identification code
Returned value: if the execution is successful, the priority of the current process (-20--20) is returned. The smaller the value, the higher the priority. If an error occurs,-1 is returned. The cause is errno.
Header file: # include <sys/resource. h>
Simple Example:
[Keven @ localhost systemcall] $ cat-N get_process_information.c
# Include <stdio. h> # include <unistd. h> # include <sys/resource. h> int main (/* int argc, char ** argv */) {printf ("this process's PID is: % d", getpid ()); printf ("/nthis process's farther PID is: % d", getppid (); printf ("/nthis process's group PID is: % d ", getpgid (getpid (); printf ("/nthis process's group PID is: % d", getpgrp (); printf ("/nthis process's priority is: % d/N ", getpriority (prio_process, getpid (); Return 0 ;}
[Keven @ localhost systemcall] $./get_process_information
This process's PID is: 6172
This process's farther PID is: 5681
This process's group PID is: 6172
This process's group PID is: 6172
This process's priority is: 0
[Keven @ localhost systemcall] $