• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1400
  • Last Modified:

using O_SYNC, but still need fsync()

At the start of my app, depending on a command line parameter, I may redirect stdout and stderr to a file (see below)
all of my output is done using printf's....so the app can be run in verbose or quiet mode to a file.  It is necessary for me to be able to check the contents of the log as the program progresses.  Currently nothing shows until I call fsync().  I though opening with O_SYNC would force data to be written to disk.  I considered O_DIRECT until I noticed it's definiation "/* direct disk access hint - currently ignored */"
Is there way other than following every printf() with an fsync() to get data writting to disk real time?
Thank You

  iStdOutAndErr = open("myfile",O_CREAT|O_TRUNC|O_APPEND|O_RDWR|O_SYNC,S_IRWXG|S_IRWXU|S_IRWXO);
   if (iStdOutAndErr) {
      int iFileToClose;

      iFileToClose = fileno(stdout);
      if (iFileToClose) {
       close(iFileToClose);
       dup2(iStdOutAndErr,iFileToClose);
      }
      iFileToClose = fileno(stderr);
      if (iFileToClose) {
       close(iFileToClose);
       dup2(iStdOutAndErr,iFileToClose);
      }
   } // if could open the file
0
PhilC
Asked:
PhilC
1 Solution
 
ravenplCommented:
For disk access use O_DIRECT flag, but then, be aware, that operations has to be aligned to 512 bytes. size of r/w has to be multiply of 512, buffer memory has to have aligned address(man posix_memalign).
For sockets pipes etc it's ignored as well.
0
 
PhilCAuthor Commented:
I was hoping to avoid wrapping all my printf()'s in order to handle screen or file output...the alignment requirement would make this necessary too.
Is there any other way to runtime optionally redirect stdout and stderr to a file?
Thank you very much
0
 
Duncan RoeSoftware DeveloperCommented:
I believe your problem is this: you are using streams(3) calls (like printf) but operating on raw file entities (like dup2(2)).
You need to eliminate the normal streams buffering of printf() &c. This is actually quite easy - man setvbuf for details. You want _IONBUF so every printf causes immediate calls of write()
Odd how fsync() works for you though - maybe it is gratuitously calling fflush() for you(?) (I doubt it though - maybe streams i/o is super clever somehow)
Actually you shouldn't need O_SYNC at all (unless viewing the file over NFS) - with no streams buffering your output will all go out to Linux buffers and any other app opening the same file will see it whether it's been actually written to disk or not
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
leisnerCommented:
I'm not sure exactly what you're doing, but the fsync and the O_SYNC doesn't have anything to do with printf -- printf  is buffered I/O.   setvbuf is on the right track -- what type of system are you running on?
streams is not supper clever (its super confusing sometimes!!).
0
 
PhilCAuthor Commented:
would I have to change my open() to an fopen()?
setvbuf fails (dumps) when I try to change the mode
setvbuf(iStdOutAndErr, NULL,_IONBF,0);

Thank You
0
 
Duncan RoeSoftware DeveloperCommented:
Should be all right to leave as_is. And from now on, always compile with -Wall, then you will see your errors more easily.
 iStdOutAndErr is a file number (i.e. a small number), but setvbuf wants a file *pointer*. So you need to setvbuf stdout. You can do this right at the start of your program, before the open call. The man page for stderr says it's unbufferred aleady.
You could look at freopen, but since you're making the streams unbufferred you probably don't need to.
0
 
PhilCAuthor Commented:
works great, thank you
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now