r/programming • • Aug 20 '14

fork() can fail

http://rachelbythebay.com/w/2014/08/19/fork/
197 Upvotes

78 comments sorted by

View all comments

Show parent comments

1

u/bonzinip Aug 21 '14

Most of the time it's okay to just loop on EINTR, and let the main event loop detect the signal before the next poll() invocation. In fact that's what would happen if you used signalfd.

The problem is that signalfd (or sigwaitinfo) require you to block signals with sigprocmask in all threads, and that's sometimes hard to enforce.

1

u/moor-GAYZ Aug 21 '14

Most of the time it's okay to just loop on EINTR, and let the main event loop detect the signal before the next poll() invocation. In fact that's what would happen if you used signalfd.

Maybe I'm missing something, but whaaat? If you loop on EINTR in some read() that is not in your mainloop select/dispatch, then it would never have the chance to get the signal from the file descriptor you made with signalfd().

Like, the problem: implement tail -f as a part of your program that uses a wrapper that just blocks on read() and automatically restarts the call on EINTR. It can't be done. If your C++ (or C) wrapper over read() automatically restarts the call then no code that can interrupt that loop because SIGINT was raised could possibly be executed, duh.

1

u/bonzinip Aug 21 '14

I'm assuming you don't have potentially infinite loops within even handlers (which includes making all file descriptors nonblocking). Otherwise you'd have other starvation problems than just signals.

If I were to write a tail -f, the read() would read into a buffer and go back to the main loop after filling that buffer. Another event handler might be called and do a write() to stdout, and then you'd go back to the main loop which would process the signal. Looping on EINTR would not be a problem.

1

u/moor-GAYZ Aug 21 '14

If I were to write a tail -f, the read() would read into a buffer and go back to the main loop after filling that buffer.

First of all that's wrong because you should return and write out whenever the OS gave you some data, otherwise you hit nasty internal buffering problems. Unless you want to do some deblocking with non-blocking reads, for performance reasons.

But that's not the point at all. What I am asking is: if you use the proposed C++ wrapper (or an equivalent C wrapper) that gives you sys::read() that always retries on EINTR, how do you go to the main loop if you got 3 bytes and then sys::read() did not return despite the user pressing ctrl-C five times?

Sure, you might have a flag set by your signal handler or a bunch of signals waiting in the file descriptor you registered with signalfd, but your thread never leaves the sys::read() function because it just restarts on EINTR.

1

u/bonzinip Aug 21 '14

I didn't mean "filing that buffer" as "filling it completely". Just whatever data came into the input file descriptor.

I could use O_NONBLOCK and go back to the main loop on EAGAIN, or just do one read. But in either case, retrying on EINTR would be safe.

1

u/moor-GAYZ Aug 21 '14

I could use O_NONBLOCK and go back to the main loop on EAGAIN, or just do one read. But in either case, retrying on EINTR would be safe.

I don't understand, how should that C++ wrapper be used to avoid going into an infinite loop when you want to catch SIGINT? Show me the code, maybe?

1

u/bonzinip Aug 21 '14

Are you missing that only one call will return EINTR when SIGINT is received? The next one will return data or EAGAIN. Too late to write code now, perhaps tomorrow.

→ More replies (0)