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

时间:2022-11-11 03:09:45

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内核源代码文件都有当前->文件。那么什么是“电流”呢?

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;
 }

2 个解决方案

#1


22  

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(其他archs的类似文件)。

#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 Drivers chapter 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(如果需要的话)来使用特定于进程的信息。[…]

#2


0  

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

当前是类型struct task_struct的全局变量。你可以在[1]中找到它的定义。

Files is a struct files_struct and it contains information of the files used by the current process.

文件是一个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

#1


22  

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(其他archs的类似文件)。

#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 Drivers chapter 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(如果需要的话)来使用特定于进程的信息。[…]

#2


0  

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

当前是类型struct task_struct的全局变量。你可以在[1]中找到它的定义。

Files is a struct files_struct and it contains information of the files used by the current process.

文件是一个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