le strutture interne usate per il file locking.
\begin{figure}[htb]
- \centering \includegraphics[width=13cm]{img/file_posix_lock}
+ \centering \includegraphics[width=9cm]{img/file_lock_dead}
\caption{Schema di una situazione di \textit{deadlock}.}
\label{fig:file_flock_dead}
\end{figure}
-
Non operando a livello di interi file, il file locking POSIX introduce
un'ulteriore complicazione; consideriamo la situazione illustrata in
\figref{fig:file_flock_dead}, in cui il processo A blocca la regione 1 e il
impedirle restituendo un errore di \macro{EDEADLK} alla funzione che cerca di
acquisire un lock che porterebbe ad un \textit{deadlock}.
-
\begin{figure}[htb]
\centering \includegraphics[width=13cm]{img/file_posix_lock}
\caption{Schema dell'architettura del file locking, nel caso particolare
--- /dev/null
+/* 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);
+}