父子进程共享内存问题
时间:2023-03-30 05:34:36 点击:217

  #include

  #include

  #include

  #include

  #include

  #include

  int

  main (int argc, char *argv[])

  {

  pid_t cpid;

  int *data = NULL;

  int fd;

  fd = open (“/dev/zero“, O_RDWR);

  if (fd < 0) {

  perror (“open /dev/zero“);

  exit(1);

  }

  data = (void *) mmap (0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

  if (data == MAP_FAILED) {

  perror (“mmap“);

  exit(2);

  }

  *data = 0;

  close (fd);

  cpid = fork ();

  if (cpid == 0)

  {

  for (;;)

  {

  sleep (1);

  printf (“Child-0: %dn“, *data);

  }

  } else if (cpid > 0)

  {

  cpid = fork ();

  if (cpid == 0)

  {

  for (;;)

  {

  sleep (1);

  (*data)++;

  printf (“Child-1: %dn“, *data);

  }

  } else if (cpid > 0)

  {

  for (;;)

  {

  sleep (1);

  printf (“Parent-0: %dn“, *data);

  }

  }

  }

  munmap (data, 4096);

  return 0;

  }

最新游戏更多

最新软件更多

  • 玩家推荐
  • 游戏攻略

金钼软件下载站 Copyright(C) 2008- 601958.cn All Rights Reserved!

闽ICP备2023004188号| 免责声明