Linux – Check if fork() is secure

Check if fork() is secure… here is a solution to the problem.

Check if fork() is secure

If there are multiple threads in the process, the behavior of fork() is undefined. How do I check if there is only one thread (mostly on Linux, but also interested in Windows, Darwin)?

Solution

Under Linux, the behavior of fork() is not undefined in a multithreaded process, but it does something that is not usually very helpful.

Or rather, if you fork() and don’t call exec() immediately, you run the risk of leaking unspecified resources, possibly including locks that can lead to deadlocks.

Of course, you can ask Linux (via procfs) how many threads are in the current thread group. If the answer is 1, it means that the process is single-threaded.

Related Problems and Solutions