Sleep in driver kernel thread - Linux

This is a discussion on Sleep in driver kernel thread - Linux ; Hi at all, I'm a newbie of linux word, I'm developing a kernel driver needed for check a special I/O signal of a custom hardware. This signal change very fast so I need a dedicated thread that every 20 milliseconds ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Sleep in driver kernel thread

  1. Sleep in driver kernel thread

    Hi at all,

    I'm a newbie of linux word, I'm developing a kernel driver needed for
    check a special I/O signal of a custom hardware. This signal change
    very fast so I need a dedicated thread that every 20 milliseconds
    check if the signal switch state. I have red the Linux Kernel Driver
    book and checking around I found an example code explaining how to
    create a thread in kernel mode, follow a short example code:

    static pid_t khubd_pid = 0;
    static DECLARE_COMPLETION(khubd_exited);

    static int input_check_thread(void* arg)
    {
    daemonize("khubd");
    allow_signal(SIGKILL);

    do
    {
    // SIGNAL CHECKING CODE
    // SLEEP FOR 20 MILLISECOND

    } while(!signal_pending(current));

    complete_and_exit(&khubd_exited, 0);
    }


    init_module()
    {
    khubd_pid = kernel_thread(input_check_thread, NULL,
    CLONE_KERNEL));
    }

    cleanup_module()
    {
    kill_proc(khubd_pid, SIGKILL, 1);
    wait_for_completion(&khubd_exited);
    }

    Is this method correct? My problem is that I don't know how to force
    the thread to sleep for the required amount of time. First I tried
    with msleep(20) but it seem don't working inside a thread, so how can
    resolve the problem??
    Thank you

    P.S. By the way, do you know a book or a web site where is possible to
    have an organized list about all the available functions in kernel
    mode (similar to Microsoft MSDN)?

  2. Re: Sleep in driver kernel thread

    A few things to think about:

    20ms isn't really considered "very fast". There should be no problem
    with being able to check for a state change that often.

    Is your device capable of generating an interrupt when the signal
    changes state? This would be the preferred way to notify software that
    there is work to do.

    Even if this isn't possible, you probably don't need a kernel thread.
    You can simply set a kernel timer (see init_timer/add_timer) to wake up
    your driver at the appropriate time.

    If you really do want to use a kernel thread, the function
    schedule_timeout is what you are looking for. You would write
    something like this:
    set_current_state(TASK_INTERRUPTIBLE);
    schedule_timeout (delay);

    You should take a look at the book Linux Device Drivers version 3
    available online at
    http://lwn.net/Kernel/LDD3/
    This is about as close as you will find to a reference for the Linux
    kernel API. Chapter 7 in particular is all about timers and delaying
    execution.

    GH


    Fabio wrote:
    > I'm a newbie of linux word, I'm developing a kernel driver needed for
    > check a special I/O signal of a custom hardware. This signal change
    > very fast so I need a dedicated thread that every 20 milliseconds
    > check if the signal switch state. I have red the Linux Kernel Driver
    > book and checking around I found an example code explaining how to
    > create a thread in kernel mode, follow a short example code:
    >
    > static pid_t khubd_pid = 0;
    > static DECLARE_COMPLETION(khubd_exited);
    >
    > static int input_check_thread(void* arg)
    > {
    > daemonize("khubd");
    > allow_signal(SIGKILL);
    >
    > do
    > {
    > // SIGNAL CHECKING CODE
    > // SLEEP FOR 20 MILLISECOND
    >
    > } while(!signal_pending(current));
    >
    > complete_and_exit(&khubd_exited, 0);
    > }
    >
    >
    > init_module()
    > {
    > khubd_pid = kernel_thread(input_check_thread, NULL,
    > CLONE_KERNEL));
    > }
    >
    > cleanup_module()
    > {
    > kill_proc(khubd_pid, SIGKILL, 1);
    > wait_for_completion(&khubd_exited);
    > }
    >
    > Is this method correct? My problem is that I don't know how to force
    > the thread to sleep for the required amount of time. First I tried
    > with msleep(20) but it seem don't working inside a thread, so how can
    > resolve the problem??
    > Thank you
    >
    > P.S. By the way, do you know a book or a web site where is possible to
    > have an organized list about all the available functions in kernel
    > mode (similar to Microsoft MSDN)?



  3. Re: Sleep in driver kernel thread

    On 22 May 2006 04:33:24 -0700, gil_hamilton@hotmail.com wrote:

    >If you really do want to use a kernel thread, the function
    >schedule_timeout is what you are looking for. You would write
    >something like this:
    > set_current_state(TASK_INTERRUPTIBLE);
    > schedule_timeout (delay);
    >
    >You should take a look at the book Linux Device Drivers version 3
    >available online at
    >http://lwn.net/Kernel/LDD3/
    >This is about as close as you will find to a reference for the Linux
    >kernel API. Chapter 7 in particular is all about timers and delaying
    >execution.


    It's the book that I'm reading, thank you for your help. I come from
    the Windows programming world and I still have some difficult to
    understand the Linux kernel programming logic, however I hope to
    improve my knowledge in the future.
    Thank you again for your reply

  4. Re: Sleep in driver kernel thread

    you can make your thread periodic with period 20msec.

    As soon as the thread will finish
    the required job, you can call
    a function "pthread_wait_np()"
    to make thread go into sleep
    for its next period.


  5. Download Driver

    Download Driver From Here


    Download New Drivers For Free

+ Reply to Thread