Linux process identification methods

In Linux, there are many processes running at the same time, there are two ways to identify these processes:

  • Process descriptor address
  • Process identifier

For each individual process in the current operating system, its process descriptor corresponding address and the process identifier is unique.

1. process descriptor

To manage the process, Linux kernel must understand the current status of the implementation of each process, these states include the priority of processes, operational status of the process, the process of allocating address space. Thus, Linux kernel provides a process descriptor structure task_struct type (process descriptor) to store the information.

Linux kernel provides a process for storing an array of task descriptors, all of which contain pointers pointing system task_struct structure. The maximum number of processes in the system is limited by the size of the array task, the default value is typically 512.

When you create a new process, allocating memory in Linux systems from a task task_struct structure and add it to the array. The current structure with the current running process pointer to indicate.

Here are the main structure and a description of the process descriptor:

struct task struct 
{
    yolatile long state;
    //进程的运行时状态,-1代表不可运行,0代表可运行,>0代表已停止。    
    unsigned int flags;    
    //flags 是进程当前的状态标识,具体说明如下:    
    //0x00000002 表示进程正在被创建    
    //0x00000004 表示进程正准备退出    
    //0x00000040 表示此进程被 fork 出,但是并没有执行 exec    
    //0x00000400 表示此进程由于其他进程发送相关信号而被杀死    
    unsigned int rt_priority;    
    //进程的运行优先级    
    truct list_head tasks;    
    //list_head 结构体    
    struct mm_struct *mm;    
    //内存使用的相关情况    
    int exit_state;    
    int exit_code, exit_signal;    
    pid_t pid;    
    //进程标识号    
    pid_t tgid;    
    //进程组号    
    struct task_struct *real_parent;    
    //real_parent 是该进程的“亲生父亲”,不管其是否被 “寄养”    
    struct task_struct *parent;    
    //parent 是该进程现在的父进程,有可能是“继父”    
    struct list_head children;      
    //children 指的是该进程孩子的链表,可以得到所有子进程的进程描述符    
    struct list_head sibling;      
    //sibling 是该进程兄弟的链表,也就是其父亲的所有孩子的链表,用法与 children 相似          
    struct task_struct *group_leader;    
    //主线程的进程描述符    
    struct list_head thread_group;    
    //进程所有线程的链表    
    处理器 time_t utime, stime;    
    //进程相关时间    
    struct timespec start_time;    
    struct timespec real_ start_time;    
    //进程启动时间    
    char comm[TASK_COMM_LEN];    
    //这个是该进程所有线程的链表    
    int link_count, total_link_count;    
    //文件系统信息计数    
    struct thread_struct thread;    
    //特定处理器下的状态    
    struct fs_struct *fs;    
    //文件系统相关信息结构体    
    struct files_struct * files;    
    //打开的文件相关信息结构体    
    struct signal_struct *signal;    
    struct sighand_struct sighand;    
    //信号相关信息的句柄    
    unsigned long timer_slack_ns;    
    unsigned long default_timer_slack_ns;    
    //松弛时间值,用来规定 select() 和 poll() 的超时时间,单位是纳秒
};

2. The process identifier

Process identifier (Process ID) is the most important part of the process descriptor in the current in the Linux system is only a non-negative integer, and corresponds to a unique identifier for the process.

Linux kernel uses a data type to store process pid_t process identifier, the data type is actually a 32-bit unsigned integer data. Process identifiers are numbered consecutively, before the value of the process is generally a process of adding an identifier. Process identifier can be reused, when a process is recycled, its identifier over time and can be used again. On the Linux kernel are usually allowed to use the process identifier is 0 to 32767.

In Linux, there are several special process identifier corresponding to the process.

  • Process identifier 0: corresponds to the exchange process (swapper), calling for the execution of multiple processes.
  • Process identifier 1: corresponds to the initialization process (init), when the bootstrap process is completed by the kernel calls, the corresponding file is / sbin / init, Linux is responsible for the start of work, the process is not terminated during system operation It can be said that the current operating system, all processes are derived from this process.
  • Process identifier 2: page may correspond daemon (pagedaemon), for operating a virtual memory paging system.

Use the command ps -aux can view the process identifier systems currently running processes and other information.

You can use getpid series function to get the process identifier of the current process.

#include <sys/types.h>
#include <unistd.h>
pid_t getpid(void);
pid_t getppid(void);
  • getpid function is used to obtain the current process identifier of the calling process.
  • getppid processes currently used to obtain the calling process's parent process identifier.

3. Linux user process

Process also has corresponding actual user ID, real group ID, a valid user ID, effective group ID, a For these users, there are also a corresponding process for each identifier, Linux provides a function for acquiring the corresponding identifiers.

 #include <unistd.h>
 #include <sys/types.h>
 uid_t getuid(void);
 uid_t geteuid(void);
 gid_t getgid(void);
 gid_t getegid(void);
  • getuid: return process actual user identifier.

  • geteuid: it returns a valid user ID of the calling process.

  • getgid: Returns the real group identifier of the calling process.

  • getegid: Returns the effective group identifier of the calling process.

4. The process identified by the acquired instance

Published 71 original articles · won praise 131 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_43239560/article/details/102830781