博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux kernel "current" macro
阅读量:4356 次
发布时间:2019-06-07

本文共 2084 字,大约阅读时间需要 6 分钟。

-------------------------------source---------------------------------

#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/tty.h>
/* 函数声明 */
void tty_write_message1(struct tty_struct *, char * );
/* 初始化函数 */
static int my_init( void )
{
        char *msg = "Hello tty!\n";
        printk( "hello, from the kernel...\n" );
        if( current != NULL )
                printk( "pid(command) = %d(%s) \n", current->pid,
                        current->comm );

}

 

 

/* 清理函数 */

static void my_cleanup( void )
{
        printk( "Goodbye, from the kernel ...\n" );
}
module_init( my_init );
module_exit( my_cleanup );
// this routine was borrowed from <printk.c>
void tty_write_message1( struct tty_struct *tty, char *msg )
{
        if( tty && tty->driver && tty->driver->ops && tty->driver->ops->write )
                tty->driver->ops->write( tty,  msg, strlen( msg) );

  // 写tty

        return ;
}

------------------------------output---------------------

$ sudo insmod currentptr.ko

Hello tty!

 

[ 1029.174761] hello, from the kernel...

[ 1029.174777] pid(command) = 2726(insmod)
[ 1029.174780] parent pid = 2725(sudo)
[ 1029.174782] parent parent pid = 7556(bash)
[ 1029.174784] parent parent parent pid = 7529(gnome-terminal)

 

----------------------------------------------------------------------------------------

在linux的内核的世界里, 有了current的个宏, 就可以去探索进程相关的代码

如: 打印进程列表

struct task_struct *task

for_each_process(task)

    {
        printk( KERN_WARNING "%8d%8ld%8d%s\n", task->pid,
            task->state, task->on_cpu, task->comm  );
    }   

 

---------------------------------------

[ 2597.598496]        1       1       0  init

[ 2597.598499]        2       1       0  kthreadd
[ 2597.598501]        3       1       0  ksoftirqd/0
[ 2597.598503]        6       1       0  migration/0
[ 2597.598506]        7       1       0  watchdog/0
[ 2597.598508]        8       1       0  migration/1

 

task_struct 是进程结构 这个结构的大小 大约是4k的大小 可见一个进程的结构关联了很多的信息, 而且这个结构中有大量的指针,

0008 struct ;0009 0010 (struct  *, );0011 0012 static  struct  *(void)0013 {0014     return ();0015 }0016 0017 #define  () 如果给current赋值的话 current = NULL; lvalue required as left operand of assignment 可以current 不是一个变量 在多核系统中怎么保证current 不会指向其他核上的宏呢 根据,  就可有知道一些信息

 

 
 

转载于:https://www.cnblogs.com/kwingmei/p/3731746.html

你可能感兴趣的文章
L2-001 紧急救援 [Dijkstra]
查看>>
【洛谷4219】[BJOI2014]大融合(线段树分治)
查看>>
深度学习面试题19:1*1卷积核的作用
查看>>
OData services入门----使用ASP.NET Web API描述
查看>>
python 解析html 时lxml跟beautifulSoup对比
查看>>
C# 获取系统开机时间
查看>>
ASP.NET Core 2.0 : 七.一张图看透启动背后的秘密
查看>>
wpf CollectionViewSource与ListBox的折叠、分组显示,及输入关键字 Filter的筛选
查看>>
【DDD】领域驱动设计实践 —— 框架实现
查看>>
SQL Server 2016新特性:DROP IF EXISTS
查看>>
用css3实现各种图标效果(2)
查看>>
防SQL注入:生成参数化的通用分页查询语句
查看>>
算法系列15天速成——第十四天 图【上】
查看>>
SQL通过日期计算年龄
查看>>
10天学安卓-第六天
查看>>
js验证身份证号
查看>>
在打包程序中自动安装SQL Server数据库 .
查看>>
删除sql server中重复的数据
查看>>
[Windows Phone] 如何撰写连接 Wifi、蓝芽、网路、飞航模式的网路设定功能
查看>>
[原创].NET 分布式架构开发实战之四 构建从理想和实现之间的桥梁(前篇)
查看>>