I am trying to wait on waitpid() and read() in a while-true loop. Specifically, I am waiting for either one of these two events and then process it in each iteration of the loop. Currently, I have the following implementation (which is not I desired).
while (true) {
pid_t pid = waitpid(...);
process_waitpid_event(...);
ssize_t sz = read(socket, ....);
process_read_event(...);
}
The problem with this implementation is that the processing of the second event depends on the completion of the first event. Instead of processing these two events sequentially, I wish to process whichever event that comes first in each iteration of the loop. How should I do this?
If you don't want to touch threading, you can include this in the options of the call to
waitpid:As from the manpage for
waitpid:As such, if
waitpidisn't ready, it won't block and the program will just keep going to the next line.As for the
read, if it is blocking you might want to have a look atpoll(2). You can essentially check to see if your socket is ready every set interval, e.g. 250ms, and then callreadwhen it is. This will allow it to not block.Your code might look a bit like this:
This is just assuming that you're
reading from a socket, judging from the variable names in your code. As others have said, your problem might require something a bit more heavy duty like threading.