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

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

问题描述

在浏览下面的Linux char驱动程序代码块时,我在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\n",
    current->pid, current->comm);
    wait_event_interruptible(wq, flag != 0);
    flag = 0;
    printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
    return 0;
}

推荐答案

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

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

来自文档:

当前流程

尽管内核模块不像应用程序那样顺序执行, 内核执行的大多数动作都与特定的 过程.内核代码可以知道当前驱动它的过程 访问当前的全局项,指向struct task_struct的指针, 从内核版本2.4开始声明 <asm/current.h>,由<linux/sched.h>包含. 当前指针 指当前正在执行的用户进程.执行期间 系统调用(例如打开或读取)的当前进程 内核代码可以使用特定于流程的代码 如果需要,可以通过使用电流来获取信息.一个例子 该技术在设备文件上的访问控制"中 第5章,增强的Char驱动程序操作".

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内核中.开发人员优化了对 通过将当前过程隐藏在堆栈中来描述当前过程的结构 页.您可以在<asm/current.h>中查看当前的详细信息.尽管 您要查看的代码可能看起来很冗长,我们必须记住 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)\n",
  current->comm, current->pid);

当前-> 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天全站免登陆