Does calling wait() poll?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Does calling wait() poll?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Does calling wait() poll?  
K-mart Cashier


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-14-07 06:26 AM

Say I have a program that calls this function every 2 seconds

static int
tel(char *user, char *tty, const char *what)
{
pid_t   cpid, wpid;
int     stats;

cpid = fork();
if (cpid < 0) {
err("fork failed:", cpid);
}
if((execlp("tel", "tel", user, tty, what, (char *)0)) < 0)
perror("execlp tel failed\n");
_exit(EXIT_FAILURE);
}


while ((wpid= wait(&stats)) != cpid && wpid != -1)
;

return (WIFEXITED(stats) ? WEXITSTATUS(stats) : -2);
}


would calling wait() every 2 seconds, would I be 'polling'?

Chad






[ Post a follow-up to this message ]



    Re: Does calling wait() poll?  
Logan Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-14-07 06:26 AM

K-mart Cashier wrote:
> would calling wait() every 2 seconds, would I be 'polling'?

Maybe I'm missing some subtlety, but in my view, doing ANYTHING
every 2 seconds would be polling.

- Logan





[ Post a follow-up to this message ]



    Re: Does calling wait() poll?  
K-mart Cashier


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-14-07 06:26 AM

On Jul 13, 7:27 pm, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
> K-mart Cashier wrote: 
>
> Maybe I'm missing some subtlety, but in my view, doing ANYTHING
> every 2 seconds would be polling.
>
>    - Logan


Bear with this while I have the "I'm 33, work as a clerk for 2 dollars
above the minimum wage, and got rejected by UC-Berkeley on a few
occasions" moment.

For whatever resons, I thought if used some kind of pipe() vs just
regular fork/exec, there was no polling.






[ Post a follow-up to this message ]



    Re: Does calling wait() poll?  
William Pursell


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-14-07 12:21 PM

On Jul 14, 2:32 am, K-mart Cashier <cdal...@gmail.com> wrote:
> Say I have a program that calls this function every 2 seconds
>
> static int
>  tel(char *user, char *tty, const char *what)
> {
>   pid_t   cpid, wpid;
>   int     stats;
>
>   cpid = fork();
>   if (cpid < 0) {
>     err("fork failed:", cpid);
>   }
>     if((execlp("tel", "tel", user, tty, what, (char *)0)) < 0)
>       perror("execlp tel failed\n");
>     _exit(EXIT_FAILURE);
>   }
>
>   while ((wpid= wait(&stats)) != cpid && wpid != -1)
>     ;
>
>   return (WIFEXITED(stats) ? WEXITSTATUS(stats) : -2);
>
> }
>
> would calling wait() every 2 seconds, would I be 'polling'?

In this case, you are never going to reach the while
statment, since both the parent and the child
are going to exec tel.  I don't think that's
what you intended.

In general, wait will not poll.  When you call wait, the
process will go to sleep and will not receive processor
time until its child terminates and the kernel sends
it the child's status and a SIGCHLD.  So, the answer is
no, you're not polling.     If you fix the
error so that the parent doesn't exec tel, it is
going to wait until tel finishes, and it won't call
the tel function again until some time after that
event.  If tel takes 28 seconds to execute, the
parent is going to wait.  In other words, you're not
calling that function every 2 seconds.  (If you've set a SIGALRM
or something else occurs, wait may return early
with errno==EINTR).

The while loop, however, is a really bad idea.  If wait
returns -1, you need to react to that.  You shouldn't
just ignore it and wait again, as an error indicates
that something has happened.








[ Post a follow-up to this message ]



    Re: Does calling wait() poll?  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-16-07 06:20 PM

On Jul 13, 6:32 pm, K-mart Cashier <cdal...@gmail.com> wrote:

> would calling wait() every 2 seconds, would I be 'polling'?

No. Once you fix the bugs in your program, it will be calling 'wait'
to wait until the child is finished. It's only 'polling' if you called
'wait' periodically to see if the child was finished. Since you are
waiting until it is finished, you are blocking, not polling.

Blocking is like "call me when you're ready". Polling is like "are you
ready" repeated every few seconds. Your invocation of 'wait' only
occurs once for each child and it waits until the child is finished.

DS






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:54 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register