Re: [ilugd] Waiting for a child to die

2005-12-25 Thread Gora Mohanty
Mayank Jain [EMAIL PROTECTED] wrote: On 12/25/05, Gora Mohanty  wrote:
   Um, what do you mean by a non-blocking wait. Calling
  wait()/waitpid() means that you *want* the parent process to
  wait for the child to finish processing (or, some other 
  condition occurs, as specified in the 3rd argument).

 What if i want just to check the status of the child? Is it alive or
 not? I dont want to *wait*...
 As Manish suggests, checking the return value of waitpid() with the
 WNOHANG option will tell you whether the child exited immediately.
 With WNOHANG, if the return value is 0, the child has not yet exited
 (really, not changed state), else if the return value is non-zero, then
 it is the PID of the child process that changed state. If you want to
 be informed as and when the child exits, a much better way than
 looping is to set up the child to signal the parent.
Also, I did not notice this in my initial reading of your message, but
 there is no need to loop around waitpid(). A single call is sufficient.
 Try the following code (note that I have added an immediate exit in the
 child):
 
 #include unistd.h
 #include sys/types.h
 #include stdio.h
 #include sys/wait.h
 #include stdlib.h
 int main()
 {
pid_t child = 0;
int status = 0;
printf(forking!\n);
 
child = fork();
if(child == 0)
  {
  exit(0);
  printf(Inside child\n);
  sleep(2);
  printf(Finishing if\n);
   }
else
   {
  pid_t retval;
  printf(Inside Parent\nCalling waitpid\n);
  retval = waitpid(child, status, WNOHANG);
  if(WIFEXITED(status))
{
   printf(Child exited: waitpid returned %d\n, retval);
 }
 printf(Finishing else\n);
  }
   return 0;
 }
 
 With this, you should get some process ID for the return value of
 waitpid(). If you remove the exit(0) in the  child, you should get zero
 for the retunr value. Is that what you wanted?
 
 Regards,
 Gora


Send instant messages to your online friends http://in.messenger.yahoo.com 
___
ilugd mailinglist -- ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/ilugd@lists.linux-delhi.org/



[ilugd] Waiting for a child to die

2005-12-24 Thread Mayank Jain
#include unistd.h
#include sys/types.h
#include stdio.h
#include sys/wait.h

int main()
{
pid_t child = 0;
int status = 0;
printf(forking!\n);

child = fork();
if(child == 0)
{
printf(Inside child\n);
sleep(2);
printf(Finishing if\n);
}
else
{
printf(Inside Parent\n);
while(1)
{
printf(Calling waitpid\n);
waitpid(child, status, 0);
if(WIFEXITED(status))
{
printf(Child exited\n);
break;
}
}
printf(Finishing else\n);
}
}


The above code works perfectly, the parent waits for the child to die
 then exits itself.

[EMAIL PROTECTED]:~/junk# ./child
forking!
Inside child
Inside Parent
Calling waitpid
Finishing if
Child exited
Finishing else
[EMAIL PROTECTED]:~/junk#


However, how do i make this wait by the parent a non-blocking wait?
This is because while checking for has the child died, parent is
blocked by waitpid  hence cannot do other processing.

I've tried using WNOHANG in waitpid as waitpid(child, status,
WNOHANG)  this is the output i get

[EMAIL PROTECTED]:~/junk# ./child
forking!
Inside child
Inside Parent
Calling waitpid
Child exited
Finishing else
[EMAIL PROTECTED]:~/junk#
[EMAIL PROTECTED]:~/junk# Finishing if
[EMAIL PROTECTED]:~/junk#

Notice that parent exited before child died.

Am i using WNOHANG in a wrong way? or in a wrong place? What can be
other strategies i can use? I dont want to use threads as they'll be
an overkill (all i want to do is keep parent in non-blocking state,
while looping for child's alive status).


--
regards,
makuchaku
---
http://makuchaku.info
When you speak out with the courage of your convictions, people listen!
-- Valmik Thapar, Wildlife Conservationist.

___
ilugd mailinglist -- ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/ilugd@lists.linux-delhi.org/



Re: [ilugd] Waiting for a child to die

2005-12-24 Thread Gora Mohanty
Mayank Jain [EMAIL PROTECTED] wrote: [Code, and successful experiment 
deleted]

 However, how do i make this wait by the parent a non-blocking 
  wait?
   
 Um, what do you mean by a non-blocking wait. Calling
 wait()/waitpid() means that you *want* the parent process to
 wait for the child to finish processing (or, some other condition
 occurs, as specified in the 3rd argument).
 
 This is because while checking for has the child died, parent 
  is blocked by waitpid  hence cannot do other processing.

 If you want the parent to continue some processing, you can 
 either fork another child for this, or set up the child to signal the
 parent when it exits, rather than using wait()/waitpid().
 
 I've tried using WNOHANG in waitpid as waitpid(child, status,
  WNOHANG)  this is the output i get
 This behaviour with WNOHANG is not surprising. As the manpage
 tells you, with this option, waitpid() returns immediately if the child
 has not exited. Incidentally, though this is probably not an issue here,
 you cannot depend on the order of messages from a child/parent to
 tell you which finished processing first.
 
 Regards,
 Gora


Send instant messages to your online friends http://in.messenger.yahoo.com 
___
ilugd mailinglist -- ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/ilugd@lists.linux-delhi.org/



Re: [ilugd] Waiting for a child to die

2005-12-24 Thread Ritesh Raj Sarraf
Your subject line got me really confused. :-)

rrs

On Sat, 24 Dec 2005, Mayank Jain wrote:

 #include unistd.h
 #include sys/types.h
 #include stdio.h
 #include sys/wait.h

 int main()
 {
   pid_t child = 0;
   int status = 0;
   printf(forking!\n);

   child = fork();
   if(child == 0)
   {
   printf(Inside child\n);
   sleep(2);
   printf(Finishing if\n);
   }
   else
   {
   printf(Inside Parent\n);
   while(1)
   {
   printf(Calling waitpid\n);
   waitpid(child, status, 0);
   if(WIFEXITED(status))
   {
   printf(Child exited\n);
   break;
   }
   }
   printf(Finishing else\n);
   }
 }


 The above code works perfectly, the parent waits for the child to die
  then exits itself.

 [EMAIL PROTECTED]:~/junk# ./child
 forking!
 Inside child
 Inside Parent
 Calling waitpid
 Finishing if
 Child exited
 Finishing else
 [EMAIL PROTECTED]:~/junk#


 However, how do i make this wait by the parent a non-blocking wait?
 This is because while checking for has the child died, parent is
 blocked by waitpid  hence cannot do other processing.

 I've tried using WNOHANG in waitpid as waitpid(child, status,
 WNOHANG)  this is the output i get

 [EMAIL PROTECTED]:~/junk# ./child
 forking!
 Inside child
 Inside Parent
 Calling waitpid
 Child exited
 Finishing else
 [EMAIL PROTECTED]:~/junk#
 [EMAIL PROTECTED]:~/junk# Finishing if
 [EMAIL PROTECTED]:~/junk#

 Notice that parent exited before child died.

 Am i using WNOHANG in a wrong way? or in a wrong place? What can be
 other strategies i can use? I dont want to use threads as they'll be
 an overkill (all i want to do is keep parent in non-blocking state,
 while looping for child's alive status).


 --
 regards,
 makuchaku
 ---
 http://makuchaku.info
 When you speak out with the courage of your convictions, people listen!
 -- Valmik Thapar, Wildlife Conservationist.



-- 
Ritesh Raj Sarraf
RESEARCHUT -- http://www.researchut.com
Stealing logic from one person is plagiarism, stealing from many is research.
Necessity is the mother of invention.


___
ilugd mailinglist -- ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/ilugd@lists.linux-delhi.org/



Re: [ilugd] Waiting for a child to die

2005-12-24 Thread Mayank Jain
On 12/25/05, Gora Mohanty [EMAIL PROTECTED] wrote:
 Mayank Jain [EMAIL PROTECTED] wrote: [Code, and successful experiment 
 deleted]

  However, how do i make this wait by the parent a non-blocking
   wait?

  Um, what do you mean by a non-blocking wait. Calling
  wait()/waitpid() means that you *want* the parent process to
  wait for the child to finish processing (or, some other condition
  occurs, as specified in the 3rd argument).

What if i want just to check the status of the child? Is it alive or
not? I dont want to *wait*...

--
regards,
makuchaku
---
http://makuchaku.info
When you speak out with the courage of your convictions, people listen!
-- Valmik Thapar, Wildlife Conservationist.

___
ilugd mailinglist -- ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/ilugd@lists.linux-delhi.org/



Re: [ilugd] Waiting for a child to die

2005-12-24 Thread [EMAIL PROTECTED]
Ritesh Raj Sarraf wrote:
 Your subject line got me really confused. :-)
 
 rrs
 
Same here

it got me quite upset actually.

ram

___
ilugd mailinglist -- ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/ilugd@lists.linux-delhi.org/



Re: [ilugd] Waiting for a child to die

2005-12-24 Thread Manish Malik
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


Mayank Jain wrote:
   waitpid(child, status, 0);
   if(WIFEXITED(status))
   {
   printf(Child exited\n);
   break;
   }
.
.
 I've tried using WNOHANG in waitpid as waitpid(child, status,
 WNOHANG)  this is the output i get
.
.
 an overkill (all i want to do is keep parent in non-blocking state,
 while looping for child's alive status).

Check for the waitpid() value 0 (which is returned when WNOHANG is used,
and no child has exited.)

in your code:

waitpid_result = waitpid(child, status, WNOHANG);
if (waitpid_result  WIFEXITED(status))
{
printf(Child exited\n);
break;
}

should give you the expected results.


Note: Do take a look at your program logic here in scenarios where the
child does not exit normally. waitpid() will return the PID of the
child, but WEXITED() will not be true in such cases. Hence the parent
process (in your program) will go into an endles loop.


Manish

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDrkAD9364pdQFFqARAsTgAJ0aqNPj1tKeOl7IBRENZTlimvDBfgCfS/Ox
s1LESo8V/Y13EKxrQkdtuIE=
=2hmG
-END PGP SIGNATURE-

___
ilugd mailinglist -- ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/ilugd@lists.linux-delhi.org/