May 15th Lecture ================= FreeBSD I/O: provides interface to devices, both for user space programs and other parts of the kernel * filesystem interface * character device interface * socket interface filesystems -- structured, managed interface to I/O, especially storage devices * hierarchical namespace organization * extra features such as locking, quotas, permissions, etc. character devices -- unstructured access to devices * actual character devices (e.g., terminals, mice, keyboards, etc.) or "raw" access to block devices (e.g., disks) * does not use buffer cache * appear in the filesystem namespace ECS 30: fopen, fprintf, fgets, FILE* * library calls implemented on top of system calls open() read() write() lseek() * system calls * based on file descriptors file descriptor is an integer * index into open file table for current process * file entry contains type of I/O (file, device, socket), a pointer to the underlying object data (vnode, socket, etc.), an dfunction pointers to various operations Creation: * vnodes: open() * sockets: socket() * fifos: pipe() 0 = stdin 1 = stdout 2 = stderr Some semantics enforced at general FS interface level before type-specific functions are called * read/write mode * locking dup(), dup2() can be used to duplicate file descriptors: * dup() -- fills first hole in FD table * dup2() -- fills specific slot, closes descriptor already there Locking * early unix didn't have locking; used file creation (atomic) to simulate it - procs have to loop to test lockfile - stale locks have to be cleaned by hand - root automatically overrides "create only" restriction * real locks added in BSD 4.2 While file locking * serializes access * inherited by children * auto-released when last descriptor closed * BSD 4.2 & 4.3 used this Byte-Range Locks * locks piece of file (as small as 1 byte) * not powerful enough to do nested hierarchical locks required by DB's * complex implementation * mandated by POSIX standard, added to BSD 4.4 * released on each close() Advisor vs mandatory: * only enforced on procs that request / pay attention to them * e.g., flock() Shared vs Exclusive, reader vs writers BSD 4.4 and beyond: * both whole file and byte-range locking options (whole file = special case of byte range) * advisory locks * can request shared / exclusive * may request while opening a file * may request error rather than block on failure to acquire