#1
  1. No Profile Picture
    "Null, The Terminator"
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    11
    Rep Power
    0

    linux C questions


    hi friends

    i am new to linux programming using C ( i am neither noew to lin or C). I want to achieve some things in lin...but couldnt get the properway to do it.

    1. I want nonblocking wait sys call....like it waits for sometime..if the child is finished till then OK..otherwise kill the child....

    2. How do i know the status of the child process if its alive or dead. I read somewhere, use c=kill(pid,0) if c=0 the child is there otherwise its gone...but it doesnt worx on lin.

    3. I want a child to suspend and resume its job as per the parent wants.

    the help in this regard will be a lot appriciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    I haven't written code to test it, but at least according to my Linux man pages kill behaves identically on Linux to how it behaves on OpenBSD and FreeBSD. It should return 0 if the process exists and will accept the signal, and a non-zero return otherwise. The errno to check for is ESRCH, indicating that the process did not exist. Any other error would tend to indicate that it exists but isn't interested in talking to you.

    Also, while working on the C language, could you work on the English language? I'll understand if it isn't your native tongue, but your question was a little hard to understand.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    I think you're talking about the wait() function. The int* status parameter returns the exit code of the child process, assuming that it terminated normally; otherwise status is NULL. There are macros you can use to evaluate a non-NULL status -- read the man page (man 2 wait on Red Hat 7).

    For non-blocking, use waitpid() with the WNOHANG option set. You can read about this function on the wait man page.
  6. #4
  7. No Profile Picture
    "Null, The Terminator"
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    11
    Rep Power
    0
    Originally posted by dwise1_aol
    I think you're talking about the wait() function. The int* status parameter returns the exit code of the child process, assuming that it terminated normally; otherwise status is NULL. There are macros you can use to evaluate a non-NULL status -- read the man page (man 2 wait on Red Hat 7).

    For non-blocking, use waitpid() with the WNOHANG option set. You can read about this function on the wait man page.
    Ok i will read abt waitpid() thx for the help....
    and for the other part i wast talking abt the exit status...but the process state..if it is still in the memory or not....

    and more over and call that wud get rid of all the zombie processes under one parent ie of the same process group ???
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    To kill off any zombies, wait until all children have closed before exiting. Alternatively you can jump through a few hoops to put descendent processes into their own process group, which makes them init's lookout to clean up after, thus preventing zombies.
  10. #6
  11. No Profile Picture
    "Null, The Terminator"
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    11
    Rep Power
    0
    unfortunately i will have a few zimbies in my program....and i cant afford to give them to init...

    is there no way to clean up zombies??
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    You can't clean up after a zombie, you can just take steps to avoid them. A zombie process occurs because you didn't clean up after it. It has an exit status and resource usage report that it wants to hand off to somebody. It's going to hang around until somebody collects it.

    If you can't afford to put them in their own process group, you can avoid zombies by creating a master process that will wait for them all to finish. This will be the program that created them all.

    To make this work, you need a table of all of the processes that you have created. Wait for any child process to exit. When it exits, remove it's entry from the table, then check to see if it needs to wait for any more. If all of the children have exited, then the master process can exit too.

    If you will be launching a number of jobs that you can't predict ahead at compile time, this approach is the best solution that I've figured out. Somebody may have a better one, but this works for me.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  14. #8
  15. No Profile Picture
    "Null, The Terminator"
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    11
    Rep Power
    0
    well thx a lot ClayDowling.....i am pondering over it

IMN logo majestic logo threadwatch logo seochat tools logo