my dup2
#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的更多相关文章
- [APUE]不用fcntl实现dup2函数功能
dup2的函数定义为: #include <unistd.h> int dup2(int src_fd, int new_fd); 自己实现dup2函数有几个关键点: 1,检查给定的源fd ...
- Linux进程间通信(三):匿名管道 popen()、pclose()、pipe()、close()、dup()、dup2()
在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一.什 ...
- dup和dup2用法小结
今天和同学探讨了一下关于重定向输出到文件的问题,其中需要用到dup和dup2函数,因此来小小的总结一下. 首先来man一下: dup直接返回一个新的描述符和原来的描述符一样代表同一个资源,描述符的值就 ...
- Linux内核分析:dup、dup2的实现
一.首先需要看一下这两个函数的作用: #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); 根据m ...
- dup2()函数的使用,
#define STR "xiamanman\n"#define STR_LEN 10#define STDOUT 1 #include <stdio.h>#inclu ...
- linux下dup/dup2函数的用法
系统调用dup和dup2能够复制文件描述符.dup返回新的文件文件描述符(没有用的文件描述符最小的编号).dup2可以让用户指定返回的文件描述符的值,如果需要,则首先接近newfd的值,他通常用来重新 ...
- dup和dup2函数
下面两个函数都可用来复制一个现存的文件描述符: #include<unistd.h> int dup(int filedes); int dup2(int filedes,int file ...
- dup和dup2函数以及管道的实现
疑问:管道应该不是这样实现的,因为这要求修改程序的代码 dup和dup2也是两个非常有用的调用,它们的作用都是用来复制一个文件的描述符.它们经常用来重定向进程的stdin.stdout和stderr. ...
- 文件I/O(不带缓冲)之dup和dup2函数
下面两个函数都可用来复制一个现有的文件描述符: #include <unistd.h> int dup( int filedes ); int dup2( int filedes, int ...
- IPC with pipes, also dup2 redirect stream handle
#include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unist ...
随机推荐
- layout布局实例化
实例化xml中的Layout布局在开发中经常会用到,有几种方法可以使用 1.在Activity中使用getLayoutInflater()方法 View layout = getLayoutInfla ...
- Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- Android(java)学习笔记249:ContentProvider使用之获得系统联系人信息01
1.系统联系人的数据库(3张最重要的表) (1)raw_contacts 联系人表 保存联系人的id contact_id (2)data 数据表 保存联系人的数据 ( ...
- NYOJ-129 并查集
这个题基本上是并查集稍微一变, 只是加了一些判断条件而已,就是将点合并成树, 最后遍历一下, 统计一下有多少棵树, 如果不是1的话, 肯定不是树,所以,可以根据这个来判断 #include <s ...
- jQuery mini ui 2
1.<a class="mini-button" iconCls="icon-add" onclick="addRow()" plai ...
- Windows服务器之间rsync同步文件
两台windows7机器 server:192.168.12.104 client:192.168.12.103 目的:将server上的E盘的目录FYFR里面的内容定时同步到client上的D盘下F ...
- 关于Adobe Flash 11.3 引起的火狐使用问题
Adobe Flash 更新到11.3之后,为火狐引入Flash沙盒安全模式,但同时,又造成了部分兼容性问题,导致 Windows vista及 Windows 7上部分火狐崩溃,并致使一些使用Fla ...
- FAL[client]: Failed to request gap sequence GAP - thread 1 sequence 29-29
一:问题描述 主备库所在机器都重启后,rac(主库)开机自动启动,但备库(单实例)不是开机启动,现已手工启动到mount,并已在备库上alter database recover standby d ...
- C#中英文混合字符串过长截断
/// <summary> /// 截断字符串 /// </summary> /// <param name="maxLength">最大长度& ...
- 数据库SQLite在Qt5+VS2012使用规则总结---中文乱码
VS2012默认格式为 "GB2312-80",而有时我们用到字符串需要显示中文时,就会出现乱码.下面仅就Qt5和VS2012中使用数据库SQLite时,做一个简单的备忘录 #in ...