Linux 内核源代码中的“当前”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12434651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:21:44  来源:igfitidea点击:

What is the "current" in Linux kernel source?

linuxlinux-kernelkernel

提问by Kahn Cse

I'm studying about Linux kernel and I have a problem.

我正在研究 Linux 内核,但遇到了问题。

I see many Linux kernel source files have current->files. So what is the current?

我看到很多 Linux 内核源文件都有current->files. 那么什么是current

struct file *fget(unsigned int fd)
{
     struct file *file;
     struct files_struct *files = current->files;

     rcu_read_lock();
     file = fcheck_files(files, fd);
     if (file) {
             /* File object ref couldn't be taken */
             if (file->f_mode & FMODE_PATH ||
                 !atomic_long_inc_not_zero(&file->f_count))
                     file = NULL;
     }
     rcu_read_unlock();

     return file;
 }

采纳答案by Mat

It's a pointer to the current process (i.e. the process that issued the system call).

它是指向当前进程(即发出系统调用的进程)的指针。

On x86, it's defined in arch/x86/include/current.h(similar files for other archs).

在 x86 上,它是在arch/x86/include/current.h(其他拱门的类似文件)中定义的。

#ifndef _ASM_X86_CURRENT_H
#define _ASM_X86_CURRENT_H

#include <linux/compiler.h>
#include <asm/percpu.h>

#ifndef __ASSEMBLY__
struct task_struct;

DECLARE_PER_CPU(struct task_struct *, current_task);

static __always_inline struct task_struct *get_current(void)
{
    return percpu_read_stable(current_task);
}

#define current get_current()

#endif /* __ASSEMBLY__ */

#endif /* _ASM_X86_CURRENT_H */

More information in Linux Device Driverschapter 2:

Linux 设备驱动程序第 2 章中的更多信息:

The current pointer refers to the user process currently executing. During the execution of a system call, such as open or read, the current process is the one that invoked the call. Kernel code can use process-specific information by using current, if it needs to do so. [...]

当前指针指的是当前正在执行的用户进程。在执行系统调用(例如 open 或 read)期间,当前进程是调用该调用的进程。如果需要,内核代码可以通过使用 current 来使用特定于进程的信息。[...]

回答by coredump

Currentis a global variable of type struct task_struct. You can find it's definition at [1].

Current是一个类型为 的全局变量struct task_struct。您可以在 [1] 中找到它的定义。

Filesis a struct files_structand it contains information of the files used by the current process.

Files是一个struct files_struct,它包含当前进程使用的文件的信息。

[1] http://students.mimuw.edu.pl/SO/LabLinux/PROCESY/ZRODLA/sched.h.html

[1] http://students.mimuw.edu.pl/SO/LabLinux/PROCESY/ZRODLA/sched.h.html