From: Simone Piccardi Date: Sat, 16 Nov 2002 14:11:55 +0000 (+0000) Subject: Figure per il deadlock, e programma di prova per il file locking X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=commitdiff_plain;h=32564231c62ef917086f71a223a1847c859edf0e;p=gapil.git Figure per il deadlock, e programma di prova per il file locking --- diff --git a/fileadv.tex b/fileadv.tex index bff4ab7..e8948b9 100644 --- a/fileadv.tex +++ b/fileadv.tex @@ -1600,12 +1600,11 @@ necessario per soddisfare l'operazione richiesta, aggiornando opportunamente 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 @@ -1620,7 +1619,6 @@ questo motivo il kernel si incarica di rilevare situazioni di questo tipo, ed 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 diff --git a/img/file_lock_dead.dia b/img/file_lock_dead.dia new file mode 100644 index 0000000..a414a5d Binary files /dev/null and b/img/file_lock_dead.dia differ diff --git a/sources/Flock.c b/sources/Flock.c new file mode 100644 index 0000000..67a4209 --- /dev/null +++ b/sources/Flock.c @@ -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 /* error definitions and routines */ +#include /* C standard library */ +#include /* unix standard library */ +#include /* standard I/O library */ +#include /* string functions */ +#include /* 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); +} diff --git a/sources/Makefile b/sources/Makefile index 1704f20..e2c9331 100644 --- a/sources/Makefile +++ b/sources/Makefile @@ -11,6 +11,9 @@ OBJ = SockRead.o SockWrite.o FINAL = forktest errcode echo echod daytimed iterdaytimed daytime testfopen \ testren fortune fortuned +flock: Flock.c + $(CC) $^ -o $@ + mqfortune: MQFortuneClient.c $(CC) $^ -o $@