红联Linux门户
Linux帮助

linux 下关闭标准输入、输出在打开

发布时间:2008-07-22 21:03:58来源:红联作者:Lhhba
在linux下,关闭stdin,stdout,stderr后重新打开的方法:先用dup2(...)函数复制stdin,stdout,stderr文件描述符,然后复制回去。代码示例:

引用:
#include

#include

int main()
{
int fd1;
dup2( 1, fd1 );
printf("copy 1 to fd1\n");
close(1);
printf("close 1\n");
dup2(fd1, 1);
printf("cope fd1 to 1\n");
return 0;
}


输出结果:

copy 1 to fd1
copy fd1 to 1

因为标准输出被close所以close 1没有被输出。
文章评论

共有 0 条评论