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 ]
|