fork,在子进程中重定向输出,然后执行shell程序
- if( (pid = fork() ) == -1 ){
- perror("fork"); exit(1);
- }
- /* child does the work */
- if ( pid == 0 ){
- /* close then open */
- close(1);
- if ( open(file,O_WRONLY|O_CREAT|O_APPEND,DFLMODE) != 1 )
- perror(file);
- else {
- execlp( "who", "who", NULL ); /* and run */
- perror("execlp");
- }
- exit(1);
- }
- /* parent waits then reports */
- if ( pid != 0 ){
- wait(NULL);
- printf("Done running who. results in %s\n", file);
- }
阅读(2476) | 评论(0) | 转发(0) |