看开源代码遇到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的屏蔽, 这种代码经常遇到。





本文转载:CSDN博客