I am working on embedded linux. I need to send a signal to certain user space process from kernel space(kernel module). Since the PID is dynamic, I need to obtain the pid for this process to send a signal to it? How do it obtain PID of a process from its name in kernel space?
For every user process in user space there is an associated task_struct( which is a circular linked list) in kernel space.Which have all the process details ,So you can just walk through that and check for your process name.
Writing down example
for_each_process(task) {
/* compare your process name with each of the task struct process name*/
if ( (strcmp( task->comm,your_process_name) == 0 ) ) {
/* if matched that is your user process PID */
process_id = task->pid;
}
}
Processes don't necessarily have a name. A running program can have changed its name, So your plan only works if the process you try to find is collaborative and well-behaved. Try for instance to run the program below and (from a different terminal) run ps:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int pid,rc;
char *args[] = { "", "-", NULL };
pid = fork();
if (pid) {
sleep(60);
}
else {
execve( "/bin/cat" , args, NULL);
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.