I have a program that I want to exit when I can not open a pipe for read, after N (lets say 30) seconds.
My code works with blocking name pipes and I can not change this.
I know about select() and poll() but I can not get them to work without turning my pipes into non-blocking.
This is my code so far:
struct pollfd fds[1];
int pol_ret;
fds[0].fd = open(pipe_name, O_RDONLY /* | O_NONBLOCK */);
if (fds[0].fd < 0)
{
// send_signal_to_parent();
std::cout << "error while opening the pipe for read, exiting!" << '\n';
return -1;
}
fds[0].events = POLLIN;
int timeout_msecs = 30000; // (30 seconds)
pol_ret = poll(fds, 1, timeout_msecs);
std::cout << "poll returned: "<< pol_ret << '\n';
if (pol_ret == 0)
{
std::cout << "im leaving" << '\n';
return -1;
}
How can I wait only for 30 seconds for a pipe to open for read?
I'm running Linux, debian in particular.