C - Executing zombie and orphan process in a single program Code Example

The code for Executing zombie and orphan process in a single program

#include <stdio.h>
#include <unistd.h>

int main() {
  int x;
  x = fork();

  if (x > 0)
    printf("Parent Process (Process ID: %d)\n", getpid());

  else if (x == 0) {
    sleep(5);
    x = fork();

    if (x > 0) {
      printf("Child Process (Process IDs: Parent - %d, Child - %d)\n", getpid(),
             getppid());

      while (1) sleep(1);

      printf("Child Process (Process ID: %d)\n", getppid());
    }

    else if (x == 0)
      printf("IN CHILD'S CHILD PROCESS\nMY PARENT ID : %d\n", getppid());
  }

  return 0;
}
Code by IncludeHelp, on August 11, 2022 20:17

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.