LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   what happens to threads if i kill the process ? (https://www.linuxquestions.org/questions/programming-9/what-happens-to-threads-if-i-kill-the-process-575811/)

psh2001 08-09-2007 02:11 AM

what happens to threads if i kill the process ?
 
Hi ,
I am using the Linux 2.6 kernel.

I have an application which creates multiple threads.

And then i have a driver which is opened by the application process.

If i kill the process(abnormally) ,will the threads also be killed ?

Also the pid alive function shows the application process as alive .

Also 2nd question is ,the threads created in the application at some point of time have to execute in kernel context. So if i print the Process ID it shows differnet PID for each thread. So Are kernel threads and process are maintained by the same mm_struct ?

Pls let me know your comments,

Regards,
Shiv

ta0kira 08-09-2007 01:03 PM

Threads exist in the same process space as the main function. The main function of the program is a thread itself, and signals happen independently of that thread. As long as the signal is being handled (if it is,) the threads keep processing. When the default handler gets called the _exit cleanup function isn't called, so nothing gets cleaned up. The threads are just different contexts from the same memory space that the kernel switches out, so when the process is killed it just stops switching out the associated threads. If you really think about it, unless a thread has to clean something up outside of the process space (such as shared memory,) it doesn't make much of a difference for the process to be instantly terminated.
ta0kira

edit: We may be talking about different types of thread.

psh2001 08-10-2007 06:54 AM

ta0kira,

Thanks for your reply,
I have one more concern.
In linux there's no connection b/w user space threads (i.e POSIX based) and Kernel threads.
But i also heard that there's a one to one mapping for the user to kernel threads.

My concern is ,if that user thread is executing in one of the kernel thread ,and if the parent process which opened the driver get killed (this is one of the abnormal scenario's) ,what will happen to this kernel thread ?

Also in driver release function ,in normal scenario, the parent process will be having the current context.But since the parent process is killed ,one of the threads is getting the current context. Why is this so ?

Currently what i see is that the threads are not getting killed.
Also if i check the task_struct list by using the function

static inline int pid_alive(struct task_struct *p)

it shows the killed process as alive.

Pls let me know if you have any idea on this.

Regards,
Shiv

ta0kira 08-10-2007 09:51 AM

I haven't done a whole lot of kernel programming. Mostly just simple crypto modules. Sorry.
ta0kira

sundialsvcs 08-11-2007 11:21 PM

The most-appropriate design for a device driver is that any and all kernel-threads which may be associated with the driver are thought-of as belonging to the device, not to any of the user processes and/or threads which may have opened the device.

The user-land activity, no matter what it may be and no matter which or how-many threads or processes may be involved, "is simply 'customers to be served,' and nothing more." Just as a fast-food restaurant does not increase or decrease the number of employees as the crowds come and go, neither should your device driver.


All times are GMT -5. The time now is 11:07 PM.