今天看APUE上一道题,要求不能用fcnt1来替换dup1.

刚开始的思路是dup一个,测试发现与期望的不一致就马上关闭,发现遇到无限循环,刚才想了下,才发现一旦close掉,再次dup仍然是分配最小的fd,所以永远也得不到最终的结果。囧!

好吧,依据网友的,自己整理了下:

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <error.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> #define err_sys(info) \
{ \
fprintf(stderr, "%s.\n", info); \
exit(EXIT_FAILURE); \
}
#define MaxFD 1024 int test_dup(int fd1,int fd2){
/* dup2(fd1,fd2) */
int fd=0;
int array_fd[MaxFD];
int i=0; printf(" dup2(%d,%d) \n", fd1, fd2);
if((fd1 < 0) || (fd2 < 0) || fd2 > MaxFD)
{
err_sys(" Please check file_id. ");
}
fd = dup(fd1);
if(fd < 0)
{
err_sys(" the file id has no corresponding file ");
} while(fd < fd2 && i++ < MaxFD)
{
//close(array_fd[i-1]); 千万不能在这里关闭
array_fd[i]=fd;
fd = dup(fd1);
if (fd < 0)
{
err_sys(" can't open it ");
} printf("fd = %d \n", fd);
}
if(fd > fd2)
{
close(fd2);
fd = dup(fd1);
}
for(;i>=0;i--)
{
close(array_fd[i]);
} return fd;
}
int main(int argc, char* argv[])
{
test_dup(1,100);
exit(0);
}

  

dup2替换的更多相关文章

  1. UNIX环境高级编程APUE练习3.2-不用fcntl实现dup2的功能

    1 题面 编写与dup2功能相同的函数,要求不调用fcntl函数,并且要有正确的出错处理. 2 基本思路 不能用fcntl,能够返回一个文件描述符的只有open和dup.而open会创建一个新的文件表 ...

  2. dup和dup2函数以及管道的实现

    疑问:管道应该不是这样实现的,因为这要求修改程序的代码 dup和dup2也是两个非常有用的调用,它们的作用都是用来复制一个文件的描述符.它们经常用来重定向进程的stdin.stdout和stderr. ...

  3. Linux Shell编程(28)——进程替换

    进程替换与命令替换很相似. 命令替换把一个命令的结果赋给一个变量,例如 dir_contents=`ls -al`或xref=$. 进程替换则是把一个进程的输出回馈给另一个进程 (换句话说,它把一个命 ...

  4. dup,dup2函数【转】

    转自:http://eriol.iteye.com/blog/1180624 转自:http://www.cnblogs.com/jht/archive/2006/04/04/366086.html ...

  5. UNIX 系统调用:dup,dup2实现重定向

    重定向一般在命令行里就是把原本输出到屏幕的数据转而输出到一个指定的文件当中.如 $ pwd > workdir.txt 此时workdir.txt内就存储了pwd命令的输出,当前所在的工作目录 ...

  6. Unix 网络编程 dup和dup2函数

    dup和dup2也是两个很实用的调用,它们的作用都是用来复制一个文件的描写叙述符. 它们经经常使用来重定向进程的stdin.stdout和stderr.这两个函数的原形例如以下: #include & ...

  7. Shell替换

    如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. #!/bin/bash a= echo -e "Value of a is ...

  8. [APUE]不用fcntl实现dup2函数功能

    dup2的函数定义为: #include <unistd.h> int dup2(int src_fd, int new_fd); 自己实现dup2函数有几个关键点: 1,检查给定的源fd ...

  9. ContentProvider域名替换小工具

    开发项目域名想怎么换就怎么换,就是这么任性! 这是一个很有意思的小工具! 这是一个方便开发人员和测试人员的小工具!! 吐槽: 一直在做Android开发,一直总有一个问题存在:做自己公司的apk开发时 ...

随机推荐

  1. 【转】角落的开发工具集之Vs(Visual Studio)2017插件推荐

    因为最近录制视频的缘故,很多朋友都在QQ群留言,或者微信公众号私信我,问我一些工具和一些插件啊,怎么使用的啊?那么今天我忙里偷闲整理一下清单,然后在这里面公布出来. Visual Studio 201 ...

  2. hdu 1027 输出第m个全排列(next_permutation)

    Sample Input6 4 //输出第4个全排列11 8 Sample Output1 2 3 5 6 41 2 3 4 5 6 7 9 8 11 10 # include <cstdio& ...

  3. S2750&S5700&S6700 V200R003(C00&C02&C10) MIB参考

    https://support.huawei.com/enterprise/docinforeader.action?contentId=DOC1000027337&idPath=791971 ...

  4. springbank 开发日志 SpringMVC是如何找到handler找到对应的方法并执行的

    从DispatcherServlet说起,本文讨论的内容都是DispatcherServlet的doDispatch方法完成 mappedHandler是一个HandlerExecutionChain ...

  5. 利用sed把一行的文本文件改成每句一行

    事实证明,至少在mac-osx上,利用sed插入一个换行符是很难的,常规的命令如: sed 's/[\.\?\!]/\n' file 是不可能的,会把那些标点符号变成n. 尝试了SO上的很多方法 ht ...

  6. ZOJ - 3471

    壮压水一水,刚开始脑残了非要开两维dp... #include<cstdio> #include<cstring> #include<algorithm> #def ...

  7. Linux 僵尸进程的筛选和查杀

    一.筛选 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 二.查杀 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' ...

  8. 【LeetCode】163. Missing Range

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given a sorted integer array where the rang ...

  9. with 重写enter exit 方法

  10. 第一次亲密接触MSF

    第一次亲密接触MSF Metasploit Framework介绍 Metasploit是一款开源安全漏洞检测工具,附带数百个已知的软件漏洞,并保持频繁更新.被安全社区冠以“可以黑掉整个宇宙”之名的强 ...