[root@uPus root]# lsmain.c[root@uPus root]# cat main.c #include "stdio.h"#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>
int main(int argc, char*argv[]){ int nfd = open("/dev/null",O_RDWR,0777); printf("new stdout fd=%d/n",nfd); dup2(nfd,1);
while(1) { printf("wwwwwwwwwwwwww/n"); sleep(3); } return(0);}[root@uPus root]# gcc main.c [root@uPus root]# ./a.out &[1] 1682new stdout fd=3[root@uPus root]# mknod pipe p [root@uPus root]# chmod 777 pipe [root@uPus root]# ll总用量 16-rwxr-xr-x 1 root root 11902 10月 16 22:21 a.out-rw-r--r-- 1 root root 282 10月 16 22:05 main.cprwxrwxrwx 1 root root 0 10月 16 22:22 pipe[root@uPus root]# cat pipe &[2] 1687[root@uPus root]# gdb -q -p 1682Attaching to process 1682Reading symbols from /root/a.out...done.Reading symbols from /lib/tls/libc.so.6...done.Loaded symbols for /lib/tls/libc.so.6Reading symbols from /lib/ld-linux.so.2...done.Loaded symbols for /lib/ld-linux.so.20xffffe002 in ?? ()(gdb) p dup(1)$1 = 4(gdb) p open("./pipe",777)$2 = 5(gdb) p dup2(5,1)$3 = 1(gdb) cContinuing.wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Program received signal SIGINT, Interrupt.0xffffe002 in ?? ()(gdb) p dup2(4,1)$4 = 1(gdb) p close(5)$5 = 0(gdb) p close(4)$6 = 0(gdb) qThe program is running. Quit anyway (and detach it)? (y or n) yDetaching from program: /root/a.out, process 1682[2]+ Done cat pipe[root@uPus root]# ps -aux | grep a.outroot 1682 0.0 0.5 1348 308 pts/1 S 22:22 0:00 ./a.outroot 1690 0.0 1.0 4816 644 pts/1 S 22:27 0:00 grep a.out[root@uPus root]# lsa.out main.c pipe[root@uPus root]#