尝试在c中实现ps命令,但出现奇怪的错误
我试图用C实现ps命令,因此我得到了很多奇怪的错误 我的代码:尝试在c中实现ps命令,但出现奇怪的错误,c,ubuntu,compiler-errors,ps,C,Ubuntu,Compiler Errors,Ps,我试图用C实现ps命令,因此我得到了很多奇怪的错误 我的代码: 我认为这是Ubuntu中预先定义的东西。有什么建议吗?关于您的错误问题: 定义char*procbuf 然后尝试检查/proc目录是否已装入,以便您可以访问或访问pid名称目录属性。另外,请阅读有关ioctl函数参数请求的更多信息,以便了解在这个问题上可以采取何种方法。procbuf是C++,因此您需要将访问它的任何代码编译为C++。请参阅示例:如果要使用c,请定义char*procbuf;然后使用calloc和realloc动态分
我认为这是Ubuntu中预先定义的东西。有什么建议吗?关于您的错误问题:
定义char*procbuf
然后尝试检查/proc目录是否已装入,以便您可以访问或访问pid名称目录属性。另外,请阅读有关ioctl函数参数请求的更多信息,以便了解在这个问题上可以采取何种方法。procbuf是C++,因此您需要将访问它的任何代码编译为C++。请参阅示例:如果要使用c,请定义char*procbuf;然后使用calloc和realloc动态分配一个大小为strlen(dirent->name)+1+6,nul字节和/proc/。您的strcat线路交换目录中也有一个错误
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/procfs.h>
#include <fcntl.h>
#include <sys/ioctl.h>
int fdproc;
DIR *dirp;
struct dirent *DirEntry;
struct elf_prpsinfo pinfo;
/*
* open the /proc directory for reading process statuses
*/
int main()
{
if ((dirp = opendir("/proc")) == (DIR *)NULL)
{
perror("/proc");
exit(1);
}
/*
* loop for each process in the system
*/
while(DirEntry = readdir(dirp))
{
if (DirEntry->d_name[0] != '.')
{
strcpy(procbuf, "/proc/");
strcat(procbuf, DirEntry->d_name);
}
if ((fdproc = open(procbuf, O_RDONLY)) < 0)
{
continue;
}
/*
* get the ps status for the process
*/
if (ioctl(fdproc, PIOCPSINFO, &pinfo) < 0)
{
close(fdproc);
continue;
}
/* process the pinfo stuff here, see
/usr/include/sys/procfs.h for details */
close(fdproc);
}
}
‘PIOCPSINFO’ undeclared
‘procbuf’ undeclared