试图在 c 中实现 ps 命令但出现奇怪的错误
trying to implement ps command in c but getting strange errors
我正在尝试在 C 中实现 ps 命令,因此出现了很多奇怪的错误。
我的代码:
#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);
}
}
我在 Internet 上的某个地方找到了这段代码,在执行了一些操作之后,我仍然遇到一些奇怪的错误,例如:
‘PIOCPSINFO’ undeclared
‘procbuf’ undeclared
我认为这是 Ubuntu 中预先定义的内容。有什么建议吗?
针对您的错误问题:
定义 char *procbuf
然后尝试检查 /proc 目录是否已安装,以便您可以访问或 pid 名称目录 attributes.also 阅读更多关于 ioctl 函数的参数 requests 以便您来知道什么方法可以让你在这个问题上走得更远。
我正在尝试在 C 中实现 ps 命令,因此出现了很多奇怪的错误。
我的代码:
#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);
}
}
我在 Internet 上的某个地方找到了这段代码,在执行了一些操作之后,我仍然遇到一些奇怪的错误,例如:
‘PIOCPSINFO’ undeclared
‘procbuf’ undeclared
我认为这是 Ubuntu 中预先定义的内容。有什么建议吗?
针对您的错误问题:
定义 char *procbuf
然后尝试检查 /proc 目录是否已安装,以便您可以访问或 pid 名称目录 attributes.also 阅读更多关于 ioctl 函数的参数 requests 以便您来知道什么方法可以让你在这个问题上走得更远。