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

Scott Lambdin via TriLUG trilug at trilug.org
Tue Sep 8 15:09:43 EDT 2015


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.


More information about the TriLUG mailing list