LECTURE 7 ¥«¡¼¥Í¥ë¥¿¥¤¥Þ¡¼¤ò»È¤¦

¡û ¤Ï¤¸¤á¤Ë

º£²ó¤Ï¡¢¥«¡¼¥Í¥ëÆâ¤Ç»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤­¤ë¥¿¥¤¥Þ¡¼½èÍý¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Þ¤¹¡£°ìÄê»þ´Ö·Ð²á¸å¤ËÆÃÄê¤Î½èÍý¤ò¼Â¹Ô¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¥¿¥¤¥Þ¡¼¤Ï¡¢¥×¥í¥°¥é¥à¤Ç»ÈÍѤ¹¤ëµ¡²ñ¤¬Èó¾ï¤Ë¿¤¯ ( ¥Ý¡¼¥ê¥ó¥°¤Ë¤è¤ë¥Ï¡¼¥É¥¦¥§¥¢¤Î´Æ»ë¤Ê¤É )¡¢³Ð¤¨¤Æ¤ª¤¯¤ÈÊØÍø¤À¤È»×¤¤¤Þ¤¹¡£

¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à

¥«¡¼¥Í¥ë¥¿¥¤¥Þ¡¼¤ò»È¤Ã¤¿¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à timer.c ¤Ç¤¹¡£ ¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤«¤é¡¢ Á´¤Æ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò ¸Ç¤á¤¿ tar ¥Ü¡¼¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤¹¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¡£

insmod ¤Ç¥í¡¼¥É¤·¤¿Ä¾¸å¤«¤é¡¢°Ê²¼¤ÎÍͤÊÆâÍÆ ( ¿ôÃͤϰۤʤë ) ¤¬°ìÄê»þ´ÖËè¤Ë¥«¡¼¥Í¥ë¥í¥°¤Ë·«¤êÊÖ¤·É½¼¨¤µ¤ì¤Þ¤¹¡£

<6>module [timer.o] : Timeout ( registered 1140490, now 1140990 )
<4>module [timer.o] : Register timer (1140990)

cat /dev/chardev ¤ÎÍͤˤ·¤Æ /dev/chardev ¤ËÂФ·¤ÆÆɤ߹þ¤ß½èÍý¤ò¼Â¹Ô¤¹¤ë¤È¡¢"Delete registerd timer." ¤Èɽ¼¨¤µ¤ì¡¢¥«¡¼¥Í¥ë¥¿¥¤¥Þ¡¼¤¬Ää»ß¤·¤Þ¤¹¡£

--- timer.c ---

    1    #include <linux/fs.h>       /* inode, file, file_operations, ... */
    2    #include <linux/kernel.h>   /* printk() */
    3    #include <linux/module.h>
    4    #include <linux/sched.h>    /* task_struct */
    5    #include <linux/timer.h> 
    6    
    7    
    8    MODULE_AUTHOR( "Ryo" );
    9    MODULE_DESCRIPTION( "Sample character device driver." );
   10    MODULE_LICENSE( "GPL" );
   11    
   12    
   13    static int   devmajor = 77;
   14    static char* devname  = "chardev";
   15    static char* msg      = "module [timer.o]";
   16    
   17    
   18    /* 500 tick = 5 sec. */
   19    static int timeout = 500; 
   20    MODULE_PARM( timeout, "i" );
   21    
   22    static struct timer_list chardev_timer;
   23    static int timerstop = 0;
   24    
   25    static void chardev_timer_handler( unsigned long data );
   26    static void chardev_del_timer();
   27    
   28    
   29    /*
   30     * ¥í¥°½ÐÎÏ¥Õ¥©¡¼¥Þ¥Ã¥ÈÍÑ´Ø¿ô
   31     */
   32    static void 
   33    log_format( char* body )
   34    {
   35        if ( current != NULL ) {
   36            printk( KERN_INFO "%s : PID = %-5i cmdline = \"%-10s\" : %s\n", 
   37                msg, current->pid, current->comm, body );
   38        }
   39        else {
   40            printk( KERN_INFO "%s : %s\n", msg, body );
   41        }
   42    }
   43    
   44    
   45    /* 
   46     * ¥¿¥¤¥Þ¡¼ÅÐÏ¿½èÍý 
   47     */
   48    static void 
   49    chardev_timer_register( void )
   50    {
   51        init_timer( &chardev_timer );
   52    
   53        chardev_timer.expires  = jiffies + timeout;
   54        chardev_timer.data     = ( unsigned long )jiffies;
   55        chardev_timer.function = chardev_timer_handler;
   56    
   57        printk( "%s : Register timer (%ld)\n", msg, jiffies );
   58        add_timer( &chardev_timer );
   59    }
   60    
   61    
   62    /* 
   63     * ¥¿¥¤¥à¥¢¥¦¥È½èÍý 
   64     */
   65    static void 
   66    chardev_timer_handler( unsigned long data )
   67    {
   68        printk( KERN_INFO "%s : Timeout ( registered %ld, now %ld )\n", 
   69            msg, data, jiffies );
   70    
   71        if ( timerstop ) {
   72            printk( KERN_INFO "%s : Timer is not to be restarted.\n", msg );
   73            return;
   74        }
   75    
   76        chardev_timer_register();
   77    }
   78    
   79    
   80    /*
   81     * open()
   82     */
   83    static int 
   84    chardev_open( struct inode* inode, struct file* filp )
   85    {
   86        log_format( "open()  called" );
   87        return 0;
   88    }
   89    
   90    
   91    /*
   92     * release()
   93     */
   94    static int 
   95    chardev_release( struct inode* inode, struct file* filp )
   96    {
   97        log_format( "close() called" );
   98        return 0;
   99    }
  100    
  101    
  102    /*
  103     * read()
  104     * ¥¿¥¤¥Þ¡¼ºï½ü´Ø¿ô¤ò¼Â¹Ô¤¹¤ë¡£
  105     */
  106    static ssize_t 
  107    chardev_read( struct file* filp, char* buf, size_t count, loff_t* pos )
  108    {
  109        log_format( "read()  called" );
  110        log_format( "Delete registered timer." );
  111    
  112        chardev_del_timer();
  113        return 0;
  114    }
  115    
  116    
  117    /*
  118     * ¥¿¥¤¥Þ¡¼ºï½ü´Ø¿ô
  119     * ¥¿¥¤¥ÞÄä»ß¥Õ¥é¥°¤òÀßÄꤷ¡¢del_timer_sync ¤Ç´û¸¤Î¥¿¥¤¥Þ¡¼´Ø¿ô¤ò
  120     * ºï½ü¤¹¤ë¡Ê¼Â¹ÔÃæ¤Î¾ì¹ç¤Ï½ªÎ»¤¹¤ë¤Þ¤ÇÂԤġˡ£
  121     */
  122    static void
  123    chardev_del_timer()
  124    {
  125        timerstop = 1;
  126        del_timer_sync( &chardev_timer );
  127    }
  128    
  129    
  130    static struct file_operations chardev_fops = 
  131    {
  132        owner   : THIS_MODULE,
  133        read    : chardev_read,
  134        open    : chardev_open,
  135        release : chardev_release,
  136    };
  137    
  138    
  139    /*
  140     * ¥â¥¸¥å¡¼¥ë¤Î½é´ü½èÍý
  141     * insmod »þ¤Ë¸Æ¤Ð¤ì¤ë
  142     */
  143    int 
  144    init_module( void )
  145    {
  146        if ( register_chrdev( devmajor, devname, &chardev_fops ) ) {
  147            log_format( "register_chrdev failed" );
  148            return -EBUSY;
  149        }
  150    
  151        chardev_timer_register();
  152    
  153        log_format( "loaded  into kernel" );
  154        return 0;
  155    }
  156    
  157    
  158    /*
  159     * ¥â¥¸¥å¡¼¥ë¤Î²òÊü½èÍý
  160     * ¥â¥¸¥å¡¼¥ë¤Î»²¾È¿ô¤¬ 0 ¤Ç¤¢¤ì¤Ð¡¢rmmod »þ¤Ë¸Æ¤Ð¤ì¤ë
  161     */
  162    void 
  163    cleanup_module( void )
  164    {
  165        chardev_del_timer();
  166    
  167        if ( unregister_chrdev( devmajor, devname ) ) {
  168            log_format( "unregister_chrdev failed" );
  169            /* Âкö̵¤· ... */
  170        }
  171    
  172        log_format( "removed from kernel" );
  173    }
  174    
  175    
  176    /* End of timer.c */

¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÎÀâÌÀ

¤Ç¤Ï¡¢¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Æ¤¤¤­¤Þ¤¹¡£

  • 5 ¹ÔÌÜ

    ¥¿¥¤¥Þ¡¼µ¡Ç½¤ò»È¤¦¤¿¤á¤ËɬÍפʥإåÀ¥Õ¥¡¥¤¥ë¤ò¥¤¥ó¥¯¥ë¡¼¥É¤·¤Æ¤¤¤Þ¤¹¡£

  • 18 ¡Á 20 ¹ÔÌÜ

    ¥¿¥¤¥Þ¡¼¤òµ¯Æ°¤·¤Æ¤«¤é¤Î¥¿¥¤¥à¥¢¥¦¥È»þ´Ö¤ò timeout ÊÑ¿ô¤ËÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£ º£²ó»ÈÍѤ¹¤ë¥¿¥¤¥Þ¡¼¤Ç¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥ÈÃͤò¥·¥¹¥Æ¥à¤Î¥¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß²ó¿ô¤Ç»ØÄꤷ¤Þ¤¹¡£ ¤³¤³¤Ç¤Ï¥¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß 500 ²ó¤Ç¥¿¥¤¥à¥¢¥¦¥ÈÃͤò½é´ü²½¤·¤Æ¤¤¤Þ¤¹¡£

    ¥·¥¹¥Æ¥à¤Î¥¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß´Ö³Ö¤Ï¡¢1 Éô֤ËȯÀ¸¤¹¤ë¥¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß²ó¿ô¤È¤·¤Æ HZ ¤È¤¤¤¦¥Þ¥¯¥í¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£HZ ¤Ï asm/param.h ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£ ¤³¤ÎÃͤϥץé¥Ã¥È¥Õ¥©¡¼¥à¤Ë¤è¤Ã¤Æ°Û¤Ê¤ë¤Î¤Ç¡¢ÆÃÄêÃͤǤ¢¤ë¤È¤Ï»×¤ï¤Ê¤¤Íͤˤ·¤Æ²¼¤µ¤¤¡£ ¤Ê¤ª¡¢¥·¥¹¥Æ¥à¤¬µ¯Æ°¤·¤Æ¤«¤é¤ÎÁí¥¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß²ó¿ô¤Ï jiffies ¤È¤¤¤¦¥°¥í¡¼¥Ð¥ëÊÑ¿ô¤Ë³ÊǼ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£jiffies ¤Ï linux/sched.h Æâ¤ÇÀë¸À¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

    MODULE_PARM ¥Þ¥¯¥í¤Ï¡¢³°Éô¤«¤éÊÑ¿ôÃͤÎÀßÄê¤ò²Äǽ¤Ë¤¹¤ë¥Þ¥¯¥í¤Ç¤¹¡£ ÊÑ¿ô̾¤È·¿¤ò°ú¿ô¤È¤¹¤ë¤³¤È¤Ç¡¢´Êñ¤Ë insmod Åù¤ÇÊÑ¿ôÀßÄê¤Ç¤­¤ëÍͤˤʤê¤Þ¤¹¡£ ·¿¤Ï°Ê²¼¤Î¤¤¤º¤ì¤«¤òʸ»úÎó¤È¤·¤Æ»ØÄꤷ¤Þ¤¹("" ¤Ç°Ï¤à)¡£

    • b ( byte )
    • h ( short )
    • i ( int )
    • l ( long )
    • s ( ʸ»úÎó )

    ³°Éô¤«¤éÀßÄꤵ¤ì¤Ê¤¤¾ì¹ç¤Î¤¿¤á¡¢ÊÑ¿ô¤Ë¤Ïɬ¤º¥Ç¥Õ¥©¥ë¥ÈÃͤòÀßÄꤷ¤Æ¤ª¤¯Íͤˤ·¤Þ¤·¤ç¤¦¡£

  • 22 ¡Á 23 ¹ÔÌÜ

    chardev_timer ¤¬¥«¡¼¥Í¥ë¥¿¥¤¥Þ¡¼ ¤Ç¡¢struct timer_list ¹½Â¤ÂΤΠÊÑ¿ô¤È¤·¤ÆÀë¸À¤·¤Æ¤¤¤Þ¤¹¡£timerstop ¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥éÆâ¤Ç»²¾È¤¹¤ë¥¿¥¤¥Þ¡¼Ää»ß¥Õ¥é¥°¤Ç¤¹ ( ¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤ÎÀâÌÀ»²¾È )¡£

  • 45 ¡Á 59 ¹ÔÌÜ ( chardev_timer_register )

    ¥¿¥¤¥Þ¡¼¤ÎÅÐÏ¿½èÍý¤ò¹Ô¤¤¤Þ¤¹¡£timer.c ¤Ç¤Ï init_module ¤Ç¥­¥ã¥é¥¯¥¿¥Ç¥Ð¥¤¥¹ ¤È¤·¤Æ¤ÎÅÐÏ¿¤ò¹Ô¤Ã¤¿Ä¾¸å¤Ë¼Â¹Ô¤µ¤ì¤ë¤³¤È¤Ç¡¢ºÇ½é¤Î¥¿¥¤¥Þ¡¼¤òµ¯Æ°¤·¤Æ¤¤¤Þ¤¹¡£

    ºÇ½é¤Ë init_timer ¤Ç¥«¡¼¥Í¥ë¥¿¥¤¥Þ¡¼¤ò½é´ü²½¤·¤Æ¤¤¤Þ¤¹¡£ ¥«¡¼¥Í¥ë¥¿¥¤¥Þ¡¼¤ò»È¤¦ºÝ¤Ë¤Ï¡¢É¬¤ººÇ½é¤Ë init_timer ¤Ç½é´ü²½¤ò¹Ô¤Ã¤Æ²¼¤µ¤¤¡£

    #include <linux/timer.h>
    void init_timer( struct timer_list* timer );
         

    ¼¡¤Ë¡¢¥«¡¼¥Í¥ë¥¿¥¤¥Þ¡¼¤ËɬÍפÊÀßÄê¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£ÀßÄꤹ¤ëɬÍפ¬¤¢¤ë¤Î¤Ï

    • ¥¿¥¤¥à¥¢¥¦¥È»þ´Ö ( expires )
    • ¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤ËÅϤ¹¥Ç¡¼¥¿ ( data )
    • ¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é ( function )

    ¤Ç¤¹¡£

    expires ¤Ï¡¢¥¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß²ó¿ô¤ÇÀßÄꤷ¤Þ¤¹¡£¤¿¤À¤·¡¢¸½ºßÃͤ«¤é¤ÎÁêÂÐÃͤǤϤʤ¯¡¢ ¥·¥¹¥Æ¥àµ¯Æ°Ä¾¸å¤«¤é¤ÎÀäÂÐÃͤÇÀßÄꤷ¤Þ¤¹¡£¤½¤Î¤¿¤á¡¢ jiffies + timeout ¤ò expires ¤ËÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£¤Ê¤ª¡¢jiffies + HZ ¤òÀßÄꤹ¤ë¤È¡¢ 1 Éøå¤Ë¥¿¥¤¥à¥¢¥¦¥È¤¹¤ë¤³¤È¤Ë¤Ê¤ê¤Þ¤¹ ( HZ ¤Ï 1 Éô֤Υ¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß²ó¿ô¤Î¤¿¤á )¡£

    data ¤Ë¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤ËÅϤ¹¥Ç¡¼¥¿¤òÀßÄꤷ¤Þ¤¹¡£timer.c ¤Ç¤Ï¡¢ ¥¿¥¤¥Þ¡¼ÅÐÏ¿»þ¤Î jiffies ¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£ ¤³¤³¤Ë¥Ý¥¤¥ó¥¿¤òÀßÄꤷ ¤Æ¤ª¤¯¤È¡¢¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥éÆâ¤ÇǤ°Õ¤Î¥Ç¡¼¥¿¹½Â¤¤ò»²¾È¤¹¤ë¤³¤È¤â¤Ç¤­¤ë ¤È¤¤¤¦¤ï¤±¤Ç¤¹¡£

    function ¤Ë¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤Î´Ø¿ô¥Ý¥¤¥ó¥¿¤òÀßÄꤷ¤Þ¤¹¡£ ¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤ÏÁ°½Ò¤ÎÄ̤ê void ( *function )( unsigned long ) ·Á¼°¤ÇÀë¸À¤µ¤ì¤Æ¤¤¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£

    ÀßÄê¤ò¹Ô¤Ã¤¿¸å¤Ë¡¢add_timer ´Ø¿ô¤Ç¥¿¥¤¥Þ¡¼¤ò¥«¡¼¥Í¥ë¤Î ¥¿¥¤¥Þ¡¼¥ê¥¹¥È¤ËÅÐÏ¿¤·¤Æ¤¤¤Þ¤¹¡£

    #include <linux/timer.h>
    void add_timer( struct timer_list* timer );
         
  • 62 ¡Á 77 ¹ÔÌÜ ( chardev_timer_handler )

    ¥¿¥¤¥à¥¢¥¦¥È¤¬È¯À¸¤·¤¿ºÝ¤Ë¼Â¹Ô¤µ¤ì¤ë¥¿¥¤¥Þ¡¼´Ø¿ô ( ¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é ) ¤Ç¤¹¡£ ¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤Ï void ( *function )( unsigned long ) ·Á¼°¤Î´Ø¿ô¤Ç¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£

    Ãæ¤Ç¤ä¤Ã¤Æ¤¤¤ë¤³¤È¤Ïñ½ã¤Ç¡¢¤Þ¤º¥«¡¼¥Í¥ë¥í¥°¤Ë¥í¥°¤òɽ¼¨¤·¤Æ¤¤¤Þ¤¹¡£ °ú¿ô¤Î data ¤Ï¡¢¸å½Ò¤Î¥¿¥¤¥Þ¡¼ÅÐÏ¿½èÍý ( chardev_timer_register ) ¼Â¹Ô»þ¤Ë¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤Î°ú¿ô¤ËÅϤ¹ÃͤȤ·¤ÆÀßÄꤷ¤¿ÃͤǤ¹¡£

    ¥í¥°¤òɽ¼¨¤·¤¿¸å¤Ï¥¿¥¤¥Þ¡¼Ää»ß¥Õ¥é¥°¤ò»²¾È¤·¤Æ¤¤¤Þ¤¹¡£¥¿¥¤¥Þ¡¼Ää»ß ¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤¿¤é¤½¤Î¤Þ¤Þ½ªÎ»¤·¡¢ÀßÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¸å½Ò¤Î ¥¿¥¤¥Þ¡¼ÅÐÏ¿½èÍý ( chardev_timer_register ) ¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç¡¢ºÆÅÙ ¥¿¥¤¥Þ¡¼¤òµ¯Æ°¤·¤Æ¤¤¤Þ¤¹¡£¤³¤Î¥¿¥¤¥Þ¡¼Ää»ß¥Õ¥é¥°¤Ï¡¢ del_timer_sync ( ¸å½Ò ) ¼Â¹Ô¸å¤Ë¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤¬¼Â¹Ô¤µ¤ì¤Æ¤¤¤Ê¤¤¤³¤È¤òÊÝ¾Ú ¤¹¤ë¤¿¤á¤Ë»È¤Ã¤Æ¤¤¤Þ¤¹¡£

  • 102 ¡Á 114 ¹ÔÌÜ ( chardev_read )

    timer.c ¤Ç¤Ï¡¢chardev_read ¤ò¥¿¥¤¥Þ¡¼Ää»ß¤ò»Ø¼¨¤¹¤ë¤¿¤á¤Î´Ø¿ô¤È¤·¤ÆÍøÍѤ·¤Æ¤¤¤Þ¤¹¡£ ¼Â¹Ô¤µ¤ì¤ë¤È¡¢chardev_del_timer ´Ø¿ô¤ò¼Â¹Ô¤·¤Æ¤«¤é½ªÎ»¤·¤Þ¤¹¡£

  • 117 ¡Á 127 ¹ÔÌÜ ( chardev_del_timer )

    ¥¿¥¤¥Þ¡¼ºï½ü´Ø¿ô¤Ç¡¢chardev_read ¤ª¤è¤Ó cleanup_module ¤Ç¼Â¹Ô¤µ¤ì¤Þ¤¹¡£ ¤Þ¤º¡¢¥¹¥Ô¥ó¥í¥Ã¥¯¤ò¼èÆÀ¤·¤Æ¤«¤é¥¿¥¤¥Þ¡¼Ää»ß¥Õ¥é¥°¤òÀßÄꤷ¤Þ¤¹¡£ ¤³¤ì¤Ë¤è¤ê¡¢°Ê¸å chardev_timer_handler ¼Â¹Ô»þ¤ËºÆÅÙ¥¿¥¤¥Þ¡¼¤¬ÅÐÏ¿¤µ¤ì¤ë¤Î¤ò Ëɤ¤¤Ç¤¤¤Þ¤¹¡£¤½¤Î¸å del_timer_sync ¤ò¼Â¹Ô¤·¤Þ¤¹¡£

    #include <linux/timer.h>
    int del_timer_sync( struct timer_list* timer );
         

    del_timer_sync ¤¬Ìá¤Ã¤ÆÍ褿ºÝ¤Ë¤Ï¡¢¤É¤Î CPU ¤Ç¤â¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤¬ ¼Â¹Ô¤µ¤ì¤Æ¤¤¤Ê¤¤¤³¤È¤¬Êݾڤµ¤ì¤Þ¤¹¡£¤¿¤À¤·¡¢¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥éÆâ¤Ç ºÆÅÙ¥¿¥¤¥Þ¡¼¤¬ÅÐÏ¿¤µ¤ì¤Æ¤·¤Þ¤¦¤È¡¢del_timer_sync ¤¬Ìá¤Ã¤ÆÍ褿¸å¤Ë ¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤¬¼Â¹Ô¤µ¤ì¤ë²ÄǽÀ­¤¬¤¢¤ë¤¿¤á¡¢Ê̤˥¿¥¤¥Þ¡¼Ää»ß¥Õ¥é¥°¤ò ÍÑ°Õ¤·¤ÆºÆÅÐÏ¿¤µ¤»¤Ê¤¤¤³¤È¤Ç¡¢Êݾڤò³Î¼Â¤Ê¤â¤Î¤Ë¤·¤Æ¤¤¤Þ¤¹¡£

  • 139 ¡Á 155 ¹ÔÌÜ ( init_module )

    register_chrdev ľ¸å¤Ë¡¢chardev_timer_register ¤Ë¤è¤Ã¤Æ ºÇ½é¤Î¥¿¥¤¥Þ¡¼ÅÐÏ¿¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£

  • 158 ¡Á 173 ¹ÔÌÜ ( cleanup_module )

    unregister_chrdev ¤ò¼Â¹Ô¤¹¤ëÁ°¤Ë cheardev_del_timer ¤ò¼Â¹Ô¤·¤Æ¤¤¤Þ¤¹¡£ ¥¿¥¤¥Þ¡¼¤¬ÅÐÏ¿¤µ¤ì¤¿¾õÂÖ¤Ç rmmod ¤Ë¤è¤ê¥â¥¸¥å¡¼¥ë¤¬¥¢¥ó¥í¡¼¥É¤µ¤ì¤ë¤È¡¢ ¥«¡¼¥Í¥ë¤¬´û¤Ë¼ÂÂΤ¬Ìµ¤¯¤Ê¤Ã¤Æ¤·¤Þ¤Ã¤¿¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥é¤ò¼Â¹Ô¤·¤è¤¦¤È¤·¤Æ ¥Ï¥ó¥°¥¢¥Ã¥×¤·¤Æ¤·¤Þ¤¦¤¿¤á¡¢¤³¤Î½èÍý¤¬É¬ÍפǤ¹¡£

°Ê¾å¤Ç timer.c ¤Î²òÀâ¤Ï½ª¤ï¤ê¤Ç¤¹¡£¥¿¥¤¥Þ¡¼¤Ï³Ð¤¨¤ë¤È·ë¹½»È¤¤ÅÓ¤¬¤¢¤ëÍͤʵ¤¤¬¤¹¤ë¤Î¤Ç¡¢¤¼¤Ò¤ß¤Ê¤µ¤óÍ·¤ó¤Ç¤ß¤Æ²¼¤µ¤¤¡£

¡û ¥¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß´Ö³Ö̤Ëþ¤ÎÃÙ±ä½èÍý¤Ë¤Ä¤¤¤Æ

¥«¡¼¥Í¥ë¥¿¥¤¥Þ¡¼¤Ï¡¢¥·¥¹¥Æ¥à¤Î¥¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß²ó¿ô¤ò»ØÄꤹ¤ë¤³¤È¤Ç¥¿¥¤¥à¥¢¥¦¥È»þ¹ï¤òÀßÄꤷ¤Þ¤¹¡£ ¤½¤Î¤¿¤á¡¢¥¿¥¤¥Þ¡¼³ä¤ê¹þ¤ß´Ö³Ö¤è¤êû¤¤»þ´Ö¤ÎÃÙ±ä½èÍý¤ò¼Â¸½¤¹¤ë¤³¤È¤Ï¤Ç¤­¤Þ¤»¤ó ( ¿¤¯¤Î¥×¥é¥Ã¥È¥Õ¥©¡¼¥à¾å¤Ç¤Ï 1 / 100 sec. = 10 msec. ÄøÅÙ )¡£

¤è¤Ã¤Æ¡¢¥Þ¥¤¥¯¥íÉää¿ô¥ß¥êÉäÎÃÙ±ä¤ò¼Â¸½¤·¤¿¤¤¾ì¹ç¤Ï¡¢°Ê²¼¤Î´Ø¿ô¤ò»È¤¤¤Þ¤¹¡£

#include <linux/delay.h>
void udelay( unsigned long usecs );
void mdelay( unsigned long msecs );

¤³¤ì¤é¤Ï¥½¥Õ¥È¥¦¥§¥¢¥ë¡¼¥×¤ÇÃÙ±ä¤ò¼Â¸½¤¹¤ë´Ø¿ô¤Ç¡¢linux/delay.h ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ë¤³¤È¤Ç»ÈÍѤǤ­¤Þ¤¹¡£¼Â¹ÔÃæ¤Ï CPU ¤òÀêÍ­¤·¤Æ¤·¤Þ¤¦¤³¤È¤ËÃí°Õ¤·¤Æ²¼¤µ¤¤¡£¤Þ¤¿¡¢udeley ¤Ï 1000 ¥Þ¥¤¥¯¥íÉðʾå¤ÎÃÙ±ä¤Ë¤Ï»ÈÍѤ·¤Ê¤¤Íͤˤ·¤¿Êý¤¬¤¤¤¤ÍͤǤ¹¡£

¡û ¥¹¥ê¡¼¥×¤Ë¤Ä¤¤¤Æ

¤½¤Î¾ì¤Çñ½ã¤Ë¥¹¥ê¡¼¥×¤·¤¿¤¤¾ì¹ç¤Ë¤Ï¡¢interruptible_sleep_on_timeout ¤ò»È¤¤¤Þ¤¹¡£

#include <linux/sched.h>
void interruptible_sleep_on_timeout( wait_queue_head_t* queue, long timeout );

timeout ¤Ï¥«¡¼¥Í¥ë¥¿¥¤¥Þ¡¼Æ±ÍÍ jiffies ¤Ç»ØÄꤷ¤Þ¤¹¡£interruptible_sleep_on_timeout ¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È»þ´Ö¤ò¤Ì¤±¤ë¤«¡¢wake_up_interruptible Åù¤Îµ¯¾²´Ø¿ô¤¢¤ë¤¤¤Ï¥·¥°¥Ê¥ëÅù¤Ë¤è¤Ã¤Æµ¯¾²¤µ¤ì¤¿¾ì¹ç¤Ë½ªÎ»¤·¤Þ¤¹¡£

¥·¥°¥Ê¥ë¤¬È¯À¸¤·¤Æ¤¤¤ë¾ì¹ç¤Ï -ERESTARTSYS ¤òÊÖ¤·¤Æ¨ºÂ¤Ë½ªÎ»¤¹¤ëɬÍפ¬¤¢¤ë¤¿¤á¡¢signal_pending ¤ò»È¤Ã¤Æ¥·¥°¥Ê¥ë¤¬È¯À¸¤·¤Æ¤¤¤ë¤«¤É¤¦¤«³Îǧ¤·¤Þ¤¹¡£

#include <linux/sched.h>
int signal_pending( struct task_struct* p  );

¸½ºß¤Î CPU ¾õÂÖ¤òɽ¤¹¥°¥í¡¼¥Ð¥ëÊÑ¿ô current ¤ò°ú¿ô¤Ë¤·¤Æ¼Â¹Ô¤¹¤ë¤È¡¢¥·¥°¥Ê¥ë¤¬È¯À¸¤·¤Æ¤¤¤ë¾ì¹ç¤Ï 1 ¤ò¡¢È¯À¸¤·¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï 0 ¤òÊÖ¤·¤Þ¤¹¡£