#include #include #include /* for write*/ #include /* for exit */ int glob = 6; /* external variable in initialized data */ char buf[] = "a write to stdout\n"; int main(void) { int var; /* automatic variable on the stack */ pid_t pid; var = 88; if (write( STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1) { printf("write error\n"); exit(-1); } printf("before fork %i\n", STDOUT_FILENO); /* we don't flush stdout */ /*fflush(stdout);*/ if ( (pid = fork()) < 0) { printf("fork error\n"); exit(-1); } else if (pid == 0) { /* child */ glob++; /* modify variables */ var++; } else sleep(2); /* parent */ printf("pid = %d, ppid=%d, glob = %d, var = %d\n", getpid(), getppid(), glob, var); exit(0); }