📜  如何在 fork 中的两个进程中共享变量 - 无论代码示例

📅  最后修改于: 2022-03-11 14:56:24.538000             🧑  作者: Mango

代码示例1
#include  
#include  
#include  

#define   MAX_COUNT  200
#define   BUF_SIZE   100

void  main(void)
{
     pid_t  pid;
     int    i;
     char   buf[BUF_SIZE];

     fork();
     pid = getpid();
     for (i = 1; i <= MAX_COUNT; i++) {
          sprintf(buf, "This line is from pid %d, value = %d\n", pid, i);
          write(1, buf, strlen(buf));
     } 
}