#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int mydup(int i_OldFd, int i_NewFd);

int main(void)
{
int fd = -1;
fd = open("tests.c", O_RDONLY);

if(fd < 0)
{
printf("error.\n");
return -1;
}

printf("the old_fd:%d.\n", fd);

int tmp = mydup(fd, 100);

printf("the new_fd:%d.\n",tmp);
return 1;
}

int mydup(int i_OldFd, int i_NewFd)
{
if(i_OldFd < 0 || i_OldFd >256)
{
printf("Oldfd is wrong.\n");
return -1;
}

if(i_NewFd < 0 || i_NewFd > 256)
{
printf("Newfd is wrong.\n");
return -1;
}

if( i_NewFd == i_OldFd)
return i_OldFd;

int i_Arrayfd[256];
int count = 0;
close(i_NewFd);

do
{
i_Arrayfd[count] = dup(i_OldFd);
if(i_Arrayfd[count] == -1)
{
printf("dup error.\n");
break;
}
count++;

}while(i_Arrayfd[count-1] != i_NewFd);

for(int i = 0; i < count-1 ; i++)
{
close(i_Arrayfd[i]);
}

return i_Arrayfd[count-1];
}

my dup2的更多相关文章

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

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

  2. Linux进程间通信(三):匿名管道 popen()、pclose()、pipe()、close()、dup()、dup2()

    在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一.什 ...

  3. dup和dup2用法小结

    今天和同学探讨了一下关于重定向输出到文件的问题,其中需要用到dup和dup2函数,因此来小小的总结一下. 首先来man一下: dup直接返回一个新的描述符和原来的描述符一样代表同一个资源,描述符的值就 ...

  4. Linux内核分析:dup、dup2的实现

    一.首先需要看一下这两个函数的作用: #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); 根据m ...

  5. dup2()函数的使用,

    #define STR "xiamanman\n"#define STR_LEN 10#define STDOUT 1 #include <stdio.h>#inclu ...

  6. linux下dup/dup2函数的用法

    系统调用dup和dup2能够复制文件描述符.dup返回新的文件文件描述符(没有用的文件描述符最小的编号).dup2可以让用户指定返回的文件描述符的值,如果需要,则首先接近newfd的值,他通常用来重新 ...

  7. dup和dup2函数

    下面两个函数都可用来复制一个现存的文件描述符: #include<unistd.h> int dup(int filedes); int dup2(int filedes,int file ...

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

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

  9. 文件I/O(不带缓冲)之dup和dup2函数

    下面两个函数都可用来复制一个现有的文件描述符: #include <unistd.h> int dup( int filedes ); int dup2( int filedes, int ...

  10. IPC with pipes, also dup2 redirect stream handle

    #include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unist ...

随机推荐

  1. ubuntu eclipse CDT 问题

    问题一:ubuntu eclipse c++ launch failed binary not found 解决:建完项目后 查看在项目中是不是有debug目录,说明没有编译.仅仅是须要做例如以下操作 ...

  2. seq2sparse(4)之PartialVectorMergeReducer源码分析

    继前篇blogseq2sparse(3)之TFParitialVectorReducer源码分析 之后,继续分析下面的代码,本次分析的是PartialVectorMergeReducer的源码,这个r ...

  3. Sublime Text 常用快捷键

    /* 之前用过的好多的编辑器,从IT大牛们的博客里知道了他们所谓的Vim,Vi,Emacs等,也都挨个装上试了,不尽人意,但自从遇到了Sublime Text,甚是喜欢,有道是“情不知何而起,一往而深 ...

  4. C++[类设计] ini配置文件读写类config

      //in Config.h #pragma once #include <windows.h> #include <shlwapi.h> #pragma comment(l ...

  5. AsMVC:一个简单的MVC框架的Java实现

    当初看了<从零开始写一个Java Web框架>,也跟着写了一遍,但当时学艺不精,真正进脑子里的并不是很多,作者将依赖注入框架和MVC框架写在一起也给我造成了不小的困扰.最近刚好看了一遍sp ...

  6. Java基础知识强化23:Java中数据类型转换(面试题)

    1. 以下代码输出结果是( D ).                     public  class  Test   {                                       ...

  7. iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容

    iis 7上发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 提示里面的解决方法是: 如果不希望启用目录浏览,请确保配置了默认文档并且该文件存在. 使用 II ...

  8. EasyUi DataGrid 绑定数据格式问题

    如果显示汇总记录则需设置页脚属性:首先设置showFooter:true, 然后后台计算出合计数据,一起传过来,类似如下:{"total":28,"rows": ...

  9. String[]和ArrayList和LinkedList区别

    String[]和ArrayList和LinkedList区别 参考文档如下: http://www.blogjava.net/flysky19/articles/92775.html http:// ...

  10. ASP.NET中的MD5加密

    新人冒泡,打今起在园子里算是开博了,先来写点关于基础性的东西 为以后的写其他的文章做准备. 今天就先来说说MD5加密与在ASP.NET中如何实现MD5加密. MD5加密简单的说就是把一段明文 通过某种 ...