Cse-V-Operating Systems (10CS53) - Notes PDF
Cse-V-Operating Systems (10CS53) - Notes PDF
Cse-V-Operating Systems (10CS53) - Notes PDF
10CS53
Operating Systems
I.A. Marks : 25
Exam Hours: 03
Exam Marks: 100
PART -A
UNIT -1 INTRODUCTION TO OPERATING SYSTEMS, SYSTEM STRUCTURES: What operating
systems do; Computer System organization; Computer System architecture; Operating System structure;
Operating System operations; Process management; Memory management; Storage management; Protection
and security; Distributed system; Special-purpose systems; Computing environments. Operating System
Services; User -Operating System interface; System calls; Types of system calls; System programs; Operating
System design and implementation; Operating System structure; Virtual machines; Operating System
generation; System boot.
6 Hours
UNIT -2 Process Management: Process concept; Process scheduling; Operations on processes; Inter-process
communication. Multi-Threaded Programming: Overview; Multithreading models; Thread Libraries;
Threading issues. Process Scheduling: Basic concepts; Scheduling criteria; Scheduling algorithms; MultipleProcessor scheduling; Thread scheduling.7 Hours
UNIT -3 PROCESS SYNCHRONIZATION: Synchronization: The Critical section problem; Petersons
solution; Synchronization hardware; Semaphores; Classical problems of synchronization; Monitors.
7 Hours
UNIT -4 DEADLOCKS: Deadlocks: System model; Deadlock characterization; Methods for handling deadlocks;
Deadlock prevention; Deadlock avoidance; Deadlock detection and recovery from deadlock.
6 Hours
Operating Systems
10CS53
PART -B
UNIT -5
MEMORY MANAGEMENT: Memory Management Strategies: Background; Swapping; Contiguous
memory allocation; Paging; Structure of page table; Segmentation. Virtual Memory Management:
Background; Demand paging; Copy-on-write; Page replacement; Allocation of frames; Thrashing.
7 Hours
UNIT -6 FILE SYSTEM, IMPLEMENTATION OF FILE SYSTEM: File System: File
concept; Access methods; Directory structure; File system mounting; File sharing; Protection.
Implementing File System: File system structure; File system implementation; Directory
implementation; Allocation methods; Free space management.
7 Hours
UNIT-7 SECONDARY STORAGE STRUCTURES, PROTECTION: Mass storage structures; Disk
structure; Disk attachment; Disk scheduling; Disk management; Swap space management. Protection: Goals of
protection, Principles of protection, Domain of protection, Access matrix, Implementation of access
6 Hours
UNIT -8 CASE STUDY: THE LINUX OPERATING SYSTEM: Linux history; Design principles; Kernel
modules; Process management; Scheduling; Memory management; File systems, Input and output; Inter-process
6Hours
communication.
TEXT BOOK:
1. Operating System Principles Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, 8th edition, WileyIndia, 2009
REFERENCE BOOKS:
nd
1. Operating Systems: A Concept Based Approach D.M Dhamdhere, 2 Edition, Tata McGraw-Hill, 2002.
nd
Operating Systems
10CS53
Table of Contents
Topics
UNIT 1: INTRODUCTION TO OPERATING SYSTEMS, STRUCTURES
Page no
7-26
27-53
Operating Systems
10CS53
54-67
3.1 SYNCHRONIZATION
3.2 THE CRITICAL SECTION PROBLEM
3.3 PETERSONS SOLUTION
3.4 SYNCHRONIZATION HARDWARE
3.5 SEMAPHORES
3.6 CLASSICAL PROBLEMS OF SYNCHRONIZATION
3.7 MONITORS
UNIT 4: DEADLOCK
68 -78
4.1 DEADLOCKS
4.2 SYSTEM MODEL
4.3 DEADLOCK CHARACTERIZATION
4.4 METHODS FOR HANDLING DEADLOCKS
4.5 DEADLOCK PREVENTION
4.6 DEADLOCK AVOIDANCE
4.7 DEADLOCK DETECTION
4.8 RECOVERY FROM DEADLOCK
Operating Systems
10CS53
79-104
105-121
122-134
Operating Systems
10CS53
SYSTEM.
135-150
Operating Systems
10CS53
Operating Systems
10CS53
It provides a basis for application program & acts as an intermediary between user of computer & computer
hardware.
The purpose of an OS is to provide a environment in which the user can execute the program in a
convenient & efficient manner.
OS is an important part of almost every computer systems.
A computer system can be roughly divided into four components
The Hardware
The OS
The application Program
The user
The Hardware consists of memory, CPU, ALU, I/O devices, peripherals devices & storage devices.
The application program mainly consisted of word processors, spread sheets, compilers & web browsers
defines the ways in which the resources are used to solve the problems of the users.
The OS controls & co-ordinates the use of hardware among various application program for various users.
1.2 COMPUTER SYSTEM ORGANIZATION
The following figure shows the conceptual view of a computer system
Views OF OS
1. User Views:-The user view of the computer depends on the interface used.
i. Some users may use PCs. In this the system is designed so that only one user can utilize the resources and
mostly for ease of use where the attention is mailnly on performances and not on the resource
utilization.
ii. Some users may use a terminal connected to a mainframe or minicomputers.
iii. Other users may access the same computer through other terminals. These users may share resources and
exchange information. In this case the OS is designed to maximize resource utilization-so that all
available CPU time, memory & I/O are used efficiently.
iv. Other users may sit at workstations, connected to the networks of other workstation and servers. In this
case OS is designed to compromise between individual visibility & resource utilization.
Operating Systems
10CS53
2. System Views:
i. We can view system as resource allocator i.e. a computer system has many resources that may be used to
solve a problem. The OS acts as a manager of these resources. The OS must decide how to allocate
these resources to programs and the users so that it can operate the computer system efficiently and
fairly.
ii. A different view of an OS is that it need to control various I/O devices & user programs i.e. an OS is a
control program used to manage the execution of user program to prevent errors and improper use of
the computer.
iii. Resources can be either CPU Time, memory space, file storage space, I/O devices
and so on. The OS must support the following tasks
a. Provide the facility to create, modification of programs & data files using on editors.
b. Access to compilers for translating the user program from high level language to machine language.
c. Provide a loader program to move the compiled program code to computers memory for execution.
d. Provides routines that handle the details of I/O programming.
1.3 OPERATING SYSTEM ARCHITECTURE Mainframe System:
a. Mainframe systems are mainly used for scientific & commercial applications.
b. An OS may process its workload serially where the computer runs only one application
or concurrently where computer runs many applications. Batch Systems:
a. Early computers where physically large machines.
b. The common I/P devices are card readers & tape drives.
c. The common O/P devices are line printers, tape drives & card punches.
d. The user do not interact directly with computers but we use to prepare a job with the program, data & some
control information & submit it to the computer operator.
e. The job was mainly in the form punched cards.
f. At later time the O/P appeared and it consisted of result along with dump of memory and register content for
debugging.
The OS of these computers was very simple. Its major task was to transfer control from one job to the next. The
OS was always resident in the memory. The processing of job was very slow. To improve the processing speed
operators batched together the jobs with similar needs and processed it through the computers. This is called Batch
Systems.
In batch systems the CPU may be idle for some time because the speed of the mechanical devices slower
compared to the electronic devices.
Later improvement in technology and introduction of disks resulted in faster I/O devices.
The introduction of disks allowed the OS to store all the jobs on the disk. The OS could perform the
scheduling to use the resources and perform the task efficiently.
Disadvantages of Batch Systems:
1. Turn around time can be large from user.
2. Difficult to debug the program.
3. A job can enter into infinite loop.
4. A job could corrupt the monitor.
5. Due to lack of protection scheme, one job may affect the pending jobs.
Operating Systems
10CS53
10
Operating Systems
10CS53
The goals pf those OS changed later with the time and new systems includes Microsoft Windows & Apple
Macintosh.
The Apple Macintosh OS ported to more advanced hardware & includes new features like virtual memory &
multi tasking.
Micro computers are developed for single user in 1970s & they can accommodate software with
large capacity & greater speeds. MS-DOS is an example for micro computer OS & are used by commercial,
educational, government enterprises.
III. Multi Processor Systems:
Multi processor systems include more than one processor in close communication.
They share computer bus, the clock, m/y & peripheral devices.
In symmetric multi processing, each processors runs an identical copy of OS and they communicate with
one another as needed. All the CPU shares the common memory.
In asymmetric multi processing, each processors is assigned a specific task. It uses a master slave
relationship. A master processor controls the system. The master processors schedules and allocates work to slave
processors. The following figure shows asymmetric multi processors.
SMP means al processors are peers i.e. no master slave relationship exists between processors. Each
processors concurrently runs a copy of OS.
The differences between symmetric & asymmetric multi processing may be result of either H/w or S/w.
Special H/w can differentiate the multiple processors or the S/w can be written to allow only master & multiple
slaves.
Advantages of Multi Processor Systems:
1. Increased Throughput:-By increasing the Number of processors we can get more work done in less time. When
multiple process co operate on task, a certain amount of overhead is incurred in keeping all parts working
correctly.
2. Economy Of Scale:-Multi processor system can save more money than multiple single processor, since they
share peripherals, mass storage & power supplies. If many programs operate on same data, they will be stored
on one disk & all processors can share them instead of maintaining data on several systems.
3. Increased Reliability:-If a program is distributed properly on several processors, than the failure of one
processor will not halt the system but it only slows down.
11
Operating Systems
10CS53
A system call can be used to terminate the program either normally or abnormally. Reasons for abnormal
termination are dump of m/y, error message generated etc.
Debugger is mainly used to determine problem of the dump & returns back the dump to the OS.
In normal or abnormal situations the OS must transfer the control to the command interpreter system.
In batch system the command interpreter terminates the execution of job & continues with the next job.
Some systems use control cards to indicate the special recovery action to be taken in case of errors.
Normal & abnormal termination can be combined at some errors level. Error level is defined before & he
command interpreter uses this error level to determine next action automatically.
MS-DOS:
MS-DOS is an example of single tasking system, which has command interpreter system i.e. invoked when the
computer is started. To run a program MS-DOS uses simple method. It does not create a process when one process
is running MS-DOS the program into m/y & gives the program as much as possible. It lacks the general
multitasking capabilities.
BSD:Free BSD is an example of multitasking system. In free BSD the command interpreter may continue running
while other program is executing. FORK is used to create new process.
12
Operating Systems
10CS53
Process requires certain resources like CPU time, M/y, I/O devices. These resources are allocated to the
process when it created or while it is running.
When process terminates the process reclaims all the reusable resources.
Main M/y is the array of bytes ranging from hundreds of thousands to billions. Each byte will have their
own address.
The central processor reads the instruction from main M/y during instruction fetch cycle & it both reads &
writes the data during the data-fetch cycle. The I/O operation reads and writes data in main M/y.
The main M/y is generally a large storage device in which a CPU can address & access directly.
When a program is to be executed it must be loaded into memory & mapped to absolute address. When it is
executing it access the data & instruction from M/y by generating absolute address. When the program terminates all
available M/y will be returned back.
To improve the utilization of CPU & the response time several program will be kept in M/y.
Several M/y management scheme are available & selection depends on the H/w design of the
13
Operating Systems
etc.
10CS53
File Management:
File management is one of the most visible component of an OS.
Computer stores data on different types of physical media like Magnetic Disks, Magnetic tapes, optical disks
For convenient use of the computer system the OS provides uniform logical view of information storage.
The OS maps file on to physical media & access these files via storage devices.
A file is logical collection of information.
File consists of both program & data. Data files may be numeric, alphabets or alphanumeric.
Files can be organized into directories. The OS is responsible for the following activities,
Creating & deleting of files.
Creating & deleting directories.
Supporting primitives for manipulating files & directories.
Maping files onto secondary storage.
Backing up files on stable storage media.
Is a mechanism where the computer system may store information in a way that it can be retrieved later.
Since the size of the M/y is small & volatile Secondary storage devices is used.
Magnetic disk is central importance of computer system. The OS is responsible for the following
activities,
Storage allocation.
Disk scheduling. The entire speed of computer system depends on the speed of the disk sub system.
Each I/o device has a device handler that resides in separate process associated with that device. The I/O
management consists of,
The process communicates with one another through various communication lines like high
speed buses or N/w. Following parameters are considered while designing the N/w,
Topology of N/w.
Type of N/w.
Physical media.
Communication protocol,
Routing algorithms.
14
Operating Systems
10CS53
Modern computer system supports many users & allows the concurrent execution of multiple processes
organization rely on computers to store information. It necessary that the information & devices must be protected
from unauthorized users or processors.
The protection is a mechanism for controlling the access of program, processes or users to the resources
defined by a computer system.
Protection can improve reliability by detecting latent errors at the interface B/w component sub system.
A distributed system is one in which H/w or S/w components located at the networked computers
communicate & co ordinate their actions only by passing messages.
A distributed systems looks to its user like an ordinary OS but runs on multiple, Independent CPUs.
Distributed systems depends on networking for their functionality which allows for communication so that
distributed systems are able to share computational tasks and provides rich set of features to users.
N/w may vary by the protocols used, distance between nodes & transport media. Protocols->TCP/IP, ATM
etc. Network-> LAN, MAN, WAN etc. Transport Media-> copper wires, optical fibers & wireless transmissions
Client-Server Systems:
Since PCs are faster, power full, cheaper etc. designers have shifted away from the centralized system
architecture.
PCs are introduced in 1970s they are considered as standalone computers i.e. only one user can use it at a
time.
With wide spread use of internet PCs were connected to computer networks.
With the introduction of the web in mid 1990s N/w connectivity became an essential component of a
computer system.
All modern PCs & workstation can run a web. Os also includes system software that enables the computer
to access the web.
In distributed systems or loosely coupled couple systems, the processor can communicate with one another
through various communication lines like high speed buses or telephones lines.
A N/w OS which has taken the concept of N/w & distributed system which provides features fir file sharing
across the N/w and also provides communication which allows different processors on different computers to share
resources.
15
Operating Systems
10CS53
Like parallel systems the clustered systems will have multiple CPU but they are composed of two or more
individual system coupled together.
Clustered systems share storage & closely linked via LAN N/w.
Clustered systems are integrated with H/w & S/w. H/w clusters means sharing of high performance disk.
S/w clusters are in the form of unified control of a computer system in a cluster.
A layer of S/w cluster runs on the cluster nodes. Each node can monitor one or more of the others. If the
monitored M/c fails the monitoring M/c take ownership of its storage and restart the application that were running
on failed M/c.
In asymmetric clustering one M/c is in hot standby mode while others are running the application. The hot
standby M/c does nothing but it monitors the active server. If the server fails the hot standby M/c becomes the active
server.
In symmetric mode two or more hosts are running the Application & they monitor each other. This mode is
more efficient since it uses all the available H/w.
Parallel clustering and clustering over a LAN is also available in clustering. Parallel clustering allows
multiple hosts to access the same data on shared storage.
Clustering technology is changing & include global clusters in which M/c could be anywhere in the world.
Real-Time Systems
Real time system is one which were originally used to control autonomous systems like satellites, robots,
hydroelectric dams etc.
Real time system is one that must react to I/p & responds to them quickly.
A hard real time system guarantees that the critical tasks to be completed on time. This goal requires that all
delays in the system be bounded from the retrieval of stored data to time that it takes the OS to finish the request.
In soft real time system is a less restrictive one where a critical real time task gets priority over other tasks &
retains the property until it completes. Soft real time system is achievable goal that can be mixed with other type of
systems. They have limited utility than hard real time systems.
Soft real time systems are used in area of multimedia, virtual reality & advanced scientific projects. It cannot
be used in robotics or industrial controls due to lack of deadline support.
Real time OS uses priority scheduling algorithm to meet the response requirement of a real time application.
Soft real time requires two conditions to implement, CPU scheduling must be priority based & dispatch
latency should be small.
The primary objective of file management in real time systems is usually speed of access, rather than
efficient utilization of secondary storage.
Dept of CSE, SJBIT
16
Operating Systems
10CS53
Traditional Computing.
Web Based Computing.
Embedded Computing.
Traditional Computing Typical office environment uses traditional computing. Normal PC is used in
traditional computing environment. N/w computers are essential terminals that understand web based computing. In
domestic application most of the user had a single computer with internet connection. Cost of accessing internet is
high.
Web Based Computing has increased the emphasis on N/w. Web based computing uses PC, handheld PDA
& cell phones. One of the feature of this type is load balancing. In load balancing, N/w connection is distributed
among a pool of similar servers.
Embedded computing uses real time OS. Application of embedded computing is car engines,
manufacturing robots, microwave ovens. This type of system provides limited features.
1.13 OPERATING SYSTEM SERVICES:
An OS provides services for the execution of the programs and the users of such programs. The services provided by
one OS may be different from other OS. OS makes the programming task easier. The common services provided by
the OS are
1. Program Execution:-The OS must able to load the program into memory & run that program. The program must
end its execution either normally or abnormally.
2. I/O Operation:-A program running may require any I/O. This I/O may be a file or a specific device users cant
control the I/O device directly so the OS must provide a means for controlling I/O devices.
3. File System Interface:-Program need to read or write a file. The OS should provide permission for the creation
or deletion of files by names.
4. Communication:-In certain situation one process may need to exchange information with another process. This
communication May takes place in two ways.
a. Between the processes executing on the same computer.
b. Between the processes executing on different computer that are connected by a network. This
communication can be implemented via shared memory or by OS.
5. Error Detection:-Errors may occur in CPU, I/O devices or in M/y H/w. The OS constantly needs to be aware
of possible errors. For each type of errors the OS should take appropriate actions to ensure correct &
consistent computing.
OS with multiple users provides the following services,
a. Resource Allocation:-When multiple users logs onto the system or when multiple jobs are running, resources must
be allocated to each of them. The OS manages different types of OS resources. Some resources may need some
special allocation codes & others may have some general request & release code.
b. Accounting:-We need to keep track of which users use how many & what kind of resources. This record keeping
may be used for accounting. This accounting data may be used for statistics or billing. It can also be used to improve
system efficiency.
c. Protection:-Protection ensures that all the access to the system are controlled. Security starts with each user
having authenticated to the system, usually by means of a password. External I/O devices must also be protected
from invalid access. In multi process environment it is possible that one process may interface with the other or with
the OS, so protection is required.
Dept of CSE, SJBIT
17
Operating Systems
10CS53
Command interpreter system between the user & the OS. It is a system program to the OS.
Command interpreter is a special program in UNIX & MS DOS OS i.e. running when the user logs on.
Many commands are given to the OS through control statements when the user logs on, a program that reads
& interprets control statements is executed automatically. This program is sometimes called the control card
interpreter or command line interpreter and is also called as shell.
The command statements themselves deal with process creation & management, I/O handling, secondary
storage management, main memory management, file system access, protection & N/w.
1.15 SYSTEM CALLS
A system call instruction generates an interrupt and allows OS to gain control of the processors.
System calls occur in different ways depending on the computer. Some time more information is needed to
identify the desired system call. The exact type & amount of information needed may vary according to the
particular OS & call.
TYPES OF SYSTEM CALLS
PASSING PARAMETERS TO OS
Three general methods are used to pass the parameters to the OS.
The simplest approach is to pass the parameters in registers. In some there can be more parameters than
register. In these the parameters are generally in a block or table in m/y and the address of the block is passed as
parameters in register. This approach used by Linux.
Parameters can also be placed or pushed onto stack by the program & popped off the stack by the OS.
Some OS prefer the block or stack methods, because those approaches do not limit the number or length of
parameters being passed.
Many system calls are used to transfer information between user program & OS. Example:-Most systems
Dept of CSE, SJBIT
18
Operating Systems
10CS53
have the system calls to return the current time & date, number of current users, version number of OS, amount of
free m/y or disk space & so on.
In addition the OS keeps information about all its processes & there are system calls to access
The name of the other communicating party should be known, it ca be on the same computer or it can be on
another computer connected by a computer network.
Each computer in a network may have a host name like IP name similarly each process can have a process
The get host id & process id system call do this translation. These identifiers are then passed to the open &
close connection system calls.
The recipient process must give its permission for communication to take place with an accept connection
call.
Most processes receive the connection through special purpose system program dedicated for that purpose
called daemons. The daemon on the server side is called server daemon & the daemon on the client side is called
client daemon.
2. Shared Memory:
In this the processes uses the map m/y system calls to gain access to m/y owned by another process.
The OS tries to prevent one process from accessing another process m/y.
In shared m/y this restriction is eliminated and they exchange information by reading and writing data in
shared areas. These areas are located by these processes and not under OS control.
They should ensure that they are not writing to same m/y area.
Both these types are commonly used in OS and some even implement both.
Message passing is useful when small number of data need to be exchanged since no conflicts are to be
avoided and it is easier to implement than in shared m/y. Shared m/y allows maximum speed and convenience of
communication as it is done at m/y speed when within a computer.
19
Operating Systems
10CS53
System calls can be used to create & deleting of files. System calls may require the name of the files with
attributes for creating & deleting of files.
Other operation may involve the reading of the file, write & reposition the file after it is opened.
For directories some set of operation are to be performed. Sometimes we require to reset some of the
attributes on files & directories. The system call get file attribute & set file attribute are used for this type of
operation.
DEVICE MANAGEMENT:
In multi user environment the requirement are made to use the device. After using the device must be
released using release system call the device is free to be used by another user. These function are similar to open &
close system calls of files.
Read, write & reposition system calls may be used with devices.
MS-DOS & UNIX merge the I/O devices & the files to form file services structure. In file device structure
I/O devices are identified by file names.
1.18 OPERATING SYSTEM STRUCTURES
For designing the system different types of structures are used. They are,
Simple structures.
Layered structured.
Micro kernels
Simple Structures
Simple structure OS are small, simple & limited systems.
The structure is not well defined
MS-DOS is an example of simple structure OS.
MS-DOS layer structure is shown below
20
Operating Systems
a. Kernel
10CS53
Kernel is further separated into series of interfaces & device drivers which were added & expanded as the
UNIX evolved over years.
The kernel also provides the CPU scheduling, file system, m/y management & other OS function through
system calls.
System calls define API to UNIX and system programs commonly available defines the user interface. The
programmer and the user interface determines the context that the kernel must support.
New versions of UNIX are designed to support more advanced H/w. the OS can be broken down into large
number of smaller components which are more appropriate than the original MS-DOS.
21
Operating Systems
10CS53
Layered Approach
In this OS is divided into number of layers, where one layer is built on the top of another layer. The bottom
layer is hardware and higher layer is the user interface.
An OS is an implementation of abstract object i.e. the encapsulation of data & operation to manipulate
these
data.
The main advantage of layered approach is the modularity i.e. each layer uses the services & functions
provided by the lower layer. This approach simplifies the debugging & verification. Once first layer is debugged
the
correct functionality is guaranteed while debugging the second layer. If an error is identified then it is a problem in
that layer because the layer below it is already debugged.
Each layer is designed with only the operations provided by the lower level layers.
Each layer tries to hide some data structures, operations & hardware from the higher level layers.
A problem with layered implementation is that they are less efficient then the other types.
22
Operating Systems
10CS53
In this OS is divided into number of layers, where one layer is built on the top of another layer. The bottom
layer is hardware and higher layer is the user interface.
An OS is an implementation of abstract object i.e. the encapsulation of data & operation to manipulate these
data.
The main advantage of layered approach is the modularity i.e. each layer uses the services & functions
provided by the lower layer. This approach simplifies the debugging & verification. Once first layer is debugged the
correct functionality is guaranteed while debugging the second layer. If an error is identified then it is a problem in
that layer because the layer below it is already debugged.
Each layer is designed with only the operations provided by the lower level layers.
Each layer tries to hide some data structures, operations & hardware from the higher level layers.
A problem with layered implementation is that they are less efficient then the other types.
Micro Kernels
Micro kernel is a small Os which provides the foundation for modular extensions.
The main function of the micro kernels is to provide communication facilities between the current program
and various services that are running in user space.
This approach was supposed to provide a high degree of flexibility and modularity.
This benefits of this approach includes the ease of extending OS. All the new services are added to the user
space & do not need the modification of kernel.
Most of the services will be running as user process rather than the kernel process.
Micro kernel architecture assign only a few essential functions to the kernel including address space, IPC &
basic scheduling.
23
Operating Systems
10CS53
NON-VIRTUAL MACHINE
VIRTUALMACHINE
The virtual-machine concept provides complete protection of system resources since each virtual machine is
isolated from all other virtual machines. This isolation, however, permits no direct sharing of resources.
A virtual-machine system is a perfect vehicle for operating-systems research and development. System
development is done on the virtual machine, instead of on a physical machine and so does not disrupt normal system
operation.
The virtual machine concept is difficult to implement due to the effort required to provide an exact duplicate
to the underlying machine.
24
Operating Systems
10CS53
Compiled Java programs are platform-neutral bytecodes executed by a Java Virtual Machine (JVM).
Traditionally written in assembly language, operating systems can now be written in higher-level languages.
An operating system is far easier to port (move to some other hardware) if it is written in a high-level
language.
1.21 SYSTEM BOOT
Operating systems are designed to run on any of a class of machines; the system must be configured for each
specific computer site.
SYSGEN program obtains information concerning the specific configuration of the hardware system.
Bootstrap program code stored in ROM that is able to locate the kernel, load it into memory, and start its
execution.
25
Operating Systems
10CS53
IMPORTANT QUESTIONS
1. What are the three main purposes of an operating system?
2. What is the main advantage of multiprogramming?
3. What are the main differences between operating systems for mainframe computers and personal computers?
4. Define the essential properties of the following types of operating systems:
a. Batch
b. Interactive
c. Time sharing
d. Real time
e. Network
f. Distributed
. What are the differences between a trap and an interrupt? What is the use of each function?
. What are the five major activities of an operating system in regard to process management?
. What are the three major activities of an operating system in regard to secondary-storage management?
. List five services provided by an operating system.
. What is the main advantage of the layered approach to system design?
. 10. What is the main advantage for an operating-system designer of using a virtual-machine architecture? What
is the main advantage for a user?
26
Operating Systems
10CS53
TOPICS
2.13 PROCESS CONCEPT.
2.14 PROCESS SCHEDULING.
2.15 OPERATIONS ON PROCESSES.
2.16 INTER-PROCESS COMMUNICATION.
2.17 MULTI-THREADED PROGRAMMING.
2.18 OVERVIEW; MULTITHREADING MODELS.
2.19 THREAD LIBRARIES; THREADING ISSUES.
2.20 PROCESS SCHEDULING: BASIC CONCEPTS.
2.21 SCHEDULING CRITERIA.
2.22 SCHEDULING ALGORITHMS.
2.23 THREAD SCHEDULING.
2.24 MULTIPLE-PROCESSOR SCHEDULING.
27
Operating Systems
10CS53
Process is a dynamic entity. A process is a sequence of instruction execution process exists in a limited span
of time. Two or more process may execute the same program by using its own data & resources.
A program is a static entity which is made up of program statement. Program contains the instruction. A
program exists in a single space. A program does not execute by itself.
A process generally consists of a process stack which consists of temporary data & data section which
consists of global variables.
A process is more than the program code which is also called text section.
Process State:
The process state consist of everything necessary to resume the process execution if it is somehow put aside
temporarily. The process state consists of at least following:
x Code for the program.
x Program's static data.
x Program's dynamic data.
x Program's procedure call stack.
x Contents of general purpose registers.
x Contents of program counter (PC)
x Contents of program status word (PSW).
x Operating Systems resource in use.
28
Operating Systems
10CS53
Schedulers:
The following are the different type of schedulers
1. Long-term scheduler (or job scheduler) selects which processes should be brought into the ready queue.
2. Short-term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU.
3. Medium-term schedulers
29
Operating Systems
10CS53
Process Creation
In general-purpose systems, some way is needed to create processes as needed during operation. There are four
principal events led to processes creation.
x System initialization.
x Execution of a process Creation System
x A user request to create a new process.
x Initialization of a batch job.
Foreground processes interact with users. Background processes that stay in background sleeping but suddenly
springing to life to handle activity such as email, webpage, printing, and so on. Background processes are called
daemons. This call creates an exact clone of the calling process. A process may create a new process by some create
process such as 'fork'. It choose to does so, creating process is called parent process and the created one is called the
child processes. Only one parent is needed to create a child process. Note that unlike plants and animals that use
sexual representation, a process has only one parent. This creation of process (processes) yields a hierarchical
structure of processes like one in the figure. Notice that each child has only one parent but each parent may have
many children. After the fork, the two processes, the parent and the child, have the same memory image, the same
environment strings and the same open files. After a process is created, both the parent and child have their own
distinct address space. If either process changes a word in its address space, the change is not visible to the other
process.
Following are some reasons for creation of a process
x User logs on.
x User starts a program.
x Operating systems creates process to provide service, e.g., to manage printer.
x Some program starts another process, e.g., Netscape calls xv to display a picture.
Process Termination
30
Operating Systems
10CS53
A process terminates when it finishes executing its last statement. Its resources are returned to the system, it is
purged from any system lists or tables, and its process control block (PCB) is erased i.e., the PCB's memory space is
returned to a free memory pool. The new process terminates the existing process, usually due to following reasons:
x Normal
Exist Most processes terminates because they have done their job. This call is exist in UNIX. x Error Exist
When process discovers a fatal error. For example, a user tries to compile a program that does not exist. x Fatal
Error An error caused by process due to a bug in program for example, executing an illegal instruction, referring
non-existing memory or dividing by zero. x Killed by another Process A process executes a system call telling the
Operating Systems to terminate some other process. In UNIX, this call is kill. In x some systems when a process
kills all processes it created are killed as well (UNIX does not work this way).
x New State The process being created. x Terminated State The process has finished execution. x Blocked
(waiting) State When a process blocks, it does so because logically it cannot
continue, typically because it is waiting for input that is not yet available. Formally, a process is said to be
blocked if it is waiting for some event to happen (such as an I/O completion) before it can proceed. In this
state a process is unable to run until some external event happens.
x Running State A process is said t be running if it currently has the CPU, that is, actually using the CPU at that
particular instant. x Ready State A process is said to be ready if it use a CPU if one were available. It is runable but
temporarily stopped to let another process run.
Logically, the 'Running' and 'Ready' states are similar. In both cases the process is willing to run, only in the case of
'Ready' state, there is temporarily no CPU available for it. The 'Blocked' state is different from the 'Running' and
'Ready' states in that the process cannot run, even if the CPU is available.
31
Operating Systems
10CS53
information.
The PCB is a certain store that allows the operating systems to locate key information about a process. Thus, the
PCB is the data structure that defines a process to the operating systems.
The following figure shows the process control block.
Context Switch:
1. When CPU switches to another process, the system must save the state of the old process and load the saved
state for the new process.
2. Context-switch time is overhead; the system does no useful work while switching.
3. Time dependent on hardware support
Cooperating Processes & Independent Processes
Independent process: one that is independent of the rest of the universe.
x Its state is not shared in any way by any other process.
x Deterministic: input state alone determines results.
x Reproducible.
x Can stop and restart with no bad effects (only time varies). Example:
32
Operating Systems
10CS53
programming: a single process is run to completion before anything else can be run
on the processor.
Multiprogramming: share one processor among several processes. If no shared state,
then order of dispatching is irrelevant.
U
n
i Multiprocessing: if multiprogramming works, then it should also be ok to run
p processes in parallel on separate processors.
r
o given process runs on only one processor at a time.
.
o A
g process may run on different processors at different times (move state, assume
o A
.
r
processors are
identical).
a
.
o Cannot distinguish multiprocessing from multiprogramming on a very fine grain.
m
m processes:
Cooperating
i
n must model the social structures of the people that use it. People cooperate, so
x Machine
g
machine
: support that cooperation. Cooperation means shared state, e.g. a single file system. x
must
Cooperating processes are those that share state. (May or may not actually be "cooperating") x
a is nondeterministic: depends on relative execution sequence and cannot be predicted a
Behavior
priori. x Behavior is irreproducible. x Example: one process writes "ABC", another writes
"CBA"s. Can get different outputs, cannot
tell iwhat comes from which. E.g. which process output first "C" in "ABCCBA"? Note the
subtnle state sharing that occurs here via the terminal. Not just anything can happen, though.
Forgexample, "AABBCC" cannot occur.
l
e
1. Independent process cannot affect or be affected by the execution of another process
2. Cooperapting process can affect or be affected by the execution of another process
r
3. Advantages of process cooperation
o
c
Information sharing
e
Computation speed-up
s
Modularity
s
Convenience
i
s
r
u
n
t
o
U
n
33
Operating Systems
10CS53
Direct Communication
1. Processes must name each other explicitly:
x send (P, message) send a message to process P x receive(Q, message) receive a message from process Q
2. Properties of communication link
x Links are established automatically x A link is associated with exactly one pair of
communicating processes x Between each pair there exists exactly one link x The link may be
unidirectional, but is usually bi-directional
Indirect Communication
1. Messages are directed and received from mailboxes (also referred to as ports)
x Each mailbox has a unique id
Dept of CSE, SJBIT
34
Operating Systems
10CS53
4.Primitives are defined as: send(A, message) send a message to mailbox A receive(A, message) receive a
message from mailbox A
5.Mailbox sharing P1, P2, and P3 share mailbox A P1, sends; P2 and P3 receive Who gets the message?
6. Solutions Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation Allow the system to select
arbitrarily the receiver. Sender is notified who the receiver was.
Synchronization
1. Message passing may be either blocking or non-blocking
2. Blocking is considered synchronous
->Blocking send has the sender block until the message is received. ->Blocking receive has the receiver block
until a message is available.
3. Non-blocking is considered asynchronous
->Non-blocking send has the sender send the message and continue.
->Non-blocking receive has the receiver receive a valid message or null.
Buffering
->Queue of messages attached to the link; implemented in one of three ways
1. Zero capacity 0 messages sender must wait for receiver (rendezvous)
2. Bounded capacity finite length of n messages Sender must wait if link full
Dept of CSE, SJBIT
35
Operating Systems
10CS53
Processes Vs Threads
As we mentioned earlier that in many respect threads operate in the same way as that of processes. Some of
the similarities and differences are:
Similarities
x Like processes threads share CPU and only one thread active (running) at a time.
x Like processes, threads within a processes, threads within a processes execute sequentially.
x Like processes, thread can create children.
x And like process, if one thread is blocked, another thread can run.
36
Operating Systems
10CS53
Differences
x Unlike
x Unlike
x Unlike
Why Threads?
Following are some reasons why we use threads in designing operating systems.
1.
2.
3.
4.
5.
6.
7.
A process with multiple threads make a great server for example printer server.
Because threads can share common data, they do not need to use interprocess communication.
Because of the very nature, threads can take advantage of multiprocessors.
Responsiveness
Resource Sharing
Economy
Utilization of MP Architectures
User-Level Threads
1. Thread management done by user-level threads library
Dept of CSE, SJBIT
37
Operating Systems
10CS53
Disadvantages:
There is a lack of coordination between threads and operating system kernel. Therefore, process as
whole gets one time slice irrespect of whether process has one thread or 1000 threads within. It is up
to each thread to relinquish control to other threads.
x User-level threads requires non-blocking systems call i.e., a multithreaded kernel. Otherwise, entire
process will blocked in the kernel, even if there are runable threads left in the processes. For example,
if one thread causes a page fault, the process blocks.
x
Kernel-Level Threads
1. Supported by the Kernel
Examples: ->Windows XP/2000, ->Solaris , ->Linux, ->Tru64 UNIX, ->Mac OS X
In this method, the kernel knows about and manages the threads. No runtime system is needed in this case.
Instead of thread table in each process, the kernel has a thread table that keeps track of all threads in the
system. In addition, the kernel also maintains the traditional process table to keep track of processes.
Operating Systems kernel provides system call to create and manage threads.
Advantages:
x Because
kernel has full knowledge of all threads, Scheduler may decide to give more time to a
process having large number of threads than process having small number of threads. x
38
Operating Systems
10CS53
Kernel-level threads are especially good for applications that frequently block.
Disadvantages:
x The kernel-level threads are slow and inefficient. For instance, threads operations are hundreds of times
slower than that of user-level threads.
x Since kernel must manage and schedule threads as well as processes. It require a full thread control
block (TCB) for each thread to maintain information about threads. As a result there is significant
overhead and increased in kernel complexity.
The major disadvantage if that if the kernel is single threaded, a system call of one thread will
block the whole process and CPU may be idle during the blocking period.
x Security Since there is, an extensive sharing among threads there is a potential problem of security. It is
quite possible that one thread over writes the stack of another thread (or damaged shared data)
although it is very unlikely since threads are meant to cooperate on a single task.
Many-to-One
One-to-One
Many-to-Many
39
Operating Systems
10CS53
One-to-One
1. Each user-level thread maps to kernel thread
Many-to-Many Model
1.
2.
3.
4.
40
Operating Systems
10CS53
When a new thread is created it shares its code section, data section and operating system resources like open
files with other threads. But it is allocated its own stack, register set and a program counter.
The creation of a new process differs from that of a thread mainly in the fact that all the shared resources of a
thread are needed explicitly for each process. So though two processes may be running the same piece of code
they need to have their own copy of the code in the main memory to be able to run. Two processes also do not
share other resources with each other. This makes the creation of a new process very costly compared to that
of a new thread.
Thread Pools
1. Create a number of threads in a pool where they await work
Advantages: Usually slightly faster to service a request with an existing thread than create a new
thread
Allows the number of threads in the application(s) to be bound to the size of the pool
Context Switch
To give each process on a multiprogrammed machine a fair share of the CPU, a hardware clock generates
interrupts periodically. This allows the operating system to schedule all processes in main memory (using
scheduling algorithm) to run on the CPU at equal intervals. Each time a clock interrupt occurs, the interrupt
handler checks how much time the current running process has used. If it has used up its entire time slice,
then the CPU scheduling algorithm (in kernel) picks a different process to run. Each switch of the CPU from
one process to another is called a context switch.
values of the CPU registers are saved in the process table of the process that was running just before the
clock interrupt occurred. x The registers are loaded from the process picked by the CPU scheduler to run next.
In a multiprogrammed uniprocessor computing system, context switches occur frequently enough that all
processes appear to be running concurrently. If a process has more than one thread, the Operating System can
use the context switching technique to schedule the threads so they appear to execute in parallel. This is the
case if threads are implemented at the kernel level. Threads can also be implemented entirely at the user level
in run-time libraries. Since in this case no thread scheduling is provided by the Operating System, it is the
responsibility of the programmer to yield the CPU frequently enough in each thread so all threads in the
process can make progress.
41
Operating Systems
10CS53
The threads share a lot of resources with other peer threads belonging to the same process. So a context
switch among threads for the same process is easy. It involves switch of register set, the program counter and
the stack. It is relatively easy for the kernel to accomplished this task.
When the PCB of the currently executing process is saved the operating system loads the PCB of the next
process that has to be run on CPU. This is a heavy task and it takes a lot of time.
2.8 PROCESS SCHEDULING: BASIC CONCEPTS
The assignment of physical processors to processes allows processors to accomplish work. The problem of
determining when processors should be assigned and to which processes is called processor scheduling or
CPU scheduling. When more than one process is runable, the operating system must decide which one first.
The part of the operating system concerned with this decision is called the scheduler, and algorithm it uses is
called the scheduling algorithm.
CPU Scheduler
a. Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of
them
b. CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is nonpreemptive
42
Operating Systems
10CS53
Dispatcher
1. Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this
involves:
switching context
switching to user mode
jumping to the proper location in the user program to restart that program
2. Dispatch latency time it takes for the dispatcher to stop one process and start another running.
2.9 SCHEDULING CRITERIA
1.
2.
3.
4.
5.
General Goals
Fairness
Fairness is important under all circumstances. A scheduler makes sure that each process gets its fair
share of the CPU and no process can suffer indefinite postponement. Note that giving equivalent or equal
time is not fair. Think of safety control and payroll at a nuclear plant.
Policy Enforcement
The scheduler has to make sure that system's policy is enforced. For example, if the local policy is safety
then the safety control processes must be able to run whenever they want to, even if it means delay in payroll
processes.
Efficiency
Scheduler should keep the system (or in particular CPU) busy cent percent of the time when possible.
If the CPU and all the Input/Output devices can be kept running all the time, more work gets done per
second than if some components are idle.
Response Time
A scheduler should minimize the response time for interactive user.
Turnaround
A scheduler should minimize the time batch users must wait for an output.
Throughput
A scheduler should maximize the number of jobs processed per unit time.
A little thought will show that some of these goals are contradictory. It can be shown that any scheduling
Dept of CSE, SJBIT
43
Operating Systems
10CS53
algorithm that favors some class of jobs hurts another class of jobs. The amount of CPU time available is
finite, after all.
Preemptive Vs Nonpreemptive Scheduling
The Scheduling algorithms can be divided into two categories with respect to how they deal with clock
interrupts.
Nonpreemptive Scheduling
A scheduling discipline is nonpreemptive if, once a process has been given the CPU, the CPU cannot be
taken away from that process.
Following are some characteristics of nonpreemptive scheduling
1. In nonpreemptive system, short jobs are made to wait by longer jobs but the overall treatment of all
processes is fair.
2. In nonpreemptive system, response times are more predictable because incoming high priority jobs can not
displace waiting jobs.
3. In nonpreemptive scheduling, a schedular executes jobs in the following two situations.
a. When a process switches from running state to the waiting state.
b. When a process terminates.
Preemptive Scheduling
A scheduling discipline is preemptive if, once a process has been given the CPU can taken away.
The strategy of allowing processes that are logically runable to be temporarily suspended is called
Preemptive Scheduling and it is contrast to the "run to completion" method.
2.10 SCHEDULING ALGORITHMS
CPU Scheduling deals with the problem of deciding which of the processes in the ready queue is to be
allocated the CPU. Following are some scheduling algorithms we will study.
FCFS Scheduling.
Round Robin Scheduling.
SJF Scheduling.
SRT Scheduling.
Priority Scheduling.
Multilevel Queue Scheduling.
Multilevel Feedback Queue Scheduling.
44
Operating Systems
10CS53
x Run-Until-Done
Perhaps, First-Come-First-Served algorithm is the simplest scheduling algorithm is the simplest scheduling
algorithm. Processes are dispatched according to their arrival time on the ready queue. Being a nonpreemptive
discipline, once a process has a CPU, it runs to completion. The FCFS scheduling is fair in the formal sense
or human sense of fairness but it is unfair in the sense that long jobs make short jobs wait and unimportant
jobs make important jobs wait.
FCFS is more predictable than most of other schemes since it offers time. FCFS scheme is not useful in
scheduling interactive users because it cannot guarantee good response time. The code for FCFS scheduling is
simple to write and understand. One of the major drawback of this scheme is that the average time is often
quite long. The First-Come-First-Served algorithm is rarely used as a master scheme in modern operating
systems but it is often embedded within other schemes.
Example:-Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3 ,The Gantt Chart for the schedule is:
Waiting time for P1 = 0; P2 = 24; P3 = 27, Average waiting time: (0 + 24 + 27)/3 = 17 Suppose that the
processes arrive in the order P2 , P3 , P1 , The Gantt chart for the schedule is:
45
Operating Systems
10CS53
process waiting in a queue. The preempted process is then placed at the back of the ready list.
x Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in timesharing
environments in which the system needs to guarantee reasonable response times for interactive users.
x The only interesting issue with round robin scheme is the length of the quantum. Setting the quantum
too short causes too many context switches and lower the CPU efficiency. On the other hand, setting
the quantum too long may cause poor response time and appoximates FCFS.
In any event, the average waiting time under round robin scheduling is often quite long.
1. Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time
has elapsed, the process is preempted and added to the end of the ready queue.
2. If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the
CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.
3. Performance ->q large . FIFO ->q small . q must be large with respect to context switch, otherwise
overhead is too high. Example:Process Burst Time
P1 53 P2 17 P3 68 P4 24
The Gantt chart is: ->Typically, higher average turnaround than SJF, but better response
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
Shortest-Job-First (SJF) Scheduling
x Other name of this algorithm is Shortest-Process-Next (SPN).
x Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process) with the
smallest estimated run-time-to-completion is run next. In other words, when CPU is available, it is
assigned to the process that has smallest next CPU burst.
x The SJF scheduling is especially appropriate for batch jobs for which the run times are known in
advance. Since the SJF scheduling algorithm gives the minimum average time for a given set of
processes, it is probably optimal.
x The SJF algorithm favors short jobs (or processors) at the expense of longer ones. x
The obvious problem with SJF scheme is that it requires precise knowledge of how
long a job or process will run, and this information is not usually available. x The best
SJF algorithm can do is to rely on user estimates of run times.
In the production environment where the same jobs run regularly, it may be possible to provide
reasonable estimate of run time, based on the past performance of the process. But in the development
environment users rarely know how their program will execute.
Like FCFS, SJF is non preemptive therefore, it is not useful in timesharing environment in which reasonable
response time must be guaranteed.
46
Operating Systems
10CS53
1
Associate with each process the length of its next CPU burst. Use these lengths to schedule the
process with the shortest time
2
Two schemes:
x nonpreemptive once CPU given to the process it cannot be preempted until completes its
CPU burst
x preemptive if a new process arrives with CPU burst length less than remaining time of
current executing process, preempt. This scheme is know as the ShortestRemainingTime-First (SRTF)
3. SJF is optimal gives minimum average waiting time for a given set of processes
Process Arrival Time Burst Time
P1
0.0
P2
2.0
P3
4.0
P4
5.0
47
Operating Systems
10CS53
next,
including new arrivals.
x In SJF scheme, once a job begin executing, it run to completion.
x In SJF scheme, a running process may be preempted by a new arrival process with shortest
estimated run-time.
x The algorithm SRT has higher overhead than its counterpart SJF.
x The SRT must keep track of the elapsed time of the running process and must handle occasional
preemptions.
x In this scheme, arrival of small processes will run almost immediately. However, longer jobs
have even longer mean waiting time.
Priority Scheduling
x A priority number (integer) is associated with each process
x The CPU is allocated to the process with the highest priority (smallest integer { highest priority)
o ->Preemptive
o ->nonpreemptive
x SJF is a priority scheduling where priority is the predicted next CPU burst time
x Problem { Starvation low priority processes may never execute
x Solution { Aging as time progresses increase the priority of the process
The basic idea is straightforward: each process is assigned a priority, and priority is allowed to run. EqualPriority processes are scheduled in FCFS order. The shortest-Job-First (SJF) algorithm is a special case of
general priority scheduling algorithm. An SJF algorithm is simply a priority algorithm where the priority is
the inverse of the (predicted) next CPU burst. That is, the longer the CPU burst, the lower the priority and
vice versa.
Priority can be defined either internally or externally. Internally defined priorities use some measurable
quantities or qualities to compute priority of a process.
Examples of Internal priorities are
48
Operating Systems
10CS53
x Time
Externally defined priorities are set by criteria that are external to operating system such as
x The importance of process. x Type or amount of funds being paid for computer use. x The department
sponsoring the work. x Politics.
preemptive priority algorithm will preemptive the CPU if the priority of the newly
arrival process is higher than the priority of the currently running process. x A nonpreemptive priority algorithm will simply put the new process at the head of the ready
queue.
A major problem with priority scheduling is indefinite blocking or starvation. A solution to the problem of
indefinite blockage of the low-priority process is aging. Aging is a technique of gradually increasing the
priority of processes that wait in the system for a long period of time.
Multilevel Queue Scheduling
A multilevel queue scheduling algorithm partitions the ready queue in several separate queues, for instance,
In a multilevel queue scheduling processes are permanently assigned to one queues. The processes are
permanently assigned to one another, based on some property of the process, such as Memory size , Process
priority , Process type . Algorithm choose the process from the occupied queue that has the highest priority,
and run that process either
Preemptive or Non-preemptively Each queue has its own scheduling algorithm or policy.
PossibilityI
If each queue has absolute priority over lower-priority queues then no process in the queue could run
unless the queue for the highest-priority processes were all empty. For example, in the above figure no
process in the batch queue could run unless the queues for system processes, interactive processes, and
interactive editing processes will all empty.
49
Operating Systems
10CS53
Possibility II
If there is a time slice between the queues then each queue gets a certain amount of CPU times, which it can
then schedule among the processes in its queue. For instance;
x 80%
x 20%
Since processes do not move between queue so, this policy has the advantage of low scheduling overhead, but
it is inflexible.
only when
A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If
it does not finish in 8 milliseconds, job is moved to queue Q1.
At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete,
it is preempted and moved to queue Q2.
50
Operating Systems
10CS53
51
Operating Systems
10CS53
Algorithm Evaluation
x Deterministic modeling takes a particular predetermined workload and defines the
performance of each algorithm for that workload. x Queueing models x Implementation
Evaluation of CPU Schedulers by Simulation
52
Operating Systems
10CS53
IMPORTANT QUESTIONS:
1.
2.
3.
4.
5.
53
Operating Systems
10CS53
54
Operating Systems
10CS53
3.1 SYNCHRONIZATION
Since processes frequently needs to communicate with other processes therefore, there is a need for a wellstructured communication, without using interrupts, among processes.
Race Conditions
In operating systems, processes that are working together share some common storage (main memory, file
etc.) that each process can read and write. When two or more processes are reading or writing some shared
data and the final result depends on who runs precisely when, are called race conditions. Concurrently
executing threads that share data need to synchronize their operations and processing in order to avoid race
condition on shared data. Only one customer thread at a time should be allowed to examine and update the
shared variable. Race conditions are also possible in Operating Systems. If the ready queue is implemented as
a linked list and if the ready queue is being manipulated during the handling of an interrupt, then interrupts
must be disabled to prevent another interrupt before the first one completes. If interrupts are not disabled than
the linked list could become corrupt.
1. count++ could be implemented as
register1 = count
register1 = register1 + 1
count = register1
2. count--could be implemented as
register2 = count
register2 = register2 1
count = register2
3. Consider this execution interleaving with count = 5 initially:
S0: producer execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1
{register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 =
register2 -1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count
= register2 {count = 4}
3.2 THE CRITICAL SECTION PROBLEM
55
Operating Systems
10CS53
1. Mutual Exclusion -If process Pi is executing in its critical section, then no other processes can be executing
in their critical sections
2. Progress -If no process is executing in its critical section and there exist some processes that wish to enter
their critical section, then the selection of the processes that will enter the critical section next cannot be
postponed indefinitely
3. Bounded Waiting -A bound must exist on the number of times that other processes are allowed to enter
their critical sections after a process has made a request to enter its critical section and before that request
is granted
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the N processes
A. Critical Section
The key to preventing trouble involving shared storage is find some way to prohibit more than one process
from reading and writing the shared data simultaneously. That part of the program where the shared memory
is accessed is called the Critical Section. To avoid race conditions and flawed results, one must identify codes
in Critical Sections in each thread. The characteristic properties of the code that form a Critical Section are
x Codes that
reference one or more variables in a read-update-write fashion while any of those variables is
possibly being altered by another thread. x Codes that alter one or more variables that are possibly being
referenced in read-updata-write
fashion by another thread. x Codes use a data structure while any part of it is possibly being altered by
another thread. x Codes alter any part of a data structure while it is possibly in use by another thread.
Here, the important point is that when one process is executing shared modifiable data in its
critical section, no other process is to be allowed to execute in its critical section. Thus, the
execution of critical sections by the processes is mutually exclusive in time.
B. Mutual Exclusion
A way of making sure that if one process is using a shared modifiable data, the other processes will be
excluded from doing the same thing. Formally, while one process executes the shared variable, all other
processes desiring to do so at the same time moment should be kept waiting; when that process has finished
executing the shared variable, one of the processes waiting; while that process has finished executing the
shared variable, one of the processes waiting to do so should be allowed to proceed. In this fashion, each
Dept of CSE, SJBIT
56
Operating Systems
10CS53
process executing the shared data (variables) excludes all others from doing so simultaneously. This is called
Mutual Exclusion.
Note that mutual exclusion needs to be enforced only when processes access shared modifiable data when
processes are performing operations that do not conflict with one another they should be allowed to proceed
concurrently.
two processes may at the same moment inside their critical sections.
assumptions are made about relative speeds of processes or number of CPUs.
process should outside its critical section should block other processes.
process should wait arbitrary long to enter its critical section.
Problem
When one process is updating shared modifiable data in its critical section, no other process should allowed to
enter in its critical section.
57
Operating Systems
10CS53
Conclusion
The flaw in this proposal can be best explained by example. Suppose process A sees that the lock is 0. Before
it can set the lock to 1 another process B is scheduled, runs, and sets the lock to 1. When the process A runs
again, it will also set the lock to 1, and two processes will be in their critical section simultaneously.
It is a system call that causes the caller to block, that is, be suspended until some other
process wakes it up. x Wakeup
o It is a system call that wakes up the process.
o
Both 'sleep' and 'wakeup' system calls have one parameter that represents a memory address used to
match up 'sleeps' and 'wakeups' .
58
Operating Systems
10CS53
as bounded buffer problem. Two processes share a common, fixed-size (bounded) buffer. The producer puts
information into the buffer and the consumer takes information out.
Trouble arises when
1. The producer wants to put a new data in the buffer, but buffer is already full. Solution: Producer goes to
sleep and to be awakened when the consumer has removed data.
2. The consumer wants to remove data the buffer but buffer is already empty. Solution: Consumer goes to
sleep until the producer puts some data in buffer and wakes consumer up.
Conclusion
This approaches also leads to same race conditions we have seen in earlier approaches. Race condition can
occur due to the fact that access to 'count' is unconstrained. The essence of the problem is that a wakeup
call, sent to a process that is not sleeping, is lost.
3.4 SYNCHRONIZATION HARDWARE
1. Many systems provide hardware support for critical section code
2. Uniprocessors could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
3. Modern machines provide special atomic hardware instructions
->Atomic = non-interruptable
3.5 SEMAPHORES
E.W. Dijkstra (1965) abstracted the key notion of mutual exclusion in his concepts of semaphores.
Definition
A semaphore is a protected variable whose value can be accessed and altered only by the operations P and V
and initialization operation called 'Semaphoiinitislize'.
59
Operating Systems
10CS53
Binary Semaphores can assume only the value 0 or the value 1 counting semaphores also called general
semaphores can assume only nonnegative values. The P (or wait or sleep or down) operation on semaphores
S, written as P(S) or wait (S), operates as follows:
P(S): IF S>0
THEN S:= S-1
ELSE (wait on S)
The V (or signal or wakeup or up) operation on semaphore S, written as V(S) or signal (S), operates as
follows:
60
Operating Systems
10CS53
Operations P and V are done as single, indivisible, atomic action. It is guaranteed that once a semaphore
operations has stared, no other process can access the semaphore until operation has completed. Mutual
exclusion on the semaphore, S, is enforced within P(S) and V(S).
If several processes attempt a P(S) simultaneously, only process will be allowed to proceed. The other
processes will be kept waiting, but the implementation of P and V guarantees that processes will not suffer
indefinite postponement. Semaphores solve the lost-wakeup problem
If we could arrange matters such that no two processes were ever in their critical sections simultaneously, we
could avoid race conditions. We need four conditions to hold to have a good solution for the critical section
problem (mutual exclusion).
x No
two processes may at the same moment inside their critical sections.
x No
x No
process should outside its critical section should block other processes.
x No
Note that mutual exclusion needs to be enforced only when processes access shared modifiable data when
processes are performing operations that do not conflict with one another they should be allowed to proceed
concurrently.
61
Operating Systems
10CS53
wakeup remove one of processes in the waiting queue and place it in the ready queue. ->Implementation of
wait: wait (S){ value--;
if (value < 0) { add this process to waiting queue
block(); }
}
->Implementation of signal:
Signal (S){
value++;
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); }
62
Operating Systems
10CS53
}
3.6 CLASSICAL PROBLEMS OF SYNCHRONIZATION
1. Bounded-Buffer Problem
2. Readers and Writers Problem
3. Dining-Philosophers Problem
Bounded-Buffer Problem
1.
2.
3.
4.
5.
63
Operating Systems
10CS53
signal (wrt) ; }
5. The structure of a reader process
while (true) { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex)
// reading is performed
wait (mutex) ;
readcount --;
if (readcount == 0) signal (wrt) ;
signal (mutex) ;
}
Dining-Philosophers Problem
1. Shared data
.
.
64
Operating Systems
10CS53
3.7 MONITORS
1. high-level abstraction that provides a convenient and effective mechanism for process synchronization
2. Only one process may be active within the monitor at a time monitor monitor-name
{ // shared variable declarations procedure P1 () { . }
procedure Pn () {}
Initialization code ( .) { } }
}
65
Operating Systems
10CS53
void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self [i].wait;
}
void putdown (int i) { state[i] = THINKING; // test left and right neighbors
test((i + 4) % 5); test((i + 1) % 5); }
void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] !=
EATING) ) {
state[i] = EATING ; self[i].signal () ; } }
initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING;
}
} ->Each philosopher I invokes the operations pickup() and putdown() in the following sequence:
dp.pickup (i) EAT dp.putdown (i)
Monitor Implementation Using Semaphores
1. Variables semaphore mutex; // (initially = 1) semaphore next; // (initially = 0) int next-count =
0;
2.Each procedure F will be replaced by wait(mutex);
body of F; if (next-count > 0)
66
Operating Systems
10CS53
signal(next); else
signal(mutex); wait(x-sem); x-count--;
6. The operation x.signal can be implemented as:
if (x-count > 0) { next-count++; signal(x-sem); wait(next); next-count--;
}
Initialization
x Set
full buffer slots to 0. i.e., semaphore Full = 0. x Set empty buffer slots to N. i.e., semaphore empty = N. x For
control access to critical section set mutex to 1. i.e., semaphore mutex = 1.
Producer ( ) WHILE (true) produce-Item ( ); P (empty); P (mutex); enter-Item ( ) V (mutex) V (full);
Consumer ( )
WHILE (true) P (full) P (mutex); remove-Item ( ); V (mutex); V (empty);
consume-Item (Item)
IMPORTANT QUESTIONS:
1. What is the meaning of the term busy waiting?
2. Explain semaphores with the help of an example.
3. Explain dining philosophers problem, and how it is solved.
4. What is race condition? Explain how it is handled.
5. Define process synchronization.
6. How is producer-consumer problem with the help of semaphores?
67
Operating Systems
10CS53
UNIT 4 DEADLOCK
TOPICS
4.9 DEADLOCKS
4.10 SYSTEM MODEL
4.11 DEADLOCK CHARACTERIZATION
4.12 METHODS FOR HANDLING DEADLOCKS
4.13 DEADLOCK PREVENTION
4.14 DEADLOCK AVOIDANCE
4.15 DEADLOCK DETECTION
4.16 RECOVERY FROM DEADLOCK
68
Operating Systems
10CS53
4.1 DEADLOCKS
When processes request a resource and if the resources are not available at that time the process enters
into waiting state. Waiting process may not change its state because the resources they are requested are held
by other process. This situation is called deadlock.
The situation where the process waiting for the resource i.e., not available is called deadlock.
4.2 SYSTEM MODEL
A system may consist of finite number of resources and is distributed among number of processes.
There resources are partitioned into several instances each with identical instances.
A process must request a resource before using it and it must release the resource after using it. It can
request any number of resources to carry out a designated task. The amount of resource requested may not
exceed the total number of resources available.
A process may utilize the resources in only the following sequences:
1. Request:-If the request is not granted immediately then the requesting process must wait it can acquire the
resources.
2. Use:-The process can operate on the resource.
3. Release:-The process releases the resource after using it.
Deadlock may involve different types of resources.
For eg:-Consider a system with one printer and one tape drive. If a process Pi currently holds a
printer and a process Pj holds the tape drive. If process Pi request a tape drive and process Pj request a
printer then a deadlock occurs.
Multithread programs are good candidates for deadlock because they compete for shared
resources.
4.3 DEADLOCK CHARACTERIZATION
Necessary Conditions:A deadlock situation can occur if the following 4 conditions occur simultaneously in a
system:-1. Mutual Exclusion:Only one process must hold the resource at a time. If any other process requests
for the resource, the requesting process must be delayed until the resource has been released.
1. Hold and Wait:-A process must be holding at least one resource and waiting to acquire additional
resources that are currently being held by the other process.
2. No Preemption:-Resources cant be preempted i.e., only the process holding the resources must release it
after the process has completed its task.
3. Circular Wait:-A set {P0,P1..Pn} of waiting process must exist such that P0 is waiting for a resource
i.e., held by P1, P1 is waiting for a resource i.e., held by P2. Pn-1 is waiting for resource held by process
Pn and Pn is waiting for the resource i.e., held by P1. All the four conditions must hold for a deadlock to
occur.
69
Operating Systems
7.
10CS53
A directed edge from process Pi to resource type Rj denoted by Pi->Ri indicates that Pi requested
an instance of resource Rj and is waiting. This edge is called Request edge.
8. A directed edge Ri-> Pj signifies that resource Rj is held by process Pi. This is called
Assignment edge
Eg:
R1
R3
R2
R4
If the graph contain no cycle, then no process in the system is deadlock. If the graph contains a cycle
then a deadlock may exist.
If each resource type has exactly one instance than a cycle implies that a deadlock has occurred. If
each resource has several instances then a cycle do not necessarily implies that a deadlock has occurred.
4.4 METHODS FOR HANDLING DEADLOCKS
There are three ways to deal with deadlock problem x We can use a protocol to prevent deadlocks ensuring
that the system will never enter into the
deadlock state. x We allow a system to enter into deadlock state, detect it and recover from it. x We
ignore the problem and pretend that the deadlock never occur in the system. This is used by
most OS including UNIX.
To ensure that the deadlock never occur the system can use either deadlock avoidance or a deadlock
prevention.
Deadlock prevention is a set of method for ensuring that at least one of the necessary conditions does
not occur.
Deadlock avoidance requires the OS is given advance information about which resource a process
will request and use during its lifetime.
If a system does not use either deadlock avoidance or deadlock prevention then a deadlock situation
may occur. During this it can provide an algorithm that examines the state of the system to determine whether
a deadlock has occurred and algorithm to recover from deadlock.
Dept of CSE, SJBIT
70
Operating Systems
10CS53
HANDLING DEADLOCKS
There are three ways to deal with deadlock problem x We can use a protocol to prevent deadlocks ensuring
that the system will never enter into the deadlock state. x We allow a system to enter into deadlock state,
detect it and recover from it. x We ignore the problem and pretend that the deadlock never occur in the
system. This is used by most OS including UNIX.
To ensure that the deadlock never occur the system can use either deadlock avoidance or a deadlock
prevention.
Deadlock prevention is a set of method for ensuring that at least one of the necessary conditions does
not occur.
Deadlock avoidance requires the OS is given advance information about which resource a process
will request and use during its lifetime.
If a system does not use either deadlock avoidance or deadlock prevention then a deadlock situation
may occur. During this it can provide an algorithm that examines the state of the system to determine whether
a deadlock has occurred and algorithm to recover from deadlock.
71
Operating Systems
10CS53
72
Operating Systems
10CS53
Deadlock prevention algorithm may lead to low device utilization and reduces system throughput.
Avoiding deadlocks requires additional information about how resources are to be requested. With the
knowledge of the complete sequences of requests and releases we can decide for each requests whether or not
the process should wait.
For each requests it requires to check the resources currently available, resources that are currently
allocated to each processes future requests and release of each process to decide whether the current requests
can be satisfied or must wait to avoid future possible deadlock.
A deadlock avoidance algorithm dynamically examines the resources allocation state to ensure that a
circular wait condition never exists. The resource allocation state is defined by the number of available and
allocated resources and the maximum demand of each process.
Safe State:
A state is a safe state in which there exists at least one order in which all the process will run
completely without resulting in a deadlock.
A sequence of processes <P1,P2,..Pn> is a safe sequence for the current allocation state if for
each Pi the resources that Pi can request can be satisfied by the currently available resources.
If the resources that Pi requests are not currently available then Pi can obtain all of its needed resource
to complete its designated task.
Whenever a process request a resource i.e., currently available, the system must decide whether
resources can be allocated immediately or whether the process must wait. The request is granted only if the
allocation leaves the system in safe state.
In this, if a process requests a resource i.e., currently available it must still have to wait. Thus resource
utilization may be lower than it would be without a deadlock avoidance algorithm.
73
Operating Systems
10CS53
x When a process Pi requests the resource Rj, the claim edge is converted to a request edge. x When resource Rj
is released by process Pi, the assignment edge RjPi is replaced by the claim edge PiRj.
When a process Pi requests resource Rj the request is granted only if converting the request edge
PiRj to as assignment edge RjPi do not result in a cycle. Cycle detection algorithm is used to
detect the cycle. If there are no cycles then the allocation of the resource to process leave the system
in safe state
. Bankers Algorithm:
This algorithm is applicable to the system with multiple instances of each resource types, but this is
less efficient then the resource allocation graph algorithm.
When a new process enters the system it must declare the maximum number of resources that it may
need. This number may not exceed the total number of resources in the system. The system must determine
that whether the allocation of the resources will leave the system in a safe state or not. If it is so resources are
allocated else it should wait until the process release enough resources.
Several data structures are used to implement the bankers algorithm. Let n be the number of
processes in the system and m be the number of resources types. We need the following data structures:
x Available:-A vector of length m indicates the number of available resources. If Available[i]=k, then k
instances of resource type Rj is available.
x Max:-An n*m matrix defines the maximum demand of each process if Max[i,j]=k, then Pi may request
at most k instances of resource type Rj.
x Allocation:-An n*m matrix defines the number of resources of each type currently allocated to each
process. If Allocation[i,j]=k, then Pi is currently k instances of resource type Rj.
x Need:-An n*m matrix indicates the remaining resources need of each process. If Need[i,j]=k, then Pi
may need k more instances of resource type Rj to compute its task. So Need[i,j]=Max[i,j]Allocation[i]
Safety Algorithm:
This algorithm is used to find out whether or not a system is in safe state or not. Step 1. Let work and
finish be two vectors of length M and N respectively. Initialize work = available and Finish[i]=false for
i=1,2,3,.n
Step 2. Find i such that both Finish[i]=false Need i <= work
If no such i exist then go to step 4
Step 3. Work = work + Allocation Finish[i]=true Go to step 2
Step 4. If finish[i]=true for all i, then the system is in safe state. This algorithm may require an order of m*n*n
operation to decide whether a state is safe.
74
Operating Systems
10CS53
Resource Request Algorithm:Let Request(i) be the request vector of process Pi. If Request(i)[j]=k,
then process Pi wants K instances of the resource type Rj. When a request for resources is made by process Pi
the following actions are taken.
x If Request(i) <= Need(i) go to step 2 otherwise raise an error condition since the process has exceeded its
maximum claim. x If Request(i) <= Available go to step 3 otherwise Pi must wait. Since the resources are not
available. x If the system want to allocate the requested resources to process Pi then modify the state
as follows. Available = Available Request(i) Allocation(i) = Allocation(i) + Request(i)
Need(i) = Need(i) Request(i)
x If the resulting resource allocation state is safe, the transaction is complete and Pi is allocated
its resources. If the new state is unsafe then Pi must wait for Request(i) and old resource
allocation state is restored.
4.7 DEADLOCK DETECTION
If a system does not employ either deadlock prevention or a deadlock avoidance algorithm then a deadlock
situation may occur. In this environment the system may provide x An algorithm that examines the state of
the system to determine whether a deadlock has occurred. x An algorithm to recover from the deadlock.
If all the resources have only a single instance then we can define deadlock detection algorithm that
uses a variant of resource allocation graph called a wait for graph. This graph is obtained by removing the
nodes of type resources and removing appropriate edges.
An edge from Pi to Pj in wait for graph implies that Pi is waiting for Pj to release a resource that Pi
needs.
An edge from Pi to Pj exists in wait for graph if and only if the corresponding resource allocation
graph contains the edges PiRq and RqPj.
Deadlock exists within the system if and only if there is a cycle. To detect deadlock the system needs
an algorithm that searches for cycle in a graph.
75
Operating Systems
10CS53
76
Operating Systems
10CS53
o Available:-Is a vector of length m indicating the number of available resources of each type .
o Allocation:-Is an m*n matrix which defines the number of resources of each type currently
allocated to each process.
o Request:-Is an m*n matrix indicating the current request of each process. If request[i,j]=k then Pi is
requesting k more instances of resources type Rj.
Step 1. let work and finish be vectors of length m and n respectively. Initialize Work = available/expression
For i=0,1,2.n if allocation(i)!=0 then Finish[i]=0
else Finish[i]=true
Step 2. Find an index(i) such that both Finish[i]
= false Request(i)<=work
If no such I exist go to step 4.
Step 3. Work = work + Allocation(i) Finish[i] = true Go to step 2. Step 4. If Finish[i] = false for some I where
m>=i>=1. When a system is in a deadlock state. This algorithm needs an order of m*n square operations to
detect whether the system is in deadlock state or not.
IMPORTANT QUESTIONS:
1. For the following snapshot of the system find the safe sequence (using Bankersalgorithm).
77
Operating Systems
10CS53
3. The operating system contains three resources. The numbers of instances of each resource type are (7, 7,
10). The current allocation state is given below.
a. Is the current allocation is safe?
b. find need?
c. Can the request made by the process P1(1,1,0) can be granted?
78
Operating Systems
10CS53
TOPICS
5.13 MEMORY MANAGEMENT STRATEGIES
5.14 BACKGROUND
5.15 SWAPPING
5.16 CONTIGUOUS MEMORY ALLOCATION
5.17 PAGING, STRUCTURE OF PAGE TABLE
5.18 SEGMENTATION
5.19 VIRTUAL MEMORY MANAGEMENT
5.20 BACKGROUND,DEMAND PAGING
5.21 COPY-ON-WRITE
5.22 PAGE REPLACEMENT
5.23 ALLOCATION OF FRAMES
5.24 THRASHING
79
Operating Systems
10CS53
Address Binding:
x Programs are stored on the secondary storage disks as binary executable files. x When the programs are to
be executed they are brought in to the main memory and placed within a process. x The collection of
processes on the disk waiting to enter the main memory forms the input queue. x One of the processes which
are to be executed is fetched from the queue and placed in the main memory. x During the execution it fetches
instruction and data from main memory. After the process terminates it returns back the memory space. x
During execution the process will go through different steps and in each step the address is
represented in different ways. x In source program the address is symbolic. x The compiler converts
the symbolic address to re-locatable address. x The loader will convert this re-locatable address to
absolute address.
Binding of instructions and data can be done at any step along the way:
1. Compile time:-If we know whether the process resides in memory then absolute code can be generated. If
the static address changes then it is necessary to re-compile the code from the beginning.
2. Load time:-If the compiler doesnt know whether the process resides in memory then it generates the relocatable code. In this the binding is delayed until the load time.
3. Execution time:-If the process is moved during its execution from one memory segment to another then
the binding is delayed until run time. Special hardware is used for this. Most of the general purpose
operating system uses this method.
80
Operating Systems
10CS53
5.2 BACKGROUND
81
Operating Systems
10CS53
The above figure shows that dynamic re-location which implies mapping from virtual addresses space to
physical address space and is performed by the hardware at run time.
Re-location is performed by the hardware and is invisible to the user dynamic relocation makes it
possible to move a partially executed process from one area of memory to another without affecting.
Dynamic Loading:
x For a process to be executed it should be loaded in to the physical memory. The size of the
process is limited to the size of the physical memory. x Dynamic loading is used to obtain better memory
utilization. x In dynamic loading the routine or procedure will not be loaded until it is called. x Whenever a
routine is called, the calling routine first checks whether the called routine is
already loaded or not. If it is not loaded it cause the loader to load the desired program in to the memory and
updates the programs address table to indicate the change and control is passed to newly called routine.
Advantage:x Gives better memory utilization. x Unused routine is never loaded. x Do not need special
operating system support. x This method is useful when large amount of codes are needed to
handle in frequently
occurring cases.
Overlays:
x The size of the process is limited to the size of physical memory. If the size is more than the size of
physical memory then a technique called overlays is used.
x The idea is to load only those instructions and data that are needed at any given time. When other
instructions are needed, they are loaded in to memory apace that was previously occupied by the
Dept of CSE, SJBIT
82
Operating Systems
10CS53
x Thus we define 2 overlays, overlay A which consist of symbol table, common routine and pass1 and
overlay B which consists of symbol table, common routine and pass-2.
x We add an overlay driver and start overlay A in memory. When we finish pass-1 we jump to
overlay driver, then the control is transferred to pass-2.
x Thus we can run assembler in 150k of memory.
x The code for overlays A and B are kept on disk as absolute memory images. Special re-location
and linking algorithms are needed to construct the overlays. They can be implemented using simple
file structures.
5.3 SWAPPING
Swapping is a technique of temporarily removing inactive programs from the memory of the system.
A process can be swapped temporarily out of the memory to a backing store and then brought back in
to the memory for continuing the execution. This process is called swapping.
83
Operating Systems
10CS53
Eg:-In a multi-programming environment with a round robin CPU scheduling whenever the time
quantum expires then the process that has just finished is swapped out and a new process swaps in to the
memory for execution.
A variation of swap is priority based scheduling. When a low priority is executing and if a high
priority process arrives then a low priority will be swapped out and high priority is allowed for execution.
This process is also called as Roll out and Roll in.
Normally the process which is swapped out will be swapped back to the same memory space that is
occupied previously. This depends upon address binding.
If the binding is done at load time, then the process is moved to same memory location.
If the binding is done at run time, then the process is moved to different memory location. This is
because the physical address is computed during run time.
Swapping requires backing store and it should be large enough to accommodate the copies of all
memory images.
The system maintains a ready queue consisting of all the processes whose memory images are on the
backing store or in memory that are ready to run.
Swapping is constant by other factors:x To swap a process, it should be completely idle. x A process
may be waiting for an i/o operation. If the i/o is asynchronously accessing the
user memory for i/o buffers, then the process cannot be swapped.
5.4 CONTIGUOUS MEMORY ALLOCATION
x One of the simplest method for memory allocation is to divide memory in to several fixed partition.
Each partition contains exactly one process. The degree of multi-programming depends on the
number of partitions.
x In multiple partition method, when a partition is free, process is selected from the input queue
and is loaded in to free partition of memory. x When process terminates, the memory partition
becomes available for another process. x Batch OS uses the fixed size partition scheme. x The OS keeps a
table indicating which part of the memory is free and is occupied. x When the process enters the system it
will be loaded in to the input queue. The OS keeps track
of the memory requirement of each process and the amount of memory available and determines which
process to allocate the memory. x When a process requests, the OS searches for large hole for this process,
hole is a large block of free memory available. x If the hole is too large it is split in to two. One part is
allocated to the requesting process and other is returned to the set of holes. x The set of holes are searched to
determine which hole is best to allocate. There are three strategies to select a free hole:
o First bit:-Allocates first hole that is big enough. This algorithm scans memory from the beginning
and selects the first available block that is large enough to hold the process.
o Best bit:-It chooses the hole i.e., closest in size to the request. It allocates the smallest hole i.e., big
enough to hold the process.
o Worst fit:-It allocates the largest hole to the process request. It searches for the largest hole in the
entire list.
84
Operating Systems
10CS53
First fit and best fit are the most popular algorithms for dynamic memory allocation. First fit is
generally faster. Best fit searches for the entire list to find the smallest hole i.e., large enough. Worst fit
reduces the rate of production of smallest holes.
Memory Protection:
x Memory protection means protecting the OS from user process and protecting process from one
another. x Memory protection is provided by using a re-location register, with a limit register. x Relocation register contains the values of smallest physical address and limit register contains
range of logical addresses. (Re-location = 100040 and limit = 74600). x The
logical address must be less than the limit register, the MMU maps the logical address
dynamically by adding the value in re-location register. x When the CPU scheduler
selects a process for execution, the dispatcher loads the re-location and limit register
with correct values as a part of context switch. x Since every address generated by the
CPU is checked against these register we can protect the OS and other users programs
and data from being modified.
Fragmentation:
In Internal Fragmentation there is wasted space internal to a portion due to the fact that block of
data loaded is smaller than the partition. Eg:-If there is a block of 50kb and if the process requests
40kb and if the block is allocated to the process then there will be 10kb of memory left.
External Fragmentation exists when there is enough memory space exists to satisfy the request, but it
not contiguous i.e., storage is fragmented in to large number of small holes.
One solution for over-coming external fragmentation is compaction. The goal is to move all the free
memory together to form a large block. Compaction is not possible always. If the relocation is static and is
done at load time then compaction is not possible. Compaction is possible if the re-location is dynamic and
done at execution time.
Another possible solution to the external fragmentation problem is to permit the logical address space
of a process to be non-contiguous, thus allowing the process to be allocated physical memory whenever the
latter is available.
5.5 PAGING AND STRUCTURE OF PAGE TABLE
x Paging is a memory management scheme that permits the physical address space of a
process to be non-contiguous. Support for paging is handled by hardware. x It is used to avoid
external fragmentation. x Paging avoids the considerable problem of fitting the varying sized
memory chunks on
to the backing store.
85
Operating Systems
10CS53
x When some code or date residing in main memory need to be swapped out, space must be found
on backing store.
Basic Method:
x Physical memory is broken in to fixed sized blocks called frames (f). x Logical memory is broken in to
blocks of same size called pages (p). x When a process is to be executed its pages are loaded in to
available frames from backing store. x The blocking store is also divided in to fixed-sized blocks of same
size as memory frames. x The following figure shows paging hardware:
x Logical address generated by the CPU is divided in to two parts: page number (p) and page offset (d).
x The page number (p) is used as index to the page table. The page table contains base address of each page in
physical memory. This base address is combined with the page offset to define the physical memory i.e.,
sent to the memory unit.
x
x The page size is defined by the hardware. The size of a power of 2, varying between 512 bytes and 10Mb
per page.
x If the size of logical address space is 2^m address unit and page size is 2^n, then high order m-n designates
the page number and n low order bits represents page offset.
Eg:-To show how to map logical memory in to physical memory consider a page size of 4 bytes and physical
memory of 32 bytes (8 pages).
a. Logical address 0 is page 0 and offset 0. Page 0 is in frame 5. The logical address 0 maps to
physical address 20. [(5*4) + 0].
b. Logical address 3 is page 0 and offset 3 maps to physical address 23 [(5*4) + 3].
86
Operating Systems
10CS53
c. Logical address 4 is page 1 and offset 0 and page 1 is mapped to frame 6. So logical address 4 maps to
physical address 24 [(6*4) + 0].
d. Logical address 13 is page 3 and offset 1 and page 3 is mapped to frame 2. So logical address 13 maps to
physical address 9 [(2*4) + 1].
When an associative register is presented with an item, it is compared with all the key values, if found
the corresponding value field is return and searching is fast.
TLB is used with the page table as follows:x TLB contains only few page table entries. x When a logical
address is generated by the CPU, its page number along with the frame
number is added to TLB. If the page number is found its frame memory is used to access the actual memory.
x If the page number is not in the TLB (TLB miss) the memory reference to the page table
is made. When the frame number is obtained use can use it to access the memory. x If the
TLB is full of entries the OS must select anyone for replacement. x Each time a new page table is
selected the TLB must be flushed [erased] to ensure that
next executing process do not use wrong information. x The percentage of time that a page
number is found in the TLB is called HIT ratio.
87
Operating Systems
10CS53
Protection:
x
Memory protection in paged environment is done by protection bits that are
associated with each frame these bits are kept in page table. x One bit can define a page to be
read-write or read-only. x
To find the correct frame number every reference to the memory should go through page
table. At the same time physical address is computed. x The protection bits can be
checked to verify that no writers are made to read-only
page. x Any attempt to write in to read-only page causes a hardware trap to the OS. x This
approach can be used to provide protection to read-only, read-write or
execute-only pages. x One more bit is generally added to each entry in the page table: a valid-invalid bit.
x A valid bit indicates that associated page is in the processes logical address space and
thus it is a legal or valid page.
x If the bit is invalid, it indicates the page is not in the processes logical addressed space
and illegal. Illegal addresses are trapped by using the valid-invalid bit.
x The OS sets this bit for each page to allow or disallow accesses to that page.
Recent computer system support a large logical address apace from 2^32 to 2^64. In this system the
page table becomes large. So it is very difficult to allocate contiguous main memory for page table. One
simple solution to this problem is to divide page table in to smaller pieces. There are several ways to
accomplish this division.
One way is to use two-level paging algorithm in which the page table itself is also paged. Eg:-In a 32
bit machine with page size of 4kb. A logical address is divided in to a page number consisting of 20 bits and a
page offset of 12 bit. The page table is further divided since the page table is paged, the page number is
further divided in to 10 bit page number and a 10 bit offset. So the logical address is
88
Operating Systems
10CS53
Hashed page table handles the address space larger than 32 bit. The virtual page number is used as
hashed value. Linked list is used in the hash table which contains a list of elements that hash to the same
location.
Each element in the hash table contains the following three fields:x Virtual page number x Mapped
page frame value x Pointer to the next element in the linked list
Working:
x Virtual page number is taken from virtual address.
x Virtual page number is hashed in to hash table.
x Virtual page number is compared with the first element of linked list.
x Both the values are matched, that value is (page frame) used for
calculating the physical address. x If not match then entire linked list is searched for matching virtual page
number. x Clustered pages are similar to hash table but one difference is that each entity in the hash table
refer to several pages.
c. Inverted Page Tables:Since the address spaces have grown to 64 bits, the traditional page tables
become a problem. Even with two level page tables. The table can be too large to handle. An inverted page
table has only entry for each page in memory. Each entry consisted of virtual address of the page stored in
that read-only location with information about the process that owns that page.
89
Operating Systems
10CS53
Each virtual address in the Inverted page table consists of triple <process-id , page number , offset >. The
inverted page table entry is a pair <process-id , page number>. When a memory reference is made, the part of
virtual address i.e., <process-id , page number> is presented in to memory sub-system. The inverted page
table is searched for a match. If a match is found at entry I then the physical address <i , offset> is generated.
If no match is found then an illegal address access has been attempted. This scheme decreases the amount of
memory needed to store each page table, it increases the amount of time needed to search the table when a
page reference occurs. If the whole table is to be searched it takes too long.
Advantage:
x Eliminates fragmentation.
x Support high degree of multiprogramming.
x Increases memory and processor utilization.
x Compaction overhead required for the re-locatable partition scheme is also eliminated.
Disadvantage:
x Page address mapping hardware increases the cost of the computer.
x Memory must be used to store the various tables like page tables, memory map table etc.
x Some memory will still be unused if the number of available block is not sufficient for
the address space of the jobs to be run.
Shared Pages:
Another advantage of paging is the possibility of sharing common code. This is useful in timesharing
environment. Eg:-Consider a system with 40 users, each executing a text editor. If the text editor is of
150k
and data space is 50k, we need 8000k for 40 users. If the code is reentrant it can be shared. Consider the
following figure
90
Operating Systems
10CS53
If the code is reentrant then it never changes during execution. Thus two or more processes can
execute same code at the same time. Each process has its own copy of registers and the data of two processes
will vary.
Only one copy of the editor is kept in physical memory. Each users page table maps to same physical
copy of editor but date pages are mapped to different frames.
So to support 40 users we need only one copy of editor (150k) plus 40 copies of 50k of data space i.e.,
only 2150k instead of 8000k.
5.6 SEGMENTATION
Basic method:
x Most users do not think memory as a linear array of bytes rather the users thinks
memory as a collection of variable sized segments which are dedicated to a particular
use such as code, data, stack, heap etc.
x A logical address is a collection of segments. Each segment has a name and length. The
address specifies both the segment name and the offset within the segments.
x The users specifies address by using two quantities: a segment name and an offset. x For simplicity the
segments are numbered and referred by a segment number. So the logical address consists of <segment
number, offset>.
Hardware support:x We must define an implementation to map 2D user defined address in to 1D physical
address.
x This mapping is affected by a segment table. Each entry in the segment table has a
segment base and segment limit. The segment base contains the starting physical
address where the segment resides and limit specifies the length of the segment.
91
Operating Systems
10CS53
The use of segment table is shown in the above figure:x Logical address consists of two parts: segment
numbers and an offsetd
to that segment. x The segment number is used as an index to segment table. x The
offset d must bi in between 0 and limit, if not an error is reported to
OS. x If legal the offset is added to the base to generate the actual physical address. x The segment table is an
array of base limit register pairs.
Protection and Sharing:x A particular advantage of segmentation is the association of protection with
the segments.
x The memory mapping hardware will check the protection bits associated with each segment
table entry to prevent illegal access to memory like attempts to write in to read-only segment.
x Another advantage of segmentation involves the sharing of code or data. Each process has a
segment table associated with it. Segments are shared when the entries in the segment tables
of two different processes points to same physical location.
x Sharing occurs at the segment table. Any information can be shared at the segment level.
Several segments can be shared so a program consisting of several segments can be shared.
x We can also share parts of a program.
Advantages:x Eliminates fragmentation. x Provides virtual growth. x Allows dynamic segment
growth. x Assist dynamic linking. x Segmentation is visible.
Differences between segmentation and paging:Segmentation:
x Program is divided in to variable sized segments. x User is responsible for dividing the program
in to segments.
x Segmentation is slower than paging.
x Visible to user.
x Eliminates internal fragmentation.
x Suffers from external fragmentation.
x Process or user segment number, offset to calculate absolute address.
Paging:
x Programs are divided in to fixed size pages.
x Division is performed by the OS.
x Paging is faster than segmentation. x Invisible to user. x Suffers from internal fragmentation. x
No external fragmentation. x Process or user page number, offset to calculate absolute address.
Dept of CSE, SJBIT
92
Operating Systems
10CS53
Virtual memory is the separation of users logical memory from physical memory. This separation
allows an extremely large virtual memory to be provided when these is less physical memory.
Separating logical memory from physical memory also allows files and memory to be shared by
several different processes through page sharing.
A demand paging is similar to paging system with swapping when we want to execute a process we
swap the process the in to memory otherwise it will not be loaded in to memory.
A swapper manipulates the entire processes, where as a pager manipulates individual pages of the
process.
Basic concept:Instead of swapping the whole process the pager swaps only the necessary
pages in to memory. Thus it avoids reading unused pages and decreases the swap time and amount of
physical memory needed.
The valid-invalid bit scheme can be used to distinguish between the pages that are on the disk and that are in
memory.
o If the bit is valid then the page is both legal and is in memory.
o If the bit is invalid then either page is not valid or is valid but is currently
on the disk. Marking a page as invalid will have no effect if the processes
never access to that page. Suppose if it access the page which is marked invalid, causes a page fault trap. This
may result in failure of OS to bring the desired page in to memory.
93
Operating Systems
10CS53
The step for handling page fault is straight forward and is given below:
1. We check the internal table of the process to determine whether the reference made is valid or invalid.
2. If invalid terminate the process,. If valid, then the page is not yet loaded and we now page it in.
3. We find a free frame.
4. We schedule disk operation to read the desired page in to newly allocated frame.
5. When disk reed is complete, we modify the internal table kept with the process to indicate that the page is
now in memory.
6. We restart the instruction which was interrupted by illegal address trap. The process can
now access the page. In extreme cases, we start the process without pages in memory. When
the OS points to the instruction of process it generates a page fault. After this page is brought in to memory
the process continues to execute, faulting as necessary until every demand paging i.e., it never brings the page
in to memory until it is required.
Hardware support:
For demand paging the same hardware is required as paging and swapping.
1. Page table:-Has the ability to mark an entry invalid through valid-invalid bit.
2. Secondary memory:-This holds the pages that are not present in main memory. I
Performance of demand paging:Demand paging can have significant effect on the performance of the
computer system.
Let P be the probability of the page fault (0<=P<=1)
Effective access time = (1-P) * ma + P * page fault. Where P = page fault and ma = memory access
time. Effective access time is directly proportional to page fault rate. It is important to keep page fault
rate low in demand paging.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
94
Operating Systems
10CS53
The step for handling page fault is straight forward and is given below:
1. We check the internal table of the process to determine whether the reference made is valid or invalid.
2. If invalid terminate the process,. If valid, then the page is not yet loaded and we now page it in.
3. We find a free frame.
4. We schedule disk operation to read the desired page in to newly allocated frame.
5. When disk reed is complete, we modify the internal table kept with the process to indicate that the
page is now in memory.
6. We restart the instruction which was interrupted by illegal address trap. The process can
Demand paging is used when reading a file from disk in to memory. Fork () is used to create a process and it
initially bypass the demand paging using a technique called page sharing. Page sharing provides rapid speed
for process creation and reduces the number of pages allocated to the newly created process. Copy-on-write
technique initially allows the parent and the child to share the same pages. These pages are marked as copyon-write pages i.e., if either process writes to a shared page, a copy of shared page is created. Eg:-If a child
process try to modify a page containing portions of the stack; the OS recognizes them as a copy-on-write page
and create a copy of this page and maps it on to the address space of the child process. So the child process
will modify its copied page and not the page belonging to parent. The new pages are obtained from the pool
of free pages.
95
Operating Systems
10CS53
.
.
.
.
.
5.9 COPY-ON-WRITE
Demand paging is used when reading a file from disk in to memory. Fork () is used to create a process and it
initially bypass the demand paging using a technique called page sharing. Page sharing provides rapid speed
for process creation and reduces the number of pages allocated to the newly created process. Copy-on-write
technique initially allows the parent and the child to share the same pages. These pages are marked as copyon-write pages i.e., if either process writes to a shared page, a copy of shared page is created. Eg:-If a child
process try to modify a page containing portions of the stack; the OS recognizes them as a copy-on-write page
and create a copy of this page and maps it on to the address space of the child process. So the child process
will modify its copied page and not the page belonging to parent. The new pages are obtained from the pool
of free pages.
Memory Mapping:Standard system calls i.e., open (), read () and write () is used for sequential read of a file.
Virtual memory is used for this. In memory mapping a file allows a part of the virtual address space to be
logically associated with a file. Memory mapping a file is possible by mapping a disk block to page in
memory.
5.10 PAGE REPLACEMENT
Demand paging shares the I/O by not loading the pages that are never used.
Demand paging also improves the degree of multiprogramming by allowing more process to run at
the some time.
Page replacement policy deals with the solution of pages in memory to be replaced by a new page that
must be brought in. When a user process is executing a page fault occurs.
The hardware traps to the operating system, which checks the internal table to see that this is a page
fault and not an illegal memory access.
The operating system determines where the derived page is residing on the disk, and this finds that
thee are no free frames on the list of free frames.
When all the frames are in main memory, it is necessary to bring a new page to satisfy the page fault,
replacement policy is concerned with selecting a page currently in memory to be replaced.
The page i,e to be removed should be the page i,e least likely to be referenced in future.
96
Operating Systems
10CS53
Victim Page
The page that is supported out of physical memory is called victim page. x If no frames are free, the two page
transforms come (out and one in) are read. This will see the effective access time.
x Each page or frame may have a dirty (modify) bit associated with the hardware. The modify bit for a
page is set by the hardware whenever any word or byte in the page is written into, indicating that the
page has been modified.
x When we select the page for replacement, we check its modify bit. If the bit is set, then the page is
modified since it was read from the disk.
x If the bit was not set, the page has not been modified since it was read into memory. Therefore, if the
copy of the page has not been modified we can avoid writing the memory page to the disk, if it is
already there. Sum pages cannot be modified.
We must solve two major problems to implement demand paging: we must develop a frame allocation
algorithm and a page replacement algorithm. If we have multiple processors in memory, we must decide how
many frames to allocate and page replacement is needed.
97
Operating Systems
10CS53
x The first three references (7,0,1) cases page faults and are brought into the empty
frames. x The next references 2 replaces page 7 because the page 7 was brought in first. x
Since 0 is the next references and 0 is already in memory e has no page faults. x The next
references 3 results in page 0 being replaced so that the next references to 0
causer page fault. This will continue till the end of string. There are 15 faults all together.
Beladys Anamoly
For some page replacement algorithm, the page fault may increase as the number of allocated frames
increases. FIFO replacement algorithm may face this problem.
Optimal Algorithm
x Optimal page replacement algorithm is mainly to solve the problem of Beladys
Anamoly. x Optimal page replacement algorithm has the lowest page fault rate of all
algorithms. x An optimal page replacement algorithm exists and has been called OPT.
The working is simple Replace the page that will not be used for the longest period of time Example:
consider the following reference string
x The first three references cause faults that fill the three empty frames.
x The references to page 2 replaces page 7, because 7 will not be used until reference 18.
Dept of CSE, SJBIT
98
Operating Systems
10CS53
FIFO algorithm uses the time when the pages was built in and OPT uses the time when a page is to be
used.
The LRU algorithm replaces the pages that have not been used for longest period of time. x The LRU
associated its pages with the time of that pages last use. x This strategy is the optimal page replacement
algorithm looking backward in time
rather than forward. Ex: consider the following reference string
99
Operating Systems
10CS53
LRU Approximation
An LRU page replacement algorithm should update the page removal status information after every
page reference updating is done by software, cost increases.
But hardware LRU mechanism tend to degrade execution performance at the same time, then
substantially increases the cost. For this reason, simple and efficient algorithm that approximation the LRU
have been developed. With h/w support the reference bit was used. A reference bit associate with each
memory block and this bit automatically set to 1 by the h/w whenever the page is referenced. The single
reference bit per clock can be used to approximate LRU removal.
The page removal s/w periodically resets the reference bit to 0, write the execution of the users job
causes some reference bit to be set to 1.
If the reference bit is 0 then the page has not been referenced since the last time the reference bit was
set to 0.
100
Operating Systems
10CS53
The allocation policy in a virtual memory controls the operating system decision regarding the
amount of real memory to be allocated to each active process.
In a paging system if more real pages are allocated, it reduces the page fault frequency and improved
turnaround throughput.
If too few pages are allocated to a process its page fault frequency and turnaround times may
deteriorate to unacceptable levels.
The minimum number of frames per process is defined by the architecture, and the maximum number
of frames. This scheme is called equal allocation.
With multiple processes competing for frames, we can classify page replacement into two broad
categories a) Local Replacement: requires that each process selects frames from only its own sets of
allocated frame. b). Global Replacement: allows a process to select frame from the set of all frames. Even
if the frame is currently allocated to some other process, one process can take a frame from another.
In local replacement the number of frames allocated to a process do not change but with global replacement
number of frames allocated to a process do not change global replacement results in greater system
throughput.
Other consideration
There is much other consideration for the selection of a replacement algorithm and allocation policy.
1) Preparing: This is an attempt to present high level of initial paging. This strategy is to bring into memory all
the pages at one time. 2) TLB Reach: The TLB reach refers to the amount of memory accessible from the
TLB and is simply the no of entries multiplied by page size.
3) Page Size: following parameters are considered a) page size us always power of 2 (from 512 to 16k) b)
Internal fragmentation is reduced by a small page size. c) A large page size reduces the number of
pages needed.
4) Invented Page table: This will reduces the amount of primary memory i,e. needed to track virtual to physical
address translations. 5) Program Structure: Careful selection of data structure can increases the locality and
hence lowers the page fault rate and the number of pages in working state.
6) Real time Processing: Real time system almost never has virtual memory. Virtual memory is the
antithesis of real time computing, because it can introduce unexpected long term delay in the
execution of a process.
5.12 THRASHING
If the number of frames allocated to a low-priority process falls below the minimum number required
by the computer architecture then we suspend the process execution.
If the processes do not have enough number of frames, it will quickly page fault. During this it must
replace some page that is not currently in use. Consequently it quickly faults again and again. The process
continues to fault, replacing pages for which it then faults and brings back. This high paging activity is called
thrashing. The phenomenon of excessively moving pages back and forth b/w memory and secondary has been
called thrashing.
101
Operating Systems
10CS53
As the degree of multi programming increases, more slowly until a maximum is reached. If the degree
of multi programming is increased further thrashing sets in and the cpu utilization drops sharply.
At this point, to increases CPU utilization and stop thrashing, we must increase degree of multi
programming. We can limit the effect of thrashing by using a local replacement algorithm. To prevent
thrashing, we must provide a process as many frames as it needs.
.
Locality of Reference: x As the process executes it moves from locality to locality. x A locality is a set
of pages that are actively used. x A program may consist of several different localities, which may
overlap. x Locality is caused by loops in code that find to reference arrays and other data structures by
indices. The ordered list of page number accessed by a program is called reference string. Locality is
of two types
1) spatial locality 2) temporal locality
102
Operating Systems
10CS53
x At any given time, all pages referenced by a process in its last 4 seconds of execution are
considered to compromise its working set.
x A process will never execute until its working set is resident in main memory.
x Pages outside the working set can be discarded at any movement.
Working sets are not enough and we must also introduce balance set.
a) If the sum of the working sets of all the run able process is greater than the size of memory the refuse some
process for a while.
b) Divide the run able process into two groups, active and inactive. The collection of active set is called the
balance set. When a process is made active its working set is loaded.
c) Some algorithm must be provided for moving process into and out of the balance set.
As a working set is changed, corresponding change is made to the balance set. Working set presents
thrashing by keeping the degree of multi programming as high as possible. Thus if optimizes the
CPU utilization. The main disadvantage of this is keeping track of the working set.
IMPORTANT QUESTIONS:
1. Name two differences between logical and physical addresses.
2. Explain the difference between internal and external fragmentation.
3. Describe the following allocation algorithms:
a. First fit
b. Best fit
c. Worst fit
4. Given memory partitions of 100K, 500K, 200K, 300K, and 600K (in order), how would each of the Firstfit, Best-fit, and Worst-fit algorithms place processes of 212K, 417K, 112K, and 426K (in order)? Which
algorithm makes the most efficient use of memory?
5. Why are page sizes always powers of 2?
6. Consider a logical address space of eight pages of 1024 words each, mapped onto a physical memory of 32
frames.
a. How many bits are there in the logical address?
b. How many bits are there in the physical address?
7. Why are segmentation and paging sometimes combined into one scheme?
8. Under what circumstances do page faults occur? Describe the actions taken by the operating system when a
page fault occurs.
9. Which of the following programming techniques and structures are good for a demand paged environment ?
Which are not good? Explain your answers.
a. Stack
b. Hashed symbol table
c. Sequential search
d. Binary search
e. Pure code
f. Vector operations
g. Indirection
10. Consider the following page-replacement algorithms. Rank these algorithms on a Five point
scale from bad to perfect according to their page-fault rate. Separate those algorithms
that suffer from Beladys anomaly from those that do not.
a. LRU replacement
b. FIFO replacement
Dept of CSE, SJBIT
103
Operating Systems
10CS53
104
Operating Systems
10CS53
105
Operating Systems
10CS53
File Attributes:-File attributes varies from one OS to other. The common file attributes are:
1
Name:-The symbolic file name is the only information kept in human readable form.
2
Identifier:-The unique tag, usually a number, identifies the file within the file system. It is the nonreadable name for a file.
3
Type:-This information is needed for those systems that supports different types.
4
Location:-This information is a pointer to a device and to the location of the file on that device.
5
Size:-The current size of the file and possibly the maximum allowed size are included in this attribute.
6
Protection:-Access control information determines who can do reading, writing, execute and so on.
7
Time, data and User Identification:-This information must be kept for creation, last modification and
last use. These data are useful for protection, security and usage monitoring.
File Operation:-File is an abstract data type. To define a file we need to consider the operation that can be
performed on the file. Basic operations of files are:
1. Creating a file:-Two steps are necessary to create a file. First space in the file system for file is found.
Second an entry for the new file must be made in the directory. The directory entry records the name of
the file and the location in the file system.
2. Writing a file:-System call is mainly used for writing in to the file. System call specify the name of the
file and the information i.e., to be written on to the file. Given the name the system search the entire
directory for the file. The system must keep a write pointer to the location in the file where the next write
to be taken place.
3. Reading a file:-To read a file system call is used. It requires the name of the file and the memory address.
Again the directory is searched for the associated directory and system must maintain a read pointer to the
location in the file where next read is to take place.
4. Delete a file:-System will search for the directory for which file to be deleted. If entry is found it releases
all free space. That free space can be reused by another file.
5. Truncating the file:-User may want to erase the contents of the file but keep its attributes. Rather than
forcing the user to delete a file and then recreate it, truncation allows all attributes to remain unchanged
except for file length.
Dept of CSE, SJBIT
106
Operating Systems
10CS53
6. Repositioning within a file:-The directory is searched for appropriate entry and the current file position is
set to a given value. Repositioning within a file does not need to involve actual i/o. The file operation is
also known as file seeks.
In addition to this basis 6 operations the other two operations include appending new information to the end
of the file and renaming the existing file. These primitives can be combined to perform other two operations.
Most of the file operation involves searching the entire directory for the entry associated with the file. To
avoid this OS keeps a small table containing information about an open file (the open table). When a file
operation is requested, the file is specified via index in to this table. So searching is not required. Several
piece of information are associated with an open file:
x File pointer:-on systems that does not include offset an a part of the read and write system calls, the system
must track the last read-write location as current file position pointer. This pointer is unique to each process
operating on a file.
x File open count:-As the files are closed, the OS must reuse its open file table entries, or it could run out of
space in the table. Because multiple processes may open a file, the system must wait for the last file to close
before removing the open file table entry. The counter tracks the number of copies of open and closes and
reaches zero to last close.
x Disk location of the file:-The information needed to locate the file on the disk is kept in memory to avoid
having to read it from the disk for each operation.
x Access rights:-Each process opens a file in an access mode. This information is
stored on per-process table the
107
Operating Systems
10CS53
In addition to this basis 6 operations the other two operations include appending new information to the end
of the file and renaming the existing file. These primitives can be combined to perform other two operations.
Most of the file operation involves searching the entire directory for the entry associated with the file. To
avoid this OS keeps a small table containing information about an open file (the open table). When a file
operation is requested, the file is specified via index in to this table. So searching is not required. Several
piece of information are associated with an open file:
x File pointer:-on systems that does not include offset an a part of the read and write
system calls, the system must track the last read-write location as current file position
pointer. This pointer is unique to each process operating on a file.
x File open count:-As the files are closed, the OS must reuse its open file table entries, or
it could run out of space in the table. Because multiple processes may open a file, the
system must wait for the last file to close before removing the open file table entry.
The counter tracks the number of copies of open and closes and reaches zero to last
close.
x Disk location of the file:-The information needed to locate the file on the disk is
kept in memory to avoid having to read it from the disk for each operation. x
Access rights:-Each process opens a file in an access mode. This information is
stored on per-process table the OS can allow OS deny subsequent i/o request.
6.2 ACCESS METHODS The information in the file can be accessed in several ways. Different file access
methods are:
1. Sequential Access:Sequential access is the simplest access method. Information in the file is processed
in order, one record after another. Editors and compilers access the files in this fashion. Normally read and
write operations are done on the files. A read operation reads the next portion of the file and automatically
advances a file pointer, which track next i/I track. Write operation appends to the end of the file and such a
file can be next to the beginning.
108
Operating Systems
10CS53
Indexing Method:x The index is like an index at the end of a book which contains pointers to various
blocks. x To find a record in a file, we search the index and then use the pointer to access
the file directly and to find the desired record. With large files index file itself can be
very large to be kept in memory. One solution to create an index to the index files itself. The primary index
file would contain pointer to secondary index files which would point to the actual data items. Two types of
indexes can be used:
a. Exhaustive index:-Contain one entry for each of record in the main file. An index itself is organized as a
sequential file.
b. Partial index:-Contains entries to records where the field of interest exists with records of variable length,
soma record will not contain an fields. When a new record is added to the main file, all index files must be
updated.
6 . 3 DIRECTORY STRUCTURE
The files systems can be very large. Some systems stores millions of files on the disk. To manage all this data
we need to organize them. This organization is done in two parts:
1. Disks are split in to one or more partition also known as minidisks.
2. Each partition contains information about files within it. This information is
kept in entries in a device directory or volume table of contents. The device directory or simple
directory records information as name, location, size, type for all files on the partition. The directory can be
viewed as a symbol table that translates the file names in to the directory entries. The directory itself can be
organized in many ways. When considering a particular directory structure, we need to keep in mind the
operations that are to be performed on a directory.
x Search for a file:-Directory structure is searched for finding particular file in the directory. Files
have symbolic name and similar name may indicate a relationship between files, we may want
to be able to find all the files whose name match a particular pattern.
x Create a file:-New files can be created and added to the directory.
x Delete a file:-when a file is no longer needed, we can remove it from the directory.
x List a directory:-We need to be able to list the files in directory and the contents of the directory
entry for each file in the list.
x Rename a file:-Name of the file must be changeable when the contents or use of the file is
changed. Renaming allows the position within the directory structure to be changed.
x Traverse the file:-it is always good to keep the backup copy of the file so that or it can be used
when the system gets fail or when the file system is not in use.
109
Operating Systems
10CS53
x Search for a file:-Directory structure is searched for finding particular file in the directory. Files
have symbolic name and similar name may indicate a relationship between files, we may want
to be able to find all the files whose name match a particular pattern.
x Create a file:-New files can be created and added to the directory.
x Delete a file:-when a file is no longer needed, we can remove it from the directory.
x List a directory:-We need to be able to list the files in directory and the contents of the directory
entry for each file in the list.
x Rename a file:-Name of the file must be changeable when the contents or use of the file is
changed. Renaming allows the position within the directory structure to be changed.
x Traverse the file:-it is always good to keep the backup copy of the file so that or it can be used
when the system gets fail or when the file system is not in use.
1. Single-level directory:This is the simplest directory structure. All the files are contained in the same
directory which is easy to support and understand.
Disadvantage:x Not suitable for a large number of files and more than one user. x Because of
single directory files, files require unique file names. x Difficult to remember names of
all the files as the number of files
increases. MS-DOS OS allows only 11 character file name where as UNIX allows 255
character.
2. Two-level directory:x A single level directory often leads to the confusion of file names between different
users. The solution here is to create separate directory or each user.
x In two level directories each user has its own directory. It is called User File Directory
(UFD). Each UFD has a similar structure, but lists only the files of a single user.
x When a user job starts or users logs in, the systems Master File Directory (MFD) is
searched. The MFD is indexed by the user name or account number and each entry points
to the UFD for that user.
x When a user refers to a particular file, only his own UFD is searched. Thus different users
may have files with the same name.
110
Operating Systems
10CS53
x To create a file for a user, OS searches only those users UFD to as certain whether another file of that name
exists.
x To delete a file checks in the local UFD so that accidentally delete another users file with the same name.
Although two-level directories solve the name collision problem but it still has some
disadvantage. This structure isolates one user from another. This isolation is an advantage.
When the users are independent but disadvantage, when some users want to co-operate on some table and to
access one another file.
3. Tree-structured directories:MS-DOS use Tree structure directory. It allows users to create their
own subdirectory and to organize their files accordingly. A subdirectory contains a set of files or
subdirectories. A directory is simply another file, but it is treated in a special way. The entire directory will
have the same internal format. One bit in each entry defines the entry as a file
(0) and as a subdirectory (1). Special system calls are used to create and delete directories. In normal use each
uses has a current directory. Current directory should contain most of the files that are of the current interest
of users. When a reference to a file is needed the current directory is searched. If file is needed i.e., not in the
current directory to be the directory currently holding that file.
Path name can be of two types:
a. Absolute path name:-Begins at the root and follows a path down to the specified file, giving the
directory names on the path.
b. Relative path name:-Defines a path from the current directory. One important policy in this structure is
low to handle the deletion of a directory.
a. If a directory is empty, its entry can simply be deleted.
b. If a directory is not empty, one of the two approaches can be used.
i. In MS-DOS, the directory is not deleted until it becomes empty.
ii. In UNIX, RM command is used with some options for deleting directory.
4. Acyclic graph directories:x It allows directories to have shared subdirectories and files. x
Same file or directory may be in two different directories.
x
A graph with no cycles is a generalization of the tree structure subdirectories
scheme. x Shared files and subdirectories can be implemented by using links. x
A link is a pointer to another file or a subdirectory.
Dept of CSE, SJBIT
111
Operating Systems
10CS53
112
Operating Systems
10CS53
The lowest level is the i/o control consisting of device drivers and interrupt handless to transfer the
information between memory and the disk system. The device driver is like a translator. Its input is a high
level command and the o/p consists of low level hardware specific instructions, which are used by the
hardware controllers which interface I/O device to the rest of the system. The basic file system needs only to
issue generic commands to the appropriate device drivers to read and write physical blocks on the disk. The
file organization module knows about files and their logical blocks as well as physical blocks. By knowing
the type of file allocation used and the location of the file, the file organization module can translate logical
Dept of CSE, SJBIT
113
Operating Systems
10CS53
block address to the physical block address. Each logical block is numbered 0 to N. Since the physical blocks
containing the data usually do not match the logical numbers, So a translation is needed to locate each block.
The file allocation modules also include free space manager which tracks the unallocated blocks and provides
these blocks when requested. The logical file system uses the directory structure to provide the file
organization module with the information, given a symbolic file name. The logical file system also
responsible for protection and security. Logical file system manages metadata information. Metadata includes
all the file system structures excluding the actual data. The file structure is maintained via file control block
(FCB). FCB contains information about the file including the ownership permission and location of the file
contents.
6.8 FILE SYSTEM IMPLEMENTATION
x File system is implemented on the disk and the memory. x The implementation of the file system varies
according to the OS and the file system, but there are some general principles. If the file system is
implemented on the disk it contains the following information:
e. Boot Control Block:-can contain information needed by the system to boot an OS from that partition. If
the disk has no OS, this block is empty. It is the first block of the partition. In UFSboot block, In
NTFSpartition boot sector.
f. Partition control Block:-contains partition details such as the number of blocks in partition, size of the
blocks, number of free blocks, free block pointer, free FCB count and FCB pointers. In NTFSmaster file
tables, In UFSsuper block.
g. Directory structure is used to organize the files.
h. An FCB contains many of the files details, including file permissions, ownership, size, location of the data
blocks. In UFSinode, In NTFS this information is actually stored within master file table.
Structure of the file system management in memory is as follows:
e. An in-memory partition table containing information about each mounted information.
f. An in-memory directory structure that holds the directory information of recently accessed directories.
g. The system wide open file table contains a copy of the FCB of each open file as well as other information.
h. The per-process open file table contains a pointer to the appropriate entry in the
system wide open file table as well as other information. A typical file control blocks is
shown below
File Permission
File dates (create, access, write)
File owner, group, acc
File size
File data blocks
Partition and Mounting:x A disk can be divided in to multiple partitions. Each partition can be either
raw i.e., containing no file system and cooked i.e., containing a file system. x Raw disk is used where no file
system is appropriate. UNIX swap space can use a raw partition and do not use file system.
x Some db uses raw disk and format the data to suit their needs. Raw disks can hold
Dept of CSE, SJBIT
114
Operating Systems
10CS53
115
Operating Systems
10CS53
If the file is n blocks long and starts at location b, then it occupies blocks b, b+1, b+2.b+n
1. The file allocation table entry for each file indicates the address of starting block and the length of the area
allocated for this file. Contiguous allocation is the best from the point of view of individual sequential file. It
is easy to retrieve a single block. Multiple blocks can be brought in one at a time to improve I/O performance
for sequential processing. Sequential and direct access can be supported by contiguous allocation. Contiguous
allocation algorithm suffers from external fragmentation. Depending on the amount of disk storage the
external fragmentation can be a major or minor problem. Compaction is used to solve the problem of external
fragmentation. The following figure shows the contiguous allocation of space after compaction. The original
disk was then freed completely creating one large contiguous space. If the file is n blocks long and starts at
location b, then it occupies blocks b, b+1, b+2.b+n
2. The file allocation table entry for each file indicates the address of starting block and the length of the area
allocated for this file. Contiguous allocation is the best from the point of view of individual sequential file. It
is easy to retrieve a single block. Multiple blocks can be brought in one at a time to improve I/O performance
for sequential processing. Sequential and direct access can be supported by contiguous allocation. Contiguous
allocation algorithm suffers from external fragmentation.
116
Operating Systems
10CS53
Characteristics:x Supports variable size portion. x Pre-allocation is required. x Requires only single entry for
a file. x Allocation frequency is only once.
Advantages:x Supports variable size problem. x Easy to retrieve single block. x
Accessing a file is easy. x It provides good performance.
Disadvantage:x Pre-allocation is required. x It suffers from external fragmentation.
2. Linked Allocation:x It solves the problem of contiguous allocation. This allocation is on the basis
of an
individual block. Each block contains a pointer to the next block in the chain. x The disk
block can be scattered any where on the disk. x The directory contains a pointer to the first
and the last blocks of the file. x The following figure shows the linked allocation. To create a
new file, simply create a new entry in the directory.
117
Operating Systems
10CS53
The indexes are not stored as a part of file allocation table rather than the index is kept as a separate block and
the entry in the file allocation table points to that block. Allocation can be made on either fixed size blocks or
variable size blocks. When the file is created all pointers in the index block are set to nil. When an entry is
made a block is obtained from free space manager. Allocation by fixed size blocks eliminates external
fragmentation where as allocation by variable size blocks improves locality. Indexed allocation supports both
direct access and sequential access to the file.
Advantages:Supports both sequential and direct access.
No external fragmentation. Faster then other two methods.
Supports fixed size and variable sized blocks.
Dept of CSE, SJBIT
118
Operating Systems
10CS53
The indexes are not stored as a part of file allocation table rather than the index is kept as a separate block and
the entry in the file allocation table points to that block. Allocation can be made on either fixed size blocks or
variable size blocks. When the file is created all pointers in the index block are set to nil. When an entry is
made a block is obtained from free space manager. Allocation by fixed size blocks eliminates external
fragmentation where as allocation by variable size blocks improves locality. Indexed allocation supports both
direct access and sequential access to the file.
If the file is n blocks long and starts at location b, then it occupies blocks b, b+1, b+2.b+n
1. The file allocation table entry for each file indicates the address of starting block and the length of the area
allocated for this file. Contiguous allocation is the best from the point of view of individual sequential file. It
is easy to retrieve a single block. Multiple blocks can be brought in one at a time to improve I/O performance
for sequential processing. Sequential and direct access can be supported by contiguous allocation. Contiguous
allocation algorithm suffers from external fragmentation. Depending on the amount of disk storage the
external fragmentation can be a major or minor problem. Compaction is used to solve the problem of external
fragmentation. The following figure shows the contiguous allocation of space after compaction. The original
disk was then freed completely creating one large contiguous space. If the file is n blocks long and starts at
location b, then it occupies blocks b, b+1, b+2.b+n
2. The file allocation table entry for each file indicates the address of starting block and the length of the area
allocated for this file. Contiguous allocation is the best from the point of view of individual sequential file. It
is easy to retrieve a single block. Multiple blocks can be brought in one at a time to improve I/O performance
for sequential processing. Sequential and direct access can be supported by contiguous allocation. Contiguous
allocation algorithm suffers from external fragmentation.
119
Operating Systems
10CS53
Bit map
Must be kept on disk
Copy in memory and disk may differ.
Cannot allow for block[i] to have a situation where bit[i] = 1 in memory and bit[i] = 0 on disk.
Solution:
Set bit[i] = 1 in disk.
Allocate block[i]
Set bit[i] = 1 in memory
IMPORTANT QUESTIONS:
1. Explain the purpose of the open and close operations.
2. Give an example of an application in which data in a file should be accessed in the following order:
a. Sequentially
b. Randomly
3. In some systems, a subdirectory can be read and written by an authorized user, just as ordinary files can be.
a. Describe the protection problems that could arise.
b. Suggest a scheme for dealing with each of the protection problems you named in part a.
4. What is a file?
5. List sample file types, based on use, on the VAX under VMS.
Dept of CSE, SJBIT
120
Operating Systems
10CS53
121
Operating Systems
10CS53
122
Operating Systems
10CS53
123
Operating Systems
10CS53
If the disk head is initially at 53, it will first move from 53 to 98 then to 183 and then to 37, 122, 14, 124, 65,
67 for a total head movement of 640 cylinders. The wild swing from 122 to 14 and then back to 124
illustrates the problem with this schedule. If the requests for cylinders 37 and 14 could be serviced together
before or after 122 and 124 the total head movement could be decreased substantially and performance could
be improved.
2. SSTF ( Shortest seek time first) algorithm:This selects the request with minimum seek time
from the current head position. Since seek time increases with the number of cylinders
traversed by head, SSTF chooses the pending request closest to the current head position. Eg::-consider a disk queue with request for i/o to blocks on cylinders. 98, 183, 37, 122, 14, 124,
65, 67
If the disk head is initially at 53, the closest is at cylinder 65, then 67, then 37 is closer then 98 to 67. So it
services 37, continuing we service 14, 98, 122, 124 and finally 183. The total head movement is only 236
cylinders. SSTF is essentially a form of SJF and it may cause starvation of some requests. SSTF is a
Dept of CSE, SJBIT
124
Operating Systems
10CS53
If the disk head is initially at 53 and if the head is moving towards 0, it services 37 and then 14. At cylinder 0
the arm will reverse and will move towards the other end of the disk servicing 65, 67, 98, 122, 124 and 183. If
a request arrives just in from of head, it will be serviced immediately and the request just behind the head will
have to wait until the arms reach other end and reverses direction. The SCAN is also called as elevator
algorithm.
4. C-SCAN (Circular scan) algorithm:
C-SCAN is a variant of SCAN designed to provide a more uniform wait time. Like SCAN, C-SCAN moves
the head from end of the disk to the other servicing the request along the way. When the head reaches the
other end, it immediately returns to the beginning of the disk, without servicing any request on the return. The
C-SCAN treats the cylinders as circular list that wraps around from the final cylinder to the first one. Eg:-
125
Operating Systems
10CS53
and CSCAN are called Look and C-Look scheduling because they look for a request before
continuing to move in a given direction. Eg:
Low-level formatting, or physical formatting Dividing a disk into sectors that the disk controller
can read and write.
To use a disk to hold files, the operating system still needs to record its own data structures on
the disk.
Partition the disk into one or more groups of cylinders.
Logical formatting or making a file system.
126
Operating Systems
10CS53
Swap-space can be carved out of the normal file system,or, more commonly, it can be in a separate
disk partition.
4.3BSD allocates swap space when process starts; holds text segment (the program) and data segment.
Kernel uses swap maps to track swap-space use.Solaris 2 allocates swap space only when a page is
forced out of physical memory, not when the virtual memory page is first created.
RAID Structure
Disk striping uses a group of disks as one storage unit schemes improve performance and improve the
reliability of the storage system by storing redundant data
Mirroring or shadowing (RAID 1) keeps duplicate of each disk
Striped mirrors (RAID 1+0) or mirrored stripes (RAID 0+1) provides high performance and high
reliability
Block interleaved parity (RAID 4, 5, 6) uses much less redundancy
RAID within a storage array can still fail if the array fails, so automatic replication of the data
between arrays is common.Frequently, a small number of hot-spare disks are left unallocated, automatically
replacing a failed disk and having
data rebuilt onto them
RAID multiple disk drives provides reliability via redundancy. It is arranged into six different
levels.Several improvements in disk-use techniques involve the use of multiple disks working
cooperatively.Disk striping uses a group of disks as one storage unit.
RAID schemes improve performance and improve the reliability of the storage system by storing
redundant data.
Mirroring or shadowing keeps duplicate of each disk.
Block interleaved parity uses much less redundancy.
RAID alone does not prevent or detect data corruption or other errors, just disk failures .Solaris ZFS
adds checksums of all data and metadata Checksums kept with pointer to object, to detect if object is the right
one and whether it changed can detect and correct data and metadata corruptionZFS also removes volumes,
partititions .Disks allocated in pools
Filesystems with a pool share that pool, use and release space like malloc and free memory
allocate / release calls
127
Operating Systems
10CS53
Explain how protection domains combined with an access matrix are used to specify the resources a
process may access.Examine capability and language-based protection systems
Operating system consists of a collection of objects, hardware or software Each object has a unique
name and can be accessed through a well-defined set of operations
Protection problem -ensure that each object is accessed correctly and only by those processes that are
allowed to do so
7.6 PRINCIPLES OF PROTECTION
Guiding principle principle of least privilege
Programs, users and systems should be given just enough privileges to perform their
tasks Disk Attachment : Stable-Storage Implementation
UNIX
Domain = user-id
Domain switch accomplished via file system
128
Operating Systems
10CS53
copy op from Oi to Oj
control Di can modify Dj access rights
Mechanism
Operating system provides access-matrix + rules
If ensures that the matrix is only manipulated by authorized agents and that rules are strictly enforced
Policy
User dictates policy
Who can access what object and in what mode
129
Operating Systems
10CS53
130
Operating Systems
10CS53
Capability List Scheme required to locate capability in the system before capability can be revoked
Reacquisition
Back-pointers
Indirection
Keys
131
Operating Systems
10CS53
Common examples of removable media are floppy disks and CD-ROMs; other types are available
Removable Disks
Floppy disk thin flexible disk coated with magnetic material, enclosed in a protective plastic case
Most floppies hold about 1 MB; similar technology is used for removable disks that hold more than 1
GB
Removable magnetic disks can be nearly as fast as hard disks, but they are at a greater risk of damage
from exposure
A magneto-optic disk records data on a rigid platter coated with magnetic material
Laser heat is used to amplify a large, weak magnetic field to record a bit
Laser light is also used to read data (Kerr effect)
The magneto-optic head flies much farther from the disk surface than a magnetic disk head, and the
magnetic material is covered with a protective layer of plastic or glass; resistant to head crashes
Optical disks do not use magnetism; they employ special materials that are altered by laser light
WORM (Write Once, Read Many Times) disks can be written only once
To write a bit, the drive uses a laser light to burn a small hole through the aluminum; information can
be destroyed by not altered
Read-only disks, such ad CD-ROM and DVD, com from the factory with the data pre-recorded Tapes
Compared to a disk, a tape is less expensive and holds more data, but random access is much slower
Tape is an economical medium for purposes that do not require fast random access, e.g., backup
copies of disk data, holding huge volumes of data
Large tape installations typically use robotic tape changers that move tapes between tape drives and
storage slots in a tape library
stacker library that holds a few tapes
silo library that holds thousands of tapes
A disk-resident file can be archived to tape for low cost storage; the computer can stage it back into
disk storage for active use
132
Operating Systems
10CS53
Removable Disks
Floppy disk thin flexible disk coated with magnetic material, enclosed in a protective plastic case
Most floppies hold about 1 MB; similar technology is used for removable disks that hold more than 1
GB
Removable magnetic disks can be nearly as fast as hard disks, but they are at a greater risk of damage
from exposure
A magneto-optic disk records data on a rigid platter coated with magnetic material
Laser heat is used to amplify a large, weak magnetic field to record a bit
Laser light is also used to read data (Kerr effect)
The magneto-optic head flies much farther from the disk surface than a magnetic disk head, and the
magnetic material is covered with a protective layer of plastic or glass; resistant to head crashes
Optical disks do not use magnetism; they employ special materials that are altered by laser light
WORM Disks
WORM (Write Once, Read Many Times) disks can be written only once
To write a bit, the drive uses a laser light to burn a small hole through the aluminum; information can
be destroyed by not altered
Read-only disks, such ad CD-ROM and DVD, com from the factory with the data pre-recorded Tapes
Compared to a disk, a tape is less expensive and holds more data, but random access is much slower
Tape is an economical medium for purposes that do not require fast random access, e.g., backup
copies of disk data, holding huge volumes of data
Large tape installations typically use robotic tape changers that move tapes between tape drives and
storage slots in a tape library
stacker library that holds a few tapes
silo library that holds thousands of tapes
A disk-resident file can be archived to tape for low cost storage; the computer can stage it back into
disk storage for active use
Application Interface
Most OSs handle removable disks almost exactly like fixed disks a new cartridge is formatted and
an empty file system is generated on the disk
Tapes are presented as a raw storage medium, i.e., and application does not not open a file on the tape,
it opens the whole tape drive as a raw device
Usually the tape drive is reserved for the exclusive use of that application
Since the OS does not provide file system services, the application must decide how to use the array
of blocks
Since every application makes up its own rules for how to organize a tape, a tape full of data can
generally only be used by the program that created it
133
Operating Systems
10CS53
Language implementation can provide software for protection enforcement when automatic hardwaresupported checking is unavailable
Protection in Java 2
The protection domain indicates what operations the class can (and cannot) perform
If a library method is invoked that performs a privileged operation, the stack is inspected to ensure the
operation can be performed by the library
Interpret protection specifications to generate calls on whatever protection system is provided by the
hardware and the operating system
IMPORTANT QUESTIONS
What are the main differences between capability lists and access lists?
a. What hardware features are needed for efficient capability manipulation? Can these be used for memory
protection? What protection problems may arise if a shared stack is used for parameter passing?
b. What is the need-to-know principle? Why is it important for a protection systemto adhere to this principle?
Why is it difficult to protect a system in which users are allowed to do their own I/O?
c. Describe how the Java protection model would be sacrificed if a Java program were allowed to directly
alter the annotations of its stack frame. What are two advantages of encrypting data stored in the computer
system.
Dept of CSE, SJBIT
134
Operating Systems
10CS53
135
Operating Systems
10CS53
To explore the history of the UNIX operating system from which Linux is derived and the principles
which Linux is designed upon
To examine the Linux process model and illustrate how Linux schedules processes and provides
interprocess communication
To explore how Linux implements file systems and manages I/O devices
History
First developed as a small but self-contained kernel in 1991 by Linus Torvalds, with the major design
goal of UNIX compatibility
Its history has been one of collaboration by many users from all around the world, corresponding
almost exclusively over the Internet
It has been designed to run efficiently and reliably on common PC hardware, but also runs on a
variety of other platform.
The core Linux operating system kernel is entirely original, but it can run much existing free
UNIX software, resulting in an entire UNIX-compatible operating system free from proprietary
code. The core Linux operating system kernel is entirely original, but it can run much existing free
UNIX software, resulting in an entire UNIX-compatible operating system free from proprietary
code.
Version 0.01 (May 1991) had no networking, ran only on 80386compatible Intel pr Many, varying Linux
Distributions including the kernel, applications, and management tools
ocessors and on PC hardware, had extremely limited device-drive support, and supported only the Minix file
system
Linux 1.0 (March 1994) included these new features:
Support for UNIXs standard TCP/IP networking protocols
136
Operating Systems
10CS53
History
First developed as a small but self-contained kernel in 1991 by Linus Torvalds, with the major design
goal of UNIX compatibility
Its history has been one of collaboration by many users from all around the world, corresponding
almost exclusively over the Internet
It has been designed to run efficiently and reliably on common PC hardware, but also runs on a
variety of other platform.
The core Linux operating system kernel is entirely original, but it can run much existing free
UNIX software, resulting in an entire UNIX-compatible operating system free from proprietary
code. The core Linux operating system kernel is entirely original, but it can run much existing free
UNIX software, resulting in an entire UNIX-compatible operating system free from proprietary
code.
Version 0.01 (May 1991) had no networking, ran only on 80386compatible Intel pr Many, varying Linux
Distributions including the kernel, applications, and management tools
ocessors and on PC hardware, had extremely limited device-drive support, and supported only the Minix file
system
Linux 1.0 (March 1994) included these new features:
Support for UNIXs standard TCP/IP networking protocols
137
Operating Systems
10CS53
Version 1.2 (March 1995) was the final PC-only Linux kernel
Released in June 1996, 2.0 added two major new capabilities:
Support for multiple architectures, including a fully 64-bit native Alpha port
Support for multiprocessor architectur
Other new features included:
Improved memory-management code
Support for internal kernel threads, for handling dependencies between loadable modules, and for
automatic loading of modules on demand
Standardized configuration interface
Available for Motorola 68000-series processors, Sun Sparc systems, and for PC and
PowerMac systems.2.4 and 2.6 increased SMP support, added journaling file system,
preemptive kernel, 64-bit memory support
8.2 DESIGN PRINCIPLES Linux is a multiuser, multitasking system with a full set of UNIXcompatible tools Its file system adheres to traditional UNIX semantics, and it fully
implements the standard UNIX networking model
Linux is designed to be compliant with the relevant POSIX documents; at least two Linux
distributions have achieved official POSIX certification
138
Operating Systems
10CS53
of three main bodies of code; the most important distinction between the kernel and all other
components
x Like most UNIX implementations, Linux is composed of three main bodies of code; the most
important distinction between the kernel and all other components x The kernel is responsible for
maintaining the important abstractions of the operating system
o Kernel code executes in kernel mode with full access to all the physical resources of the computer
o All kernel code and data structures are kept in the same single address space
x The system libraries define a standard set of functions through which applications interact with the kernel,
and which implement much of the operating-system functionality that does not need the full privileges of
kernel code
x The system utilities perform individual specialized management tasks x Sections of kernel code
that can be compiled, loaded, and unloaded independent of the rest of the kernel
8.3 KERNEL MODULES
x A kernel module may typically implement a device driver, a file system, or a networking protocol
x The module interface allows third parties to write and distribute, on their own terms, device drivers or file
systems that could not be distributed under the GPL
x Kernel modules allow a Linux system to be set up with a standard, minimal kernel, without any extra
device drivers built in
x Three components to Linux module support:
o module management
o driver registration
o conflict resolution
o MODULE MANAGEMENT
o Supports loading modules into memory and letting them talk to the rest of the kernel
139
Operating Systems
10CS53
o The module requestor manages loading requested, but currently unloaded, modules; it also
regularly queries the kernel to see whether a dynamically loaded module is still in use, and will unload it
when it is no longer actively needed
o Driver register
o Allows modules to tell the rest of the kernel that a new driver has become available
o The kernel maintains dynamic tables of all known drivers, and provides a set of routines to allow
drivers to be added to or removed from these tables at any time
o Device drivers
o File systems
o Network protocols
o Binary format
o Conflict Resolution
o A mechanism that allows different device drivers to reserve hardware resources and to protect
those resources from accidental use by another driver
o Resolve conflicts with multiple drivers trying to access the same hardware
8.4 PROCESS MANAGEMENT
o UNIX process management separates the creation of processes and the running of a new program
into two distinct operations.
o Under UNIX, a process encompasses all the information that the operating system must maintain t
track the context of a single execution of a single program
o Under Linux, process properties fall into three groups: the processs identity, environment, and
context
o Process Identity
o Process ID (PID). The unique identifier for the process; used to specify processes to the operating
system when an application makes a system call to signal, modify, or wait for another process
o Credentials. Each process must have an associated user ID and one or more group IDs that
determine the processs rights to access system resources and files
o Personality. Not traditionally found on UNIX systems, but under Linux each process has an
associated personality identifier that can slightly modify the semantics of certain system calls
o Used primarily by emulation libraries to request that system calls be compatible with certain
specific flavors of UNIX
o Process Environment
o The processs environment is inherited from its parent, and is composed of two null-terminated
vectors:
o The argument vector lists the command-line arguments used to invoke the running program;
Dept of CSE, SJBIT
140
Operating Systems
10CS53
o The environment vector is a list of NAME=VALUE pairs that associates named environment
variables with arbitrary textual values
o Passing environment variables among processes and inheriting variables by a processs children
are flexible means of passing information to components of the user-mode system software
o The environment-variable mechanism provides a customization of the operating system that can be
set on a per-process basis, rather than being configured for the system as a whole
o Process Context
o The scheduling context is the most important part of the process context; it is the information that
the scheduler needs to suspend and restart the process
o The kernel maintains accounting information about the resources currently being consumed by
each process, and the total resources consumed by the process in its lifetime so far
o When making file I/O system calls, processes refer to files by their index into this table
o Whereas the file table lists the existing open files, the file-system context applies to requests to
open new files
o The current root and default directories to be used for new file searches are stored here
o The signal-handler table defines the routine in the processs address space to be called when
specific signals arrive
o The virtual-memory context of a process describes the full contents of the its private address
space
o Linux uses the same internal representation for processes and threads; a thread is simply a new
process that happens to share the same address space as its parent
o A distinction is only made when a new thread is created by the clone system call
o fork creates a new process with its own entirely new process context
o clone creates a new process with its own identity, but that is allowed to share the data structures of
its parent
o Using clone gives an application fine-grained control over exactly what is shared between two
threads
8.5 SCHEDULING
o The job of allocating CPU time to different tasks within an operating system
o While scheduling is normally thought of as the running and interrupting of processes, in Linux,
scheduling also includes the running of the various kernel tasks
o Running kernel tasks encompasses both tasks that are requested by a running process and tasks that
execute internally on behalf of a device driver
141
Operating Systems
10CS53
Kernel Synchronization
o A request for kernel-mode execution can occur in two ways:
A running program may request an operating system service, either explicitly via a system call, or
implicitly, for example, when a page fault occurs
A device driver may deliver a hardware interrupt that causes the CPU to start executing a kerneldefined handler for that interrupt
Kernel synchronization requires a framework that will allow the kernels critical sections to run
without interruption by another critical section
142
Operating Systems
10CS53
By using the processors interrupt control hardware to disable interrupts during a critical
section, the kernel guarantees that it can proceed without the risk of concurrent access of
shared data structures
.
o To avoid performance penalties, Linuxs kernel uses a synchronization architecture that allows
long critical sections to run without having interrupts disabled for the critical sections entire duration
o Interrupt service routines are separated into a top half and a bottom half.
The top half is a normal interrupt service routine, and runs with recursive interrupts disabled
The bottom half is run, with all interrupts enabled, by a miniature scheduler that ensures that bottom
halves never interrupt themselves
This architecture is completed by a mechanism for disabling selected bottom halves while executing
normal, foreground kernel code
o Interrupt Protection Levels
o Each level may be interrupted by code running at a higher level, but will never be interrupted by code
running at the same or a lower level
143
Operating Systems
10CS53
o User processes can always be preempted by another process when a time-sharing scheduling interrupt
occurs
PROCESS SCHEDULING
o Linux uses two process-scheduling algorithms:
A time-sharing algorithm for fair preemptive scheduling between multiple processes
A real-time algorithm for tasks where absolute priorities are more important than fairness
o Symmetric Multiprocessing
o Linux 2.0 was the first Linux kernel to support SMP hardware; separate processes or threads can
execute in parallel on separate processors
o Linuxs physical memory-management system deals with allocating and freeing pages, groups of
pages, and small blocks of memory
o It has additional mechanisms for handling virtual memory, memory mapped into the address space
of running processes
144
Operating Systems
10CS53
o The page allocator allocates and frees all physical pages; it can allocate ranges of physicallycontiguous pages on request
o The allocator uses a buddy-heap algorithm to keep track of available physical pages
Each allocatable memory region is paired with an adjacent partner
Whenever two allocated partner regions are both freed up they are combined to form a larger region
If a small memory request cannot be satisfied by allocating an existing small free region, then a larger
free region will be subdivided into two partners to satisfy the request
o Memory allocations in the Linux kernel occur either statically (drivers reserve a contiguous area of
memory during system boot time) or dynamically (via the page allocator)
o The VM system maintains the address space visible to each process: It creates pages of virtual
memory on demand, and manages the loading of those pages from disk or their swapping back out to disk as
required
A logical view describing instructions concerning the layout of the address space
x The address space consists of a set of nonoverlapping regions, each representing a continuous, pagealigned subset of the address space
A physical view of each address space which is stored in the hardware page tables for the process
o When a process runs a new program with the exec system call
o Creating a new process with fork involves creating a complete copy of the existing processs virtual
address space
Dept of CSE, SJBIT
145
Operating Systems
10CS53
The kernel copies the parent processs VMA descriptors, then creates a new set of page tables for the
child
The parents page tables are copied directly into the childs, with the reference count of each page
covered being incremented
After the fork, the parent and child share the same physical pages of memory in their address spaces
o The VM paging system relocates pages of memory from physical memory out
The pageout-policy algorithm decides which pages to write out to disk, and when
The paging mechanism actually carries out the transfer, and pages data back into physical memory as
needed
o The Linux kernel reserves a constant, architecture-dependent region of the virtual address space of
every process for its own internal use
o Linux maintains a table of functions for loading programs; it gives each function the opportunity to
try loading the given file when an exec system call is made
o The registration of multiple loader routines allows Linux to support both the ELF and a.out binary
formats
Only when a program tries to access a given page will a page fault result in that
page being loaded into physical memory
146
Operating Systems
10CS53
o A program whose necessary library functions are embedded directly in the programs executable
binary file is statically linked to its libraries
o The main disadvantage of static linkage is that every program generated must contain copies of
exactly the same common system library functions
o Dynamic linking is more efficient in terms of both physical memory and disk-space usage because
it loads the system libraries into memory only once
8.7 FILE SYSTEMS
o To the user, Linuxs file system appears as a hierarchical directory tree obeying UNIX semantics
o Internally, the kernel hides implementation details and manages the multiple different file systems
via an abstraction layer, that is, the virtual file system (VFS)
o The Linux VFS is designed around object-oriented principles and is composed of two components:
A set of definitions that define what a file object is
allowed to look like x The inode-object and the file-object
structures represent individual files
x the file system object represents an entire file
system
147
Operating Systems
10CS53
o Block Devices
o Provide the main interface to all disk devices in a system
o The block buffer cache serves two main purposes:
it acts as a pool of buffers for active I/O
it serves as a cache for completed I/O
o The request manager manages the reading and writing of buffer contents to and from a block
device driver
o Character Devices
o A device driver which does not offer random access to fixed blocks of data
o A character device driver must register a set of functions which implement the drivers various file
I/O operations
o The kernel performs almost no preprocessing of a file read or write request to a character device,
but simply passes on the request to the device
o The main exception to this rule is the special subset of character device drivers which implement
terminal devices, for which the kernel maintains a standard interface
8.9INTERPROCESS COMMUNICATION
o Like UNIX, Linux informs processes that an event has occurred via signals
o There is a limited number of signals, and they cannot carry information: Only the fact that a signal
occurred is available to a process.
o The Linux kernel does not use signals to communicate with processes with are running in kernel
mode, rather, communication within the kernel is accomplished via scheduling states and wait.queue
structures
Dept of CSE, SJBIT
148
Operating Systems
10CS53
to its parent, data written to one end of the pipe can be read a the other
o Shared memory offers an extremely fast way of communicating; any data written by one process to
a shared memory region can be read immediately by any other process that has mapped that region into its
address space
o To obtain synchronization, however, shared memory must be used in conjunction with another
Interprocess-communication mechanism
o The shared-memory object acts as a backing store for shared-memory regions in the same way as a
file can act as backing store for a memory-mapped memory region
o Shared-memory mappings direct page faults to map in pages from a persistent shared-memory
object
o Shared-memory objects remember their contents even if no processes are currently mapping them
into virtual memory
o Network Structure
o It also implements protocols native to nonUNIX operating systems, in particular, protocols used on
PC networks, such as Appletalk and IPX
o Protocol drivers
o Security
o PAM is based on a shared library that can be used by any system component that needs to
authenticate users
o Access control under UNIX systems, including Linux, is performed through the use of unique
numeric identifiers (uid and gid)
o Access control is performed by assigning objects a protections mask, which specifies which access
modesread, write, or executeare to be granted to processes with owner, group, or world access
149
Operating Systems
10CS53
IMPORTANT QUESTIONS:
1. Describe three different ways that threads could be implemented. Explain how these ways compare to the
Linux clone mechanism
2. What are the extra costs incurred by the creation and scheduling of a process, as compared to the cost of a
cloned thread?
3. The Linux scheduler implements soft real-time scheduling. What features are missing thatare necessary
for some real-time programming tasks?
4. How might they be added to thekernel?.What effect does thisrestriction have on the kernels design?
5. What are two advantages and two disadvantages of this design decision?
6. What is the advantage of keeping this functionality out of the kernel? Are there any drawbacks?
7. What are three advantages of dynamic (shared) linkage of libraries compared to static linkage? What are
two cases where static linkage is preferable.
150