#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
// 父子进程遵循读时共享,写时复制的原则
int var = 100;
int main() {
pid_t pid;
pid = fork();
if (pid == -1) {
perror("fork error");
exit(1);
} else if (pid > 0) {
var = 200;
printf("parent, var = %d\n", var);
} else if (pid == 0) {
// var = 300;
printf("child, var = %d\n", var);
}
return 0;
}
标签:fork,int,pid,父子,var,printf,进程,include
From: https://www.cnblogs.com/hacker-dvd/p/17533532.html