fopen

This is a discussion on fopen within the C Programming forums, part of the General Programming Boards category; Is it going to a be an great impact on the performace of the system if opeing a text file ...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

    fopen

    Is it going to a be an great impact on the performace of the system if opeing a text file and closing quite offen? I mean what happens when i call fopen. I think it traps the OS to open a file and to write and at last to close a file. If that was the case why not use open/close to all i/o file operations?

    Thank you

    ssharish

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    The performance impact is, like so many other things, system-specific. If you're worried about it, you can profile the code (using callgrind on Linux, perhaps other x86 unix systems, for example).

    fopen() and fclose() are portable to all (hosted) ANSI/ISO-conforming implementations. open() and close() are not; they're POSIX (and, quite probably, others).

    On POSIX systems fopen() and fclose() will use open() and close(), sure. They're not the same, though: the ISO FILE* routines are, by default buffered. Maybe this is good for you, maybe not.

    Basically, if you want maximally portable code, use FILE*. If you are targeting POSIX and have a need for unbuffered file operations, perhaps you'd want to do things with file descriptors instead. As usual, it comes down to choosing what's right for a particular task.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Opening and closing files is definitely on the "expensive" list of calls you can make - how expensive will depend on many different things - the filesystem type, the storage media, etc, etc.

    Opening the same file over and over again is slightly better than opening different files, because the localtion of the file, etc, can (and will) be cached by the OS.

    But it's certainly better to open the file once, write to it many times, and then close it - at least if this is something that happens many times a second. If it's a log-file written to once a minute or so under normal circumstances, then you may just as well open and close it when you write to it - it's not going to make any difference to the overall application performance.

    And yes, the summary of how it works is fair: fopen calls the OS "open"[1] function which is some sort of trap-type function to get to the kernel, and inside the kernel there's a chain of generic code calling the file-system driver for the relevant storage, followed by device driver code to access the drive itself. In this particular case, it's quite a bit of work inside the OS - the actual trap into the OS is relatively small in comparison.

    [1] OpenFile in Win32, open() in Linux.

    --
    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.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Thanks a lot every one. It had been much helpful. Matsp and yes you guessed it right, i am in the process of creating a log file in which the file could be opened and closed quite frequently.

    Hence i was more concerned about the performs. And this system is suppose to run on embedded broad and I don't want to give too much stress to the process and the OS since the process works with max spreed of up to 200 MHz.

    And ya, even i through of keeping the file opened all the time. If I need reading it anyway i could create a new stream in a read mode. The way it works is , there is a background process running at all the time which gets all the data from other process which need writing. Since only this process had opened the file in write mode. All other should send the data to this process to write on to the file.

    This way I could control opening the file many time and so on. The problem which i have got is that, i keep on writing the data on to the file. But when I read it from the file through a different process. It looks like nothing has written on to the file. And found why, since the file wasn't closed nothing was updated to the file.

    Is that true if you don't close the file after u write. The contact wouldn't get updated to the file unless it is closed?

    Thanks again everyone for your help

    ssharish
    Last edited by ssharish2005; 09-25-2007 at 02:39 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to ensure that the file is "upto date", use fflush() - that's what the this function is for.

    If you are dealing with the file at a lower level, you may be able to do O_DIRECT as well, which essentially tells the OS "don't buffer this".

    Flushing, and using O_DIRECT may have performance impact - the latter is better for really large writes under the right conditions [1] and worse if you do lots of small writes. Flushing will almost certainly reduce the throughput, but it will do that LESS than opening and closing the file lots.

    [1] This is similar to using non-temporal writes on a processor to bypass using the cache - it's faster to do this when you will "run out of" cache anyways - e.g. if you are filling 2MB of RAM on a machine with 256KB of L2 cache, it's hardly much point in filling the cache with 256KB, and then replace that with the next 256KB and so on - might just as well get the processor to write directly to memory without passing it through the cache - particularly as a write to the cache enforces a read of the remaining cache-line, to ensure that the cache-line is always complete.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  2. fopen help.
    By chriscolden in forum C Programming
    Replies: 17
    Last Post: 01-13-2006, 05:27 AM
  3. problem with fopen command
    By emon in forum C Programming
    Replies: 2
    Last Post: 03-11-2004, 11:11 PM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen vs open
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2002, 01:30 PM

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