Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从/proc/pid打印信息/_C_Linux - Fatal编程技术网

如何从/proc/pid打印信息/

如何从/proc/pid打印信息/,c,linux,C,Linux,今天的第二个问题第一个问题真的很有帮助 这是我的代码: #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> #inc

今天的第二个问题第一个问题真的很有帮助

这是我的代码:

#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;
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
char*getWaitChannel(int-pid);
char*getPath(int-pid);
char*getUserName(int-uid);
intgetbytes(intpid);
int main(int argc,char*argv[])
{
长x;
如果(argc<2){
//错误消息
}
x=strtol(argv[1],NULL,10);
printf(“良好1\n”);
获取_信息(x,argc,argv);
}
int get_info(pid_t pid)
{
字符路径[40],行[100],*p,stateChar[100],名称[100];
文件*statusf;
char-buf[100];
printf(“这是pid%d\n”,pid);
int-uid,vM;
snprintf(路径,40,“/proc/%d/status”,pid);
statusf=fopen(路径“r”);
if(statusf==NULL)
返回-1;
while(fgets(buf,sizeof buf,statusf)!=NULL){
sscanf(buf,“状态:%s”,stateChar);
sscanf(buf,“名称:%s”,名称);
sscanf(buf,“Uid:%d”、&Uid);
sscanf(buf,“VmPeak:%d”、&vM);
}
char*channel=getWaitChannel(pid);
char*full_path=getPath(pid);
char*user=getUserName(uid);
intb=getBytes(pid);
printf(“状态:%s\n”,stateChar);
printf(“名称:%s\n”,名称);
printf(“Uid:%d\n”,Uid);
printf(“用户名:%s\n”,用户);
printf(“最大虚拟内存:%d\n”,vM);
printf(“完整路径:%s\n”,完整路径);
printf(“写入存储层的字节:%d\n”,b);
printf(“等待通道:%s\n”,通道);
}
char*getUserName(int-uid)
{
结构passwd*pw=getpwuid(uid);
如果(pw)
{
返回pw->pw_名称;
}
返回“”;
}
int getBytes(int pid)
{   
文件*statusf2;
字符路径[40];
char-buf2[100];
int存储字节;
snprintf(路径,40,“/proc/%d/io”,pid);
状态F2=fopen(路径“r”);
if(statusf2==NULL)
返回-1;
while(fgets(buf2,sizeof buf2,statusf2)!=NULL){
sscanf(buf2,“写入字节:%d”和存储字节);
返回存储字节;
}   
}
char*getPath(int-pid)
{   
文件*statusf3;
字符路径[40];
char-buf3[100];
字符*完整路径;
snprintf(路径,40,“/proc/%d/cmdline”,pid);
状态f3=fopen(路径“r”);
如果(状态f3==NULL)
返回“”;
while(fgets(buf3,buf3的大小,statusf3)!=NULL){
sscanf(buf3,“/%s”,全路径);
返回全路径;
}
}
char*getWaitChannel(int-pid)
{   
文件*statusf4;
字符路径[40];
char-buf4[100];
字符*通道;
snprintf(路径,40,“/proc/%d/stack”,pid);
状态f4=fopen(路径“r”);
如果(状态f4==NULL)
返回“”;
while(fgets(buf4,buf4的大小,statusf4)!=NULL){
sscanf(buf4,[]%s”,通道);
返回通道;
}
}

我正在获取有关名称、状态、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;
}
}
char*getWaitChannel(int-pid)
{
文件*statusf4;
字符路径[40];
char-buf4[100];
字符*通道;
信道=malloc(1024);
/*在此处添加malloc失败的错误处理*/
snprintf(路径,40,“/proc/%d/stack”,pid);
状态f4=fopen(路径“r”);
如果(状态f4==NULL)
返回“”;
while(fgets(buf4,buf4的大小,statusf4)!=NULL){
sscanf(buf4,[]%s”,通道);
返回通道;
}
}

检查您的代码,查看是否存在任何其他变量(例如fullpath)需要内存分配,并通过注释代码的某些部分来执行逐步调试以捕获错误。

代码似乎没有将任何内容分配到
char*channel
,因此读取到它所指向的位置会调用未定义的行为。您能给出解决方案吗?我知道问题是什么,我无法访问文件和io堆栈,因为权限被拒绝。你知道如何解决这个问题吗?我认为它需要根访问权限。尝试从root用户运行程序。我正在从root用户运行程序。您是否能够使用cat命令显示内容。cat“/proc//io”。如果可以,那么程序也应该可以运行。可能出于测试目的,您可以使用getpid()使用进程本身的pid,例如用getpid()替换x。然后,当您具有根访问权限时,可以测试其他PID。获取信息(getpid(),argc,argv);