|
先看代码,很简单
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- int i;
- int main()
- {
- int newpid;
- void child_code(), parent_code();
- int count = 3;
- for ( i = 0; i < count; ++i ){
- if ( (newpid = fork()) == -1 )
- perror("fork");
- else if ( newpid == 0 )
- child_code();
- else
- parent_code(newpid);
- }
- return 0;
- }
- /*
- * new process takes a nap and then exits
- */
- void child_code()
- {
- exit(17);
- }
- /*
- * parent waits for child then prints a message
- */
- void parent_code(int childpid)
- {
- int wait_rv; /* return value from wait() */
- wait_rv = wait(NULL);
- printf ( "the No.%d child has down\n", i );
- }
复制代码
编译生成a.out
结果
- the No.0 child has down
- the No.1 child has down
- the No.2 child has down
复制代码
接下来这么执行
- ./a.out > testfile
- more testfile
复制代码
结果
- the No.0 child has down
- the No.0 child has down
- the No.1 child has down
- the No.0 child has down
- the No.1 child has down
- the No.2 child has down
复制代码
也就是说把输入重定向到文件就会初见这么个结果……
why? |
|