/* ************************************************************************ * Filename: fork.c * Description: * Version: 1.0 * Created: 2011年04月13日 19时38分08秒 * Revision: none * Compiler: gcc * Author: wenhao * Company: sunplusapp * ************************************************************************/ #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> int glob = 6; char buf[] = "a write to stdout/n"; int main() { int var; pid_t pid; var = 88; fprintf(stdout,"hello world!/n"); if(write(STDOUT_FILENO,buf,sizeof(buf)-1) != sizeof(buf)-1) fprintf(stdout,"write error/n"); fprintf(stdout,"before fork/n"); pid = fork(); if(pid<0) { fprintf(stdout,"fork error"); } else if(pid == 0) { glob++; var++; } else { sleep(2); } fprintf(stdout,"pid = %d, glob=%d, var=%d/n",getpid()/ ,glob,var); exit(0); } 下面是运行程序和重定向到文件中是文件里的显示内容: