In a multithreaded program (running on ARM) I have
a main thread which, among other things, periodically checks with popen( "pidof -s prog" )
whether another program is running. I use the O_CLOEXEC
flag for the file descriptor and check whether fgetc()
receives anything from the pipe. Setting the file descriptor "non-blocking" results in nothing being read and won't help. The same pidof
command from the shell command is performing fine.
In another thread, fork()
with an immediate execl()
in the child process is used to start an rsync
operation whenever a specific event occurs. The parent uses a signal handler to observe the child status, and has the option to kill the child at another specific event. It doesn't matter whether I invoke exec()
with rsync
or sleep
- the result is the same.
The problem is that the fgetc()
in the main thread blocks until the child process terminates.
I'll try to solve this problem by fork()
ing early (at some point where the application is single-threaded, as supposed in another post which I started).
But anyway:
I'd like to understand what's causing the fgetc()
to block when reading from the pipe.
A few things I've tried so far:
- I tried to reproduce the problem with a small example application that does what I've described above and hoped it would show the same erroneous behaviour, but unfortunately it works fine, which is why I do not provide any code here yet. Maybe I'm missing the relevant point.
- Using the same
rsync
invocation viasystem()
doesn't cause any issues I've had a look at a
system()
implementation and can see that the signals are manipulated beforefork()
ing:- SIGCHLD is blocked
- SIGINT and SIGQUIT are ignored
I need the signal handler for SIGCHLD, but out of curiousity I tried to do the same as in the code from above (I replaced
sigprocmask()
withpthread_sigmask()
) - without any success, the behaviour stays the same.I couldn't find any implementation of
system()
in the sources provided with my BSP.
The program opens other files via fstream
- and without O_CLOEXEC (will be a bit cumbersome to change that)