Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zombie Threads on POSIX systems

How do zombie threads get formed in C/C++, and what do you need to make sure to do in order to prevent them from being created? I know they're just normal threads that didn't terminate properly, but I'm a little hazy on the specifics.

like image 329
John Humphreys Avatar asked Aug 08 '11 15:08

John Humphreys


People also ask

How do POSIX threads work?

The POSIX thread libraries are a standards based thread API for C/C++. It allows one to spawn a new concurrent process flow. It is most effective on multi-processor or multi-core systems where the process flow can be scheduled to run on another processor thus gaining speed through parallel or distributed processing.

Is STD thread a POSIX?

actually, std::threads provides portability across all platforms that support C++11, whereas POSIX threads is only available on POSIX platforms (or platforms that strive for some minimal compatability).

What is the zombie process in Unix operating system?

On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table: it is a process in the "Terminated state".

What causes a zombie process?

Every-time a process is removed from a Linux system, it informs its parent process about its execution. And until it has notified its parent, it stays in the process descriptor's memory. This means that a dead process isn't immediately removed and continues to hog the system's memory, hence becoming a zombie.


2 Answers

A zombie thread is a joinable thread which has terminated, but which hasn't been joined. Normally, either a thread should be joined at some time, or it should be detached. Otherwise, the OS maintains its state for some possible future join, which takes resources.

like image 83
James Kanze Avatar answered Sep 29 '22 10:09

James Kanze


Do you mean pthreads or zombie processes? A zombie process (not thread) gets created when a parent doesn't reap its child. It's because the OS keeps the return state of the process if the parent needs it later. If the parent dies, the child is given to the init thread which just sits and calls "wait" over and over again (reaping any children that die). So a zombie process can only be created when the parent is still alive and the child has terminated.

The same applies for pthreads. If you detach the thread, it will not keep that process termination state around after it finishes (similar to processes).

like image 22
Jonathan Sternberg Avatar answered Sep 29 '22 12:09

Jonathan Sternberg