I was programming with C language to get some information of the processes and tasks on Ubuntu(Ubuntu 20.04 and gcc 9.3.0).
I used the following code to scan the directory of the task of a process with a known 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);
where name_list[i]->d_name
is the pid of the process in the form of char*.
Then I used the result in task_list to visit the status of those tasks:
char path1[20] = "/proc/";
strcat(path1, task_list[j]->d_name);
strcat(path1, "/status");
FILE *fp = fopen(path1, "r");
It's just simple code. The problem I came cross is that although most of the tasks can be visited normally, there existed some task id that made fopen return a null pointer fp. I used assert statement to detect that. But when I use cat /proc/tid/status
to check the file, it's there and could be normally accessed.
When I changed the directory into /proc/pid/task/tid/status
, the problem still existed.
I want to know why I can't visit the certain task file, there seems no difference between it and other status files. The tid of the task that returns a null pointer is fixed until I compile the source code another time.