linux内核源代码中的"当前"是什么

Kah*_*Cse 23 linux kernel linux-kernel

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

我看到很多linux内核源文件都有current-> files.那么"当前"是什么?

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;
 }
Run Code Online (Sandbox Code Playgroud)

Mat*_*Mat 33

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

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

#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 */
Run Code Online (Sandbox Code Playgroud)

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

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