Thread: Not sure why waitpid() isn't working

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Question Not sure why waitpid() isn't working

    Hello,

    I'm trying to create a child process and waitpid() on a parent process that's running in background. But when I check the process list, child process is 'defunct':


    Code:
        [...]
        int *stat = NULL;
        proc_id = fork();
        if(proc_id == 0)
              execl(path_to_proc, name_of_proc, NULL)
         else if(proc_id>0)
              waitpid(proc_id, stat, WNOHANG)
        [...]

    Any thoughts?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,487
    Why are you using WNOHANG ?
    This returns immediately, whether the child has exited or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Hi Salem,

    I'm using WNOHANG because I want the parent process to go on with the rest of the code without waiting the child process to finish it job

    My idea was to ensure that parent is waiting when the child finish (with waitpid), but without being blocked on that part of the code

    not sure if thats the right way to accomplish this

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,487
    Why bother with the waitpid at all, if you're just going to carry on.

    All you're achieving is a very small delay in calling waitpid with nohang.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. waitpid() in loop, not ending correctly
    By Gatsu in forum C Programming
    Replies: 5
    Last Post: 03-21-2013, 02:05 AM
  2. waitpid behaving strange...
    By mattholm in forum C Programming
    Replies: 6
    Last Post: 11-28-2011, 01:12 PM
  3. waitpid() non-blocking fork
    By codevyper in forum C Programming
    Replies: 5
    Last Post: 06-21-2011, 04:05 PM
  4. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  5. help with waitpid()
    By kotoko in forum C Programming
    Replies: 1
    Last Post: 09-28-2008, 01:23 PM

Tags for this Thread