Laface 2007 File system 3.1 Operating System Design Other filesystem system calls pipe dup mount umount link unlink system popen.

Slides:



Advertisements
Similar presentations
Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
Advertisements

Unix programming Term: III B.Tech II semester Unit-V II PPT Slides Text Books: (1)unix the ultimate guide by Sumitabha Das (2)Advanced programming.
NCHU System & Network Lab Lab 15 Record Locking. NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same.
Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
UNIX File Systems (Chap 4. in the book “the design of the UNIX OS”) Acknowledgement : Soongsil Univ. Presentation Materials.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
TDC368 UNIX and Network Programming Camelia Zlatea, PhD Week 6:  Inter-Process Synchronization  Signals.
1 Case Study 1: UNIX and LINUX Chapter History of unix 10.2 Overview of unix 10.3 Processes in unix 10.4 Memory management in unix 10.5 Input/output.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Today’s topic Inter-process communication with pipes.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
1 UNIX Systems Programming Interprocess communication.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files Special Topics in Comp. Eng. 2 Semester.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 5 Acknowledgements: The syllabus and power point presentations are modified versions.
SIMULATED UNIX FILE SYSTEM Implementation in C Tarek Youssef Bipanjit Sihra.
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
CSC Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
Chapter 4. INTERNAL REPRESENTATION OF FILES
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
10/23/ File System Architecture. 10/23/ / bin unixdev etc user jim mike x y z tty00 tty01 File System architecture.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Chapter 6 UNIX Special Files Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
UNIX Files File organization and a few primitives.
System calls for Process management
Chapter 4. INTERNAL REPRESENTATION OF FILES
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Project 6 Unix File System. Administrative No Design Review – A design document instead 2-3 pages max No collaboration with peers – Piazza is for clarifications.
Solutions for the First Quiz COSC 6360 Spring 2014.
1 Chapter 4. INTERNAL REPRESENTATION OF FILES THE DESIGN OF THE UNIX OPERATING SYSTEM Maurice J. bach Prentice Hall.
Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()
Linux File system Implementations
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
Interprocess Communication
Named Pipes. Kinds of IPC u Mutexes/Conditional Variables/Semaphores u Pipes u Named pipes u Signals u Shared memory u Messages u Sockets.
THE FILE SYSTEM Files long-term storage RAM short-term storage Programs, data, and text are all stored in files, which is stored on.
Lecture 19 Linux/Unix – File System
4P13 Week 9 Talking Points
Interprocess Communication. Resource Sharing –Kernel: Data structures, Buffers –Processes: Shared Memory, Files Synchronization Methods –Kernel: Wait.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
System calls for Process management Process creation, termination, waiting.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Today topics: File System Implementation
FILE LOCK #include <stdio.h> #include <stdlib.h>
Chapter 4: System calls for the file system
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
IPC Prof. Ikjun Yeom TA – Hoyoun
Advanced UNIX progamming
FILE I/O File Descriptors I/O Efficiency File Sharing
UNIX File Systems (Chap 4. in the book “the design of the UNIX OS”)
Internal Representation of Files
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

Laface 2007 File system 3.1 Operating System Design Other filesystem system calls pipe dup mount umount link unlink system popen

Laface 2007 File system 3.2 Operating System Design Unnamed and named pipes Pipes and FIFOs (also known as named pipes) provide a unidirectional interprocess communication channel. The difference between them is the manner in which they are created and opened. I/O on pipes and FIFOs has exactly the same well known bounded buffer producer-consumer semantics Read pointer Write pointer

Laface 2007 File system 3.3 Operating System Design Unnamed pipe process A process Bprocess C process Dprocess E calls pipe() Cannot share pipe Share pipe

Laface 2007 File system 3.4 Operating System Design Unnamed pipe pipe (fdptr); fdptr is a pointer to an int array that will be filled with two file descriptors that can be used to read e write the unnamed pipe. An inode is assigned to a new pipe Two entries in the user file descriptor table and in the file table, are allocated The inode reference count indicates how many times the pipe has been opened (both for reading and writing) (2). The kernel stores in each entry of the file table the read and write reference counts The inode also includes the offset values for the next read and write (cannot be modified by means of lseek) Storing the offsets in the inode rather than in the file table allows more than one process to share the pipe both in reading and writing (each process modifies the same offset)

Laface 2007 File system 3.5 Operating System Design read and write char string[]="hello"; main(){ char buf[1024]; char *cp1, *cp2; int fds[2]; cp1=string; cp2=buf; while (*cp1) *cp2++=*cp1++; pipe(fds); for(;;) { write(fds[1], buf, 6); read (fds[0], buf, 6); } char string[]="hello"; main(){ char buf[1024]; char *cp1, *cp2; int fds[2]; cp1=string; cp2=buf; while (*cp1) *cp2++=*cp1++; pipe(fds); for(;;) { write(fds[1], buf, 6); read (fds[0], buf, 6); }

Laface 2007 File system 3.6 Operating System Design Pipe open A process opening a pipe for reading will be suspended until another process open the pipe for writing (and viceversa) It is possible to open a FIFO using the flags O-NONBLOCK or O-NDELAY O_ASYNC − Setting the O_ASYNC flag for the read end of a pipe causes a signal ( SIGIO by default) to be generated when new input becomes available on the pipe Non-blocking I/O is also possible by using the fcntl F_SETFL operation to enable the O_NONBLOCK open file status flag.

Laface 2007 File system 3.7 Operating System Design Named pipe Example of open that blocks the issuing process until the other process open the other end of a named pipe npipe-r.c npipe-w.c Example of open that blocks the issuing process until the other process open the other end of a named pipe npipe-r.c npipe-w.c

Laface 2007 File system 3.8 Operating System Design Pipe write Writes of less than PIPE_BUF bytes (4KB on Linux) are atomic n <= PIPE_BUF O_NONBLOCK disabled –All n bytes are written atomically; –write may block if there is not room for n bytes to be written immediately O_NONBLOCK enabled –If there is room to write n bytes to the pipe, then write succeeds immediately, writing all n bytes; –otherwise it fails, with errno set to EAGAIN.

Laface 2007 File system 3.9 Operating System Design Pipe write Writes of more than PIPE_BUF bytes may be non-atomic n > PIPE_BUF O_NONBLOCK disabled –the write is non-atomic: the data given to write may be interleaved with writes by other process; –the write blocks until n bytes have been written. O_NONBLOCK enabled –If the pipe is full, then write fails, with errno set to EAGAIN. –Otherwise, a "partial write" of up to n bytes may occur, and these bytes may be interleaved with writes by other processes.

Laface 2007 File system 3.10 Operating System Design Pipe close If all file descriptors referring to the write end of a pipe have been closed, then an attempt to read from the pipe return 0. If all file descriptors referring to the read end of a pipe have been closed, then a write will cause a SIGPIPE signal to be generated for the calling process. If the calling process is ignoring this signal, then write fails with the error EPIPE. An application that uses pipe and fork should close unnecessary duplicate file descriptors to ensures that end-of-file and SIGPIPE/EPIPE are delivered when appropriate.

Laface 2007 File system 3.11 Operating System Design Other examples of pipe use Client-server with named pipe –pipe1.cpipe1.c –client_fifo.cclient_fifo.c –server-fifo.cserver-fifo.c Use of pipe, dup, exec, getenv –pipe2.cpipe2.c

Laface 2007 File system 3.12 Operating System Design Other examples of pipe use Client- server using an unnamed pipe (pipe1.c server1.c client1.c) PARENT CHILD PIPE1PIPE

Laface 2007 File system 3.13 Operating System Design dup – dup2 newfd = dup (fd); Duplicates the fd pointer in the first free entry of the user file description table newfd = dup2 (fd1, fd2); Duplicates the fd1 pointer in the fd2 entry of the user file description table

Laface 2007 File system 3.14 Operating System Design Comparison between open and dup User file descriptor table File table Inode table 0 1 C=2 C=1 C=3 (/etc/passwd) C=1 (local)

Laface 2007 File system 3.15 Operating System Design Other examples of pipe use Use of pipe, dup, exec, getenv –pipe2.cpipe2.c

Laface 2007 File system 3.16 Operating System Design dup example #include main () { int i,j; char buf1[512], buf2[512]; i = open("/etc/passwd", O_RDONLY); j = dup(i); read(i,buf1, sizeof(buf1)); read(j,buf2, sizeof(buf2)); close(i); read(j, buf2, sizeof(buf2)); } #include main () { int i,j; char buf1[512], buf2[512]; i = open("/etc/passwd", O_RDONLY); j = dup(i); read(i,buf1, sizeof(buf1)); read(j,buf2, sizeof(buf2)); close(i); read(j, buf2, sizeof(buf2)); }

Laface 2007 File system 3.17 Operating System Design Output redirection fd = open(”file_output”, O_CREAT|O_WRONLY); close(1); dup(fd); close(fd); write(1,buf, sizeof(buf)); fd = open(”file_output”, O_CREAT|O_WRONLY); close(1); dup(fd); close(fd); write(1,buf, sizeof(buf)); Null 4

Laface 2007 File system 3.18 Operating System Design mount mount (pathname, dir pathname, options); dev pathname is – the name of the device special file corresponding to the disk partition formatted with a file system – a directory name dir pathname is the directory (mount point), in the current directory tree, where the filesystem will be mounted. options indicates the mode of mounting (ex. Read-Only)

Laface 2007 File system 3.19 Operating System Design mount bin file system /dev/dsk1 / ccdatesh usr gettypasswd etc bin / awkbanneryacc src stdio.h include uts file system root

Laface 2007 File system 3.20 Operating System Design Mount table Mounted on Inode marked as mount point Reference count = 1 Root inode of mounted File System Reference count = 1 Superblock Mounted on Inode Root Inode Mount Table Inode Table Buffer

Laface 2007 File system 3.21 Operating System Design mount proceduramount input:nome file di un file speciale a blocchi nome directory punto di mount opzioni (sola lettura) output:nessuno { if (non superuser) return(errore); prende inode file speciale a blocchi (namei); effettua controlli legalità; prende inode per nome directory "mounted on" (namei); if (non directory o contatore di riferimenti > 1){ rilascia inode (procedura iput); return(errore); } proceduramount input:nome file di un file speciale a blocchi nome directory punto di mount opzioni (sola lettura) output:nessuno { if (non superuser) return(errore); prende inode file speciale a blocchi (namei); effettua controlli legalità; prende inode per nome directory "mounted on" (namei); if (non directory o contatore di riferimenti > 1){ rilascia inode (procedura iput); return(errore); }

Laface 2007 File system 3.22 Operating System Design mount find a free entry in mount table; open the block device; getblk; read superblock; initialize superblock; iget root inode of the new filesystem store it in mount table; mark the inode of the directory as a mount point; relese the inode of the special file (iput); free the inode in memory of the mount point; } find a free entry in mount table; open the block device; getblk; read superblock; initialize superblock; iget root inode of the new filesystem store it in mount table; mark the inode of the directory as a mount point; relese the inode of the special file (iput); free the inode in memory of the mount point; }

Laface 2007 File system 3.23 Operating System Design umount umount ( special file name); Before unmounting a filesystem, the kernel controls that no file is still in use (open) searching in the inode table the files having a device field equal to the device of the filesystem we try to umount.

Laface 2007 File system 3.24 Operating System Design Virtual File System SystemFile System operation Unix open close read write ……. PCFS Remote ropen rclose rread rwrite Generic Inode Unix Inode Remote Inode

Laface 2007 File system 3.25 Operating System Design link (source name, target name); link("/usr/src/uts/sys", "/usr/include/sys"); link("/usr/include/realfile.h", /usr/src/uts/sys/testfile.h"); / usr include src uts sys inode.h testfile.h sys realfile.h

Laface 2007 File system 3.26 Operating System Design unlink (pathname) Deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open the file is deleted (reference cont and link count = 0) If the name was the last link to a file, but a process still has the file open (reference count > 0) the file will remain in existence until the last file descriptor referring to it is closed. If the name referred to a symbolic link the link is removed. If the name referred to a socket, FIFO or device the name for it is removed but processes which have the object open may continue to use it.

Laface 2007 File system 3.27 Operating System Design unlink unlink (pathname); The kernel releases in this order the file blocks: – Direct blocks – Direct blocks pointed by indirect blocks – Indirect blocks Set to 0 the entries in the inode Set to 0 the file size Update the disk copy of the inode

Laface 2007 File system 3.28 Operating System Design unlink iget inode of the file that must be removed; update the parent directory; set to 0 the status field of the inode of the erased file; release the inode of the parent directory (iput); decrements the file link count; release the file inode (iput); // iput tests link count if it is zero free and ifree iget inode of the file that must be removed; update the parent directory; set to 0 the status field of the inode of the erased file; release the inode of the parent directory (iput); decrements the file link count; release the file inode (iput); // iput tests link count if it is zero free and ifree

Laface 2007 File system 3.29 Operating System Design unlink - close A process can perform unlink of a file while itself or another process still has the file open Any process will be able to access the file, but since open increments the file’s inode reference count, the kernel will not remove the data blocks and the inode, it just decrements the link count. when a system call close is executed, the reference count becomes 0 and close call free and ifree.

Laface 2007 File system 3.30 Operating System Design Example with unlink – stat – fstat #include main (int argc, char **argv) { int fd; char buf[1024]; struct stat statbuf; if (argc != 2) exit (-1); if ((fd = open(argv[1], O_RDONLY)== -1) exit (-1); unlink(argv[1]) /*unlink of the file just opened*/ if (stat(argv[1], &statbuf) == -1)/* stat name */ printf("stat %s fails \n", argv[1]); else printf("stat %s succeed !!!\n",argv[1]); if (fstat(fd, &statbuf) == -1) /* stat through fd */ printf ("fstat %s fails \n",argv[1]); else printf("fstat %s succseed \n", argv[1]); while (read(fd,buf,sizeof(buf))>0) printf ("%1024s",buf);/*print 1KB */ } #include main (int argc, char **argv) { int fd; char buf[1024]; struct stat statbuf; if (argc != 2) exit (-1); if ((fd = open(argv[1], O_RDONLY)== -1) exit (-1); unlink(argv[1]) /*unlink of the file just opened*/ if (stat(argv[1], &statbuf) == -1)/* stat name */ printf("stat %s fails \n", argv[1]); else printf("stat %s succeed !!!\n",argv[1]); if (fstat(fd, &statbuf) == -1) /* stat through fd */ printf ("fstat %s fails \n",argv[1]); else printf("fstat %s succseed \n", argv[1]); while (read(fd,buf,sizeof(buf))>0) printf ("%1024s",buf);/*print 1KB */ }

Laface 2007 File system 3.31 Operating System Design Advisory / Mandatory locking Advisory locking ( flock BDS ) ( fcntl POSIX) –read and write are protected by an access protocol Mandatory locking (non POSIX) –kernel managed locking Behaviour similar to the Readers & Writers problem – shared or read lock  excludes other writes – exclusive or write lock  excludes other reads and writes

Laface 2007 File system 3.32 Operating System Design File locking A set of processes open a file that stores a sequence number The processes – read the sequence number – print their process identifier followed by the sequence number – Increment and store the sequence number

Laface 2007 File system 3.33 Operating System Design File locking example /* Concurrent processes updating the same file */ #include #define SEQFILE “seqno” #define MAXBUFF 100 main() { int fd, i, n, pid, seqno; char buff[MAXBUFF+1]; pid =getpid(); if ((fd = open(SEQFILE, O_RDWR))<0) err_sys(“can’t open %s”, SEQFILE); /* Concurrent processes updating the same file */ #include #define SEQFILE “seqno” #define MAXBUFF 100 main() { int fd, i, n, pid, seqno; char buff[MAXBUFF+1]; pid =getpid(); if ((fd = open(SEQFILE, O_RDWR))<0) err_sys(“can’t open %s”, SEQFILE);

Laface 2007 File system 3.34 Operating System Design File locking example for (i=0; i<10; i++){ my_lock(fd); lseek(fd, 0L, 0); if ((n = read(fd, buff, MAXBUFF))<=0)err_sys(“read error”); buff[n]=‘\0’; if ((n = sscanf(buff, “%d\n”, &seqno)) !=1) err_sys(“sscanf error”); printf(“pid = %d, seq# = %d\n”, pid, seqno); seqno++; sprintf(buff, “%03d\n”, seqno); n = strlen(buff); lseek(fd, 0L, 0); if (write(fd, buff, n) !=n) err_sys(“write error”); my_unlock(fd); } for (i=0; i<10; i++){ my_lock(fd); lseek(fd, 0L, 0); if ((n = read(fd, buff, MAXBUFF))<=0)err_sys(“read error”); buff[n]=‘\0’; if ((n = sscanf(buff, “%d\n”, &seqno)) !=1) err_sys(“sscanf error”); printf(“pid = %d, seq# = %d\n”, pid, seqno); seqno++; sprintf(buff, “%03d\n”, seqno); n = strlen(buff); lseek(fd, 0L, 0); if (write(fd, buff, n) !=n) err_sys(“write error”); my_unlock(fd); }

Laface 2007 File system 3.35 Operating System Design No locking errors my_lock(fd) int fd; { return; } my_unlock(fd) int fd; { return; } my_lock(fd) int fd; { return; } my_unlock(fd) int fd; { return; } pid = 692, seq# = 0 pid = 692, seq# = 1 pid = 693, seq# = 0 pid = 692, seq# = 2 pid = 692, seq# = 3 pid = 693, seq# = 1 pid = 692, seq# = 4 pid = 692, seq# = 5 pid = 693, seq# = 2 pid = 692, seq# = 6 pid = 692, seq# = 7 pid = 693, seq# = 2 pid = 692, seq# = 6 pid = 692, seq# = 7 pid = 693, seq# = 3 pid = 693, seq# = 4 pid = 693, seq# = 5 pid = 693, seq# = 6 pid = 693, seq# = 7 pid = 693, seq# = 8

Laface 2007 File system 3.36 Operating System Design BSD file locking operations LOCK_SHread LOCK_EXwrite LOCK_UN unlock LOCK_NBno_blocking LOCK_SHread LOCK_EXwrite LOCK_UN unlock LOCK_NBno_blocking

Laface 2007 File system 3.37 Operating System Design BSD 4.3 solution ( flock ) /* BDS 4.3 */ #include my_lock(fd) int fd; { lseek(fd, 0L, 0); if (flock(fd, LOCK_EX) == -1) err_sys(“can’t LOCK_EX”); } my_unlock(fd) int fd; { if (flock(fd, LOCK_UN, 0L) == -1) err_sys(“can’t LOCK_UN”); } /* BDS 4.3 */ #include my_lock(fd) int fd; { lseek(fd, 0L, 0); if (flock(fd, LOCK_EX) == -1) err_sys(“can’t LOCK_EX”); } my_unlock(fd) int fd; { if (flock(fd, LOCK_UN, 0L) == -1) err_sys(“can’t LOCK_UN”); } pid = 1165, seq# = 0 pid = 1165, seq# = 1 pid = 1165, seq# = 2 pid = 1165, seq# = 3 pid = 1165, seq# = 4 pid = 1164, seq# = 5 pid = 1164, seq# = 6 pid = 1165, seq# = 7 pid = 1164, seq# = 8 pid = 1165, seq# = 9 pid = 1164, seq# = 10 pid = 1165, seq# = 11 pid = 1164, seq# = 12 pid = 1165, seq# = 13 pid = 1164, seq# = 14 pid = 1165, seq# = 15 pid = 1164, seq# = 16 pid = 1164, seq# = 17 pid = 1164, seq# = 18 pid = 1164, seq# = 19 pid = 1165, seq# = 0 pid = 1165, seq# = 1 pid = 1165, seq# = 2 pid = 1165, seq# = 3 pid = 1165, seq# = 4 pid = 1164, seq# = 5 pid = 1164, seq# = 6 pid = 1165, seq# = 7 pid = 1164, seq# = 8 pid = 1165, seq# = 9 pid = 1164, seq# = 10 pid = 1165, seq# = 11 pid = 1164, seq# = 12 pid = 1165, seq# = 13 pid = 1164, seq# = 14 pid = 1165, seq# = 15 pid = 1164, seq# = 16 pid = 1164, seq# = 17 pid = 1164, seq# = 18 pid = 1164, seq# = 19

Laface 2007 File system 3.38 Operating System Design Advisory locking int fcntl(int fd, int cmd, struct flock *lock) F_GETLK, F_SETLK and F_SETLKW are used to acquire, release, and test for the existence of record locks struct flock { short l_type; //Type of lock: F_RDLCK,F_WRLCK, F_UNLCK short l_whence; // SEEK_SET, SEEK_CUR, SEEK_END off_t l_start; // Starting offset for lock off_t l_len; // Number of bytes to lock pid_t l_pid; // PID of process blocking the lock ( F_GETLK only) }; Bytes past the end of the file may be locked, but not bytes before the start of the file. Specifying 0 for l_len has the special meaning: lock all bytes starting at the location specified by l_whence and l_start through to the end of file, no matter how large the file grows.

Laface 2007 File system 3.39 Operating System Design Record locking: F_SETLK, F_SETLKW F_SETLK, F_SETLKW –Acquire a lock (when l_type is F_RDLCK or F_WRLCK ) or release a lock (when l_type is F_UNLCK ).  If a conflicting lock is held by another process, this call returns -1 and sets errno to EACCES or EAGAIN  If a conflicting lock is held by another process, waits

Laface 2007 File system 3.40 Operating System Design Record locking: F_GETLK F_GETLK –On input to this call, lock describes a lock we would like to place on the file.  If the lock could be placed, fcntl does not actually place it, but returns F_UNLCK in the l_type field of lock and leaves the other fields of the structure unchanged.  If one or more incompatible locks would prevent this lock being placed, then fcntl returns details about one of these locks in the l_type, l_whence, l_start, and l_len fields of lock and sets l_pid to be the PID of the process holding that lock.

Laface 2007 File system 3.41 Operating System Design Record locking Record locks are automatically released when the process terminates or if it closes any file descriptor referring to a file on which locks are held. –A process can lose the locks when some other process decides to open, read and close it. Record locks are not inherited by a child created via fork, but are preserved across an execve. Because of the buffering performed by the stdio library, avoid the use of record locking with stdio functions ; use read and write.

Laface 2007 File system 3.42 Operating System Design POSIX file & record locking #include int lockf(int fd, int cmd, off_t len) lockf in Linux is just an interface to fcntl

Laface 2007 File system 3.43 Operating System Design Mandatory locking (Non-POSIX) Mandatory locks are enforced for all processes. If a process tries to perform an incompatible access on a file region that has an incompatible mandatory lock, then the result depends upon whether the O_NONBLOCK flag is enabled for its open file description. –If the O_NONBLOCK flag is not enabled, then system call is blocked until the lock is removed or converted to a mode that is compatible with the access. –If the O_NONBLOCK flag is enabled, then the system call fails with the error EAGAIN or EWOULDBLOCK. To make use of mandatory locks, mandatory locking must be enabled both on the file system that contains the file to be locked, and on the file itself. Mandatory locking is enabled on a file system using the "-o mand " option to mount, or the MS_MANDLOCK flag for system call mount. Mandatory locking is enabled on a file by disabling group execute permission on the file and enabling the set-group-ID permission bit (octal 02000)

Laface 2007 File system 3.44 Operating System Design Record locking examples flock [-h] [-s start] [-l len] [-w|-r] filename flock -h print this help -s start region starting byte -l len region length (0 means all file) -w write lock -r read lock -b block when locking impossible -f enable BSD semantic

Laface 2007 File system 3.45 Operating System Design Record locking examples flock -r flock.c flock -w flock.c flock -w -s0 -l10 flock.c flock -r -s0 -l10 flock.c flock -w -s5 -l15 flock.c flock -w -s11 -l15 flock.c flock -r -s10 -l20 flock.c

Laface 2007 File system 3.46 Operating System Design Blocking record locking flock -r -b -s0 -l10 flock.c flock -w -s0 -l10 flock.c Warning!! BDS and POSIX file locking structures are independent flock -r -b -s0 -l10 flock.c flock -f -w flock.c(BDS)

Laface 2007 File system 3.47 Operating System Design Use of link for locking #define LOCKFILE “seqno.lock” #include extern int errno; my_lock(int fd) { int tempfd; char tempfile[30]; sprintf(tempfile, “LCK%d”, getpid()); if ((tempfd = creat(tempfile, 0444))<0) err_sys(“can’t creat temp file”); close(tempfd); while (link(tempfile, LOCKFILE)<0){ if (errno != EEXIST) err_sys(“Link error”); sleep(1); } if (unlink(tempfile)<0)err_sys(“Unlink error for temp file”); } my_unlock( int fd) { if (unlink(LOCKFILE)<0) err_sys(“Unlink error for LOCKFILE”); } #define LOCKFILE “seqno.lock” #include extern int errno; my_lock(int fd) { int tempfd; char tempfile[30]; sprintf(tempfile, “LCK%d”, getpid()); if ((tempfd = creat(tempfile, 0444))<0) err_sys(“can’t creat temp file”); close(tempfd); while (link(tempfile, LOCKFILE)<0){ if (errno != EEXIST) err_sys(“Link error”); sleep(1); } if (unlink(tempfile)<0)err_sys(“Unlink error for temp file”); } my_unlock( int fd) { if (unlink(LOCKFILE)<0) err_sys(“Unlink error for LOCKFILE”); }

Laface 2007 File system 3.48 Operating System Design tmpfile and mktemp FILE *tmpfile(void); Opens a unique temporary file in binary read/write (w+b) mode. The file will be automatically deleted when it is closed or the program terminates. #include int mkstemp(char *template); Generates a unique temporary filename from template. The last six characters of template must be XXXXXX and these are replaced with a string that makes the filename unique. The file is then created with mode read/write and permissions Template must be declared as a character array. The file is opened with the open O_EXCL flag, this guarantees that the process is the only user

Laface 2007 File system 3.49 Operating System Design mktemp #include char template[] = "/tmp/fileXXXXXX"; int fd; fd = mkstemp(template);

Laface 2007 File system 3.50 Operating System Design system /* creates a directory */ #include #define MAXLINE 1024 main () { char line[MAXLINE], command[MAXLINE+10]; int n; FILE *fp; if (fgets(line, MAXLINE, stdin) == NULL) err_sys(“filename read error”); sprintf(command, “mkdir %s”, line); if (system(command) != 0) err_sys(“system error”); exit(0); } /* creates a directory */ #include #define MAXLINE 1024 main () { char line[MAXLINE], command[MAXLINE+10]; int n; FILE *fp; if (fgets(line, MAXLINE, stdin) == NULL) err_sys(“filename read error”); sprintf(command, “mkdir %s”, line); if (system(command) != 0) err_sys(“system error”); exit(0); }

Laface 2007 File system 3.51 Operating System Design popen #include #define MAXLINE 1024 main () { char line[MAXLINE], command[MAXLINE+10]; int n; FILE *fp; if (fgets(line, MAXLINE, stdin) == NULL) err_sys(”filename read error”); sprintf(command, ”cat %s”, line); if ((fp = popen(command, ”r”)) == NULL) err_sys(”popen error”); while ((fgets(line, MAXLINE, fp)) != NULL) { n = strlen(line); if (write(1, line, n) != n) err_sys(”data write error”); } if (ferror(fp)) err_sys(”fgets error”); pclose(fp); exit(0); } #include #define MAXLINE 1024 main () { char line[MAXLINE], command[MAXLINE+10]; int n; FILE *fp; if (fgets(line, MAXLINE, stdin) == NULL) err_sys(”filename read error”); sprintf(command, ”cat %s”, line); if ((fp = popen(command, ”r”)) == NULL) err_sys(”popen error”); while ((fgets(line, MAXLINE, fp)) != NULL) { n = strlen(line); if (write(1, line, n) != n) err_sys(”data write error”); } if (ferror(fp)) err_sys(”fgets error”); pclose(fp); exit(0); }