Figure per il deadlock, e programma di prova per il file locking
[gapil.git] / sources / Flock.c
diff --git a/sources/Flock.c b/sources/Flock.c
new file mode 100644 (file)
index 0000000..67a4209
--- /dev/null
@@ -0,0 +1,146 @@
+/* Flock.c
+ * 
+ * Copyright (C) 2001 Simone Piccardi
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Program Flock.c: 
+ * Program to test file locking
+ *
+ * Author: Simone Piccardi
+ * Nov. 2002
+ *
+ * Usage: flock -h give all info's
+ *
+ * $Id: Flock.c,v 1.1 2002/11/16 14:11:55 piccardi Exp $
+ *
+ ****************************************************************/
+/*
+ * Include needed headers
+ */
+#include <errno.h>       /* error definitions and routines */ 
+#include <stdlib.h>      /* C standard library */
+#include <unistd.h>      /* unix standard library */
+#include <stdio.h>      /* standard I/O library */
+#include <string.h>      /* string functions */
+#include <fcntl.h>      /* string functions */
+
+
+#include "macros.h"
+
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[])
+{
+/* 
+ * Variables definition  
+ */
+    int type = F_UNLCK;
+    off_t start;
+    off_t len;
+    int fd, res, i;
+    int cmd = F_SETLK ;
+    struct flock lock; 
+    /*
+     * Input section: decode command line parameters 
+     * Use getopt function
+     */
+    opterr = 0;         /* don't want writing to stderr */
+    while ( (i = getopt(argc, argv, "hs:l:wrb")) != -1) {
+       switch (i) {
+       /* 
+        * Handling options 
+        */ 
+       case 'h':   /* help option */
+           printf("Wrong -h option use\n");
+           usage();
+           return -1;
+           break;
+       case 's':   /* take wait time for childen */
+           start = strtol(optarg, NULL, 10);    /* convert input */
+           break;
+       case 'l':   /* take wait time for childen */
+           len = strtol(optarg, NULL, 10);   /* convert input */
+           break;
+       case 'w':
+           type = F_WRLCK;
+           break;
+       case 'r':
+           type = F_RDLCK;
+           break;
+       case 'b':
+           cmd = F_SETLKW;
+           break;
+       case '?':   /* unrecognized options */
+           printf("Unrecognized options -%c\n",optopt);
+           usage();
+       default:    /* should not reached */
+           usage();
+       }
+    }
+    /* ***********************************************************
+     * 
+     *          Options processing completed
+     *
+     *               Main code beginning
+     * 
+     * ***********************************************************/
+    /* There must be remaing parameters */
+    if (type == F_UNLCK) {
+       printf("You should set a read or a write lock\n");
+       usage();
+    }
+    if ((argc - optind) != 1) {
+       printf("Wrong number of arguments %d\n", argc - optind);
+        usage();
+    }
+    fd = open(argv[optind], O_RDWR);
+    if (fd < 0) {
+       perror("Wrong filename");
+       exit(1);
+    }
+    /* setting lock structure */
+    lock.l_type = type;
+    lock.l_whence = SEEK_SET;
+    lock.l_start = start;
+    lock.l_len = len;
+    /* do lock */
+    res = fcntl(fd, cmd, &lock);
+    if (res) {
+       perror("Failed lock");
+       exit(1);
+    }
+    pause();
+    return 0;
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+    printf("Program flock: lock a region of a file \n");
+    printf("Usage:\n");
+    printf("  forktest [-h] [-s start] [-l len] [-w|-r] filename \n");
+    printf("  -h          print this help\n");
+    printf("  -s start    region starting byte\n");
+    printf("  -l len       region length (0 means all file)\n");
+    printf("  -w           write lock\n");
+    printf("  -r           read lock\n");
+    printf("  -b           block when locking impossible\n");
+    
+    exit(1);
+}