CS Electrical And Electronics
@cselectricalandelectronics

Concurrent Example code in linux operating system

All QuestionsCategory: Operating SystemConcurrent Example code in linux operating system
Anonymous asked 2 years ago
1 Answers
Anonymous answered 2 years ago
#include <unistd.h>
#include <stdio.h>

int main() {
  pid_t ret;
int i, max = 10;
  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);
      sleep(1);
    }
    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);
      sleep(1);
    }
  }
  return 0;
}