C wait code example

Example 1: c waitpid

#include <sys/types.h>
#include <sys/wait.h>

pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);

Example 2: c fork wait for child

int main(){
    pid_t pid = fork();
    if (pid == 0) {
        printf("HC: hello from child\n");
        exit(17);
    } else {
        int child_status;
        printf("HP: hello from parent\n");
        waitpid(pid, &child_status, 0); // Waits for child to end
        printf("CT: child result %d\n", WEXITSTATUS(child_status));
    }
    printf("Bye\n");
    return 0;
}

Tags:

C Example