Simple fork example – Without checking return value

All QuestionsCategory: Operating SystemSimple fork example – Without checking return value
Anonymous asked 3 years ago
1 Answers
Anonymous answered 3 years ago
#include <unistd.h>

int main() {
  printf("welcome..pid=%d\n", getpid());
  fork(); //intentionally not capturing return value, in this example
printf("Thank You\n");
  //printf("thank you,pid=%d,ppid=%d\n", getpid(), getppid());
  return 0;
}


Observations & Understanding


  • How many times printf gets executed in above code
  • Can you distinguish between parent and child in above code
  • Mapping child’s ppid with parent’s pid
  • What is parent’s ppid, to which process it belongs to?
  • Check the pid, ppid values from the output of ps, pstree commands (Ensure program won’t terminate by using sleep/getchar)
  • What if fork is called multiple times, unconditionally without checking return values