1

I'm a little bit confused about when the all process exit:

  1. When the main thread exits - do we exit from all process?
  2. When one of the threads calls exit(1) - do we exit exit from all process?
  3. When one of the threads calls return 0 - do we exit exit from all process?

    Is it different if the main thread calls it or another thread?

  4. When one of the threads calls return NULL - do we exit exit from all process? Is it different if the main thread calls it or another thread?
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
user3479031
  • 57
  • 1
  • 8
  • atleast try to bullet your questions. I got confused. – Sourav Ghosh Jul 03 '15 at 06:56
  • I invite @coolguy to see this update..:P – Sourav Ghosh Jul 03 '15 at 07:04
  • The answer to at least the first question is "it's complicated", since before it was standardized each C runtime did differently. Similarly, the `exit` function was remapped to the `exit_group` syscall on glibc 2.3, before it invoked the `exit` syscall, which only quit the current task (=thread). – Matteo Italia Jul 03 '15 at 07:08

2 Answers2

2
  1. When the main thread exits - do we exit from all process?

As already explained here, when the main thread (or any thread) calls exit, or when the initial invocation of main returns, the entire process exits. But the main thread can exit with pthread_exit without affecting other threads.

  1. When one of the threads calls exit(1) - do we exit exit from all process?

Yes, we do.

  1. When one of the threads calls return 0 - do we exit exit from all process?

No, we don't. We only return back from that thread.

From the link, A thread can terminate its execution in the following ways:

  • By returning from its first (outermost) procedure, the threads start routine
  • By calling pthread_exit(), supplying an exit status
  • By termination with POSIX cancel functions

The void pthread_exit(void *status) is used terminate a thread in a similar fashion the exit() for a process.

  1. When one of the threads calls return NULL - do we exit exit from all process?

It's same as calling return 0, as NULL is defined to be 0. So, same answer as for the above question.

4b. *Is it different if the main thread calls it (return 0 or return NULL) or another thread? *

Yes, if main thread calls, refer answer to question 1 and if another thread calls, refer answer to question 3.

In nutshell, if main exits or returns, it makes the entire process to exit. To avoid this, pthread_exit can be used which waits till the last thread terminates. If a thread calls exit(), it makes the entire process to exit, and if it calls return NULL or return 0 or pthread_exit, only that particular thread exits.

Community
  • 1
  • 1
Abhineet
  • 5,320
  • 1
  • 25
  • 43
  • 2
    If you are going to copy-paste, link/attribute the sources: [source 1](http://stackoverflow.com/questions/11875956/main-thread-exit-does-other-exit-too#comment15804566_11876049) and [source 2](http://docs.oracle.com/cd/E19455-01/806-5257/6je9h032i/index.html) – P.P Jul 03 '15 at 07:53
  • Yeah sure. I don't know how to link to the comment of the question as you did for source 1. I do not wanted to link to the accepted answer of that question. Anyways, I copied your source 1 :-) Updating my answer. – Abhineet Jul 03 '15 at 07:58
1

1) No (assuming by 'exits' you mean exiting by returning from thread's start routine or calling pthread_exit, not by calling exit function). If other threads are still running, the process continues

2) Yes, If any thread within a process calls exit, then the entire process terminates

3) No, only that thread terminates. Process will continue if other threads are running. It does not matter whether main thread or some other thread calls it.

4) Same as 3

ramana_k
  • 1,933
  • 2
  • 10
  • 14