Reputation: 63
I have a code that use popen to execute a script ,It works fine but randomly it's blocking and getting CPU to 100% ,after a little investigation I discover that it hangs on popen calls. I have put a printf after the popen showing the descriptor asigned and in this case when it blocks this printf never shows.
What can cause popen to block?
Edit: Code
FILE* pipe = popen(cpCommand, "r");
printf(....
if (pipe)
{
while (!feof(pipe))
{
if (DataReady(fileno(pipe),2500)>0)
{
if (fgets(output,sizeof(output),pipe) != NULL)
{
DataReady is just a select.. I have done a strace after it blocks and it seems to not doing anything
Upvotes: 1
Views: 649
Reputation: 70931
Check the script called by popen()
why it doesn't end.
As long a s the script does not end, popen()
blocks, will say: does not return, as observed.
I strongly doubt this is a C issue.
Upvotes: 0
Reputation: 121387
Not an answer ;-)
Try use strace
to what it's doing and which syscall hangs.
Tterminal output is line-buffered, so make sure to flush output by using fflush() or using a newline (fflush(stdout);
or printf("Debug text\n");
) to ensure it doesn't really call the printf().
Run the script manually and ensure the script doesn't hang intermittently before suspecting popen().
Upvotes: 1