#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. swift 深入理解Swift的闭包

    我们可用swift的闭包来定义变量的值. 先来一个简单的例子大家先感受感受. 定义一个字符串的变量的方法: 直接赋值 var str="JobDeer" 还可以用闭包的方式定义: ...

  2. MSSQL row_number简单使用语法

    MSSQL row_number简单使用语法 select * from ( select row_number() over(partition by threadid order by date ...

  3. Linux内核启动分析

    张超<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 我的代码可见https://www.shiyanlo ...

  4. spring06Aop

    1.实现前置增强 必须实现接口MethodBeforeAdvice接口 创建对应的文件 public interface Animal {//主业务接口 void eat(); //目标方法 void ...

  5. java: cannot execute binary file

    转自:http://jxwpx.blog.51cto.com/15242/222572 java: cannot execute binary file 如果遇到这个错,一般是操作系统位数出问题了. ...

  6. Swift函数的定义建议

    /* Swift中函数命名的智慧 */ // 1.一般情况下, 我们写一个函数是这么写的 func sayHello(name: String , greeting: String) { print( ...

  7. startActivityForResult

    Activity提供了startActivityForResult(Intent intent, int requestCode)方法打开新的Activity,新的Activity关闭后会向前面的Ac ...

  8. Apache Rewrite 拟静态配置54

    mod_rewrite 规则的使用 RewriteEngine on RewriteCond %{HTTP_HOST} !^www.php100.com  [NC]  RewriteRule   ^/ ...

  9. PHP高效的敏感词过滤方法

    <?php // 测试文件demo.php $badword = array( '张三','张三丰','张三丰田' ); // array_combine() 函数通过合并两个数组来创建一个新数 ...

  10. [算法]线段树(IntervalTree)

    转载请注明出处:http://www.cnblogs.com/StartoverX/p/4617963.html 线段树是一颗二叉搜索树,线段树将一个区间划分成一些单元区间,每一个区间对应线段树的一个 ...