From 32564231c62ef917086f71a223a1847c859edf0e Mon Sep 17 00:00:00 2001 From: Simone Piccardi Date: Sat, 16 Nov 2002 14:11:55 +0000 Subject: [PATCH 1/1] Figure per il deadlock, e programma di prova per il file locking --- fileadv.tex | 4 +- img/file_lock_dead.dia | Bin 0 -> 1532 bytes sources/Flock.c | 146 +++++++++++++++++++++++++++++++++++++++++ sources/Makefile | 3 + 4 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 img/file_lock_dead.dia create mode 100644 sources/Flock.c 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 0000000000000000000000000000000000000000..a414a5d33775e3bddfa23500159d27fe26a17d2c GIT binary patch literal 1532 zcmVE9dLC_j%ZRr92K;H0(oQ^m{a#Fh7W< z!~WmTzu)8jx5t~Wen7s6pD8Evp7@I>maSobmL!WWgTdR|8wfvQk}wX!U!Y;Y->a zC!UZ{9GQJnfz7SI1WCdUjQ@p%@i7y~9UREY0$6gF_Q9_d9k5*`rdR zN+Ck>L3wWeX%nJ)`5NbrzqJZ%{12TZ`SkuuNaPdl_wIX7?7hEb2QCyz# zgoH6cK$Z1bu=PF~z`BFLDNnh&m%Gf5$7SnWjGsi)kggC+*A^JIYd=5>ILN*;0YY{& zNE*`lXu^0@jkaCQDuqa0xH_{nn_Aocd$RQz4W_eXkNKu!A2aS#erWUtFd8m@JY#R8 z8mVI(fz_rD)>Dps8K;ErqH9vQbwxU(p!LzK_+F3-W7#`izioJ8C5=ya@#L)Utp z(+;F~!j3v+O<>C2UF+dS`qd6d+}ftmoYwCWjh6EovAs{t*U43tq1S9n3Rt=P#M|;y z-sjH{d$DTyX#q>e-s_3IR%?Z8C=b3KK#qad!52c{VGmzk^hG-QHjlp5bEdam;$(&q)lwu_X_L#yA5hSm%)B zu$b=FoS=>qoKD1o*)+RztXS||31V4-1X8hJBTKZs3nPUmO$sfPLgA2UR#3?bZU?Mj zVsVqU6%&KuT2O#kUQP_IVR#Z$tA(o}hUz)f+@LUthr6gDJ4!DRSq<5dRkT$N*_Ek0 z)iq?NRh3uQkT0f&>~&E?b`)Qv;o1kX8nSX1Xf-KaL)Llh@|zk)7x^kzdA`c?#IbkH zyod}9C@@W3M82vbva2&zXVRY#c2PxE8+`G$0L`k%%A+qLfF)HvHyg zevC93C% z_UGq?U*eF`#jpS4JA`^-(0V})G5irkw2l~@_E|icR!`Ku zQAnfd{uy)ta&VQ#cRV=7p1aA#11q74-3@@NYvOZ~g-%k4NxUTL1u8EA>SH literal 0 HcmV?d00001 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 $@ -- 2.30.2