首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是Xv6中的Omode?

什么是Xv6中的Omode?
EN

Stack Overflow用户
提问于 2018-11-18 22:23:59
回答 1查看 571关注 0票数 1

在xv6中处理文件时,我可以看到一个名为Omode的整数变量。那是什么?它能有什么价值呢?

例如,这是来自Xv6的打开系统调用:

代码语言:javascript
复制
int sys_open(void)
{
  char *path;
  int fd, omode;
  struct file *f;
  struct inode *ip;

  if (argstr(0, &path) < 0 || argint(1, &omode) < 0)
    return -1;

  begin_op();

  if (omode & O_CREATE) {
    ip = create(path, T_FILE, 0, 0);
    if (ip == 0) {
      end_op();
      return -1;
    }
  } else {
    if ((ip = namei(path)) == 0) {
      end_op();
      return -1;
    }
    ilock(ip);
    if (ip->type == T_DIR && omode != O_RDONLY) {
      iunlockput(ip);
      end_op();
      return -1;
    }
  }

  if ((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0) {
    if (f)
      fileclose(f);
    iunlockput(ip);
    end_op();
    return -1;
  }
  iunlock(ip);
  end_op();

  f->type = FD_INODE;
  f->ip = ip;
  f->off = 0;
  f->readable = !(omode & O_WRONLY);
  f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
  return fd;
}

它似乎可以是O_WRONLY、O_RDWR或O_CREATE。这些值代表什么?

EN

回答 1

Stack Overflow用户

发布于 2018-11-26 06:10:55

omode (代表打开模式),是xv6操作系统中打开系统调用的第二个参数,表示打开文件时使用的模式,第一个参数中给出了文件的名称和路径。

来自xv6的官方book

Open (文件名,标志)打开文件;标志表示读/写

此字段的有效选项为(定义位于fcntl.h):

代码语言:javascript
复制
#define O_RDONLY  0x000
#define O_WRONLY  0x001
#define O_RDWR    0x002
#define O_CREATE  0x200

其中:

  • O_RDONLY -声明文件应以只读模式打开。不要让写入由从open调用返回的文件描述符表示的文件。
  • O_WRONLY-与上面相同,但只允许写入no reading.
  • O_RDWR -允许read和write.
  • O_CREATE -允许open创建给定文件(如果该文件不存在)。

您还可以进一步跟踪代码,看看在哪里使用了readable和writable:

不允许时读取可读数据块:

代码语言:javascript
复制
// Read from file f.
int
fileread(struct file *f, char *addr, int n)
{
  int r;

  if(f->readable == 0)
    return -1;
...

writes的工作原理与writes类似:

代码语言:javascript
复制
// Write to file f.
int
filewrite(struct file *f, char *addr, int n)
{
  int r;

  if(f->writable == 0)
    return -1;
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53361908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档