![](/img/trans.png)
[英]Getting pid and other process information from /proc/<pid>/status
[英]How to print information from /proc/pid/
今天的第二个问题,第一个问题确实有帮助。
所以这是我的代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/procfs.h>
#include <sys/fcntl.h>
#include <pwd.h>
char *getWaitChannel(int pid);
char *getPath(int pid);
char *getUserName(int uid);
int getBytes(int pid);
int main(int argc, char *argv[])
{
long x;
if (argc < 2){
//error message
}
x = strtol(argv[1], NULL, 10);
printf("Good 1\n");
get_info(x, argc, argv);
}
int get_info(pid_t pid)
{
char path[40], line[100], *p, stateChar[100], Name[100];
FILE* statusf;
char buf[100];
printf("This is pid %d\n", pid);
int uid, vM;
snprintf(path, 40, "/proc/%d/status", pid);
statusf = fopen(path, "r");
if(statusf == NULL)
return -1;
while(fgets(buf,sizeof buf, statusf) != NULL){
sscanf(buf, "State: %s", stateChar);
sscanf(buf, "Name: %s", Name);
sscanf(buf, "Uid: %d", &uid);
sscanf(buf, "VmPeak: %d", &vM);
}
char *channel = getWaitChannel(pid);
char *full_path = getPath(pid);
char *user = getUserName(uid);
int b = getBytes(pid);
printf("State: %s\n", stateChar);
printf("Name: %s\n", Name);
printf("Uid: %d\n", uid);
printf("Username: %s\n", user);
printf("Max Virtual Memory: %d\n", vM);
printf("Full Path: %s\n", full_path);
printf("Bytes written to storage layer: %d\n", b);
printf("Waiting channel: %s\n", channel);
}
char *getUserName(int uid)
{
struct passwd *pw = getpwuid(uid);
if (pw)
{
return pw->pw_name;
}
return "";
}
int getBytes(int pid)
{
FILE* statusf2;
char path[40];
char buf2[100];
int storage_bytes;
snprintf(path, 40, "/proc/%d/io", pid);
statusf2 = fopen(path, "r");
if(statusf2 == NULL)
return -1;
while(fgets(buf2,sizeof buf2, statusf2) != NULL){
sscanf(buf2, "write_bytes: %d", &storage_bytes);
return storage_bytes;
}
}
char *getPath(int pid)
{
FILE* statusf3;
char path[40];
char buf3[100];
char *fullpath;
snprintf(path, 40, "/proc/%d/cmdline", pid);
statusf3 = fopen(path, "r");
if(statusf3 == NULL)
return "";
while(fgets(buf3,sizeof buf3, statusf3) != NULL){
sscanf(buf3,"/ %s", fullpath);
return fullpath;
}
}
char *getWaitChannel(int pid)
{
FILE* statusf4;
char path[40];
char buf4[100];
char *channel;
snprintf(path, 40, "/proc/%d/stack", pid);
statusf4 = fopen(path, "r");
if(statusf4 == NULL)
return "";
while(fgets(buf4,sizeof buf4, statusf4) != NULL){
sscanf(buf4,"[<c0227f4e>] %s", channel);
return channel;
}
}
我正在获取有关名称、状态、UID 和用户名以及 VmPeak 的信息。 他们正在按照我想要的方式工作。 但其他 3 个是我似乎无法使它们工作的问题,我无法弄清楚原因(完整路径,写入存储层和等待通道的字节数)。 所以我的问题是如何访问它们并打印信息。
如 alk 所示,修改 getWaitChannel() 为通道分配内存。 当不需要通道数据时,您需要稍后释放内存。
char *getWaitChannel(int pid)
{
FILE* statusf4;
char path[40];
char buf4[100];
char *channel;
channel = malloc(1024);
/* Add error handling for malloc failure here */
snprintf(path, 40, "/proc/%d/stack", pid);
statusf4 = fopen(path, "r");
if(statusf4 == NULL)
return "";
while(fgets(buf4,sizeof buf4, statusf4) != NULL){
sscanf(buf4,"[<c0227f4e>] %s", channel);
return channel;
}
}
检查您的代码并查看是否有任何其他变量(例如全路径)需要内存分配,并通过注释代码的某些部分来执行逐步调试以捕获错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.