【问题标题】:Open() syscall filedesriptorOpen() 系统调用filedesriptor
【发布时间】:2015-01-06 01:41:00
【问题描述】:

我必须更正 posix 操作系统的 open() 系统调用的返回值。我从 man-Pages 了解到它必须返回 文件描述符,并且,如果出现错误,系统调用将返回 -1 并设置 errno 值。问题是我不知道如何获取打开的节点的文件描述符。我检查了所有文件,没有找到可以将 fd 分配给进程的方法。

方法如下:

    int syscalls::open(const char *path, int oflags, mode_t mode){

    syscall_message msg;

    msg.call.type = syscalls::open_call;
    msg.open_data.path_name = &path[0];
    msg.open_data.flags = oflags;
    msg.open_data.create_mode = mode;

    syscaller::call_system(msg);

    return msg.error.number;
}

syscall_message 是一个保存系统调用数据信息的结构。 syscalls 是所有系统调用所在的namesapacesyscaller 用于将调用发送到内核,取消 call_system 方法。

call_system 方法:

syscalls::open_call:
        {
            //get the file
            i_fs_node_ptr file = i_fs::open_node( msg.open_data.path_name );

            //add the file handle
            if ( file )
            {
                cur_process->push_filehandle(
                        file,
                        msg.open_data.flags,
                        msg.open_data.create_mode );
            }
            else
            {
                msg.error.type = syscalls::e_no_such_entry;
            }

        }

【问题讨论】:

  • syscall_messagesyscallssyscaller 是什么?它们是您正在使用的某个库或框架的一部分吗?

标签: c++ operating-system system-calls


【解决方案1】:

我不知道您所说的“我无法获取文件描述符”是什么意思。正如您所提到的 open() 返回它。它只是存储在一个整数变量中。如果此变量等于 -1,则说明出现问题。例如,如果您有

int file = open(path, O_SYNC, O_DIRECT, O_RDONLY);

但您没有读取名为 path 的文件的权限,变量 file 的值将是 -1。可以通过 read()(如果文件以读取模式打开)和 write()(如果文件以写入模式打开)对打开的文件进行其他操作。我建议你仔细阅读documentation on the open() function。如果您需要对文件描述符进行更多控制,我建议您使用 fopen():

【讨论】:

  • 是的,但我指的是open()系统调用woks的方式。我发布的方法是针对 Posix 操作系统的 open() 系统调用。该调用通过call_system() 将打开文件所需的数据发送到内核。在call_system(msg) 发挥作用并且文件打开或未打开后,我必须返回该文件的fd,或者如果出现错误则返回-1。现在,msg 保存了内核用来打开新节点的数据。但是在节点打开后,它不会为该节点设置文件描述符。我将发布call_system() 方法。
猜你喜欢
  • 2015-06-02
  • 1970-01-01
  • 2010-10-10
  • 2012-10-21
  • 1970-01-01
  • 2013-02-15
  • 2021-09-16
  • 1970-01-01
  • 2015-04-20
相关资源
最近更新 更多