Aodai

process

Nov 25th, 2020 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5.  
  6. int main() {
  7.     // fork creates a child process.
  8.     pid_t child = fork();
  9.     if(child < 0) {
  10.         printf("Error forking!\n");
  11.         return 0;
  12.     }
  13.     // if the value of child pid is equal to 0 then we're in the child process
  14.     else if(child == 0) {
  15.         printf("Hello from the child (PID=%d)\n", getpid());
  16.         exit(0);
  17.     }
  18.     // Waits until the child finishes.
  19.     wait(NULL);
  20.     printf("Parent(PID=%d) exiting...\n", getpid());
  21.     return 0;
  22. }
Add Comment
Please, Sign In to add comment