How to use the _Exit function that causes normal program termination without cleaning up in C

5 Answers

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

/*
_Exit() stops the program and returns a status code to the operating system, but it does not:

Call functions registered with atexit()
Flush standard I/O buffers
Close streams through the normal cleanup process
Perform other implementation-defined termination cleanup
*/
 
void f(void) {
    puts("void f(void)");
}
 
int main(void)
{
    printf("main()\n");
    
    atexit(f);
    
    fflush(stdout);   // may not run
    
    _Exit(0);
}



/*
run:

main()

*/

 



answered Dec 20, 2022 by avibootz
edited Oct 10 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

/*
_Exit() stops the program and returns a status code to the operating system, but it does not:

Call functions registered with atexit()
Flush standard I/O buffers
Close streams through the normal cleanup process
Perform other implementation-defined termination cleanup
*/

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

int main(void)
{
    atexit(cleanup);

    printf("Hello, world!");

    _Exit(EXIT_SUCCESS);
}

// print noting 

/*
run:


*/

 



answered Oct 10 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

/*
_Exit() stops the program and returns a status code to the operating system, but it does not:

Call functions registered with atexit()
Flush standard I/O buffers
Close streams through the normal cleanup process
Perform other implementation-defined termination cleanup
*/


int main(void)
{
    printf("This may never be displayed");

    _Exit(EXIT_SUCCESS);
}


/*
run:

This may never be displayed

*/

 



answered Oct 10 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

/*
_Exit() stops the program and returns a status code to the operating system, but it does not:

Call functions registered with atexit()
Flush standard I/O buffers
Close streams through the normal cleanup process
Perform other implementation-defined termination cleanup
*/

int main(void)
{
    printf("Message\n");
    fflush(stdout);

    _Exit(EXIT_SUCCESS);
}


/*
run:

Message

*/

 



answered Oct 10 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

/*
A common use of _Exit() is in a child process after fork(). The child inherits the parent's 
buffered I/O and other process state, so calling exit() in the child can cause cleanup handlers 
to run twice or output buffers to be flushed unexpectedly.
*/

/*
fork() is a system call in C and is used to create a new process, 
known as the child process, which runs concurrently with the parent process.
*/

void cleanup(void)
{
    printf("Cleanup handler executed\n");
}

int main(void)
{
    atexit(cleanup);

    pid_t pid = fork();

    if (pid < 0) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid == 0) {
        /* Child process */
        printf("Child process\n");

        /* Exit immediately without running cleanup handlers */
        _Exit(EXIT_SUCCESS);
    }

    /* Parent process */
    printf("Parent process\n");

    return 0;
}


/*
run:

Parent process
Cleanup handler executed
Child process

*/

 



answered Oct 10 by avibootz
...