quick_exit() Function

The quick_exit() function is declared in the header file <stdlib.h>.

The quick_exit() function terminates the calling process immediately by returning control to the host environment. It performs a normal termination by calling all functions registered with at_quick_exit(), but does not execute additional cleanup tasks such as calling object destructors or flushing C streams. Its behavior regarding stream closure and temporary file removal depends on the system or library implementation.


Syntax of quick_exit()

</>
Copy
_Noreturn void quick_exit(int status);

Parameters

ParameterDescription
statusStatus code indicating termination status. A value of 0 or EXIT_SUCCESS denotes successful termination, while EXIT_FAILURE indicates unsuccessful termination. Other values depend on the system implementation.

Note that if both exit() and quick_exit() are called in the same program, or if quick_exit() is called more than once, the behavior is undefined.


Return Value

The quick_exit() function does not return; it terminates the process immediately.


Examples for quick_exit()

Example 1: Basic quick_exit() Usage with at_quick_exit Registration

This example demonstrates registering a cleanup function with at_quick_exit() and then calling quick_exit() to terminate the process.

Program

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

void cleanup(void) {
    printf("Cleanup function called via at_quick_exit.\n");
}

int main(void) {
    if (at_quick_exit(cleanup) != 0) {
        fprintf(stderr, "Failed to register cleanup function.\n");
        return EXIT_FAILURE;
    }
    printf("Before calling quick_exit().\n");
    quick_exit(EXIT_SUCCESS);
    /* The following line will never be executed */
    printf("This line will not be printed.\n");
    return 0;
}

Explanation:

  1. The program registers a cleanup function using at_quick_exit(), which will be called upon termination.
  2. A message is printed before quick_exit() is invoked.
  3. quick_exit() terminates the process immediately, calling the registered cleanup function.
  4. Any code after quick_exit() is never executed.

Program Output:

Before calling quick_exit().
Cleanup function called via at_quick_exit.

Example 2: quick_exit() Without Registered Cleanup Function

This example illustrates the behavior of quick_exit() when no cleanup function is registered. The process terminates immediately without performing any additional cleanup.

Program

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

int main(void) {
    printf("Calling quick_exit() without registered cleanup.\n");
    quick_exit(EXIT_FAILURE);
    /* This line will not be executed */
    printf("This message will never appear.\n");
    return 0;
}

Explanation:

  1. The program prints a message indicating that quick_exit() is about to be called.
  2. Since no cleanup function is registered with at_quick_exit(), quick_exit() terminates the process immediately.
  3. Any code following quick_exit() is not executed.

Program Output:

Calling quick_exit() without registered cleanup.

Example 3: Demonstrating Undefined Behavior with Multiple quick_exit() Calls

This example highlights that calling quick_exit() more than once results in undefined behavior. Therefore, such usage must be avoided.

Program

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

void dummy_cleanup(void) {
    printf("This cleanup function should only be called once.\n");
}

int main(void) {
    at_quick_exit(dummy_cleanup);
    printf("First quick_exit call.\n");
    quick_exit(EXIT_SUCCESS);
    /* The following code is undefined and will never be executed */
    printf("Second quick_exit call (undefined behavior).\n");
    quick_exit(EXIT_SUCCESS);
    return 0;
}

Explanation:

  1. The program registers a cleanup function using at_quick_exit().
  2. It then calls quick_exit() for the first time, which terminates the process after calling the cleanup function.
  3. The code following the first quick_exit() call is never executed. Calling quick_exit() a second time is undefined and should be avoided.

Program Output:

First quick_exit call.
This cleanup function should only be called once.