【发布时间】:2021-03-15 15:15:11
【问题描述】:
我正在使用 C 语言进行编程以获取 Ubuntu(Ubuntu 20.04 和 gcc 9.3.0)上的进程和任务的一些信息。
我用下面的代码扫描了一个已知pid的进程的任务目录:
char root_path[20] = "/proc/";
strcat(root_path, name_list[i]->d_name);
strcat(root_path, "/task");
tnum = scandir(root_path, &task_list, filter, alphasort);
其中name_list[i]->d_name 是进程的pid,形式为char*。
然后我使用 task_list 中的结果来访问这些任务的状态:
char path1[20] = "/proc/";
strcat(path1, task_list[j]->d_name);
strcat(path1, "/status");
FILE *fp = fopen(path1, "r");
这只是简单的代码。我遇到的问题是,虽然大多数任务都可以正常访问,但存在一些任务 id 使 fopen 返回一个空指针 fp。我使用 assert 语句来检测它。但是当我使用cat /proc/tid/status检查文件时,它就在那里并且可以正常访问。
当我把目录改成/proc/pid/task/tid/status后,问题依旧存在。
我想知道为什么我不能访问某个任务文件,它与其他状态文件似乎没有区别。返回空指针的任务的tid是固定的,直到我再次编译源代码。
【问题讨论】:
-
如果
fopen返回NULL,使用perror打印一条错误消息,说明为什么调用失败。 -
您可以将
strcat(); strcat();替换为snprintf(); -
您可能需要检查
errno值来查找错误原因。您可以通过if (fp == NULL) printf("Error: %s\n", strerror(errno));进行操作。确保#include<string.h>代表strerror和<errno.h>代表errno变量(它实际上是一个宏)。 -
“我使用 assert 语句来检测。”请注意,
assert应该用于调试,而不是用于错误检测。 -
@IanAbbott:当你不知道为什么甚至在哪里时,它肯定比“分段错误”要好。
标签: c ubuntu process operating-system