//------------------------------------------------------------------- // kthreads.c // // This module creates a pseudo-file (named '/proc/kthreads') // which shows the name and process-ID of each kernel thread. // (It is one possible solution to question 5 on Midterm II.) // // programmer: ALLAN CRUSE // written on: 14 NOV 2004 //------------------------------------------------------------------- #include // for init_module() #include // for create_proc_info_entry() static char modname[] = "kthreads"; static int my_get_info( char *buf, char **start, off_t off, int count ) { struct task_struct *tsk = &init_task; unsigned long kthreads = 0; int len; len = 0; len += sprintf( buf+len, "\nList of kernel threads\n" ); do { if ( tsk->mm == 0 ) // task is a kernel thread? { ++kthreads; len += sprintf( buf+len, "#%-2d ", kthreads ); len += sprintf( buf+len, "%15s ", tsk->comm ); len += sprintf( buf+len, "pid=%-5d \n", tsk->pid ); } tsk = tsk->next_task; } while ( tsk != &init_task ); len += sprintf( buf+len, "\n" ); return len; } int init_module( void ) { printk( "<1>\nInstalling \'%s\' module\n", modname ); create_proc_info_entry( modname, 0, NULL, my_get_info ); return 0; //SUCCESS } void cleanup_module( void ) { remove_proc_entry( modname, NULL ); printk( "<1>Removing \'%s\' module\n", modname ); } MODULE_LICENSE("GPL");