Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to threads when exec() is called?

I am taking an OS class and trying to wrap my head around this question, any help would be appreciated:

What happens to the other threads, if one of many threads within a process makes an exec() call? Why?

My understanding of exec() is that is replaces the current process with a new one, and it's main difference from fork() is that fork() creates a clone and you end up with duplicates.

So if exec() replaces the current process, would it kill the threads of the old process and replace them with the new one? Any help will be appreciated.

like image 482
GevDev Avatar asked Apr 13 '16 04:04

GevDev


1 Answers

On POSIX-compliant Unix-like systems:

A call to any exec function from a process with more than one thread shall result in all threads being terminated and the new executable image being loaded and executed. No destructor functions or cleanup handlers shall be called.

After exec* completes, there is just one thread.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html

(Incidentally, I think they meant to write "A successful call to any exec function...", as the text makes no sense for unsuccessful calls.)

like image 129
Nate Eldredge Avatar answered Sep 22 '22 13:09

Nate Eldredge