Find the answer to your Linux question:
Results 1 to 8 of 8
In my linux 2.6 kernel module, I created a kernel thread through kernel_thread function and specified the third argument as SIGCHLD. So when this child thread exits, it will send ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2014
    Posts
    8

    A good alternative to replace waitpid in a linux 2.6 kernel module?


    In my linux 2.6 kernel module, I created a kernel thread through kernel_thread function and specified the third argument as SIGCHLD. So when this child thread exits, it will send a SIGCHLD signal to the parent process. Correspondingly, in the parent process if there is pending signal SIGCHLD, I want it to wait for the termination of any child thread.

    I know that in user-space linux provides waitpid system call. We can use it like waitpid(-1, 0, WNOHANG). What I am looking for is an alternative of waitpid which can be used in a kernel module.

    Is there such an alternative or other possible mechanism?

  2. #2
    Linux Guru
    Join Date
    Dec 2013
    Location
    Victoria, B.C. Canada
    Posts
    1,650
    You might look at: wait_for_completion(struct completion *).

  3. #3
    Just Joined!
    Join Date
    Aug 2014
    Posts
    8
    Yes, that is an option. But it is kind of not what I want. In my parent process, I want to check whether the child thread sends a SIGCHLD signal. If so, I may use waitpid to wait for the termination of any child thread and then starts over creating a new child thread. Using wait_for_completion, I am not sure how to deal with the SIGCHLD signal.
    Quote Originally Posted by gregm View Post
    You might look at: wait_for_completion(struct completion *).

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    216
    Maybe I'm missing something here, but if you're going to get a SIGCHLD when it's time to do something, then why not just use the kernel version of a "sleep" call? If you have a signal handler in place to fire when the SIGCHLD arrives all you need to do it wait for it to arrive. Typically in that scenario I would set some timeout so that if the signal hasn't arrived the parent can make sure the child isn't spinning out of control. But if that sort of safety belt isn't necessary, then you don't even need to do that.

    Also, that general approach should work even if the parent has multiple child processes it's waiting on. The code just has to keep track of them all and decide which one sent the SIGCHLD that woke it up.

  6. #5
    Just Joined!
    Join Date
    Aug 2014
    Posts
    8
    Thanks for the reply. Do you know how to explicitly send a SIGCHLD signal to the parent from inside the child thread?
    Also, in my parent thread, I tried to use sys_waitpid or sys_wait4 directly upon receiving the SIGCHLD signal. I exported them from the kernel directly. I am not sure whether sys_waitpid and sys_wait4 can be used directly in kernel module. Just a hacking here.


    Quote Originally Posted by cnamejj View Post
    Maybe I'm missing something here, but if you're going to get a SIGCHLD when it's time to do something, then why not just use the kernel version of a "sleep" call? If you have a signal handler in place to fire when the SIGCHLD arrives all you need to do it wait for it to arrive. Typically in that scenario I would set some timeout so that if the signal hasn't arrived the parent can make sure the child isn't spinning out of control. But if that sort of safety belt isn't necessary, then you don't even need to do that.

    Also, that general approach should work even if the parent has multiple child processes it's waiting on. The code just has to keep track of them all and decide which one sent the SIGCHLD that woke it up.

  7. #6
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    216
    The man page for "signal" in section 7 has lots of helpful info, so here's a link in case it's easier to read in a browser.

    signal(7) - Linux manual page

    There are different calls available to send signals from one thing to another depending on whether the "things" are processes or threads within processes. So if you do need to explicitly send a signal to the parent, one of the calls listed in that man page should do the trick.

    Having said that, I'm not sure why you need to explicitly send the signal at all, since it's SIGCHLD. Why you ask? Well, while I haven't coded anything that used kernel threads, based on other experience with code that spins off processes/threads and code that does asynchronous work, the parent process should automatically get a signal when a child process is forks terminates. You don't need to send anything for the signal to be sent. You just need to install a signal handler to catch it.

    Now if the signal doesn't get sent automatically because it's a thread in the same process, or something odd about kernel threads (which I noted I haven't used), then you might have to do things differently. But It seems to me that SOME signal should be sent back to the parent process automatically when the thing it started finally terminates. If so, then you just need to block in a wait after installing a signal handler to catch whatever signal you're going to get.

  8. #7
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    216
    If the "thing" your parent is starting is a thread and not a process, then this might help.

    pthread_join(3) - Linux manual page

    Again, caveats about "kernel threads" not being like other threads applies...

  9. #8
    Linux Guru
    Join Date
    Dec 2013
    Location
    Victoria, B.C. Canada
    Posts
    1,650
    I think wait_for_completion is what is really needed. Create a completion object and when the child finishes what ever it is doing it calls complete or complete_all. SIGCHLD is sent by the kernel when the process exits or is interrupted. If you were using pthreads you could signal the thread using pthread_kill but kernel threads don't have an analogous function as far as I know.

Posting Permissions

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