user2710449
user2710449

Reputation: 63

popen Hangs and cause CPU usage to 100

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

Answers (2)

alk
alk

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

P.P
P.P

Reputation: 121387

Not an answer ;-)

  1. Try use strace to what it's doing and which syscall hangs.

  2. 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().

  3. Run the script manually and ensure the script doesn't hang intermittently before suspecting popen().

Upvotes: 1

Related Questions