- 论坛徽章:
- 0
|
本帖最后由 ppm10103 于 2012-03-16 16:34 编辑
#include "apue.h"
#include "apueerror.h"
//./a.out > temp.out 会产生两次before fork
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)
err_sys("write error");
printf("before fork\n"); /* we don't flush stdout */
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* child */
glob++; /* modify variables */
var++;
} else {
sleep(2); /* parent */
}
printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
exit(0);
}
为什么
./a.out 只会产生一次 before fork
./a.out > temp.out 会产生两次before fork
为什么重定向才会有两次呢?
源代码在附件中 |
|