In the context of programming, specifically when using the C programming language, the macro `WIFEXITED` is typically used in conjunction with the `waitpid` or `wait` system calls to determine the exit status of a child process. When `WIFEXITED` returns true, it implies that the child process has terminated normally by calling the `exit()` system call or by returning from the `main()` function.
The `WIFEXITED` macro is used to check the termination status of a child process and is often used in combination with other macros such as `WEXITSTATUS` to retrieve the exit status value. By evaluating `WIFEXITED` as true, it indicates that the child process has ended without encountering any abnormal conditions or errors. The `WEXITSTATUS` macro can then be used to retrieve the exit status value if needed.
Here's a simple example to illustrate its usage:
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// Child process
printf("Child process executing.\n");
exit(0); // Terminate child process normally
} else if (pid > 0) {
// Parent process
wait(NULL);
if (WIFEXITED(pid)) {
printf("Child process terminated normally.\n");
int exitStatus = WEXITSTATUS(pid);
printf("Exit status: %d\n", exitStatus);
}
} else {
// Fork failed
fprintf(stderr, "Fork failed.\n");
return 1;
}
return 0;
}
In this example, the parent process waits for the child process to terminate using the `wait` system call. If `WIFEXITED` returns true, it indicates that the child process has terminated normally. The exit status of the child process can be retrieved using `WEXITSTATUS` and then printed out.