Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 598 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a version of the wait() system call that sets a timeout?

#1
Is there any way to use the `wait()` system call with a timeout, besides using a busy-waiting or busy-sleeping loop?

I've got a parent process that `fork`s itself and `exec`s a child executable. It then waits for the child to finish, grabs its output by whatever means appropriate, and and performs further processing. If the process does not finish within a certain period of time, it assumes that its execution timed out, and does something else. Unfortunately, this timeout detection is necessary given the nature of the problem.
Reply

#2
You can use `waitpid` together with the `WNOHANG` option and a sleep.

while(waitpid(pid, &status, WNOHANG) == 0) {
sleep(1);
}

But this will be an active sleeping. However I see no other way using the `wait` type of functions.
Reply

#3
On linux, you can also solve this problem using `signalfd`. `signalfd` essentially takes a set of signals and creates an fd which you can read; each block you read corresponds to a signal which has fired. (You should block these signals with sigprocmask so that they are not actually sent.)

The advantage of `signalfd` is that you can use the fd with `select`, `poll`, or `epoll`, all of which allow for timeouts, and all of which allow you to wait for other things as well.

One note: If the same signal fires twice before the corresponding `struct signalfd_siginfo` is read, you'll only receive a single indication. So when you get a `SIGCHLD` indication, you need to `waitpid(-1, &status, &WNOHANG)` repeatedly until it returns -1.

On FreeBSD, you can achieve the same effect rather more directly using `kqueue` and a `kevent` of type `EVFILT_PROC`. (You can also kqueue a SIGCHLD event, but EVFILT_PROC lets you specify the events by child pid instead of globally for all children.) This should also work on Mac OS X, but I've never tried it.
Reply

#4
There's not a wait call that takes a timeout.

What you can do instead is install a signal handler that sets a flag for SIGCHLD, and use select() to implement a timeout. select() will be interrupted by a signal.

static volatile int punt;
static void sig_handler(int sig)
{
punt = 1;
}

...

struct timeval timeout = {10,0};
int rc;

signal(SIGCHLD, sig_handler);

fork/exec stuff
//select will get interrupted by a signal

rc = select(0, NULL,NULL,NULL, &timeout );
if (rc == 0) {
// timed out
} else if (punt) {
//child terminated
}

More logic is needed if you have other signal you need to handle as well though
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through