linux 获取进程信息

 
1:首先介绍下psinfo_t结构体
多种 proc 探测器具有 psinfo_tproc(4) 中记录的一种结构)类型的参数。DTrace 使用者可以使用的 psinfo_t 结构的定义如下所示
typedef struct psinfo {
	int     pr_nlwp;            /* number of active lwps in the process */
	pid_t   pr_pid;             /* unique process id */
	pid_t   pr_ppid;            /* process id of parent */
	pid_t   pr_pgid;            /* pid of process group leader */
	pid_t   pr_sid;             /* session id */
	uid_t   pr_uid;             /* real user id */
	uid_t   pr_euid;            /* effective user id */
	gid_t   pr_gid;             /* real group id */
	gid_t   pr_egid;            /* effective group id */
	uintptr_t pr_addr;          /* address of process */
	dev_t   pr_ttydev;          /* controlling tty device (or PRNODEV) */
	timestruc_t pr_start;       /* process start time, from the epoch */
	char    pr_fname[PRFNSZ];   /* name of execed file */
	char    pr_psargs[PRARGSZ]; /* initial characters of arg list */
	int     pr_argc;            /* initial argument count */
	uintptr_t pr_argv;          /* address of initial argument vector */
	uintptr_t pr_envp;          /* address of initial environment vector */
	char    pr_dmodel;          /* data model of the process */
	taskid_t pr_taskid;         /* task id */
	projid_t pr_projid;         /* project id */
	poolid_t pr_poolid;         /* pool id */
	zoneid_t pr_zoneid;         /* zone id */
} psinfo_t;pr_dmodel 字段设置为 PR_MODEL_ILP32(表示 32 位进程)或 PR_MODEL_LP64(表示 64 位进程)。

2:包含的头文件
#include <sys/procfs.h>


3:简单的例子
以下是根据进程号获取ID函数:

/**
函数名:nGetNameByPid
功能  :根据进程号得到启动状态
参数  :
       pid           进程号
       name          进程名
返回值:
      0          存在
      -1         处理失败
**/ 
 
int nGetNameByPid(int pid,char *name)
{
	char pcFileName[128];
	FILE *fp;
	psinfo_t ps;
	memset(&ps,'\0',sizeof(ps));
	sprintf(pcFileName,"/proc/%d/psinfo",pid);


	fp=fopen(pcFileName,"rb");
	if(fp==NULL){
		sprintf(name,"未启动");
		return(-1);	/**不存在**/
	}else{
		int i=0;
		fread(&ps,sizeof(psinfo_t),1,fp);
		if(ps.pr_fname[0]==' ' ||ps.pr_fname[0]=='\0'){
			sprintf(name,"未启动");
		}else{
			sprintf(name,"启动");
		}
		fclose(fp);
		return(0);		/**存在**/
	}
	return(0);
}

 

 
 
 
static pid_t getpidbyname (char *name)
{
        DIR           *dirHandle;  /* 目录句柄   */
        struct dirent *dirEntry;   /* 单个目录项 */
        psinfo_t      prp;
        pid_t         pid = -1;
        char          strPathName[100];
        FILE          *fp;

        memset(strPathName, 0, 100);

        if( ( dirHandle = opendir( "/proc" ) ) == NULL )
        {
                return( -1 );
        }
        chdir( "/proc" );
        /* 下面使用相对路径打开文件,所以必须进入/proc */
        while( ( dirEntry = readdir( dirHandle ) ) != NULL )
        {
                if( dirEntry->;d_name[0] != '.' && strcmp(dirEntry->;d_name,"sys") != 0)
                {
                        /* 拼加路径 */
                        sprintf(strPathName, "./%s/%s", dirEntry->;d_name, "psinfo");
                        /* 读取文件 */
                        if( ( fp = fopen( strPathName, "rb" ) ) == NULL )
                        {
                                printf("打开文件[%s]失败!\n", strPathName);
                                break;
                        }

                        /* 读取数据 */
                        if(fread(&prp, sizeof(psinfo_t), 1, fp) == -1)
                        {
                                printf("文件读取失败!\n");
                                fclose(fp);
                                break;
                        }
                        if ( strcmp( prp.pr_fname, name) == 0 )
                        {
                                pid = ( pid_t )atoi( dirEntry->;d_name );
                                break;
                        }
                        fclose( fp );
                }
        }  /* end of while */
        closedir( dirHandle );
        return( pid );
}  /* end of getpidbyname */

 
 
 
<think>我们正在讨论Linux系统中查看进程信息的方法根据引用[1]和[2]:Linux中查看进程状态信息的基本命令有ps和top引用[1]说明:ps(ProcessStatus)查看的是进程信息的一个快照,显示执行ps命令时进程信息引用[2]说明:top显示的是进程的动态信息,会看到进程信息的动态变化,并且可以自动刷新因此,我们可以详细介绍这两个命令,以及可能相关的其他命令(如htop,pgrep,pstree等)来丰富回答回答结构:1.介绍ps命令:基本用法、常用选项和示例2.介绍top命令:基本用法、交互命令和示例3.简要介绍其他相关命令(可选)4.总结注意:使用中文回答,并且按照系统级指令要求,所有行内数学表达式(如果有)使用$...$,独立公式使用$$...$$(但本问题可能没有数学公式,所以主要关注命令和代码块)同时,生成相关问题由于用户的问题是关于Linux命令,我们可以用代码块展示命令示例。另外,引用标识:在回答中引用了站内内容,所以在相关位置添加引用标识[^1][^2]</think>### 如何在 Linux 中查看进程信息Linux 系统中,查看进程信息主要通过以下核心命令实现: #### 1. **`ps` 命令** (进程快照) 用于获取当前时刻的进程静态信息快照[^1][^2]: ```bash ps aux # 查看所有用户的所有进程(BSD格式) ps -ef # 查看完整格式的进程列表(标准格式) ps -p PID # 查看指定PID的进程信息 ``` 常用字段说明: - `PID`:进程ID - `%CPU`:CPU占用率 - `%MEM`:内存占用率 - `COMMAND`:启动命令 #### 2. **`top` 命令** (实时动态监控) 提供实时刷新的进程动态信息[^1][^2]: ```bash top # 默认每3秒刷新一次 top -p PID # 监控指定进程 ``` 交互操作: - 按 `P`:按CPU使用率排序 - 按 `M`:按内存使用率排序 - 按 `k`:终止进程 - 按 `q`:退出 #### 3. **其他实用工具** - **`htop`** (增强版top): ```bash htop # 支持颜色标记和鼠标操作(需安装) ``` - **`pgrep`** (进程搜索): ```bash pgrep -l nginx # 查找nginx相关进程 ``` - **`pstree`** (进程树): ```bash pstree -p # 显示进程树结构及PID ``` #### 4. **关键信息文件** - `/proc/PID/status`:包含进程详细状态 - `/proc/PID/cmdline`:查看进程启动命令 ```bash cat /proc/1234/status # 查看PID=1234的进程状态 ``` > **总结建议**: > - 快速检查用 `ps`(静态快照) > - 实时监控用 `top/htop`(动态刷新) > - 深度分析查看 `/proc` 目录文件 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值