APUE习题3.2----自己实现的dup2( )函数的源码
2017-05-18 21:56
344 查看
APUE习题3.2----自己实现的dup2( )函数的源码
转载的,不清楚出处在哪里,对于我这个菜鸟来说,写的很好。
原 dup2()函数:
对于 dup2,可以用 fd2 参数指定新描述符的值。如果 fd2 已经打开,则先将其关闭。如若 fd 等于 fd2,则 dup2 返回 fd2,而不关闭它。否则,fd2 的 FD_CLOEXEC 文件描述符标志就被清除,这样 fd2 在进程调用 exec 时是打开状态。该函数返回的新文件描述符与参数 fd 共享同一个文件表项。
下面是自己实现的 dup2函数:
程序运行结果:
[zhang@localhost APUE]$ ./my_dup2
write to testfd 5 successfully
read from initial fd 3 is: Hey man!
测试通过。
转载的,不清楚出处在哪里,对于我这个菜鸟来说,写的很好。
原 dup2()函数:
#include <unistd.h> int dup2( int fd, int fd2 );
对于 dup2,可以用 fd2 参数指定新描述符的值。如果 fd2 已经打开,则先将其关闭。如若 fd 等于 fd2,则 dup2 返回 fd2,而不关闭它。否则,fd2 的 FD_CLOEXEC 文件描述符标志就被清除,这样 fd2 在进程调用 exec 时是打开状态。该函数返回的新文件描述符与参数 fd 共享同一个文件表项。
下面是自己实现的 dup2函数:
#include <unistd.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> //检查文件描述符是否有效 int isFileDescriptor( int fd ) { struct stat st; if( (-1 == fstat(fd,&st)) && (EBADF == errno) ) return -1; return 0; } int my_dup2( int oldfd, int newfd ) { int tempfd; int fd_count; int fdarray[newfd]; int res; if( -1 == isFileDescriptor( oldfd ) ) { printf("the file descriptor is invalid.\n"); return -1; } //如果newfd等于oldfd,则直接返回newfd,而不关闭它 if( oldfd == newfd ) return newfd; //否则,关闭newfd if( 0 == isFileDescriptor( newfd ) ) { res = close( newfd ); if( -1 == res ) { perror("close file descriptor failed"); return -1; } } //复制文件描述符 for( fd_count=0; fd_count<newfd; fd_count++ ) fdarray[fd_count] = 0; for( fd_count=0; fd_count<newfd; fd_count++ ) { tempfd = dup( oldfd ); if( -1 == tempfd ) return -1; if( tempfd == newfd ) break; else fdarray[fd_count] = 1; } //关闭之前寻找指定描述符时打开的描述符 for( fd_count=0; fd_count<newfd; fd_count++ ) { if( 1 == fdarray[fd_count] ) { res = close( fd_count ); if( -1 ==res ) { perror("close file descriptor failed"); return -1; } } } return tempfd; } //测试代码 int main() { int fd; int testfd; int res; char *buffer = (char *)malloc(sizeof(char)*32); fd = open("/tmp/dup2test1.txt", O_RDWR | O_CREAT, 0666); if( -1 == fd ) { perror("file open failed"); exit( EXIT_SUCCESS ); } testfd = my_dup2( fd, 5 ); res = write( testfd, "Hey man!", strlen("Hey man!") ); //通过复制得到的文件描述符 testfd 写数据 if( -1 == res ) { perror("write to testfd failed"); exit( EXIT_FAILURE ); } printf("write to testfd %d successfully\n", testfd); memset( buffer, '\0', 32 ); lseek( testfd, 0, SEEK_SET ); res = read( fd, buffer, 30 ); //通过初始的文件描述符 fd 读取数据 if( -1 == res ) { perror("read from testfd failed"); exit( EXIT_FAILURE ); } printf("read from initial fd %d is: %s\n", fd, buffer ); exit( EXIT_SUCCESS ); }
程序运行结果:
[zhang@localhost APUE]$ ./my_dup2
write to testfd 5 successfully
read from initial fd 3 is: Hey man!
测试通过。
相关文章推荐
- APUE习题3.2----自己实现的dup2( )函数的源码
- APUE(习题3.2)实现与dup2功能相同的函数
- UNIX环境高级编程习题3.2 自己实现dup2函数功能
- APUE习题[实现dup2函数功能,不使用fcntl]
- APUE习题[实现dup2函数功能,不使用fcntl]
- APUE习题3.2用dup实现dup2以及shell中重定向符号的使用
- apue第三章习题3.2 编写dup2功能相同的函数,要求不调用fcntl函数
- 习题3.2:不调用fcntl函数来实现dup2的功能
- 不调用fcntl,自己实现dup2函数功能
- APUE 习题3-2 实现dup2,要求不使用fcntl函数。
- 习题 8.17 写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为int strcmp(char *p1, char *p2);
- APUE习题4.6源代码----实现自己的简易 cp 命令
- MPI之自己实现Alltoall()函数
- sort函数的用法(C++排序库函数的调用) 对数组进行排序,在c++中有库函数帮我们实现,这们就不需要我们自己来编程进行排序了。 (一)为什么要用c++标准库里的排序函数 Sort()函数是c+
- APUE之dup,dup2函数重定向标准输出实例
- 多键比较函数的源码实现
- 自己编写函数,实现库函数里的strcpy函数
- c语言:实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定
- 自己实现字符串操作函数strlen(),strcat(),strcpy(),strcmp()
- Net如何继承IDisposable接口,实现自己的Dispose()函数