Home > Archive > Unix Programming > July 2007 > Does calling wait() poll?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Does calling wait() poll?
|
|
| K-mart Cashier 2007-07-13, 10:05 pm |
| 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
| |
| Logan Shaw 2007-07-13, 10:05 pm |
| 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
| |
| K-mart Cashier 2007-07-13, 10:05 pm |
| 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.
| |
| William Pursell 2007-07-14, 8:04 am |
| 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.
| |
|
|
| David Schwartz 2007-07-16, 10:07 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
|
|
|
|
|