Mixing user mode and OS instructions

This is a discussion on Mixing user mode and OS instructions within the C Programming forums, part of the General Programming Boards category; Hey all, I've noticed that when I code a function that contains both user mode and privileged mode (system calls) ...

  1. #1
    Registered User
    Join Date
    Oct 2007

    Question Mixing user mode and OS instructions

    Hey all,

    I've noticed that when I code a function that contains both user mode and privileged mode (system calls) instructions such as printf and write, respectively, the system calls are always processed first (or so it seems from the output) and the user mode instructions are only processed at the very end.

    For example, if I mix up lines of printf and write instructions, all the write operations will be outputted first and then ALL the printf operations will be outputted at the very end.

    Why is this?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Ontario, Canada
    This is likely caused by the output from printf being buffered before appearing on the screen. Try putting fflush(stdout) after the printf calls and see if that changes anything.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    If both printf() and write() goes to the same file descriptor, then you can achieve ordered output by ensuring that your printf() are follwed by calls to flush the output. Alternatively, you could use setvbuf() with a mode of _IONBF to force stdout to not be buffered - although this hurts performance.

    However, as a programming style, it's bad to depend on such interactions between direct system calls and C runtime library calls. If I were presented with code that does such mixing, I would send it back for a rewrite. Choose what you want to use:
    * use direct system calls- but don't mix it with FILE CRT functions - you can use sprintf() and such-like to make a string that can be "printed" with write().
    * use printf() and other FILE operations, use them only, do not mix with write(). You can use fwrite() instead, it has the same functionality as write(), but operates on the same FILE object as (f)printf().

    Also consider that there is a REASON that the files are buffered in the C runtime library: There is a "cost" involved in calling the OS to do the low-level write() operation. To bunch up a number of bytes before calling the OS, like the CRT does, will amortize this cost [the cost is essentially the same for a small number of bytes as it is for a large number].

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial port communcation and computer standby mode
    By MWAAAHAAA in forum Windows Programming
    Replies: 0
    Last Post: 07-09-2009, 10:38 AM

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