Linux是什么内核:Linux内核源代码中的“当前”是什么 (current linux kernel version)

我正在研究 Linux 内核,我有一个问题。

我看到很多 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;
 }
43

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

在 x86 上,它在arch/x86/include/asm/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 */

更多信息请参见Linux Device Drivers第 2 章:

当前指针是指当前正在执行的用户进程。在系统调用的执行过程中,如打开或读取,当前进程是调用该调用的进程。内核代码可以通过使用 current 来使用特定于进程的信息,如果它需要这样做的话。[...]

3

Currentstruct task_struct类型的全局变量。您可以在 [1] 中找到它的定义。

Filesstruct files_struct,它包含当前进程使用的文件的信息。

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

这是 ARM64 定义。在arch/arm64/include/asm/current.h中,https://elixir.bootlin.com/linux/latest/source/arch/arm64/include/asm/current.h

struct task_struct;
/*
 * We don't use read_sysreg() as we want the compiler to cache the value where
 * possible.
 */
static __always_inline struct task_struct *get_current(void)
{
    unsigned long sp_el0;
    asm ("mrs %0, sp_el0" : "=r" (sp_el0));
    return (struct task_struct *)sp_el0;
}
#define current get_current()

,它只使用sp_el0寄存器。作为指向当前进程task_struct的指针

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(893)
Php搜索功能代码:结合PHP功能-高级搜索(combining functions advanced)
上一篇
Cpu自带的散热够用么:JavaFX自带JDK8
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(60条)