Underflow with fgets() and popen()

Hi all!

I wonder if you can give me any suggestions on how to deal with this problem.
My application crashesh after a few days of looping.

GDB says:

#0 0xffffe430 in __kernel_vsyscall ()
#1 0xb7d4670b in read () from /lib/libc.so.6
#2 0xb7cefd8c in _IO_file_underflow () from /lib/libc.so.6
#3 0xb7cf175b in _IO_default_uflow () from /lib/libc.so.6
#4 0xb7cf2bc8 in __uflow () from /lib/libc.so.6
#5 0xb7ce57d6 in _IO_getline_info () from /lib/libc.so.6
#6 0xb7ce5721 in _IO_getline () from /lib/libc.so.6
#7 0xb7ce455d in fgets () from /lib/libc.so.6

The fgets() is executed on a stream opened with popen ()
Its is beeing run in 6 threads at the same time with about 50ms in between, but also at the same time.

If I use fread() I get the same results.....


//opens bourneshell and executes command stream = popen(popenstring.c_str(), "r"); if (stream != NULL) { /* //reads data if present at stdout while (fgets(buffer, sizeof(buffer)-1, stream) != NULL ) { //append data if valid if (sizeof(buffer)>0) { str.append(buffer); } }//endwhile

popen string reads " ping -I hso0 -c 2 -w 2" or similar.
I am pinging through 3 interfaces in two threads for each.
One to see if its alive, the other to gather ping results and calculate an average ping.

The lines read from popen are maximum 380 chars, buffer is at 4096.

This exact routine is beeing run by some other applications, but not in multiple threads, and then there is no problem..

I can not see any error in this popen /fgets handling, as this is how its usually done.

Can any one give me some pointers to possible causes for underflow with fgets and popen?

Many thanks for any suggestion!



  • fgets() and similar library functions are usually not reentrant (thread safe). I would make sure that the stdio.h functions you are using are safe, by checking the manual.

    These functions have a tendency to contain access to global or static variables, which will cause bugs in multi-threaded apps.
  • This post has been deleted.
  • This post has been deleted.
  • Hi!


    But I have read in several places on the web that fgets() and popen() are threadsafe, at least in their newer implementations.

    I still believe though that this Is threadrelated in some way.
    But I cannot understand how broken pipes or corrupt shellbuffers etc could crash my software.
    fgets() cannot read more bytes than its said to read, and the buffer size is less...

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!