Concurrent Execution example in operating system

All QuestionsCategory: Operating SystemConcurrent Execution example in operating system
Anonymous asked 3 years ago
1 Answers
Anonymous answered 3 years ago
#include <unistd.h>
#include <stdio.h>

int main() {
  pid_t ret
int i, max = 100;
  printf("welcome..pid=%d\n", getpid());
  ret = fork();
  if (ret < 0) {
    perror("fork");
    exit(1);
  }
  if (ret == 0) {
    printf("child--welcome,pid=%d,ppid=%d\n", getpid(), getppid());
    for (i = 1; i <= max; i++)
      printf("child--%d\n", i);
    exit(0);
  } else // ret>0
  {
    printf("parent--hello,pid=%d,ppid=%d\n", getpid(), getppid());
    for (i = 1; i <= max; i++)
      printf("parent--%d\n", i);
  }
  return 0;
}

 
Observations and Understanding
 

  • Do output from parent & child is sequential or concurrent (intermixed)?
  • Check the behavior with different max values (10,20,100,500 etc)
  • Is there any particular order between parent & child?