/* fcntlother.c */
/* This is just a driver to test the filerwlock objects defined in fcntl.c */
/* It accesses the area protected by the lock. */

#include <stdio.h>
#include <sys/types.h>
#include "fcntl.h"

#define LOCKFILE "mylock"
#define LOCKSSIZE 16


int main(int argc, char *argv[]){
    filerwlock *fl;
    int self,    //Identifier (as given by user) of current process
        bid,       //Identity of byte to be used as lock by this process
        j;       //Loop control variable

    if (argc != 3){
       perror("usage %s processid lockid\n", argv[0]);
       exit(1);
    }

    sscanf(argv[1], "%d", &self);
    sscanf(argv[2], "%d", &bid);

    fl = filerwlockCreate(LOCKFILE, LOCKSSIZE);
    for (j=0; j<15; j++) {
      filewritelock(fl,bid);
      printf("Process %d starts to sleep inside lock %d\n", self, bid);
      sleep(6);
      printf("Process %d ends sleep inside lock %d\n", self, bid);
      filerwunlock(fl, bid);
      sleep(1);
    }
    filerwlockDelete(fl);
    exit(0);
}
