#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
*/