Documentos de Académico
Documentos de Profesional
Documentos de Cultura
1.0 INTRODUCTION
In block 2 we discussed several theoretical concepts of operating systcm in general, it is
often useful to see them in practice. In this block we h v e presented in-depth study of UNIX
o p a t i n g system as an example of the various concepts presented in block 2. In this unit we
have taken up issues related to how file management, process management, memory
management is done in UNIX. Whereas rest of the units discuss about UNIX commands, its
shell programming editors, system administration etc.
1.1 OBJECTIVES
After going through this unit you will be able to:
List the basic featurcs of UNIX operating systcm
Describe UNIX file structure
Discuss CPU scheduling in UNIX system
Discuss memory management schemes in UNIX
Discuss file systems in UNIX operating system
I
Kernel intertfse to Hardware ;
Everything below the system call interface and above the physical hardware is the Kernel.
The Kernel provides the file system. CPU Scheduling. memory management and other
operating system functions through system calls.
Programs such as shell (Sh) and editors (vi) shown in the top layer interact with the Kernel
by invoking a well defined set of system calls. The system calls instruct the Kernel to do
various opxations for the calling programs and exchange data between the Kernel and the
program. i
System calls for UNIX can be roughly grouped into three categories: file manipulation.
process cqntrol and information manipulation. Another category can be considered for
device mzjnipulation, but, since devices in UNIX are treated as (special) files, the same
system calls support both files and devices.
Root
is organised as a Uee with a single root node called root (written "f'); every non-leaf-node of
the file system structure is a directory of files, and files atthe leaf nodes of the tree am either
directories or regular files or special device files. Idev contains device files, such as
/dev/console, /dev/lpO, IdevImtO and so on; /bin contains the binaries of essential UNIX
system programs.
Create, open, read, write, close, uplink and frunc are system calls which are used for basic
file manipulation. The create system call, given a pathname, creates a (empty) file (or
truncates and existing one). An existing file is opened by the open system call, which takes a
path name and a node (such as read, write or read-write) and returns a small descriptor which
may then be passed to a read or write system call (along with a buifer address and a number
'of bytes to transfer) to perform d a b transfer to or from the file.
A file descriptor is an index into a small table of open files for this process. Descriptors start
at 0 and seldom get higher than 6 or 7 for typical programs, depending on the maximum
number of simultaneously open files.
Each read or write updates the current offset into the file. which is associated with file table
entry and is used to determine the position in the file for the next read cr write.
Processing Environment
A process is a program in execution. Many processes can execute simultaneously on UNIX
System (this feature is sometimes called multiprogramming or multikking) with no logical
unit to their number, and many insmces of a program (such as copy) can exist
simultaneously in the system. Varioys system calls allow processes to create new processes,
terminate processes, synchronize stages of process execution, and communicate with the rest
of the world. For example, in UNIX new processes are created by the fork system call.
Every process except process 0 is created when another process executes the fork system
call. The process that invoked the fork system is the parent process and the newly created
UNIX Operating Syskm-I process is the cHild process. Every process has one parent process. but a process can have
many child processes. The Kernel identifies each process by its process number, called the
process ID (PID). Process 0 is a special process that is created when the System boots; after
forking a child process (process 1). process 0 becomes the swapper process. hocess 1.
known as init, ig the ancestor of every process in the system.
The CPU scheduling is strongly influenced by memory management schemes. At least part
of a process must be contained in primary memory to run; a process cannot be executed by a
CPU if it is existing entirely in main memory. It is not also possible to contain all active
processes in the main memory. For example 4MB main memory will not be able to pmvide
space for 5MB process. It is the job of memory management module to decide which process
should reside (at least partially) in main memory, and manage the parts of the virtual address
of a process which are residing on secondary storage devices. It monitors the amount of
physical memory and provide swapping of processes between physical memory and
secondary storage devices.
1.5.1 Swapping
The early deveIopment of UMX systems transferred entire processes between primary
memory and secondary storage device but did not transfer parts of a process independently,
except for shared text. Such a memory management policy is called swapping. UNIX was
fist implemented on PDP-11, where the total physical memory was limited to 256Kbytes.
The total memory resources were insufficient to justify or support complex memory
management algorithms. Thus, UNIX swapped entire process memory images.
Allocation of both main memory and swap space is done first- fit. When the size of a
process' memory image increases (due to either stack expansion or data expansion), a new
piece of memory .big enough for the whole image is allocated. The memory image is copied,
the old memory is freed, and the appropriate tables are updated. (An attempt is made in some
systems to find memory contiguous to the end of the current piece, to avoid some copying.)
If no single piece of main memory is large enough, the process is swapped out such that it
will be swapped back in with the new size.
There is no need to swap out a sharable text segment, because it is read-only, and there is no
need to read in a sharable text segment for a process when another instance is already in
memory. That is one of the main reasons for keeping track of sharable text segments: less
swap traffic. The other reason is the reduced amount of main memory required for multiple
processes using the same text segment.
Decisions regarding which processes to swap in or swap out are made by the scheduler
process (also known as the swapper). The scheduler wakes up at lcast once every 4
seconds to check for processes to be swapped in or out. A process is more likely to be
swapped out if it is idle or has been in main memory for a long time, or is large; if no
obvious candidates are found, other processes are picked by age. A process is more likely
to be swapped in if its has been swapped out a long time, or is small. There are checks to
prevent thrashing, basically by not letting a process be swapped out if it's not been in
memory for a certain amount of time.
If jobs do not need to be swapped out, the process table is searched for a process deserving to
be brought in (determined by how small the process is and how long it has been swapped
out). Processes are swapped out until there is not enough memory available.
Many UNIX systems still use the swapping scheme just described. All Berkeley UNIX
systems, on the other hand, depend primarily on paging for memory-contention management,
. and depend only secondarily on swapping. A scheme similar in outline to the traditional one
is used to determine which processes get swapped in or out, but the details differ and the
1 influence of swapping is less.
!
Q
i 1.5.2 Demand Paging
i
Berkeley introduced demand paging to UNIX with BSD (Berkeley System) which
transferred memory pages instead of processes to and from a secondary device; recent
releases of UNIX system also support demand paging. Demand paging is done in a
straightforward manner. When a process needs a page and the page is not there, a page fault
to the kernel occurs, a frame of main memdry is allocated, and then the process is loaded into
the frame by the kernel.
The advantage of demand paging policy is that it permits greater flexibility in mapping the
virtual address of a process into the physical memory of a machine, usually allowing the size
of a process to be greater than the amount of availability of physical memory and allowing
J
UNIX Operating system-I more processes to fit into main memory. The advantage of a swapping policy is that it is
easier to implement and results in less system overhead.
The 4.2BSD solution is to use two block sizes 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 (for example, 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).
The block ar)d 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 trarisfers of large files are expected, the basic block size should be,largc
Implementation details force a maximum block-to-fragment ratio of 8 9 , and a minimum
block size of4K, so typical choices are 4096 1 512 for the former case and 8192 : 1024 for
the latter.
Suppose data are written to a file in transfer sizes of 1K bytes, and the block and fragment
sizes of the flilesystem are 4K and 512 bytes. The file system will allocate a 1K fragment to
contain the data from the first transfer. The next transfer will cause a new 2K fragment to be
allocated. The data from the original fragment must be copied into this new fragment,
followed bythe second 1K transfer. The allocation routines do attempt to find the required
space on the disk immediately following the existing ferment so that no copying is necessary,
but, if they cannot do so, up to seven copies may be required before the fragment becomes a
block. Provisions have been made for programs to discover the block size for a file so that
transfers of that size can be made, to avoid fragment recopying.
Associated with each file in UNIX is a little table (on disk) called an i-node. An inode is a
record that describes the attributes of a file, including the lay out of itj data on disk. Inodes
exist in a sqtic form on disk and the kernel read them into the main memory and manipulates
them. Disk inodes consist of the following fields:
FilC owner identifier - File ownership is divided between an individual owner and a
grolup owner and defines the set of users who have access rightj to a file. There su-
pervisor has access rights to all files in the system.
File type - Files may be of type regular, directory, character or block special or
pipes.
File access permission - The system protectj files according to three classes: the
owher and the group owner of the file and other users; each class has access rights
to dead, write and execute the file which can be set individually. Although directory
is s file but it cannot be executed, execution permission for a directory gives the
right to search the directory, for a file name.
- File access times - Giving the time the file was last modified, when it was last ac-
cessed.
In addition, Chc inodc contains 15 pointers to the disk blocks containing the data contenls of
the file. The fist 12 of these pointers (as shown in figure 3) point to direct blocks; that is,
they contaiq addresses ol' blocks that contain data of
Theoretical Concepts o f
Unix Operntlng System
0
0
0
#
#.
% Er-73-
pointer to
the file. Thus, the data for small files (no more than 12 blocks) can be referenced
immediately, because a copy of the inode is kept in main mem.ory while a file is open. If the
block size is 4K, then up to 48K of data may be accessed directly from the inode.
The next three pointers in the inode point to indirect blocks. If the file is large enough to use
indirect blocks, the indirect blocks are each of the major block size; the fragment size applies
to only data blocks. The first indirect block pointer is the address of a single indirect block.
The single indirect block is an index block, containing not data, but rather the addresses of
blocks that do contain data. Then, there is a double-indirect-block pointer, the address of a
block that contains the addresses of blocks that contain pointers to the actual data blocks.
The last pointer would contain the address of a triple indirect Mock; however, there is no
need for it. The minimum block size for a file system in 4.2BSD is 4K, so files wit as many
as 232bytes will use only double, not triple, indirection. That is, as each block pointer takes 4
bytes, we have 49,152 (4K x 12) bytes accessible in direct blocks, 4,194,304 bytes
accessible by a single indirection, and 4,294,967,296 bytes reachable through double
indirection, for a total of 4,299,210,752 bytes, which is larger than 23? bytes. The number 232
is significant becau_sethe file offset in the file structure in main memory is kept in a 32-bit
word. Files therefore cannot be larger than 232bytes. Since file pointers are si ned integers
(for seeking backward and forward in a file), the actual maximum file size is 252-1 bytes.
Two gigabytes is large enough for most purposes.
Bytes R 3 2 2 2 4
Attributes
The directory structure used in UNIX is extremely simple, as shown in figure 5. Each entry
contains just a file name and its i-node number. All the information about the type. size.
times, ownership, and disk blocks is contained in the i- node (see figure 3). All directcries in
UNIX are files. and may contain arbitrarily many of these entries.
Bytes 2 14
File name
i-node
number
When a fib is opened, the file system must take the file name supplied and locate its disk
block.. Let us consider how the path name /usr/ast/mbox is looked up. We will use UMX as
an example. but the algorithm is basically the same for all hierarchical directory systems.
F i t the ffle system locates the m t directory. In UNIX its i-node is located at a fixed place
on the disk.
Then it lwks up the fmt component of the path, u&, in the root directory to find the i-node
of the file /us. From this i-node, the system locates the directory for /usr and looks up the
next component, ast. in it. when it has found the entry for ast. it has the i-node for directory
/usr/ast. From this i-node it can find the directory itself and look up mbox. The i- node for
this file islthen read into memory and kept there until the file is closed. The lookup process is
illustrated in figure 6.
size size
times times
Relative path names are looked up the same way as absolute ones, only starting from the
working directory instead of starting from the mot directorj. Every dirsctorj has entries for .
and ..which are put there when the directory is created. The entry . has the i-node number for
the current directory, and the entry for ..has the i-node number for h e parent directory, and
searches that directory for disk. NO special mechanism is needed to handle these names. As Theoretical Conccpts o f
Unix Operating Systcm
rar as the directory system is concerned, they are just ordinary ASCII strings.
1.7 SUMMARY
In this unit we discussed issues broadly related to CPU scheduling, memory management
schemes and file systems of UNIX operdting system. We did not go into implementation
details of these schemes as we11 as system calls in detail. Students are strongly advised to
refer to a book " The Design of UNIX Operating System " by Maurice J. Back. for detailed
discussion. The main points covered in this unit are:
UNIX provides a good programirling environment. ILsupports features Lhat allow
complex programs to be built from simpler programs.
The UNIX file is simply a seqlience of bytes without my me=ing imposed on it. Zts
meaning is mainly dependent upon programs that interpret it.
Kernel allocates the CPU w a process for small fraction of time, preempts the
process that exceeds its rime slice and feed it back into one of several priority
queues. G -12substantial difference between UNIX and many other systems is the
ease with ;-.,I~ichmultiple processes can be created and manipulated.
Memory management in UNIX is swapping supported by paging.
The b N X file system supports two inail1 objqcts: files and directories. Directories
arc just filzs with a special b r ~ n aso
t the representative of a file is the basic UNlX
concept.
I..