What is the difference between exit () and exit_group ()

What is the difference between exit () and exit_group () . Any process that has multiple threads should use exit_group instead of exit?

To answer the question why do you ask - we have a process that contains about forty threads. When the thread is blocked, we automatically exit the process and then restart the process. And we print the reverse line of the thread that was locked. We wanted to know if the exit call is in this case different from exit_group.

From the docs: This system call is equivalent to exit(2) except that it terminates not only the calling thread, but all threads in the calling process thread group - What is the difference between exiting a process and exiting all threads. Does not exit the process ==, exiting all threads.

+6
source share
1 answer

All the thread libraries that I know (for example, the latest glibc or musl-libc ) use the low-level clone (2) system call to implement their threads (and some C libraries even use the clone to fork process).

clone is a sophisticated Linux syscall. If you are not a streaming library developer, you should not use it directly, but only through library functions (for example, pthread_create (3) ); see also futex (7) used in the pthread_mutex * function

The clone script is used to create tasks: either threads (sharing the address space in a multi-threaded process), or processes.

The exit_group script is associated with exiting these tasks.

In short, you'll never use exit_group or clone directly. Your libc does this for you. So don't care about exit_group or _Exit ; you should use only the standard library function exit (3) , which is especially true for atexit (3) and on_exit (3) registered handlers and flushed <stdio.h> buffers. In rare cases, you do not want this to happen, use _ exit (2) (but you probably won't need it).

Of course, if you redefine your own libc from scratch, you need to take care of exit_group and clone ; but otherwise they don’t care. .

If you care about the details of the gory implementation, dive into the source code of your libc . Details can be libc version, kernel version and compiler specifics!

+3
source

Source: https://habr.com/ru/post/978742/


All Articles