logo

Process IDs in C

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(int argc, char* argv[]) { int id = fork(); if (id == 0) { sleep(1); } printf("Current ID: %d, parent ID: %d\n", getpid(), getppid()); int res = wait(NULL); if (res == -1) { printf("No children to wait for\n"); } else { printf("%d finished execution\n", res); } return 0; }
Course thumb

The C programming language made simple