转自  http://blog.csdn.net/todd911/article/details/11747097

#include <stdio.h>
#include <unistd.h> int main(void){
int fd = -;
fd = mydup2(,);
if(fd < ){
perror("mydup2");
return -;
}
printf("fd = %d\n",fd);
return ;
} int mydup2(int fd, int newfd){
//判断fd和newfd的范围是否正确
if(fd < || fd > ){
printf("fd is wrong.\n");
return -;
} int index = ;
int fdarray[newfd];
//判断fd是否已经被打开。
if((fdarray[index] = dup(fd)) == -){
printf("error while dup.\n");
return -;
}else{
close(fdarray[index]);
}
//如果相等,则直接返回。
if(fd == newfd){
return fd;
}
//将newfd关闭
close(newfd);
//获取所要的newfd
int newfdindex;
for(index=; index<=newfd; index++){
fdarray[index] = dup(fd);
if(fdarray[index] == -){
printf("error while dup.\n");
return -;
}else{
if(fdarray[index] == newfd){
newfdindex = index;
break;
}
}
}
//将之前打开的fd都关闭
for(index=; index<newfdindex; index++){
close(fdarray[index]);
}
return fdarray[newfdindex];
}

自己实现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. 【Transact-SQL】找出不包含字母、不包含汉字的数据

    原文:[Transact-SQL]找出不包含字母.不包含汉字的数据 测试的同事,让我帮忙写个sql语句,找出表中xx列不包含汉字的行. 下面的代码就能实现. IF EXISTS(SELECT * FR ...

  2. 十、es6之扩展运算符 三个点(...)

    对象的扩展运算符 对象中的扩展运算符(...)用于取出参数对象中的所有可遍历属性,拷贝到当前对象之中 let bar = { a: 1, b: 2 }; let baz = { ...bar }; / ...

  3. Linux查看系统及版本信息

    1.查看操作系统版本cat /proc/version 2.查看系统发行版cat /etc/issue 或cat /etc/redhat-release 3.查看系统内核信息uname -a

  4. 货币转换C

    描述 人民币和美元是世界上通用的两种货币之一,写一个程序进行货币间币值转换,其中:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮ ...

  5. eclipse调试之edit source lookup path解决方案

    转自:https://blog.csdn.net/zkn_CS_DN_2013/article/details/48731133

  6. 一款结合nmap及mascan还有shodan的扫描脚本

    github在这里 https://github.com/s0md3v/Silver 很是舒服 Usage Note: Silver scans all TCP ports by default i. ...

  7. Linux学习之九-Linux系统定时任务

    Linux系统定时任务 在一些实际工作中需要机器在某个时间自动执行某个任务,不需要人为在此时刻参与,可以建立一个定时任务. crond 服务是linux下用来周期性的执行某种任务或等待处理某些事件的一 ...

  8. jquery进行each遍历时,根据条件取消某项操作

    示例代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...

  9. 关闭安装包更新使用YUM在Linux中(RHEL / CentOS / Fedora)

    YUM (Yellowdog Updater Modified)  是一个开源的命令行工具,以及基于图形的软件包管理工具, 用于基于 RPM (RedHat Package Manager) 的 Li ...

  10. 软件测试第二周个人作业:WordCount

    github地址:https:/github.com/muzhailong/wc.git 第一次写博客很不容易,也算是一个好的开始吧. 1.   个人作业要求 作业简述:根据WordCount的需求描 ...