[TriLUG] Why do File IO event seem to be out-of-order in strace?

Aaron Joyner via TriLUG trilug at trilug.org
Tue Sep 8 16:40:16 EDT 2015


What Igor said, but also...

I don't believe that this code provides any guarantee that your call of
fprintf(fp, ..) will have written to the file before the subsequent fcntl()
call unlocks it.  Some options include:
- call fdatasync(fp) after fprintf()
- call fsync(fp) after fprintf()
- open fp with O_SYNC or O_DSYNC

Benchmark carefully to understand the performance impact, as
fsync/fdatasync is geared towards ensuring your change is synced to the
underlying storage system, not just visible to other readers of the same
file.

Helpful reading:
http://oldblog.antirez.com/post/fsync-different-thread-useless.html
The fcntl, open, and fopen man pages

On Tue, Sep 8, 2015 at 3:09 PM, Scott Lambdin via TriLUG <trilug at trilug.org>
wrote:

> No, my test program does not have those fancy commands.  Not sure about the
> actual professional program.  I'll study up on them.  Thanks.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>
> #include <sys/types.h>
> #include <sys/inotify.h>
> #include <unistd.h>
> #include <sys/wait.h>
> #include <fcntl.h>
>
> int var_glb; /* A global variable*/
>
> int main( int argc, char **argv )
> {
>     struct flock        fl;
>     fl.l_type   = F_WRLCK;
>     fl.l_whence = SEEK_SET;
>     fl.l_start  = 0;
>     fl.l_len    = 0;
>     fl.l_pid    = getpid();
>
>     struct flock        fl2;
>     fl2.l_type   = F_UNLCK;
>     fl2.l_whence = SEEK_SET;
>     fl2.l_start  = 0;
>     fl2.l_len    = 0;
>     fl2.l_pid    = getpid();
>     pid_t childPID = 99999;
>
>   int x;
>   sleep(3);
>   /* The loop goes while x < 5, and x increases by one every loop*/
>   for ( x = 0; x < 5; x++ ) {
>
>     if(childPID == 0) // child process
>     {
>         break;
>     }
>     childPID = fork();
>
>     if(childPID >= 0) // fork was successful
>     {
>         if(childPID == 0) // child process
>         {
>            FILE *fp;
>            int fd;
>            printf("--------------------------------------------------\n");
>            fp=fopen("/tmp/test.dest", "a+");
>            printf("opened by %d\n", getpid());
>            fd=fileno(fp);
>            printf("About to lock by %d\n", getpid());
>            if (fcntl(fd, F_SETLKW, &fl) < 0){
>               printf("fcntl twidgie failed \n");
>            }else{
>               printf("Locked by %d\n", getpid());
>            }
>            fprintf(fp, "These are the times that try men's soils. \n Why do
> the barnicles seeth in the deep? \n Maybe the stolid twidgies lurk until
> this day?\n");
>            sleep(1);
>            if (fcntl(fd, F_SETLK, &fl2) < 0){
>               printf("fcntl (unlock) twidgie failed\n");
>            }else{
>               printf("UnLocked by %d\n", getpid());
>            }
>            fclose(fp);
>         }
>         else //Parent process
>         {
>         }
>     }
>     else // fork failed
>     {
>         printf("\n Fork failed, quitting!!!!!!\n");
>         exit(2);
>     }
>
>   }
>   printf("--------------------------------------------------\n");
>   exit( 0 );
> }
>
> On Tue, Sep 8, 2015 at 2:53 PM, Aaron Joyner <aaron at joyner.ws> wrote:
>
> > Do you call fsync() (or fflush(), etc as appropriate) after the writes?
> >
> > As Bill suggests, example code would help.
> >
> > On Tue, Sep 8, 2015 at 2:39 PM, Bill Farrow via TriLUG <
> trilug at trilug.org>
> > wrote:
> >
> >> On Tue, Sep 8, 2015 at 1:58 PM, Scott Lambdin via TriLUG
> >> <trilug at trilug.org> wrote:
> >> > The program I wrote had printfs to track the events and they were
> fine :
> >>
> >> I think you have better show use the code that calls fnctl() so more
> >> eyes can check your parameters and usage.
> >>
> >> Bill
> >> --
> >> This message was sent to: Aaron S. Joyner <aaron at joyner.ws>
> >> To unsubscribe, send a blank message to trilug-leave at trilug.org from
> >> that address.
> >> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> >> Unsubscribe or edit options on the web  :
> >> http://www.trilug.org/mailman/options/trilug/aaron%40joyner.ws
> >> Welcome to TriLUG: http://trilug.org/welcome
> >>
> >
> >
>
>
> --
>
> Eat like you give a damn.  Go vegan.
> --
> This message was sent to: Aaron S. Joyner <aaron at joyner.ws>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
> address.
> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web  :
> http://www.trilug.org/mailman/options/trilug/aaron%40joyner.ws
> Welcome to TriLUG: http://trilug.org/welcome
>


More information about the TriLUG mailing list