Use of _Exit function in C/C++ with (exit vs _Exit)

The _Exit function terminates the process normally without completely cleaning the resources. It defined in the ‘stdlib.h’ header file, so you have to include the header file before using it.

The _Exit function does not invoke the functions registered with at_quick_exit  and atexit.

Syntax _Exit in C:

//Syntax of _Exit
void _Exit(int status); (since C99 and until C11)


_Noreturn void _Exit(int status);  (since C11)

 

Parameters:

status: Indicates whether the program terminated normally. It can be one of the following:

Value Description
EXIT_SUCCESS Successful termination
0 Successful termination
EXIT_FAILURE Unsuccessful termination

Return:

The _Exit function cannot return to its caller.

 

Let’s see an example code to understand the _Exit function in C. Example code does not execute functions registered with atexit.

#include <stdlib.h>
#include <stdio.h>

// _Exit does not call functions registered with atexit.
void TestFun1(void)
{
    puts("pushed first");
}

void TestFun2(void)
{
    puts("pushed second");
}

int main()
{
    printf("In main function\n\n");

    atexit(TestFun1);

    atexit(TestFun2);

    fflush(stdout);   //_Exit may not flush unwritten buffered data

    _Exit(0);
}

Output:

Let us compile and run the above program that will produce the following result and does not invoke function registered with atexit.

exit vs _exit

Some important points related to the _Exit function in C:

1. You must include stdlib.h header file before using the abort function in C.

2. The _Exit function does not invoke the functions registered with at_quick_exit  and atexit.

3. Whether open streams with unwritten buffered data are flushed, open streams are closed, or temporary files are removed is implementation-defined.

4. The status returned to the host environment is determined in the same way as for the exit function.

  • If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.
  • If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned.
  • In other cases, the implementation-defined status value is returned.

 

Difference between exit and _Exit (exit vs _Exit):

Both functions are used for the normal termination of the process, but still, both have some differences.  Here I am explaining a few differences between the exit and _Exit (exit vs _Exit).

1.  The exit function performs the regular cleanup like flushed all open streams with unwritten buffered data, close all open streams, and removed all files created by the tmpfile function. But _Exit function does not perform the complete cleanup of the resource and it also implementation-dependent.

2. exit function call the functions registered by the atexit function, in the reverse order of their registration. On the other hand, _Exit does not call the function registered with atexit.

 

Recommended Articles for you: