内核定时器,

ldd3:当定时器运行时 ,调度该定时器的进程可能正在睡眠,或在其它处理器上运行,或干脆推出。

没有运行定时器,调度它的程序推出了,定时器该如何?

2.6.35。22内核

gcc-4.4.5

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/param.h>
#include <linux/timex.h>
#include <asm/timex.h>
#include <linux/timer.h>

MODULE_LICENSE ("GPL");
struct timer_list my_timer;
void my_fun (unsigned long arg)
{
    printk (KERN_INFO "in my_fun arg = %d/n", (int)arg);
}
static int time_me_init (void)
{
    /*cycles_t bef;
      cycles_t end;
      bef = get_cycles ();
      end = get_cycles ();
      printk (KERN_INFO "time one order %lu/n",end-bef);*/

    unsigned long bef;
    unsigned long end;
    bef = jiffies;
    end = bef + 5*HZ;
    //while (time_before (jiffies,end))
    //{
    //        cpu_relax ();  //#define barrier() __asm__ __volatile__("": : :"memory")
    //        printk (KERN_INFO "jiffies 5/n");
    //schedule ();
    //    }
    /*    set_current_state (TASK_INTERRUPTIBLE);
        schedule_timeout (end);*///bu ka wu jie guo
    //    mdelay (5000);
    //    ssleep (5);
    init_timer (&my_timer);
    my_timer.function = &my_fun;
    my_timer.data = (unsigned long)5;
    my_timer.expires = jiffies + 4*HZ;
    add_timer (&my_timer);
    ssleep(10); //如果没有这句(这是保证定时器开始运行时,调用它的程序没退出),会死机
    printk (KERN_INFO "in time_me_init/n");
    return 0;
}
static void time_me_exit (void)
{

    del_time (&my_timer);
    printk (KERN_INFO "in time_me_exit/n");

}
module_init (time_me_init);
module_exit (time_me_exit);
思考:这是在init中的情况,如果在其它的函数中呢,

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/param.h>
#include <linux/timex.h>
#include <asm/timex.h>
#include <linux/timer.h>

MODULE_LICENSE ("GPL");

struct timer_list my_timer;
void my_fun (unsigned long arg)
{
    struct timer_list *my_timer_f;
    printk (KERN_INFO "in my_fun arg = /n");
    my_timer_f = ((struct timer_list *)arg);
//    mod_timer (my_timer_f,(jiffies + HZ));
    my_timer_f->expires = jiffies + HZ;
    add_timer(my_timer_f);
   
}

static int time_me_init (void)
{
    /*cycles_t bef;
      cycles_t end;
      bef = get_cycles ();
      end = get_cycles ();
      printk (KERN_INFO "time one order %lu/n",end-bef);*/
    unsigned long bef;
    unsigned long end;
    bef = jiffies;
    end = bef + 5*HZ;
    //while (time_before (jiffies,end))
    //{
    //        cpu_relax ();  //#define barrier() __asm__ __volatile__("": : :"memory")
    //        printk (KERN_INFO "jiffies 5/n");
    //schedule ();
    //    }
    /*    set_current_state (TASK_INTERRUPTIBLE);
        schedule_timeout (end);*///bu ka wu jie guo
    //    mdelay (5000);
    //    ssleep (5);
    init_timer (&my_timer);
    my_timer.function = &my_fun;
    my_timer.data = (unsigned long)(&my_timer);
    my_timer.expires = jiffies + 1*HZ;
    add_timer (&my_timer);
    ssleep(10);
    del_timer (&my_timer);
    printk (KERN_INFO "in time_me_init/n");
    return 0;
}
static void time_me_exit (void)
{
    printk (KERN_INFO "in time_me_exit/n");

}
module_init (time_me_init);
module_exit (time_me_exit);

 

你可能感兴趣的:(timer,struct,list,Module,fun)