#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 5;
int count = 0;
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
{
count ++;
printf("count_pid0 = %d\n",count);
printf("child one\n");
printf("child two\n");
value = value + 15;
printf("333\n");
count++;
printf("count_pid0_second = %d\n",count);
return 0;
}
else if (pid > 0)
{
count ++;
printf("count_pid1 = %d\n",count);
printf("father one\n");
printf("father two\n");
wait(NULL);
printf("value = %d\n", value);
return 0;
}
}
总结:pid>0 为父进程,=0 为子进程;同时运行;先运行父进程,再运行子进程,如果父进程中用wait 则等待子进程运行完再运行父进程的后半部分。