同一文件的两个文件描述符 [英] two file descriptors to same file

查看:273
本文介绍了同一文件的两个文件描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用posix的read()write()linux调用,可以确保如果我以串行方式写入一个文件描述符并读取另一个文件描述符,以使两个动作相互排斥. .我的读取文件描述符将始终看到写入文件描述符最后写入的内容?

Using the posix read() write() linux calls, is it guaranteed that if I write through one file descriptor and read through another file descriptor, in a serial fashion such that the two actions are mutually exclusive of each other... that my read file descriptor will always see what was written last by the write file descriptor?

我相信是这种情况,但是我想确定一下,手册页对此没有太大帮助

i believe this is the case, but I want to make sure and the man page isn't very helpful on this

推荐答案

这取决于您在哪里获得两个文件描述符.如果它们来自dup(2)调用,则它们共享文件的偏移量和状态,因此对一个文件执行write(2)将影响另一个文件的位置.另一方面,如果它们来自两个单独的open(2)调用,则每个调用将具有自己的文件偏移量和状态.

It depends on where you got the two file descriptors. If they come from a dup(2) call, then they share file offset and status, so doing a write(2) on one will affect the position on the other. If, on the other hand, they come from two separate open(2) calls, each will have their own file offset and status.

文件描述符主要只是对内核文件结构的引用,而正是该内核结构包含大部分状态.打开(2)文件时,您将获得一个新的内核文件结构和一个引用该文件的新文件描述符. dup(2)一个文件描述符(或通过sendmsg传递文件描述符)时,您将获得对相同内核文件结构的新引用.

A file descriptor is mostly just a reference to a kernel file structure, and it is that kernel structure that contains most of the state. When you open(2) a file, you get a new kernel file structure and a new file descriptor that refers to it. When you dup(2) a file descriptor (or pass a file descriptor through sendmsg), you get a new reference to the same kernel file struct.

这篇关于同一文件的两个文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆