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?
It deallocates the resources used by the thread but keeps an entry in the thread/process table. Theoretically, the zombie thread exits from this status by executing a _join (POSIX).
This may not be a serious problem if there are a few zombie processes but under heavier loads, this can create issues for the system such as running out of process table entries. The zombie processes can be removed from the system by sending the SIGCHLD signal to the parent, using the kill command.
The exit status of the zombie process zombie process can be read by the parent process using the wait () system call. After that, the zombie process is removed from the system. Then the process ID and the process table entry of the zombie process can be reused.
Hence, there remains an entry in the process table even after the termination of the child. This state of the child process is known as the Zombie state. Here the entry [a.out] defunct shows the zombie process. Why do we need to prevent the creation of the Zombie process?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With