文件描述符标志 文件状态标志

tb754136505 2012-07-18 11:22:00
最近在看unix环境高级编程……有两个地方不懂,百度了一圈也没找到满意答案!望大家不吝赐教!
文件描述符标志 文件状态标志
这两个名词是什么意思啊?请大家详细解释,最好配上例子,谢谢了!
...全文
367 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyuncong250 2013-06-05
  • 打赏
  • 举报
回复
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main(){ int fd=open("a.txt",O_CREAT|O_RDWR|O_APPEND); if(fd == -1) perror("open"),exit(-1); int fd1 = dup2(fd,5); //如果5被使用,返回比5大的未使用的最小值 int fd2 = fcntl(fd,F_DUPFD,5); printf("fd=%d,fd2=%d\n",fd,fd2); int flags = fcntl(fd,F_GETFL); printf("flags=%d\n",flags);//没有O_CREAT if(flags & O_CREAT) printf("有Creat\n"); if(flags & O_RDWR) printf("读写权限\n"); if(flags & O_APPEND) printf("有追加\n"); //修改时只能修改状态标记,其他都不能修改 fcntl(fd,F_SETFL,O_RDONLY|O_TRUNC); flags = fcntl(fd,F_GETFL); if(flags & 3 == 0) printf("只读权限\n"); if(flags & O_RDWR) printf("读写权限\n"); if(flags & O_TRUNC) printf("TRUNC\n"); if(flags & O_APPEND) printf("Append\n"); close(fd); } /*fd=3,fd2=6 flags=1026 读写权限 有追加 读写权限*/
zjk2752 2013-04-13
  • 打赏
  • 举报
回复
文件描述符是一个标示,非负整数,类似于windows里的句柄,为了与标准C保持一致(标准C里的文件的读写都是通过File Pointer)UNIX采用了这样的三级结构,我混淆于文件描述标志和文件状态标志,还是看英文来的有效,fd flag = close_on_exec。是在一个文件在某进程中的标示,而由于文件可以被多个进程打开,因此这个file status flag能被很多个进程访问到,它表示的是这个文件在此刻的读写等标示。下面附上一份原文。 Note the difference in scope between the file descriptor flags and the file status flags. The former apply only to a single descriptor in a single process, whereas the latter apply to all descriptors in any process that point to the given file table entry. When we describe the fcntl function in Section 3.14, we'll see how to fetch and modify both the file descriptor flags and the file status flags.
zjk2752 2013-04-13
  • 打赏
  • 举报
回复
我看到这里也纠结了,文件状态标志还好理解,就是OPEN的时候设置的RD,WR,RDWR等。但是这个文件描述符标志书中没有太多说明。楼上一大票人都没说到点子上
AnYidan 2012-08-05
  • 打赏
  • 举报
回复
a small non-negative integer called a file descriptor

The C programming Language -- 第八章
G1036583997 2012-08-05
  • 打赏
  • 举报
回复
书中的例子:对于指定的描述符打印文件标志 :是对文件描述符(文件描述符0与进程的标准输入相关联,文件描述符1与标准输出相关联,文件描述符2与标准出错相关联………………)的文件标志即他的类似权限的东西。0只能用它进行输入从中读取内容,1可以写也可以追加

ming@ming-ThinkPad-Edge:~/src/chapter3$ ./a.out 0 <data
read only
ming@ming-ThinkPad-Edge:~/src/chapter3$ ./a.out 1 >data
ming@ming-ThinkPad-Edge:~/src/chapter3$ cat data
write only
ming@ming-ThinkPad-Edge:~/src/chapter3$ :>data
ming@ming-ThinkPad-Edge:~/src/chapter3$ ./a.out 1 >>data
ming@ming-ThinkPad-Edge:~/src/chapter3$ cat data
write only, append
ming@ming-ThinkPad-Edge:~/src/chapter3$ :>data
ming@ming-ThinkPad-Edge:~/src/chapter3$ ./a.out 2 2>data
write only
ming@ming-ThinkPad-Edge:~/src/chapter3$ cat data
ming@ming-ThinkPad-Edge:~/src/chapter3$ ./a.out 2 2>>data
write only, append


以上纯属个人观点.PS:本人水平不高,正在看此书,如有错误请见谅
Universe_Admin 2012-07-19
  • 打赏
  • 举报
回复
文件描述符标志设置见 int fcntl(int fd , int cmd,...);
文件状态标志的设置见 void set_fl(int fd, int flags); void clr_fl(int fd, int flags);
tb754136505 2012-07-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

这个状态是指什么?

描述符指向文件表项。
[/Quote]
描述符指向文件表项?文件描述符不是一个标识一个已打开文件的整数吗?
我是在书上看到的名词,具体状态是什么也不清楚……
书上的图是文件指针指向一个文件表,文件表里面有一个文件状态标志……
qq120848369 2012-07-18
  • 打赏
  • 举报
回复
这个状态是指什么?

描述符指向文件表项。

70,026

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧