Like Tree1Likes
  • 1 Post By anduril462

Stream delays.

This is a discussion on Stream delays. within the C Programming forums, part of the General Programming Boards category; Hi everyone! My problem is that i have a function containing this: Code: printf("text"); send(rr->sock, "text", 4, 0); It runs ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    20

    Stream delays.

    Hi everyone!
    My problem is that i have a function containing this:
    Code:
        printf("text");
        send(rr->sock, "text", 4, 0);
    It runs in separate thread from the main function, rr->sock is initialized(checked in gdb), but somewhy, the output of both printf and send(through browser) appears after a long delay, or instantly after the main thread is finished. Looks like it would happen the same way to any other stream. I tried to google the problem, but found nothing. I have tested the program on Mint 13 and Ubuntu 12.04 - both act the same. How to solve this thing?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try putting an fsync call after your send call:
    Code:
    send(rr->sock, "text", 4, 0); 
    fsync(rr->sock);
    You should, of course, be checking the return values for an error condition.

    EDIT: I should mention that this is just a guess. Let me know if it works.
    Last edited by oogabooga; 10-09-2012 at 11:07 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,291
    stdout is typically line buffered, meaning output doesn't show up until a new line* ('\n'). Either add a new line: "text\n", or put a call to fflush(stdout); after the printf.

    * There are exceptions to the rule. Output will show up immediately on stdout, regardless of a \n, when fflush'ed, when input is requested from stdin (e.g. fgets, getchar, scanf), or if you manage to fill the internal buffer before any of the above conditions are met (i.e. you have really long lines of output). Also, if stdout is not connected to a terminal (e.g. piped to another process), it is fully buffered, meaning it only outputs when the internal buffers are full or when fflush'ed.
    oogabooga likes this.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    20
    Andurils idea about \n at the end seems to work for printf, but still no luck with send. fsync(rr->sock) shows -1, but return value of send is 4 and no errno is set. I've been programming sockets before, but never encountered such problems.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,291
    What is errno (or what does perror say) after fsync fails?

    It would help if you could provide the smallest, compilable piece of code that demonstrates this problem, along with any input data and instructions for replicating the problem. Make sure every IO operation has complete error checking, and prints a useful error message, by using perror or strerror+errno. Without that, I'm not very motivated to help you.

    What are you talking to on the other end? You could throw together a quick listener that simply echoes the data it receives on the socket, and run it locally on the loopback adapter to eliminate any strange physical/network issues or issues with the other end.

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    20
    Errno of fsync is also one strange thing:
    -1 0 - Success
    is output for
    Code:
    printf("%i %i - %s\n", fsync(rr->sock), errno, strerror(errno));
    P.S. I just tried to perform send command from another thread - parent thread of the one that i have been dealing before using the same socket and string - it worked perfectly.
    I guess something is wrong with threads.
    Last edited by wirmius; 10-09-2012 at 12:01 PM.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by wirmius
    Errno of fsync is also one strange thing
    You can't do it like that (because the order of function argument evaluation is unspecified).
    Instead, try this:
    Code:
    int err = fsync(rr->sock);
    printf("%i %i - %s\n", err, errno, strerror(errno));
    Quote Originally Posted by wirmius
    return value of send is 4 and no errno is set
    You shouldn't check errno unless the function returned it's error value (-1 in this case). Functions are not required to set errno to 0 in the case of no error.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,291
    You can't print errno that way. The order that the parameters to printf are evaluated is unspecified per the standard, so it may or may not call fsync before calling strerror. That means errno and strerror may reflect the successful state of send (which seems likely per your output) or it may reflect that of fsync. You should use an if statement to check for error:
    Code:
    if (fsync(rr->sock) == -1) {
        printf("error with fsync: %d = %s\n", errno, strerror(errno));
    }
    You should do something very similar with all of your IO calls.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    20
    Thanks everyone, I got it fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delays
    By luigi40 in forum C# Programming
    Replies: 6
    Last Post: 04-11-2005, 01:49 AM
  2. Quick Delays
    By bigB8210 in forum C Programming
    Replies: 1
    Last Post: 07-10-2003, 02:06 PM
  3. precision delays
    By marcdawson in forum C Programming
    Replies: 5
    Last Post: 11-08-2002, 10:03 AM
  4. Delays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-02-2002, 01:01 PM
  5. Delays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-04-2001, 04:46 AM

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