unix学习笔记-------利用dup()函数来达到共享同一个文件表项的作用
来源:互联网 发布:sftp端口号 编辑:程序博客网 时间:2024/06/03 15:09
dup函数
:
源代码如下:
/*
/*
dup()函数的使用:
1.dup()函数的作用是什么???
利用dup()函数来达到共享同一个文件表项的作用。
(个人):
因为一个文件描述符指向一个打开文件文件对象,所以,如果俩个文件描述符的内容是相同的话,那么这两个文件描述符指向的是同一个文件表项。
即:指向的是同一个文件。
所以:dup()的作用就是: 把一个文件描述符的内容复制到另一个新的文件描述符上去。
2.函数原型:
/*
DUP(2) Linux Programmer鈥檚Manual DUP(2)
NAME
dup, dup2, dup3 - duplicate a file descriptor
/*
remark: duplicate 报错
英 [ˈdjuːplɪkeɪt] 美 ['duplɪket] 跟读 口语练习
vt. 复制;使加倍
n. 副本;复制品
*/
SYNOPSIS
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
#define _GNU_SOURCE
#include <unistd.h>
int dup3(int oldfd, int newfd, int flags);
DESCRIPTION
These system calls create a copy of the file descriptor oldfd.
dup() uses the lowest-numbered unused descriptor for the new descriptor.
dup2() makes newfd be the copy of oldfd, closing newfd first ifnecessary, but note the following:
* If oldfd is not a valid filedescriptor, then the call fails, and newfd is not closed.
* If oldfd is a valid filedescriptor, and newfd has the same value as oldfd, then dup2() does nothing,and returns newfd.
After a successful return from one of these systemcalls, the old and new file descriptors may be used interchangeably. They refer to the same open file description
(see open(2)) and thus share file offset and file status flags; forexample, if the file offset is modified by using lseek(2) on one of thedescriptors, the offset is
also changed for the other.
The two descriptors do not sharefile descriptor flags (the close-on-exec flag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the duplicatedescriptor is off.
dup3() is the same as dup2(), except that:
* The caller can force theclose-on-exec flag to be set for the new file descriptor by specifyingO_CLOEXEC in flags. See the descriptionof the same flag in open(2)
for reasons why this may be useful.
* If oldfd equals newfd, thendup3() fails with the error EINVAL.
RETURN VALUE
On success, these system calls return the new descriptor. On error, -1 is returned, and errno is setappropriately
*/
*/
*/
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
int main()
{
//usethe open () function
intfd_t_1;
intfd_t_new;
charbuf[50];
fd_t_1 =open("/home/code/file1/my.txt",O_RDWR,775);
if(fd_t_1<0)
{
printf("openerror\n");
_exit(0);
}
else
printf("thefile descriptor is : %d \n",fd_t_1);
//call the dup()
fd_t_new =dup(fd_t_1);
printf("the new file descriptor is :%d\n",fd_t_new);
//the new descriptor is for
int rd_t=read(fd_t_new,&buf,20);
if(rd_t <0)
{
perror("readerror \n");
exit(0);
}
printf("the contents of reading is :\n");
printf("%s\n",buf);
return 0;
}
运行截图:
- unix学习笔记-------利用dup()函数来达到共享同一个文件表项的作用
- UNIX环境编程之<二>文件共享I/O操作dup,fcntl函数
- UNIX环境高级编程学习笔记(三)文件共享和其他文件I/O函数
- Linux下面dup/dup2两个函数的学习笔记
- 利用函数的作用域来封装
- UNIX环境编程学习笔记(4)——文件I/O之dup复制文件描述符
- 通过设置VMware来达到Windows与Linux共享文件
- Linux学习笔记:shell重定向功能中dup/dup2系统调用的作用
- Unix 下dup和dup2函数的解析
- linux文件函数-dup
- unix学习笔记------msgctl函数的使用
- 如何利用一个变量来达到多重循环的效果?
- 利用dimens.xml来达到资源的重用
- 利用dimens.xml来达到资源的重用
- 利用dimens.xml来达到资源的重用
- android学习笔记之:利用listview达到表格数据横向排列的效果
- 《UNIX环境高级编程》笔记--文件共享
- 《UNIX环境高级编程》笔记--文件共享
- JFreeChart的总结
- mina ProtocolCodecFilter 中文手册
- Java入门之开发环境搭建—JDK和IDE的下载和安装
- win8 java下载以及配置
- svn、git
- unix学习笔记-------利用dup()函数来达到共享同一个文件表项的作用
- V$STATNAME
- complete.cases筛选NA数据
- Linux动态加载库的使用方法
- Unity编辑时打左括号出现异常的问题
- 自己testkvo的代码心的(有一行代码要特别的注意)
- Pentest - SSH login
- 陈硕-应届生应聘程序员,简历上能写书单吗?最好有哪些信息?
- 黑马程序员——基础知识——多态