所有进程的信息 linux,LINUX下获取所有进程信息

先上代码:

process.h

/*

* process.h

*

* Created on: 2013-5-7

* Author: jason

*/

#ifndef PROCESS_H_

#define PROCESS_H_

#include

#include

#include

#include

#include

typedef struct{

pid_t pid;

char name[256];//进程名称

int vmsize;//虚拟内存信息

}proc_info_st;//保存读取的进程信息

#define PROC_NAME_LINE 1//名称所在行

#define PROC_PID_LINE 4//pid所在行

#define PROC_VMSIZE_LINE 12//虚拟内存所在行

#define BUFF_LEN 1024 //行缓冲区的长度

#define PRO_ARR_LEN 512//the length of process array

class CProcess

{

public:

CProcess();

~CProcess();

int open_read_dir();

void read_proc(proc_info_st*, const char*);//读取进程信息

int read_line(FILE* fp,char* buff,int b_l,int l);//读取一行

void printinfo();

private:

proc_info_st info[PRO_ARR_LEN];//获取进程信息

int row;

};

#endif /* PROCESS_H_ */

process.cpp

/*

* process.cpp

*

* Created on: 2013-5-7

* Author: jason

*/

#include"process.h"

#include

using namespace std;

CProcess::CProcess(void)

{

open_read_dir();

}

CProcess::~CProcess(void)

{}

int CProcess::open_read_dir()

{

//打开目录

DIR *dir;

struct dirent *ptr;

row=0;

if (!(dir = opendir("/proc")))

return 0;

//读取目录

while (ptr = readdir(dir))

{//循环读取出所有的进程文件

if (ptr->d_name[0] > '0' && ptr->d_name[0] <= '9')

{

read_proc(&info[row++],ptr->d_name);//读取信息

}

}

return 1;

}

void CProcess::read_proc(proc_info_st* infos,const char* c_pid)

{

FILE* fp = NULL;

char file[512] = {0};

char line_buff[BUFF_LEN] = {0};//读取行的缓冲区

sprintf(file,"/proc/%s/status",c_pid);//读取status文件

if (!(fp = fopen(file,"r")))

{

printf("read %s file fail!\n",file);

return;

}

char name[32];

//先读取进程名称

if (read_line(fp,line_buff,BUFF_LEN,PROC_NAME_LINE))

{

sscanf(line_buff,"%s %s",name,(infos->name));

}

fseek(fp,0,SEEK_SET);//回到文件头部

//读取进程pid

if (read_line(fp,line_buff,BUFF_LEN,PROC_PID_LINE))

{

sscanf(line_buff,"%s %d",name,&(infos->pid));

}

fseek(fp,0,SEEK_SET);//回到文件头部

//读取进程vmsize

if (read_line(fp,line_buff,BUFF_LEN,PROC_VMSIZE_LINE))

{

sscanf(line_buff,"%s %d",name,&(infos->vmsize));

}

fclose(fp);

}

int CProcess::read_line(FILE* fp,char* buff,int b_l,int l)

{

if (!fp)

return 0;

char line_buff[b_l];

int i;

//读取指定行的前l-1行,转到指定行

for (i = 0; i < l-1; i++)

{

if (!fgets (line_buff, sizeof(line_buff), fp))

{

return 0;

}

}

//读取指定行

if (!fgets (line_buff, sizeof(line_buff), fp))

{

return 0;

}

memcpy(buff,line_buff,b_l);

return 1;

}

void CProcess::printinfo()

{

//int row=sizeof(info)/sizeof(info[0]);

for(int i=0;i!=row;i++)

{

cout<

cout<

cout<

}

}

main.cpp

#include

using namespace std;

#include"process.h"

int main(void)

{

/*char *cmd = "ps -ef";

char *buf;

int n=100;

FILE *ptr;

if((ptr=popen(cmd,"r"))!=NULL)

while(fgets(buf,n,ptr)!=NULL)

cout<

CProcess *cProcess = new CProcess;

cProcess->printinfo();

return 0;

} 文中主要用到的方法是linux下的opendir和readdir方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值