2014-03-12 55 views
10

當我正在瀏覽下面的Linux字符驅動程序代碼塊時,我在printk中發現了結構指針currentLinux內核代碼中的「當前」

我想知道current指向哪個結構及其完整元素。

這個結構的用途是什麼?

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; 
} 
+2

看起來它是指由系統調用過程中的結構的完整結構。 – Barmar

+1

嘗試使用'註釋的linux內核'搜索,你應該找到一些有用的資源。 – Barmar

+4

http://kernelnewbies.org/FAQ/current – cnicutar

回答

18

它是一個指向當前進程的指針,即發出系統調用的進程。

docs

當前進程

雖然作爲應用程序執行內核模塊不按順序執行,由內核執行 最動作都與一個特定的 過程。內核代碼可以通過 驅動訪問全局項目當前的進程,指向結構task_struct的指針 ,該內核的版本2.4的內核聲明爲 <asm/current.h>,包含在<linux/sched.h>中。 當前指針 指的是當前正在執行的用戶進程。在執行系統調用 期間,如打開或讀取,當前進程是調用該調用的那個 。如果需要,內核代碼可以使用當前的進程特定的 信息。這個 技術的例子在 的第5章「增強的字符驅動程序操作」中的「對設備文件的訪問控制」中介紹。

實際上,電流不再是一個全局變量,就像 是第一個Linux內核一樣。開發人員通過將其隱藏在堆棧 頁面中來優化對描述當前進程的 結構的訪問。你可以在<asm/current.h>看看當前的細節。雖然 您看到的代碼可能看起來很毛茸茸,但我們必須記住, Linux是一個符合SMP的系統,當您處理多個CPU時,全局變量根本不會 工作。儘管如此, 實現的細節對其他內核子系統仍然是隱藏的,並且設備驅動程序可以包括並參考當前進程。

從模塊的角度來看,電流就像參考printk的外部 。模塊可以在任何合適的地方引用當前模塊。 例如,下面的語句打印過程ID,並通過在 結構的task_struct訪問某些字段中的當前進程的 命令名:

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

存儲在電流 - 該命令名> COMM是的基本名稱當前進程正在執行的 程序文件。

4

這裏是「當前」指向

task_struct 
Each task_struct data structure describes a process or task in the system. 

struct task_struct { 
/* these are hardcoded - don't touch */ 
    volatile long  state;   /* -1 unrunnable, 0 runnable, >0 stopped */ 
    long     counter; 
    long     priority; 
    unsigned    long signal; 
    unsigned    long blocked; /* bitmap of masked signals */ 
    unsigned    long flags;  /* per process flags, defined below */ 
    int errno; 
    long     debugreg[8]; /* Hardware debugging registers */ 
    struct exec_domain *exec_domain; 
/* various fields */ 
    struct linux_binfmt *binfmt; 
    struct task_struct *next_task, *prev_task; 
    struct task_struct *next_run, *prev_run; 
    unsigned long  saved_kernel_stack; 
    unsigned long  kernel_stack_page; 
    int     exit_code, exit_signal; 
    /* ??? */ 
    unsigned long  personality; 
    int     dumpable:1; 
    int     did_exec:1; 
    int     pid; 
    int     pgrp; 
    int     tty_old_pgrp; 
    int     session; 
    /* boolean value for session group leader */ 
    int     leader; 
    int     groups[NGROUPS]; 
    /* 
    * pointers to (original) parent process, youngest child, younger sibling, 
    * older sibling, respectively. (p->father can be replaced with 
    * p->p_pptr->pid) 
    */ 
    struct task_struct *p_opptr, *p_pptr, *p_cptr, 
         *p_ysptr, *p_osptr; 
    struct wait_queue *wait_chldexit; 
    unsigned short  uid,euid,suid,fsuid; 
    unsigned short  gid,egid,sgid,fsgid; 
    unsigned long  timeout, policy, rt_priority; 
    unsigned long  it_real_value, it_prof_value, it_virt_value; 
    unsigned long  it_real_incr, it_prof_incr, it_virt_incr; 
    struct timer_list real_timer; 
    long     utime, stime, cutime, cstime, start_time; 
/* mm fault and swap info: this can arguably be seen as either 
    mm-specific or thread-specific */ 
    unsigned long  min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap; 
    int swappable:1; 
    unsigned long  swap_address; 
    unsigned long  old_maj_flt; /* old value of maj_flt */ 
    unsigned long  dec_flt;  /* page fault count of the last time */ 
    unsigned long  swap_cnt;  /* number of pages to swap on next pass */ 
/* limits */ 
    struct rlimit  rlim[RLIM_NLIMITS]; 
    unsigned short  used_math; 
    char     comm[16]; 
/* file system info */ 
    int     link_count; 
    struct tty_struct *tty;   /* NULL if no tty */ 
/* ipc stuff */ 
    struct sem_undo  *semundo; 
    struct sem_queue  *semsleeping; 
/* ldt for this task - used by Wine. If NULL, default_ldt is used */ 
    struct desc_struct *ldt; 
/* tss for this task */ 
    struct thread_struct tss; 
/* filesystem information */ 
    struct fs_struct  *fs; 
/* open file information */ 
    struct files_struct *files; 
/* memory management info */ 
    struct mm_struct  *mm; 
/* signal handlers */ 
    struct signal_struct *sig; 
#ifdef __SMP__ 
    int     processor; 
    int     last_processor; 
    int     lock_depth;  /* Lock depth. 
              We can context switch in and out 
              of holding a syscall kernel lock... */ 
#endif 
}; 
+0

從上面的問題:「這個結構有什麼用途? – dragosht