Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about zombie processess and threads

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?

like image 306
Amir Zadeh Avatar asked Oct 08 '10 12:10

Amir Zadeh


People also ask

How does a zombie thread exit from a process?

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).

What happens when a process becomes a zombie process?

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.

How do I remove a zombie process in Linux?

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.

What is zombie state in process table?

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?


1 Answers

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.

like image 77
Duck Avatar answered Nov 11 '22 05:11

Duck