linux获取进程信息函数

来源:互联网 发布:淘宝代购是真的吗 编辑:程序博客网 时间:2024/05/21 17:43

Linux进程的管理 <一>获取进程信息函数

进程又称任务,是一个动态的使用系统资源、处于活动状态的应用程序。
进程的管理由进程控制块PCB、进程调度、中断管理、任务队列等组成,它是linux文件系统、存储管理、设备管理和驱动程序的基础。
进程控制块PCB中包含了进程的所有信息,主要包括进程PID、进程所占有的内存区域、文件描述符和进程环境等信息。
他用task_struct的数据结构表示,存在于include/linux/sch.h

进程状态及转换

#define TASK_RUNNING 0 //运行状态#define TASK_INTERRUPTIBLE 1 //等待状态(可被中断)#define TASK_UNINTERRUPTIBLE 2  //等待状态(不可被中断)#define TASK_STOPPED 4  //停止状态#define TASK_ZOMBIE 8  //睡眠状态#define TASK_DEAD 16  //僵死状态

进程的基本操作,六大类:

1.获取进程信息函数:主要通过读取进程控制块PCB中的信息。
(1)getpid()
功能:用来获取目前进程的进程标识。
定义函数:pid_t getpid(void)
返回值:返回当前进程的进程识别号。
头文件:#include <unistd.h>

(2)getppid()
功能:用来获取目前进程的父进程标识。
定义函数:pid_t getppid(void)
返回值:返回当前进程的父进程识别号。
头文件:#include <unistd.h>

(3)getpgid()
功能:用来获得参数pid指令进程所属于的组识别号,若参数为0,则返回当前进程的组识别码。
定义函数:pid_t getpgid(pid_t pid)
返回值:执行成功则返回正确的组识别码,若有错则返-1,错误原因存在于errno中。
头文件:#include <unistd.h>

(4)getpgrp()
功能:用来获得目前进程所属于的组识别号,等价于getpgid(0)。
定义函数:pid_t getpgrp(void)
返回值:执行成功则返回正确的组识别码。
头文件:#include <unistd.h>

(5)getpriotity(void)
功能:用来获得进程,进程组和用户的进程执行优先权。
定义函数:int getpriority(int which,int who)
参数含义:
which:
PRIO_PROCESS   who为进程的识别码
PRIO_PGRP     who为进程的组识别码
PRIO_USER     who为用户识别码
返回值:执行成功则返回当前进程的优先级(-20--20),值越小优先级越高。若出错则返-1,原因在errno中。
头文件:#include <sys/resource.h>

 

简单实例:

[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]$ 
       
 

 

原创粉丝点击