views:

33

answers:

1

Hi, i had these questions in my mind since i was reading some new topics on processes and threads. I would be glad if somebody could help me out.

1) What happens if a thread is marked uncancelable, and then the process is killed inside of the critical section?

2) Do we have a main thread for the program that is known to the operating system? i mean does the operating system give the first thread of the program some beneficial rights or something?

3) When we kill a process and the threads are not joind, do they become zombies?

+2  A: 

First, don't kill or cancel threads, ask them to kill themselves. If you kill a thread from outside you never know what side effects - variables, state of synchronization primitives, etc.- you leave behind. If you find it necessary for one thread to terminate another then have the problematic thread check a switch, catch a signal, whatever, and clean up its state before exiting itself.

1) If by uncancelable you mean detached, the same as a joined thread. You don't know what mess you are leaving behind if you are blindly killing it.

2) From an application level viewpoint the primary thing is that if the main thread exits() or returns() it is going to take down all other threads with it. If the main thread terminates itself with pthread_exit() the remaining threads continue on.

3) Much like a process the thread will retain some resources until it is reaped (joined) or the program ends, unless it was run as detached.

RE Note: The threads don't share a stack they each have their own. See clone() for some info on thread creation.

Duck
Thanks for your answer, first i don't mean detached, i mean when it marks itself as PTHREAD_CANCEL_DISABLE. Also by "each have their own" you mean like processes stack or are there diffrences?
Green Code
Sorry about (1). I don't think the cleanup handlers get called if the process is killed rather than canceled. IOW the cancellation points are operative via the pthread library; they won't prevent the thread getting smacked by the OS. It will make for a nice little weekend test project though. AFAIK there are no differences in the stack, it's just a chunk of contiguous memory for each thread.
Duck