Skip to content

Instantly share code, notes, and snippets.

@kougazhang
Created December 31, 2021 09:21
Show Gist options
  • Save kougazhang/1bb74ace323746dd1610049f2df39bb4 to your computer and use it in GitHub Desktop.
Save kougazhang/1bb74ace323746dd1610049f2df39bb4 to your computer and use it in GitHub Desktop.
#copy-on-write
#include <stdio.h>
#include <unistd.h>
int main(void) {
pid_t fpid; // fpid 表示 fork 函数返回值
int count=0;
// 调用 fork, 创建出子进程
fpid = fork();
// 所以下面的代码有两个进程执行
if (fpid < 0) {
printf("create subprocess failed\n");
} else if (fpid == 0) {
printf("i am subprocess\n");
count++;
} else {
printf("i am father process\n");
count++;
}
printf("the result of count is %d\n", count);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment