Module A: The FreeBSD System
Module A: The FreeBSD System
Operating System Concepts Essentials – 2nd Edition Silberschatz, Galvin and Gagne ©2013
Module A: The FreeBSD System
UNIX History
Design Principles
Programmer Interface
User Interface
Process Management
Memory Management
File System
I/O System
Interprocess Communication
Operating System Concepts Essentials – 2nd Edition a.2 Silberschatz, Galvin and Gagne ©2013
UNIX History
First developed in 1969 by Ken Thompson and Dennis Ritchie of the
Research Group at Bell Laboratories; incorporated features of other
operating systems, especially MULTICS
The third version was written in C, which was developed at Bell Labs
specifically to support UNIX
The most influential of the non-Bell Labs and non-AT&T UNIX
development groups — University of California at Berkeley (Berkeley
Software Distributions - BSD)
4BSD UNIX resulted from DARPA funding to develop a standard
UNIX system for government use
Developed for the VAX, 4.3BSD is one of the most influential
versions, and has been ported to many other platforms
Several standardization projects seek to consolidate the variant
flavors of UNIX leading to one programming interface to UNIX
Operating System Concepts Essentials – 2nd Edition a.3 Silberschatz, Galvin and Gagne ©2013
History of UNIX Versions
Operating System Concepts Essentials – 2nd Edition a.4 Silberschatz, Galvin and Gagne ©2013
Early Advantages of UNIX
Written in a high-level language
Distributed in source form
Provided powerful operating-system primitives on an inexpensive
platform
Small size, modular, clean design
Operating System Concepts Essentials – 2nd Edition a.5 Silberschatz, Galvin and Gagne ©2013
UNIX Design Principles
Designed to be a time-sharing system
Has a simple standard user interface (shell) that can be replaced
File system with multilevel tree-structured directories
Files are supported by the kernel as unstructured sequences of bytes
Supports multiple processes; a process can easily create new
processes
High priority given to making system interactive, and providing facilities
for program development
Operating System Concepts Essentials – 2nd Edition a.6 Silberschatz, Galvin and Gagne ©2013
Programmer Interface
Operating System Concepts Essentials – 2nd Edition a.7 Silberschatz, Galvin and Gagne ©2013
4.4BSD Layer Structure
Operating System Concepts Essentials – 2nd Edition a.8 Silberschatz, Galvin and Gagne ©2013
System Calls
System calls define the programmer interface to UNIX
The set of systems programs commonly available defines the user
interface
The programmer and user interface define the context that the kernel
must support
Roughly three categories of system calls in UNIX
File manipulation (same system calls also support device
manipulation)
Process control
Information manipulation
Operating System Concepts Essentials – 2nd Edition a.9 Silberschatz, Galvin and Gagne ©2013
File Manipulation
A file is a sequence of bytes; the kernel does not impose a structure
on files
Files are organized in tree-structured directories
Directories are files that contain information on how to find other files
Path name: identifies a file by specifying a path through the directory
structure to the file
Absolute path names start at root of file system
Relative path names start at the current directory
System calls for basic file manipulation: create, open, read,
write, close, unlink, trunc
Operating System Concepts Essentials – 2nd Edition a.10 Silberschatz, Galvin and Gagne ©2013
Typical UNIX Directory Structure
Operating System Concepts Essentials – 2nd Edition a.11 Silberschatz, Galvin and Gagne ©2013
Process Control
A process is a program in execution.
Processes are identified by their process identifier, an integer
Process control system calls
fork creates a new process
execve is used after a fork to replace on of the two processes’s
virtual memory space with a new program
exit terminates a process
A parent may wait for a child process to terminate; wait
provides the process id of a terminated child so that the parent
can tell which child terminated
wait3 allows the parent to collect performance statistics about
the child
A zombie process results when the parent of a defunct child process
exits before the terminated child.
Operating System Concepts Essentials – 2nd Edition a.12 Silberschatz, Galvin and Gagne ©2013
Illustration of Process Control Calls
Operating System Concepts Essentials – 2nd Edition a.13 Silberschatz, Galvin and Gagne ©2013
Process Control (Cont.)
Processes communicate via pipes; queues of bytes between two
processes that are accessed by a file descriptor
Operating System Concepts Essentials – 2nd Edition a.14 Silberschatz, Galvin and Gagne ©2013
Process Control (Cont.)
setuid bit sets the effective user identifier of the process to the user
identifier of the owner of the file, and leaves the real user identifier as
it was
Operating System Concepts Essentials – 2nd Edition a.15 Silberschatz, Galvin and Gagne ©2013
Signals
Facility for handling exceptional conditions similar to software
interrupts
Operating System Concepts Essentials – 2nd Edition a.16 Silberschatz, Galvin and Gagne ©2013
Process Groups
Set of related processes that cooperate to accomplish a common task
Only one process group may use a terminal device for I/O at any time
The foreground job has the attention of the user on the terminal
Background jobs – nonattached jobs that perform their function
without user interaction
Operating System Concepts Essentials – 2nd Edition a.17 Silberschatz, Galvin and Gagne ©2013
Process Groups (Cont.)
Each job inherits a controlling terminal from its parent
If the process group of the controlling terminal matches the group
of a process, that process is in the foreground
SIGTTIN or SIGTTOU freezes a background process that attempts
to perform I/O; if the user foregrounds that process, SIGCONT
indicates that the process can now perform I/O
SIGSTOP freezes a foreground process
Operating System Concepts Essentials – 2nd Edition a.18 Silberschatz, Galvin and Gagne ©2013
Information Manipulation
System calls to set and return an interval timer:
getitmer/setitmer
Operating System Concepts Essentials – 2nd Edition a.19 Silberschatz, Galvin and Gagne ©2013
Library Routines
The system-call interface to UNIX is supported and augmented by a
large collection of library routines
Operating System Concepts Essentials – 2nd Edition a.20 Silberschatz, Galvin and Gagne ©2013
User Interface
Programmers and users mainly deal with already existing systems
programs: the needed system calls are embedded within the program
and do not need to be obvious to the user.
Operating System Concepts Essentials – 2nd Edition a.21 Silberschatz, Galvin and Gagne ©2013
Shells and Commands
Shell – the user process which executes programs (also called
command interpreter)
The shell travels through the search path to find the command file,
which is then loaded and executed
The directories /bin and /usr/bin are almost always in the search
path
Operating System Concepts Essentials – 2nd Edition a.22 Silberschatz, Galvin and Gagne ©2013
Shells and Commands (Cont.)
Typical search path on a BSD system:
The shell usually suspends its own execution until the command
completes
Operating System Concepts Essentials – 2nd Edition a.23 Silberschatz, Galvin and Gagne ©2013
Standard I/O
Most processes expect three file descriptors to be open when they
start:
standard input – program can read what the user types
standard output – program can send output to user’s screen
standard error – error output
Most programs can also accept a file (rather than a terminal) for
standard input and standard output
The common shells have a simple syntax for changing what files are
open for the standard I/O streams of a process — I/O redirection
Operating System Concepts Essentials – 2nd Edition a.24 Silberschatz, Galvin and Gagne ©2013
Standard I/O Redirection
Operating System Concepts Essentials – 2nd Edition a.25 Silberschatz, Galvin and Gagne ©2013
Pipelines, Filters, and Shell Scripts
Can coalesce individual commands via a vertical bar that tells the shell
to pass the previous command’s output as input to the following
command
% ls | pr | lpr
Writing a new shell with a different syntax and semantics would change
the user view, but not change the kernel or programmer interface
Operating System Concepts Essentials – 2nd Edition a.26 Silberschatz, Galvin and Gagne ©2013
Process Management
Representation of processes is a major design problem for operating
system
Operating System Concepts Essentials – 2nd Edition a.27 Silberschatz, Galvin and Gagne ©2013
Process Control Blocks
The most basic data structure associated with processes is the
process structure
unique process identifier
scheduling information (e.g., priority)
pointers to other control blocks
Every process with sharable text has a pointer form its process
structure to a text structure
always resident in main memory
records how many processes are using the text segment
records were the page table for the text segment can be found
on disk when it is swapped
Operating System Concepts Essentials – 2nd Edition a.28 Silberschatz, Galvin and Gagne ©2013
System Data Segment
Most ordinary work is done in user mode; system calls are performed
in system mode
A kernel stack (rather than the user stack) is used for a process
executing in system mode
The kernel stack and the user structure together compose the system
data segment for the process
Operating System Concepts Essentials – 2nd Edition a.29 Silberschatz, Galvin and Gagne ©2013
Finding parts of a process using
process structure
Operating System Concepts Essentials – 2nd Edition a.30 Silberschatz, Galvin and Gagne ©2013
Allocating a New Process Structure
Fork allocates a new process structure for the child process, and
copies the user structure
new page table is constructed
new main memory is allocated for the data and stack segments of
the child process
copying the user structure preserves open file descriptors, user
and group identifiers, signal handling, etc.
Operating System Concepts Essentials – 2nd Edition a.31 Silberschatz, Galvin and Gagne ©2013
Allocating a New Process Structure (Cont.)
vfork does not copy the data and stack to t he new process; the new
process simply shares the page table of the old one
new user structure and a new process structure are still created
commonly used by a shell to execute a command and to wait for
its completion
Using vfork with a large parent process saves CPU time, but can be
dangerous since any memory change occurs in both processes until
execve occurs
execve creates no new process or user structure; rather the text and
data of the process are replaced
Operating System Concepts Essentials – 2nd Edition a.32 Silberschatz, Galvin and Gagne ©2013
CPU Scheduling
Every process has a scheduling priority associated with it; larger
numbers indicate lower priority
When that event occurs, the system process that knows about it calls
wakeup with the address corresponding to the event, and all
processes that had done a sleep on the same address are put in the
ready queue to be run
Operating System Concepts Essentials – 2nd Edition a.33 Silberschatz, Galvin and Gagne ©2013
Memory Management
The initial memory management schemes were constrained in size by
the relatively small memory resources of the PDP machines on which
UNIX was developed.
Operating System Concepts Essentials – 2nd Edition a.34 Silberschatz, Galvin and Gagne ©2013
Memory Management (Cont.)
Sharable text segments do not need to be swapped; results in less
swap traffic and reduces the amount of main memory required for
multiple processes using the same text segment.
Operating System Concepts Essentials – 2nd Edition a.35 Silberschatz, Galvin and Gagne ©2013
Paging
Berkeley UNIX systems depend primarily on paging for memory-
contention management, and depend only secondarily on swapping.
Demand paging – When a process needs a page and the page is not
there, a page fault tot he kernel occurs, a frame of main memory is
allocated, and the proper disk page is read into the frame.
Operating System Concepts Essentials – 2nd Edition a.36 Silberschatz, Galvin and Gagne ©2013
File System
The UNIX file system supports two main objects: files and directories.
Operating System Concepts Essentials – 2nd Edition a.37 Silberschatz, Galvin and Gagne ©2013
Blocks and Fragments
Most of the file system is taken up by data blocks
4.2BSD uses two block sized for files which have no indirect blocks:
All the blocks of a file are of a large block size (such as 8K), except
the last
The last block is an appropriate multiple of a smaller fragment size
(i.e., 1024) to fill out the file
Thus, a file of size 18,000 bytes would have two 8K blocks and
one 2K fragment (which would not be filled completely)
Operating System Concepts Essentials – 2nd Edition a.38 Silberschatz, Galvin and Gagne ©2013
Blocks and Fragments (Cont.)
The block and fragment sizes are set during file-system creation
according to the intended use of the file system:
If many small files are expected, the fragment size should be small
If repeated transfers of large files are expected, the basic block
size should be large
Operating System Concepts Essentials – 2nd Edition a.39 Silberschatz, Galvin and Gagne ©2013
Inodes
A file is represented by an inode — a record that stores information
about a specific file on the disk
The inode also contains 15 pointer to the disk blocks containing the
file’s data contents
First 12 point to direct blocks
Next three point to indirect blocks
First indirect block pointer is the address of a single indirect
block — an index block containing the addresses of blocks that
do contain data
Second is a double-indirect-block pointer, the address of a
block that contains the addresses of blocks that contain pointer
to the actual data blocks.
A triple indirect pointer is not needed; files with as many as
232 bytes will use only double indirection
Operating System Concepts Essentials – 2nd Edition a.40 Silberschatz, Galvin and Gagne ©2013
Directories
The inode type field distinguishes between plain files and directories
Directory entries are of variable length; each entry contains first the
length of the entry, then the file name and the inode number
The user refers to a file by a path name,whereas the file system uses
the inode as its definition of a file
The kernel has to map the supplied user path name to an inode
Directories are used for this mapping
Operating System Concepts Essentials – 2nd Edition a.41 Silberschatz, Galvin and Gagne ©2013
Directories (Cont.)
First determine the starting directory:
If the first character is “/”, the starting directory is the root directory
For any other starting character, the starting directory is the current
directory
The search process continues until the end of the path name is
reached and the desired inode is returned
Operating System Concepts Essentials – 2nd Edition a.42 Silberschatz, Galvin and Gagne ©2013
Mapping of a File Descriptor to an Inode
System calls that refer to open files indicate the file is passing a file
descriptor as an argument
The file descriptor is used by the kernel to index a table of open files
for the current process
Since the open file table has a fixed length which is only setable at
boot time, there is a fixed limit on the number of concurrently open files
in a system
Operating System Concepts Essentials – 2nd Edition a.43 Silberschatz, Galvin and Gagne ©2013
File-System Control Blocks
Operating System Concepts Essentials – 2nd Edition a.44 Silberschatz, Galvin and Gagne ©2013
Disk Structures
The one file system that a user ordinarily sees may actually consist of
several physical file systems, each on a different device
Operating System Concepts Essentials – 2nd Edition a.45 Silberschatz, Galvin and Gagne ©2013
Disk Structures (Cont.)
The root file system is always available on a drive
Operating System Concepts Essentials – 2nd Edition a.46 Silberschatz, Galvin and Gagne ©2013
Mapping File System to Physical Devices
Operating System Concepts Essentials – 2nd Edition a.47 Silberschatz, Galvin and Gagne ©2013
Implementations
The user interface to the file system is simple and well defined,
allowing the implementation of the file system itself to be changed
without significant effect on the user
For Version 7, the size of inodes doubled, the maximum file and file
system sized increased, and the details of free-list handling and
superblock information changed
In 4.0BSD, the size of blocks used in the file system was increased
form 512 bytes to 1024 bytes — increased internal fragmentation, but
doubled throughput
4.2BSD added the Berkeley Fast File System, which increased speed,
and included new features
New directory system calls
truncate calls
Fast File System found in most implementations of UNIX
Operating System Concepts Essentials – 2nd Edition a.48 Silberschatz, Galvin and Gagne ©2013
Layout and Allocation Policy
The kernel uses a <logical device number, inode number> pair to
identify a file
The logical device number defines the file system involved
The inodes in the file system are numbered in sequence
Operating System Concepts Essentials – 2nd Edition a.49 Silberschatz, Galvin and Gagne ©2013
4.3BSD Cylinder Group
Operating System Concepts Essentials – 2nd Edition a.50 Silberschatz, Galvin and Gagne ©2013
I/O System
The I/O system hides the peculiarities of I/O devices from the bulk of
the kernel
Operating System Concepts Essentials – 2nd Edition a.51 Silberschatz, Galvin and Gagne ©2013
4.3 BSD Kernel I/O Structure
Operating System Concepts Essentials – 2nd Edition a.52 Silberschatz, Galvin and Gagne ©2013
Block Buffer Cache
Consist of buffer headers, each of which can point to a piece of
physical memory, as well as to a device number and a block number
on the device.
The buffer headers for blocks not currently in use are kept in several
linked lists:
Buffers recently used, linked in LRU order (LRU list)
Buffers not recently used, or without valid contents (AGE list)
EMPTY buffers with no associated physical memory
If it is not found, a buffer is chosen from the AGE list, or the LRU list if
AGE is empty.
Operating System Concepts Essentials – 2nd Edition a.53 Silberschatz, Galvin and Gagne ©2013
Block Buffer Cache (Cont.)
Buffer cache size effects system performance; if it is large enough,
the percentage of cache hits can be high and the number of actual I/O
transfers low.
Data written to a disk file are buffered in the cache, and the disk driver
sorts its output queue according to disk address — these actions allow
the disk driver to minimize disk head seeks and to write data at times
optimized for disk rotation.
Operating System Concepts Essentials – 2nd Edition a.54 Silberschatz, Galvin and Gagne ©2013
Raw Device Interfaces
Almost every block device has a character interface, or raw device
interface — unlike the block interface, it bypasses the block buffer
cache.
Operating System Concepts Essentials – 2nd Edition a.55 Silberschatz, Galvin and Gagne ©2013
C-Lists
Terminal drivers use a character buffering system which involves
keeping small blocks of characters in linked lists.
Operating System Concepts Essentials – 2nd Edition a.56 Silberschatz, Galvin and Gagne ©2013
Interprocess Communication
The pipe is the IPC mechanism most characteristic of UNIX
Permits a reliable unidirectional byte stream between two
processes
A benefit of pipes small size is that pipe data are seldom written to
disk; they usually are kept in memory by the normal block buffer
cache
Operating System Concepts Essentials – 2nd Edition a.57 Silberschatz, Galvin and Gagne ©2013
Sockets
A socket is an endpont of communication.
Operating System Concepts Essentials – 2nd Edition a.58 Silberschatz, Galvin and Gagne ©2013
Socket Types
Stream sockets provide reliable, duplex, sequenced data streams.
Supported in Internet domain by the TCP protocol. In UNIX domain,
pipes are implemented as a pair of communicating stream sockets.
Sequenced packet sockets provide similar data streams, except that
record boundaries are provided
Used in XEROX AF_NS protocol
Datagram sockets transfer messages of variable size in either
direction. Supported in Internet domain by UDP protocol.
Reliably delivered message sockets transfer messages that are
guaranteed to arrive (Currently unsupported).
Raw sockets allow direct access by processes to the protocols that
support the other socket types; e.g., in the Internet domain, it is
possible to reach TCP, IP beneath that, or a deeper Ethernet protocol
Useful for developing new protocols
Operating System Concepts Essentials – 2nd Edition a.59 Silberschatz, Galvin and Gagne ©2013
Socket System Calls
The socket call creates a socket; takes as arguments specifications
of the communication domain, socket type, and protocol to be used
and returns a small integer called a socket descriptor.
Operating System Concepts Essentials – 2nd Edition a.60 Silberschatz, Galvin and Gagne ©2013
Socket System Calls (Cont.)
The simplest way to terminate a connection and to destroy the
associated socket is to use the close system call on its socket
descriptor.
Operating System Concepts Essentials – 2nd Edition a.61 Silberschatz, Galvin and Gagne ©2013
Network Support
Networking support is one of the most important features in 4.3BSD.
4.3BSD supports the DARPA Internet protocols UDP, TCP, IP, and
ICMP on a wide range of Ethernet, token-ring, and ARPANET
interfaces.
Operating System Concepts Essentials – 2nd Edition a.62 Silberschatz, Galvin and Gagne ©2013
Network Reference models and Layering
Operating System Concepts Essentials – 2nd Edition a.63 Silberschatz, Galvin and Gagne ©2013
End of Appendix A
Operating System Concepts Essentials – 2nd Edition Silberschatz, Galvin and Gagne ©2013