Thread: stdout printing out of order?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    stdout printing out of order?

    I don't understand how this is possible, but here it is.
    I added some debug statements to each function in this file that just tells me when a function gets called.
    In main(), right after some variables are defined, I have a DTRACE call that should print "In main()". However, when I run the program, "In main()" isn't printed first. What actually gets printed first is coming from a function that is called about 30-40 lines below my DTRACE statement.

    I even added fflush( stdout ); to the end of my DTRACE() function just to see if that was the problem, but no luck.

    This program really hates me...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Huh?
    That doesn't make any sense at all. Are you sure that your printouts in themselves are correct and not cached/buffered someplace?

    And I suppose your system is single-threaded, and not using multiple threads, right?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Is this a multithreaded program? Or one that spawns any type of process or subprocess, etc. etc..?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Are you sure DTRACE is printing to stdout, not stderr? If it was stderr, then your fflush(stdout) isn't doing anything.

    Other than that, I don't know, unless you can post code.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Hmm... This could be the problem, although I still don't understand why.

    The program takes a command line argument -o filename and prints all the output to that file. I've been running it with -o /dev/stdout > filename

    I think most of the program uses fprintf() or fputs() for its output (which in my case is using /dev/stdout), but my DTRACE() function just does plain old printf().

    Is there some strange timing difference between opening /dev/stdout and writing to it vs. doing printf()?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you open two different files to standard out, there's no prediction of whcih one "gets there first". Use the same FILE for both - e.g. change "stdout" to your output, or debug using fprintf(somefile, ...).

    Mixing and matching won't be reliable in ordering, even if it's eventually reaching the same device.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Thanks.
    I just changed my DTRACE() function to fprintf() to the same FILE* and it looks like it's printing properly now.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    Is there some strange timing difference between opening /dev/stdout and writing to it vs. doing printf()?
    They each get their own FILE handle so they each get their own buffer. /dev/stdout is not very portable, anyway. On my system, it's a link to /proc/self/fd/1. And in the past, it wasn't a real device at all, but a "simulated device" created by bash -- you could only redirect to it, not open it as a file.

    Maybe change your program so that if the "-o filename" option is not given, the output defaults to go to stdout. Most programs work that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 30
    Last Post: 10-29-2005, 11:09 PM
  2. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  3. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM
  4. Printing name in reverse order and in caps
    By Guti14 in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2003, 07:02 AM
  5. Printing String in reverse order
    By ling in forum C Programming
    Replies: 14
    Last Post: 10-18-2001, 09:03 AM