Problem with empty stream

This is a discussion on Problem with empty stream within the C Programming forums, part of the General Programming Boards category; Hi my problem is as follows: I'm coding (actually fixing a problem on a cusomer site) a program which receives ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Unhappy Problem with empty stream

    Hi
    my problem is as follows:
    I'm coding (actually fixing a problem on a cusomer site) a program which receives a stream of input from a different program which I have absolutely no control over (and asking them to make a change will take a long time).

    The program actually opens a thread for each connected client and each thread opens a buffer for the client's stream.
    When a client is connected a thread is opened with a buffer for the stream, when a client is disconnecte the thread is closed including closing the buffer.
    Opening the buffer is done with fdopen
    Reading is done with fgets
    and closing is done with fclose

    The problem occurs when the client is not sending any input and the buffer is empty when the client is disconnected.
    When trying to close with fclose, it just hangs and doesn't finish.
    I assume this is because fclose flushes the buffer before closing and the flush is stuck (I also confirmed that by trying to use fflush which also got stuck).

    I've searched books and have google'd to find ideas and solutions, I made several attempts. The functions that got "stuck" on me so far are: fclose, fflush, ftell and fseek
    How can I close the buffer or determine that the buffer is empty?

    I would appreciate any suggestion or idea

    Thanks in advance
    Sigal

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by sshahar1 View Post
    When trying to close with fclose, it just hangs and doesn't finish.
    I assume this is because fclose flushes the buffer before closing and the flush is stuck (I also confirmed that by trying to use fflush which also got stuck).
    I thought this was an input stream. You don't really "flush" those.

    What sort of descriptor are you opening with fdopen()?

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Hi
    thanks for your reply :-)
    The 2nd program gives its output to the standard output so we're reading the input stream from a pipe.
    When running fclose I don't have a choice, it flushes the buffer whether I want to or not.

    For now I have a workaround I wrote a shell wrapper that prints a blank line (with echo) and then runs our software, since our software can handle blank lines.

    Thanks
    Sigal

    Quote Originally Posted by brewbuck View Post
    I thought this was an input stream. You don't really "flush" those.

    What sort of descriptor are you opening with fdopen()?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    To reiterate what was already said, fclose() does not flush input streams, so instead of just repeating yourself, perhaps you could clarify what the heck you're talking about.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by sshahar1 View Post
    Hi
    thanks for your reply :-)
    The 2nd program gives its output to the standard output so we're reading the input stream from a pipe.
    When running fclose I don't have a choice, it flushes the buffer whether I want to or not.

    For now I have a workaround I wrote a shell wrapper that prints a blank line (with echo) and then runs our software, since our software can handle blank lines.

    Thanks
    Sigal
    What might be happening is, the client program is failing to launch. This means the write-side of the pipe, which is created by the parent, is never closed (because it would only be closed if the program actually terminated normally). Before the read side can close, the write side must also close. So the read side deadlocks when it attempts to close.

    The solution is to check if the launch of the client program actually succeeded. If it failed, you have to close the WRITE SIDE of the pipe YOURSELF before attempted to close the read side.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    fclose flushes, please refer to: http://bama.ua.edu/cgi-bin/man-cgi?fclose+3C

    Quote Originally Posted by MacGyver View Post
    To reiterate what was already said, fclose() does not flush input streams, so instead of just repeating yourself, perhaps you could clarify what the heck you're talking about.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    The launch was verified, the process is up. When closing the thread we also terminate the client before running fclose.

    Quote Originally Posted by brewbuck View Post
    What might be happening is, the client program is failing to launch. This means the write-side of the pipe, which is created by the parent, is never closed (because it would only be closed if the program actually terminated normally). Before the read side can close, the write side must also close. So the read side deadlocks when it attempts to close.

    The solution is to check if the launch of the client program actually succeeded. If it failed, you have to close the WRITE SIDE of the pipe YOURSELF before attempted to close the read side.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by sshahar1 View Post
    fclose flushes, please refer to:
    It doesn't flush INPUT STREAMS. Since no such thing exists. Read the words.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Not to sound condescending but there is a major difference between input and output under the hood.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 10:24 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. stream socket problem
    By WL in forum C Programming
    Replies: 2
    Last Post: 10-02-2001, 12:07 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 07:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21