“当前"在 Linux 内核代码中 [英] "current" in Linux kernel code

查看:18
本文介绍了“当前"在 Linux 内核代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我浏览下面的 Linux 字符驱动程序代码块时,我在 printk 中找到了结构指针 current.

As I was going through the below chunk of Linux char driver code, I found the structure pointer current in printk.

我想知道 current 指向什么结构及其完整元素.

I want to know what structure the current is pointing to and its complete elements.

这个结构有什么用途?

ssize_t sleepy_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
{
    printk(KERN_DEBUG "process %i (%s) going to sleep
",
    current->pid, current->comm);
    wait_event_interruptible(wq, flag != 0);
    flag = 0;
    printk(KERN_DEBUG "awoken %i (%s)
", current->pid, current->comm);
    return 0;
}

推荐答案

指向当前进程的指针,即发出系统调用的进程.

It is a pointer to the current process ie, the process which has issued the system call.

来自文档:

当前流程

虽然内核模块不像应用程序那样按顺序执行,内核执行的大多数操作都与特定的过程.内核代码可以通过以下方式知道驱动它的当前进程访问全局项 current,指向 struct task_struct 的指针,从内核的 2.4 版开始,它在,包含在 中.当前指针指当前正在执行的用户进程.执行过程中系统调用,例如 open 或 read,当前进程是一个 内核代码可以使用特定于进程的如果需要,请使用当前的信息.这方面的一个例子技术在设备文件的访问控制"中介绍,在第 5 章,增强的字符驱动程序操作".

Although kernel modules don't execute sequentially as applications do, most actions performed by the kernel are related to a specific process. Kernel code can know the current process driving it by accessing the global item current, a pointer to struct task_struct, which as of version 2.4 of the kernel is declared in <asm/current.h>, included by <linux/sched.h>. 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. An example of this technique is presented in "Access Control on a Device File", in Chapter 5, "Enhanced Char Driver Operations".

实际上,current 不再是一个合适的全局变量,就像它一样在第一个 Linux 内核中.开发人员优化了访问通过将当前进程隐藏在堆栈中来描述当前进程的结构页.你可以在中查看current的详细信息.尽管您将看到的代码可能看起来很麻烦,我们必须牢记Linux 是一个符合 SMP 的系统,全局变量根本不会在处理多个 CPU 时工作.详情实现对其他内核子系统仍然是隐藏的,并且设备驱动程序可以只包含并引用当前进程.

Actually, current is not properly a global variable any more, like it was in the first Linux kernels. The developers optimized access to the structure describing the current process by hiding it in the stack page. You can look at the details of current in <asm/current.h>. While the code you'll look at might seem hairy, we must keep in mind that Linux is an SMP-compliant system, and a global variable simply won't work when you are dealing with multiple CPUs. The details of the implementation remain hidden to other kernel subsystems though, and a device driver can just include and refer to the current process.

从模块的角度来看,电流就像外部参考印刷品.一个模块可以在它认为合适的任何地方引用当前.例如,以下语句打印进程 ID 和通过访问某些字段的当前进程的命令名称struct task_struct:

From a module's point of view, current is just like the external reference printk. A module can refer to current wherever it sees fit. For example, the following statement prints the process ID and the command name of the current process by accessing certain fields in struct task_struct:

 printk("The process is "%s" (pid %i)
",
  current->comm, current->pid);

存储在 current->comm 中的命令名是当前进程正在执行的程序文件.

The command name stored in current->comm is the base name of the program file that is being executed by the current process.

这篇关于“当前"在 Linux 内核代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆