2

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?

2 Answers 2

4

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; } } 
Sign up to request clarification or add additional context in comments.

Comments

2

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; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.