本文主要是介绍看开源代码遇到dup2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
看开源代码遇到dup2:
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h> int main()
{printf("hello\n");int fd = open("/dev/null", O_RDWR );if(fd != -1){dup2(fd, 0);dup2(fd, 1);dup2(fd, 2);}else{close(0);close(1);close(2);}printf("world\n");return 0;
}
不会输出world, 实际上就是实现对fd 0, 1, 2的屏蔽, 这种代码经常遇到。
这篇关于看开源代码遇到dup2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!