files。那么current是什么? struct file *fget(unsigned i-6ren">
gpt4 book ai didi

linux - Linux内核源码中的 "current"是什么?

转载 作者:IT王子 更新时间:2023-10-29 00:16:45 27 4
gpt4 key购买 nike

我正在研究 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;
}

最佳答案

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

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

更多信息请参见 Linux Device Drivers第 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. [...]

关于linux - Linux内核源码中的 "current"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12434651/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com