//------------------------------------------------------------------- // sleep.c // // This module creates a pseudo-file (named '/proc/sleep') that // an application can read in order to see the current value of // the 'sleep_time' field in each task's task_struct. // // programmer: ALLAN CRUSE // written on: 14 SEP 2004 //------------------------------------------------------------------- #include // for init_module() #include // for create_proc_read_entry() #define SUCCESS 0 static char modname[] = "sleep"; static int my_proc_read( char *buf, char **start, off_t off, int count, int *eof, void *data ) { struct task_struct *tsk = &init_task; int item, len; char state[ 40 ]; len = item = 0; do { ++item; len += sprintf( buf+len, "%5d ", tsk->pid ); len += sprintf( buf+len, "%10lu ", tsk->sleep_time ); len += sprintf( buf+len, "%-16s ", tsk->comm ); switch( tsk->state ) { case TASK_RUNNING: strcpy( state, "TASK_RUNNING" ); break; case TASK_INTERRUPTIBLE: strcpy( state, "TASK_INTERRUPTIBLE" ); break; case TASK_UNINTERRUPTIBLE: strcpy( state, "TASK_UNINTERRUPTIBLE" ); break; case TASK_STOPPED: strcpy( state, "TASK_STOPPED" ); break; case TASK_ZOMBIE: strcpy( state, "TASK_ZOMBIE" ); break; default: strcpy( state, "(other)" ); break; } len += sprintf( buf+len, "%s ", state ); len += sprintf( buf+len, "\n" ); tsk = tsk->next_task; } while ( tsk != &init_task ); len += sprintf( buf+len, "\njiffies=%lu ", jiffies ); len += sprintf( buf+len, " number of tasks = %d ", item ); len += sprintf( buf+len, "\n" ); return len; } void cleanup_module( void ) { remove_proc_entry( modname, NULL ); printk( "<1>Removing \'%s\' module\n", modname ); } int init_module( void ) { printk( "<1>\nInstalling \'%s\' module\n", modname ); create_proc_read_entry( modname, 0, NULL, my_proc_read, NULL ); return SUCCESS; } MODULE_LICENSE("GPL");