zoukankan      html  css  js  c++  java
  • linux kernel "current" macro

    -------------------------------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! ";
            printk( "hello, from the kernel... " );

            if( current != NULL )
                    printk( "pid(command) = %d(%s) ", current->pid,
                            current->comm );

    }

    /* 清理函数 */
    static void my_cleanup( void )
    {
            printk( "Goodbye, from the kernel ... " );
    }


    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 ", 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的大小 可见一个进程的结构关联了很多的信息, 而且这个结构中有大量的指针,

    在 /include/asm-generic/current.h定义 不知道是否准确

    0008 struct task_struct;
    0009 
    0010 DECLARE_PER_CPU(struct task_struct *, current_task);
    0011 
    0012 static __always_inline struct task_struct *get_current(void)
    0013 {
    0014     return this_cpu_read_stable(current_task);
    0015 }
    0016 
    0017 #define current get_current()

    如果给current赋值的话
    current = NULL;
    lvalue required as left operand of assignment
    可以current 不是一个变量

    在多核系统中怎么保证current 不会指向其他核上的宏呢
    根据DECLARE_PER_CPUthis_cpu_read_stable 就可有知道一些信息




    
    
  • 相关阅读:
    剑指offer--树的子结构
    剑指offer--合并两个排序的链表
    剑指offer--链表中倒数第k个节点
    剑指offer--反转链表
    JavaScript一个简单的显示隐藏功能
    css之animition动画帧
    css之3D变换
    css之过渡、变换
    css之!important
    分栏
  • 原文地址:https://www.cnblogs.com/kwingmei/p/3731746.html
Copyright © 2011-2022 走看看