Process id recycling and status of tasks
Hello,
1. What would be an ideal way to check if a task is alive or dead from kernel space?
I dont want to check current execution state of the task, rather I want to check whether task
with this pid is existing currently or not? I might want to do it periodically, hence even though
a certain pid might exist, it might have got recycled by the time I check again, right?
Following is an elementary attempt to achieve the same, but I am not convinced much, and
I am not even sure I can do this kind of thing without knowing the context I am executing in.
task_t *p;read_lock(&tasklist_lock);for_each_process(p) {
if ( strcmp (p->comm, $your-daemon-name) == 0)
break;
}read_unlock(&tasklist_lock);
2. Basically, what would be parameters of a task which will uniquely identify a task which ever
existed on my machine. Would it be <name, pid> tuple? Or something else which uniquely
does this job? Basically differentiate N user space threads in a process.
3. What is the algorithm followed for recycling pids? It does not look very straight fwd like
pick up the latest freed pid for new task etc. I guess the pid recycling must be starting after
certain threshold.
Any pointers will be helpful.
-Leo.
Quoted 2 lines · Leonidas .
1. What would be an ideal way to check if a task is alive or dead from kernel space?
Get a reference to the task's pid (call get_task_pid(), or get_pid() on the return value of task_pid()), then later check whether pid_task() works.
(pid_alive() is not what you want because this would require that you hold a reference to the task_struct.)
Quoted 2 lines
even though a certain pid might exist, it might have got recycled by the time I check again, right?
The functions above do not work with PID numbers but with struct pid which is a reference-counted object. (The functions with "get" in their name increase the reference count, so don't forget to put_pid() when you no longer need it.) See also the big comment in include/linux/pid.h.
HTH
Clemens
Quoted 14 lines · Clemens Ladisch
Quoted 2 lines · Leonidas .
1. What would be an ideal way to check if a task is alive or dead from kernel space?
Get a reference to the task's pid (call
get_task_pid(), orget_pid()on the return value oftask_pid()), then later check whetherpid_task()works.(
pid_alive()is not what you want because this would require that you hold a reference to thetask_struct.)Quoted 2 lines
even though a certain pid might exist, it might have got recycled by the time I check again, right?
Quoted 9 lines
The functions above do not work with PID numbers but with struct pid which is a reference-counted object. (The functions with "get" in their name increase the reference count, so don't forget to
put_pid()when you no longer need it.) See also the big comment ininclude/linux/pid.h.HTH
Clemens
Yes, the comment in pid.h says it.
Was going through pid.c, what is the fundamental difference between pid_task() and get_pid_task()? Is it correct to say that, get_pid_task() will check whether the
task struct is stale or not and return accordingly and pid_task() will blindly returntask_struct which might be stale?
Now my understanding is get_pid_task() should be followed by put_pid_task() so the reference counting work as expected, but put_pid_task() is not an exported symbol? Am I missing here something?
The obvious question which follows from above is what would be the correct way to determine whether a process is alive or not using pid_alive()? Using pid_task() does not seem correct and seemingly correct way looks unfeasible.
-Leo.
Quoted 4 lines
The obvious question which follows from above is what would be the correct way to determine whether a process is alive or not using
pid_alive()? Usingpid_task()does not seem correct and seemingly correct way looks unfeasible.
I guess, just checking task_struct->exit_state will be okay to do, right? If exit
state is DEAD then the task is stale, might avoid using the reference counting counterparts, right?
-Leo.
Quoted 6 lines · Leonidas .
Quoted 2 lines · Clemens Ladisch
Get a reference to the task's pid (...), then later check whether
pid_task()works.Was going through
pid.c, what is the fundamental difference betweenpid_task()andget_pid_task()?
As I said, the functions with "get" in their name increase the reference count. In other words, get_pid_task(p) is implemented as get_task(pid_task(p)), so, if it succeeded, you would then own a reference to the task.
Quoted 3 lines
Now my understanding is
get_pid_task()should be followed byput_pid_task()so the reference counting work as expected, butput_pid_task()is not an exported symbol?
You'd have a standard task_struct, so you'd just use put_task(). And if you don't actually want a reference to the task (because you only want to check whether the task still exists), just use pid_task() instead.
HTH
Clemens