Find the answer to your Linux question:
Results 1 to 9 of 9
i am trying to find some info on how to create/ destroy kernel thread. There's enough examples for it, including samples from "Linux Device Drivers" book. The problem is: all ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    5

    how to create kernel thread in 2.6 - ??


    i am trying to find some info on how to create/ destroy kernel thread. There's enough examples for it, including samples from "Linux Device Drivers" book. The problem is: all describe 2.4 kernel and api is changed pretty dramatically for 2.6. I've been trying to hack something together, using bits and pieces from different kernel groups

    void thread_func( struct work_struct *arg)
    {
    int i;
    wait_queue_head_t wq;
    printk("<1>::Inside Thread Function::\n");
    init_waitqueue_head(&wq);
    for (i=0; i < 100; i++) {
    interruptible_sleep_on_timeout(&wq, HZ);
    printk("HELLO THREAD ITERATION %d...\n", i);

    }
    }

    When i try to create it in init_module:
    ...
    work_struct ws;
    INIT_WORK(&ws, thread_func);
    /* and schedule it for execution */
    schedule_work(&ws);

    The thread is created ( i see "HELLO THREAD" messages printed in dmesg log) but init_module
    never return control - the "insmod ./hello.ko" runs forever.
    Can someone points me what the problem might be?
    Also, is there some tutorials on LKM development for 2.6 kernels?
    Thank you,

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    3rd edition of this book covers 2.6 kernel. Fortunately it has been open sourced and is available online in PDF format here.

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    5
    O'Reilly 3-rd edition is still outdated, For example, the book:

    INIT_WORK(struct work_struct *work, void (*function)(void *), void *data);
    In reality, current 2.6 kernel defines INIT_WORK with 2 arguments

    INIT_WORK(struct work_struct *work, void (*function)(work_struct *));

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    As it says, it is current as of 2.6.10 kernel. Your mileage will vary, the API's are constantly changing, you will have to scour some kernel mailing lists.

  6. #5
    Just Joined!
    Join Date
    Oct 2009
    Posts
    5

    re:

    I did checking on mailing lists. Found plenty of mails complaining about that change.
    Did not found anything explaining how to use it ( INIT_WORK) in new context, specifically,
    how to pass the data to thread function expecting work_struct* as argument.
    Have i been checking the wrong mailing lists?

  7. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    You could try asking on the mailing list.

  8. #7
    Just Joined!
    Join Date
    Oct 2009
    Posts
    5
    Here's another thread issue: looks like the thread "sleep" blocks hardware interrupts. Here's simple thread function that makes 10 iterations with going to sleep for some interval of time. While thread function is running, the system is "loosing" input devices, e.g. keyboard and mouse
    become nonfunctional. I wonder what the reason is. The "sleep" task is specified as interruptible, there should be no problems with hardware interrupts.
    I also tried wait_event_interruptible_timeout - with the same result.
    I am missing something ??


    void thread_func(void *ptr)
    {
    int i;
    printk("Hello kthread started\n"winking smiley;
    for (i=0; i < 10; i++)
    {
    printk("kthread sleep iteration %d\n", i);
    set_current_state(TASK_INTERRUPTIBLE);
    schedule_timeout (4*HZ);
    ..
    }


    }
    DECLARE_WORK(work, thread_func, (void*) NULL);
    static int hello_init(void)
    {
    printk("Hello, world\n"winking smiley;
    schedule_work(&work);
    return 0;
    }

  9. #8
    Just Joined!
    Join Date
    Oct 2011
    Posts
    4
    Hi vlyamtse,

    I do face the same problem with INIT_WORK accepting only two arguments and I need to pass data to my thread function. Do you have any idea on how to pass the data to thread function expecting work_struct* as argument.

    Thank you in advance,

  10. #9
    oz
    oz is offline
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,733
    Hello and welcome, sandyrmk!

    This thread is two years old so I'm going to lock it down, but please feel free to start a fresh thread of your own if you are having any problems with Linux.

    Thank you.
    oz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •