OS Book
OS Book
Faculty of science
Mathematics Dept.
Introduction to Operating
systems
Prepared by
Faculty of science
Tanta University
2024
Computer science vision رؤية برنامج علوم الحاسب اﻵلي
Leadership at the local and regional ٕ وا ي ا ا ا دة
levels in the field of computer science
to prepare a distinguished graduate در اد ٕ ما ل
capable of competing in the labor
market and conducting integrated و ا ٕ اء ق ا ا
scientific research aimed at
community service. . ا ٕف ا
***********************
ً ً و ء ٕ اد م ا
The program is committed to
preparing a qualified graduate who is
able to conduct meaningful and در، و ا م ا
constructive scientific research to
develop the educational process and دف و ء ا ٕ اء
compute in the local and international
labor market to meet the requirements ق ا وا، ا ا
of societal and environmental
problems. ت ت ا ، وا و ا
. وا ا
***********************
أهداف برنامج علوم الحاسب اﻵلي
Computer science aims to: يهدف برنامج علوم الحاسب اﻵلي
Providing students with ا و ا ب
theoretical knowledge in the
various branches of computer ٕت ا ٔ ا ًءا، وع م ا
sciences, ranging from the basics
رة ا ود ا ث ا
to the limits of free research in
selected topics with . ا ودور ما
understanding of computer
science and its role in ً ب ا ا رات ا
community development.
ا و ت ا ر ا ٕا
Developing the appropriate
2
scientific skills for students . ا ا
along with continuous training
in different programs and رات ا ب ب ا ا
programming languages.
ا وا ا
Provide students with a set of
personal and transferable skills : ذ ؛ ا ا
that increase their chances of
working in the future including ، ا ي وا وا ض ا ا
writing, oral presentation,
.ت ا رات و
teamwork, as well as
information technology skills. رة و ا ا ا
Promote self-learning of
knowledge and develop students' ت ت وا ا ر ا ب ا
ability to find, understand, and
. و و ا
analyze information and
computer data. ر ب رة ا
Develop students' ability to
apply their knowledge and skills وا ت ا ا و را
to solve theoretical and practical
ا ا ًء ت ا ا
problems in different fields
based on ethical, economic, and . د وا وا ٔ ا
environmental aspects.
Prepare students for a أو ر ب ا ٕ اد ا
specialized career path or degree . ما در
in computer science.
***************************
3
Preface
Chapter 1: Explains what operating systems are, what they do, how they
are designed and constructed. It discusses what the common features of
an operating system are, what an operating system does for the user,
and what it does for the computer-system operator.
Chapter 2: Describes the process concept and concurrency as the heart
of modern operating systems. It covers the methods for inter-process
communication.
Chapter 3: Discusses the methods of CPU scheduling.
Chapter 4: Explains process synchronization.
Chapter 5: discusses the concept of deadlock and methods of handling.
4
Chapter 6: Deals with the management of main memory during the
execution of a process. It explains different management algorithms.
Chapter 7: Discusses the secondary storage devices such as magnetic
disks.
Course objectives:
5
Chapter 1
Introduction
Different OSs are varied in accomplishing these objectives. Some OSs are
designed to be convenient, others to be efficient, and others some
combination of the two. For example:
Mainframe: OS is designed primarily to optimize utilization of hardware.
Utilization means how various hardware and software resources are
shared.
Personal computer: OS support complex games, business applications,
and everything in between.
Handheld computers: OS is designed to provide an environment in
which a user can easily interface with the computer to execute
programs.
6
1.2 What operating systems do?
1. User view: user's view of the computer varies according to the interface
being used.
PC consists of monitor, keyboard, mouse, and system unit. Users of
PCs want OS to maximize their works. So, OS is designed mostly for
ease of use, with some attention paid to performance and none paid
to resource utilization. Performance is important to the user, but
such systems are optimized for single-user not multiple users.
Users sits at a terminal connected to a mainframe or a minicomputer
are shared with other users the same computer through other
terminals. These users share resources and may exchange
information. OS in this case is designed to maximize resource
utilization (to assure that all available CPU time, memory, and I/0 are
used efficiently, and that no individual user takes more than his fair
share).
Users sit at workstation connected to networks of other workstations
and servers have dedicated resources at their disposal, but they also
share resources such as networking and servers-file, compute, and
print servers. Therefore, their OS is designed to compromise between
individual usability and resource utilization.
Handheld computers may be standalone units for individual users or
connected to networks. Their OSs are designed mostly for individual
usability, but performance per unit of battery life is important as well.
7
Embedded computers in home devices and automobiles have little or
no user view. They may have numeric keypads and may turn
indicator lights on or off to show status, but they and their OSs are
designed primarily to run without user intervention.
2. System view: From this point of view, we can view OS as:
Resource allocator: A computer system has many resources that may
be required to solve a problem: CPU time, memory space, file-storage
space, I/0 devices, and so on. OS must decide how to allocate these
resources (when there are conflicting requests for them) to specific
users so that it can operate the computer system efficiently and
fairly.
Control program: A control program manages the execution of user
programs to prevent errors and improper use of the computer. It is
especially concerned with the operation and control of I/O devices.
10
User authentication. OS must identify each user who requests access
and must discover that the user is who has the access right. The most
common authentication mechanism is password comparison.
Memory protection. Each user’s program must run in a portion of
memory protected against unauthorized accesses. The protection will
certainly prevent outsiders’ accesses, and it may also control a user’s
own access to restricted parts of the program space. Differential
security, such as read, write, and execute, may be applied to parts of
a user’s memory space. Memory protection is usually performed by
hardware mechanisms, such as paging or segmentation.
File and I/O device access control. OS is responsible for maintaining
directory and subdirectory structures, mapping file names to specific
blocks of data storage, and providing tools for navigating and utilizing
the file system. OS must protect user and system files from access by
unauthorized users. Similarly, OS is responsible for transferring data
to and from I/O devices, including keyboards, terminals, printers, and
storage devices. I/O device using must be protected.
Allocation and access control to general objects. Users need general
objects, such as constructs to permit concurrency and allow
synchronization. However, access to these objects must be controlled
so that one user does not have a negative effect on other users.
Operating systems are not just for conventional computers. Some form
of operating system can be found on any of the following objects:
11
An automobile (especially the engine performance sensors and the
automated control functions such as antilock brakes); similarly, the
avionics components of an airplane or the control system of a
streetcar or mass transit system.
A smartphone, tablet, or other web appliance.
A network appliance, such as a firewall or intrusion detection and
prevention system.
A controller for a bank of web servers.
A computer network traffic management device.
12
Application program
Layer N-1
Layer 1
Layer 0
(Hardware)
13
Figure 1.4 shows functions arranged from the most critical layer (at the
bottom) to least critical one (at the top). The functions of OS in layered
approach are grouped in three categories:
Security kernel. To enforce security.
Operating system kernel. To allocate primitive resources such as time or
access to hardware devices.
Other operating system functions. To implement the user’s interface to
hardware.
Above the operating system come some system utility functions and
then the user’s applications. The critical functions of controlling hardware and
enforcing security are said to be in lower or inner layers, and the less critical
functions in the upper or outer layers.
14
The main difficulty with the layered structure is that each layer needs to
be carefully defined. That is, deciding what order in which to place the
layers, since each layer can use only the services of lower layers and
cannot use the functionalities of any higher layers. This leads to many
chicken-and-egg situations.
The main disadvantage is that the OS tends to be less efficient, as a
request for service from a higher layer must filter through all lower
layers before it reaches the hardware, possibly with significant
processing at each step.
1.4.3 Microkernels
15
System expansion can also be easier, because it only involves adding
more system applications, not rebuilding a new kernel.
1.4.4 Modules
16
Modules are like layers in that each subsystem has clearly defined tasks
and interfaces. But they unlike layers in that any module is free to contact any
other module, eliminating the problems of going through multiple
intermediary layers, as well as the chicken-and-egg problems.
Modules are like microkernels in that the kernel is relatively small. But
they are unlike microkernels in that the kernel does not have to implement
message passing since modules are free to contact each other directly.
Operating system picks up and begins to execute one of the jobs from
memory.
Once this job needs an I/O operation OS switch to another job (CPU and
OS always busy).
Jobs in the memory are always less than the number of jobs on disk (Job
Pool).
If several jobs are ready to run at the same time, then OS chooses which
one to run through the process of CPU Scheduling.
In Multiprogramming system, CPU will never be idle and keeps on
processing.
Time Sharing Systems are very similar to Multiprogramming batch
systems.
o In Time sharing systems the prime focus is on minimizing the
response time, while in multiprogramming the prime focus is to
maximize the CPU usage.
18
Advantages of Multiprocessor Systems
Enhanced performance.
Execution of several tasks by different processors concurrently, increases
the system's throughput without speeding up the execution of a single
task.
If possible, system divides task into many subtasks and then these
subtasks can be executed in parallel in different CPUs. Thereby speeding
up the execution of single task.
Fast processing.
Less load on the host machine.
As there are multiple systems involved, user at one site can utilize the
resources of systems at other sites for resource-intensive tasks.
19
Types of distributed operating systems
20
can remain down, but the users (clients) of the application would only
see a brief interruption of service. There are two types:
o Asymmetric Clustering: In this, one machine is in hot standby
mode while the other is running the applications. The hot standby
host (machine) does nothing but monitor the active server. If that
server fails, the hot standby host becomes the active server.
o Symmetric Clustering: In this, two or more hosts are running
applications, and they are monitoring each other. This mode is
obviously more efficient, as it uses all the available hardware.
Parallel clusters allow multiple hosts to access the same data on the
shared storage. Because most operating systems lack support for this
simultaneous data access by multiple hosts, parallel clusters are usually
accomplished by special versions of software and special releases of
applications.
Hard real-time OS: guarantees the maximum time for critical operations
and complete them on time.
Soft real-time OS: can only guarantee a maximum of the time for the
critical task will get priority over other tasks but does not assure of
completing it in a defined time.
21
1.5.8 Handheld System
1.6 Interrupt
(a) No interrupts (b) Interrupts: short I/O wait (c) Interrupts: long I/O wait
Figure 1.7: Program Flow of Control without and with Interrupts
22
processor. Figure 1.7(a) illustrates this situation. The user program performs a
series of WRITE calls interleaved with processing.
23
We can illustrate the gain in efficiency of the processor in the following
two cases:
1. When the time required for the I/O operation is relatively short: less
than the time to complete the execution of instructions between WRITE
operations in the user program. Figure 1.8 shows a timing diagram based
on the flow of control in Figures 1.7(a) and 1.7(b).
2. When the time required for the I/O operation is relatively long: greater
than the time to complete the execution of instructions between WRITE
operations in the user program. Figure 1.9 shows a timing diagram based
on the flow of control in Figures 1.7(a) and 1.7(c).
24
Figure 1.9: Program Timing: Long I/O Wait
25
then makes the appropriate system calls through the system call interface,
using a table lookup to access specific numbered system calls, as shown in
Figure 1.10. Thus, most of the details of the OS interface are hidden from the
programmer by the API and are managed by the run-time support library.
Figure 1.10: handling of a user application invoking the open () system call.
26
memory, and the address of the block is passed as a parameter in a
register (see Figure 1.11).
3. Stack: Parameters also can be placed onto the stack by the program and
popped on the stack by the OS. Some OSs prefer the block or stack
method because those approaches do not limit the number or length of
parameters being passed.
Process management
All multiprogramming operating systems, from single-user systems such
as Windows for end users to mainframe systems such as IBM’s mainframe
operating system, which can support thousands of users, are built around the
concept of the process.
A program in execution.
An instance of a program running on a computer.
The entity that can be assigned to and executed on a processor.
A unit of activity characterized by the execution of a sequence of
instructions, a current state, and an associated set of system resources.
1. Program code: which may be shared with other processes that are
executing the same program,
2. Set of data: associated with program code.
3. Process control block: is data structure containing several elements that
uniquely characterize the process. These elements include the following:
a. Identifier: A unique identifier associated with process, to
distinguish it from all other processes.
28
b. State: If the process is currently executing, it is in the running state.
c. Priority: Priority level relative to other processes.
d. Program counter: The address of the next instruction in the
program to be executed.
e. Memory pointers: Includes pointers to the program code and data
associated with process, plus any memory blocks shared with other
processes.
f. Context data: data that are present in registers in the processor
while the process is executing.
g. I/O status information: Includes outstanding I/O requests, I/O
devices (e.g., disk drives) assigned to this process, a list of files in
use by the process, and so on.
h. Accounting information: May include the amount of processor time
and clock time used, time limits, account numbers, and so on.
PCB is created and managed by the OS. The significant point about the
PCB is that it contains enough information so that it is possible to interrupt a
29
running process and later resume execution as if the interruption had not
occurred. PCB is the key tool that enables the OS to support multiple processes
and to provide for multiprocessing. Figure 2.1 shows how the CPU switches
between two processes using PCBs of these two processes.
30
2.3 Process scheduling
All processes in the system are put into a job queue. The processes that
are in ready state are placed in the ready queue. This queue is generally stored
as a linked list. Each PCB includes a pointer field that points to the next PCB in
the ready queue.
2.3.2 Schedulers
31
Figure 2.3: The ready queue and various 1/0 device queues.
32
milliseconds, then 10/ (100 + 10) = 9 percent of the CPU is used (wasted)
for scheduling the work.
33
It is important that the long-term scheduler select a good process mix of
I/O-bound and CPU-bound processes.
If all processes, are I/0 bound, the ready queue will almost always be
empty, and the short-term scheduler will have little to do.
If all processes are CPU bound, the I/O waiting queue will almost always
be empty, devices will go unused, and again the system will be
unbalanced.
Similarly, a context switch occurs when the time slice for one process
has expired, and a new process is to be loaded from the ready queue. This will
cause the current process's state to be saved and the new process's state to be
restored. Saving and restoring states involves saving and restoring all registers
and program counters, as well as the PCBs as shown in Figure 2.1.
34
Figure 2.6: A tree of processes on a typical Solaris system.
35
3. The parent may be able to share some resources (such as memory
or files) among several of its children.
Two possibilities exist in terms of execution:
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all its children have terminated.
Two possibilities in terms of address space of new process:
1. The child process is a duplicate of the parent process (it has the
same program and data as the parent).
2. The child process has a new program loaded into it.
1. Normal Completion: Process completes all tasks and releases the CPU.
2. Protection Error: Process wants to use a resource that is not allowed to
use by that process. For example, if a process wants to WRITE on a file
that is READ ONLY file.
3. I/O Failure: When a process attempts to use an I/O device and I/O
device is not working fine now. For example, a process that wants to
print a file on the printer, but the printer is defective.
4. Parent Request: If a parent process request for terminating the child
process. Then, the child process should be terminated.
5. Parent Termination: When the parent is terminated, child process also
needs to be terminated.
36
6. Time Over Run: Waiting time is specified for a process that for how
much time a process can wait for a resource. In this time if the process
fails to allocate a resource, then the process needs to be terminated.
7. Arithmetic Error: There is an instruction of a process that is invalid
instruction, the process needs to be terminated. For example, if a
process wants to divide a number by zero.
8. Memory Requirement: A process requires more memory to execute but
the system fails to provide enough memory to the process for its
execution, then the process needs to be terminated.
9. Privileged Instruction: Process try to execute an instruction that is
reserved for only OS.
10. OS Involvement to terminate: In some critical cases, OS takes control of
the process and stops the execution of the process. For example, if a
deadlock occurs, or deadlock can occur, then OS terminates the process.
37
Information sharing: several users may be interested in the same piece
of information (shared file). It is important to provide an environment to
allow concurrent access to such information.
Computation speedup: often a solution to a problem can be solved
faster if the problem can be broken down into sub-tasks to be solved
simultaneously (especially when multiple processors are involved).
Modularity: The most efficient architecture may be to break a system
down into cooperating modules.
Convenience: Even a single user may be multi-tasking, such as editing,
compiling, printing, and running the same code in different windows.
38
Figure 2.7: Communications models. (a) Message passing. (b) Shared memory.
======================================================
39
Chapter 3
CPU Scheduling
CPU scheduling is the basis of multi-programmed operating systems. By
switching the CPU among processes, the operating system can make the
computer more productive.
Almost all programs have some alternating cycle of CPU running and
waiting for I/O. In a simple system running a single process, the time spent
waiting for I/O is wasted. A scheduling system allows one process to use the
CPU while another is waiting for I/O. The challenge is to make the overall
system as efficient and fair as possible.
Whenever the CPU becomes idle, it is the job of the CPU Scheduler
(short-term scheduler) to select another process from the ready queue to run
next. Note that the ready queue can be implemented as a FIFO queue, a
priority queue, a tree, or simply an unordered linked list.
40
Figure 3.1: Alternating sequence of CPU and I/O bursts.
41
If scheduling takes place only under conditions 1 and 4, the system is
said to be non-preemptive, or cooperative. Under these conditions, once a
process starts running it keeps running, until it either voluntarily blocks or until
it finishes. Otherwise, the system is said to be preemptive.
3.1.4 Dispatcher
Dispatcher is the module that gives control of the CPU to the process
selected by the scheduler. This function involves:
Switching context.
Switching to user mode.
Jumping to the proper location in the newly loaded program.
42
Waiting time: How much time processes spend in the ready queue
waiting their turn to get on the CPU.
Response time. In an interactive system, response time is the time the
system takes to start responding, not the time it takes to output the
response.
If the processes arrive in the order P1, P2, P3, we get the result shown in
the following Gantt chart:
43
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
The average waiting time is now (6 + 0 + 3)/3 = 3 ms. The total run time
for the three bursts is the same, but here two of the three processes finish
much quicker, and the other process is only delayed by a short amount.
Average waiting time under an FCFS is not minimal and may vary greatly
if the processes CPU burst times vary greatly.
FCFS scheduling algorithm is non-preemptive. Once the CPU has been
allocated to a process, this process keeps the CPU until it releases the
CPU, either by terminating or by requesting I/O.
FCFS is troublesome for time-sharing systems. Since it cannot be fair
between users whose get a share of the CPU at regular intervals.
FCFS can block the system in a busy dynamic system in a way, known as
the convoy effect. If the CPU gets the processes of the higher burst first,
then the processes of lower burst time may get blocked (they may never
get the CPU if the job in the execution has a very high burst time).
44
3.3.2 Shortest-Job-First Scheduling (SJF)
The idea behind the SJF algorithm is to pick the smallest job first, and
then pick the next smallest job to do next and so on.
Example1: consider the following four processes, with the length of the CPU
burst given in milliseconds and assume that all processes arrive at the same
time.
P4 P1 P3 P2
0 3 9 16 24
45
o A preemptive SJF algorithm will preempt the currently executing
process.
Sometimes, preemptive SJF is called shortest-remaining-
time-first.
o Whereas a non-preemptive SJF algorithm will allow the currently
running process to finish its CPU burst.
Example2: consider the following four processes, with the length of the CPU
burst given in milliseconds.
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
P1 P2 P4 P1 P3
0 1 5 10 17 26
46
practice, priorities are implemented as integers within a fixed range. We will
use low number for high priorities, with 0 being the highest possible priority.
Example1: consider the following five processes, with the length of the CPU
burst given in milliseconds and priority given as integers.
The following Gantt chart shows the scheduling with priority algorithm.
The average waiting time is 8.2 ms:
P2 P5 P1 P3 P4
0 1 6 16 18 19
CPU bursts are assigned with a small unit of time called time quantum. A
time quantum is generally from 10 to 100 ms in length.
Preemption is added to enable the system to switch between processes.
1. If the process finishes its burst before the time quantum timer expires,
then it is swapped out of the CPU just like the normal FCFS algorithm.
2. If the timer goes off first, then the process is swapped out of the CPU
and moved to the back end of the ready queue.
Example1: Consider the following three processes that arrive at time 0, with
the length of the CPU burst given in milliseconds and assume that time
quantum, q = 4.
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
48
The average waiting time is 17/3 = 5.66 ms.
General characteristics of RR
49
These two types of processes have different response-time
requirements and scheduling needs. Foreground processes may have priority
over background processes. Process is assigned to one queue, based on some
property (such as memory size, priority, or process type). Each queue has its
own scheduling algorithm:
1. Strict priority: no job in a lower priority queue runs until all higher
priority queues are empty. That is, all processes in foreground queue are
served first, then the processes in background queue.
2. RR: Each queue gets a certain amount of CPU time which it can schedule
amongst its processes; i.e., 80% to foreground in RR, whereas 20% to
background in FCFS.
50
Note that under this algorithm jobs cannot switch from queue to queue.
Once they are assigned a queue, this queue is their queue until they finish.
Figure 3.3 shows a multilevel queue scheduling algorithm with five queues,
listed in order of top-down priority.
Each queue has absolute priority over lower-priority queue. No process
in the batch queue could run unless the queues of system processes,
interactive processes, and interactive editing processes were all empty. If an
interactive editing process entered the ready queue while a batch process was
running the batch process would be preempted.
1. Process, in a higher-priority queue, that uses too much CPU time will be
moved to a lower- priority queue.
2. Process that waits too long in a lower-priority queue may be moved to a
higher-priority queue.
This form of aging prevents starvation.
51
Multilevel feedback queue scheduling is the most flexible, because it can
be tuned for any situation. But it is also the most complex to implement
because it has several parameters that must be adjusted. Some of these
parameters:
52
3. Q2 are run on an FCFS only when Q 0 and Q1 are empty.
Note that:
53
Chapter 4
Synchronization
Cooperating process is one that can affect or be affected by other
processes executing in the OS. Cooperating processes can:
Either directly share a logical address space (that is, both code and data)
Or be allowed to share data only through files or messages. Concurrent
access to shared data may result in data inconsistency.
54
int counter = 0; // holds the number of items in the buffer
void producer()
{
while (true)
{
item = produceItem();
if (counter == BUFFER_SIZE)
sleep();
/* When sleep is called, the caller is blocked until
another process wakes it up by using the
wakeup routine */
putItemIntoBuffer(item);
counter ++;
if (counter == 1)
wakeup(consumer);
}
}
// =======================================
void consumer()
{
while (true)
{
if (counter == 0)
sleep();
item = removeItemFromBuffer();
counter --;
if (counter == BUFFER_SIZE - 1)
wakeup(producer);
consumeItem(item);
}
}
Although both the producer and consumer routines described above are
correct separately, they may not function correctly when executed
concurrently. This because both the producer and the consumer are adjusting
the value of the variable counter, which can lead to a condition known as
a race condition.
55
For producer-consumer processes, the race condition may occur in two
situations:
Example1: suppose that counter = 5 (currently) and that the producer and
consumer processes execute the statements "counter ++;" and "counter --;"
concurrently. Note that, although "counter ++;" and "counter --;" are single
instructions, they are three steps at the hardware level:
If the instructions from the two processes get interleaved, there could be race
condition problem, such as illustrated below:
Rigister1 = counter
Producer: Rigister1 ++
Counter = Rigister1
Rigister2 = counter
Consumer: Rigister2 --
Counter = Rigister2
Interleaving
T0 Producer executes Rigister1 = counter {Rigister1 = 5}
T1 Producer executes Rigister1 ++ {Rigister1 = 6}
T2 Consumer executes Rigister2 = counter {Rigister2 = 5}
T3 Consumer executes Rigister2 -- {Rigister2 = 4}
T4 Producer executes Counter = Rigister1 {Counter = 6}
T5 Consumer executes Counter = Rigister2 {Counter = 4}
56
If we reverse the order of statements T4 and T5 we get the value of counter = 6!
Note that both results 4 and 6 are wrong, the correct result must be counter =
5 after one running for producer and consumer processes.
1. The consumer has just read the variable Counter = 0 and is just about to
move inside the if block.
2. Just before calling sleep, the consumer is interrupted, and the producer
is resumed.
3. The producer creates an item, puts it into the buffer, and
increases Counter.
4. Now counter = 1, the producer tries to wake up the consumer.
5. Unfortunately, the consumer was not yet sleeping, and the wakeup call
is lost. When the consumer resumes, it goes to sleep and will never be
awakened again. Since, the consumer is only awakened by the producer
when Counter = 1.
6. The producer will loop until the buffer is full, after which it will also go to
sleep.
Now the two processes are sleeping, and the deadlock occurred.
Generally, the race conditions are difficult to identify and debug, because they
only occur on rare cases, and only when the timing is just exactly right.
What is the solution of this problem? The solution is to only allow one process
at a time to manipulate the value "counter".
entry section
critical section
exit section
remainder section
} while (TRUE);
58
2. Progress: If one process does not need to execute its critical section
then it should not stop other processes to get into their critical sections.
3. Bounded waiting. Each process must have a limited waiting time. It
should not wait endlessly to access its critical section.
There are several methods can be used to solve critical-section problem
such as: Peterson solution, test-and-set, Semaphore, and Swap.
1. int turn: Indicates whose turn it is to enter its critical section. If turn = 0,
then P0 is allowed into its critical section.
2. Boolean flag[i]: Indicates when a process wants to enter its critical
section. When P0 wants to enter their critical section, it sets flag[0] to
true.
do {
flag[0] = TRUE;
turn = 1;
while (flag[1] && turn == 1);
critical section
flag[0] = FALSE;
remainder section
} while (TRUE);
Mutual Exclusion is assured as only one process can access the critical
section at any time.
Progress is also assured, as a process outside the critical section does not
block other processes from entering the critical section.
Bounded Waiting is preserved as every process gets a fair chance.
4.5 Test-and-Set
60
If the lock is locked (lock = 1), the process waits till the lock becomes
free,
If the lock is not locked (lock = 0), process takes the lock (set lock = 1)
and executes its critical section (see Figure 4.3).
do {
lock = 0;
while (test_and_set(lock));
critical section
lock = 0;
remainder section
} while (TRUE);
Figure 4.3: The definition of Test-and-Set solution.
To show how the test-and-set mechanism works, consider there are two
processes P0 and P1 trying to enter their critical sections:
61
Test-and-set solution preserves only two conditions:
Mutual Exclusion is assured as only one process can access the critical
section at any time.
Progress is also assured, as a process outside the critical section does not
block other processes from entering the critical section.
Wait(S)
{ while (S <= 0);
S --;
}
----------------------------------------------
Signal(S)
{
S ++;
}
Figure 4.4: The definition of semaphore operations.
4.6 Semaphore
62
On multi-processor machines,
Because they are often platform dependent.
Types of Semaphores
63
If the counting semaphore is greater than zero, then a process can enter
its critical section and use one of the resources.
When the counter gets to zero, then the process blocks until another
process frees up a resource and increments the counting semaphore
with a signal call.
Semaphores can also be used to synchronize certain operations between
processes. For example, suppose that process P0 must execute statement S0
before process P1 executes statement S1.
Advantages of Semaphores
Semaphores allow only one process into the critical section.
Semaphores are implemented in the machine independent.
As there is busy waiting in semaphore, there is never a wastage of
process time and resources.
They allow flexible management of resources.
64
Disadvantage of semaphores
The operating system must keep track of all calls to wait and signal
semaphore.
One of the biggest limitations of a semaphore is priority inversion. It is
scheduling problem arises when a high-priority process gets blocked
waiting for a resource that is currently held by a low-priority process.
Deadlock may occur when multiple processes are blocked, each waiting
for a resource that can only be freed by one of the other blocked
processes.
Starvation may occur when one or more processes gets blocked forever,
and never get a chance to take their turn in the critical section.
Semaphore programming is a complicated, so there are chances of not
achieving mutual exclusion.
====================================
65
Chapter 5
Deadlock
In a multiprogramming environment, several processes may compete for
a finite number of resources. A process requests resources: if the resources are
not available at that time, the process enters a waiting state. Sometimes, a
waiting process is never again able to change state because the resources it
has requested are held by other waiting processes. This situation is called a
deadlock.
1. Memory space,
2. CPU cycles,
3. files,
4. I/0 devices such as printers and DVD drives.
If a system has two CPUs, then the resource type CPU has two instances.
All resources in the same type must be equivalent. The request of this
type can be equally satisfied by any one of the instances in that type.
If there is some difference between the instances within a type, then
that type needs to be divided into separate types. For example, printers
may need to be separated into: laser printers and color inkjet printers.
66
A process must request a resource before using it and must release the
resource after using it. A process may request as many resources as it requires
to complete its tasks. A process may utilize a resource in only the following
sequence:
Request: It requests the resource. If the request cannot be granted
immediately, then the process must wait until it can acquire the
resource.
Use: It uses the resource, e.g. it prints on the printer or reads from file.
Release: It releases the resource.
67
another process requests that resource, then this process must be wait
for the resource to be released.
2. Hold and wait: A process must be holding at least one resource and
waiting to acquire additional resources that are currently being held by
other processes.
3. No preemption: Once a process is holding a resource, then that resource
cannot be taken away from that process until the process voluntarily
releases it.
4. Circular wait: A set of waiting processes {P0, Pl, ..., Pn} must exist such
that P0 is waiting for a resource held by P1, P1 is waiting for a resource
held by P2, ..., Pn-1 is waiting for a resource held by Pn and Pn is waiting for
a resource held by P0.
68
o Assignment edge: A directed edge from resource type Rj to
process Pi is denoted by Rj → Pi; it indicates that an instance of
resource type Rj has been allocated to process Pi.
69
Figure 5.2: Resource-allocation graph with a deadlock
1. P1 R1 P2 R3 P3 R2 P1.
2. P2 R3 P3 R2 P2.
Processes P1, P2, and P3 are deadlocked. Process P2 is waiting for the
resource R3, which is held by process P3. Process P3 is waiting for either process
P1 or process P2 to release resource R2. In addition, process P1 is waiting for P2
to release resource R1.
In Figure 5.3, we also have a cycle: P1 R1 P3 R2 P1. However,
there is no deadlock. Observe that process P4 may release its instance of
resource type R2. This resource can then be allocated to P3, breaking the cycle.
70
5.3 Methods for handling deadlock
71
in its execution and does not need some other resource until
much later.
b. Processes holding resources must release them before requesting
new resources. This can be a problem if a process has partially
completed an operation using a resource and then fails to get it
after releasing it.
c. Either of the above two methods can lead to starvation if a
process requires one or more popular resources.
3. No Preemption: Preemption of process resource allocations can prevent
this condition of deadlocks when it is possible.
a. One approach is that if a process is forced to wait when
requesting a new resource, then it forced to release all other
resources previously held by this process. It is forced to re-acquire
the old resources along with the new resources in a single
request.
b. Another approach is that when a resource is requested and not
available, then the system looks to see what other waiting
processes currently have those resources. If such a process is
found, then some of their resources may get preempted and
added to the list of resources for which the process is waiting.
c. Either of these approaches may be applicable for resources whose
states are easily saved and restored, such as registers and
memory, but are generally not applicable to other devices such as
printers and tape drives.
4. Circular Wait: One way to avoid circular wait is to number all resources.
For example, if the set of resource types R includes tape drives, disk
drives, and printers, then we can order these resources as follows:
72
F(tape drive) = 1
F(disk drive) = 5
F(printer) = 12
Thus, to avoid the deadlock this requires more information about each
process. The scheduler needs to know:
73
What resources may be needed in what order?
A state is safe if the system can allocate all resources requested by all
processes without entering a deadlock state. That is, a state is safe if there
exists a safe sequence of processes {P0, P1, P2, ..., PN} such that all of the
resource requests for Pi can be granted using the resources currently allocated
to Pi and all processes Pj where j < i. (i.e. if all the processes prior to Pi finish
and free up their resources, then Pi will be able to finish also, using the
resources that they have freed up).
If a safe sequence does not exist, then the system is in an unsafe state,
which may lead to deadlock. Note that, all safe states are deadlock free, but
not all unsafe states lead to deadlocks as shown in Figure 5.4.
74
For example, consider a system with 12 tape drives and 3 process. The
tape drivers, at time t0, are allocated as follow:
Note that there are 3 free tap drivers. Is this a safe state? What is the
safe sequence?
At time t0, the system is in safe state. The sequence <P1, P0, P2> satisfies
the safety condition:
Process P1 can immediately be allocated all its tape drives and then
return them (system have 5 available tape drives);
Then process P0 can get all its tape drives and return them (system have
10 available tape drives);
Finally, process P2 can get all its tape drives and return them (system
have 12 tape drives available).
If, at time t1, process P2 requests and is allocated one more tape drive.
The system is no longer in a safe state. At this point:
Only process P1 can be allocated all its tape drives. When it returns
them, the system will have only 4 available tape drives.
Since process P0 is allocated 5 tape drives but has a maximum of 10, it
may request five more tape drives. If it does so, it will have to wait,
because they are unavailable.
75
Similarly, process P2 may request 6 additional tape drives and must wait,
resulting in a deadlock.
If we had made P2 wait until either of the other processes had finished
and released its resources, then we could have avoided the deadlock. So, the
key to the safe state approach is that when a request is made for resources,
the request is granted only if the resulting allocation state is a safe one.
Figure 5.5: Left: Resource allocation graph for deadlock avoidance. Right: An unsafe state in
a resource allocation graph.
For this technique to work, all claim edges must be added to the graph
for any process before that process can request any resources.
To illustrate this algorithm, we consider the resource-allocation graph of
Figure 5.5 (Left). Suppose that P2 requests R2. Although R2 is currently free, we
cannot allocate it to P2 since this action will create a cycle in the graph (Figure
76
5.5 (right)). A cycle indicates that the system is in an unsafe state. If P 1 requests
R2, and P2 requests R1, then a deadlock will occur.
The name was chosen because the algorithm could be used in a banking
system to ensure that the bank never allocated its available cash in a way that
it could no longer satisfy the needs of all its customers.
Vector X is less than or equal vector Y if X[i] <= Y[i] for all i.
77
One row of the Need matrix, Need[i], can be treated as a vector
corresponding to the needs of process Pi, and similarly for Allocation and
Max.
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize Work
= Available and Finish[i] = false for i = 0, 1, …, n-1.
2. Find an index i such that both
a. Finish[i] == false
b. Need[i] ≤ Work
If no such i exists, go to step 4.
3. Work = Work + Allocation[i]
Finish[i] = true
Go to step 2.
4. If Finish[i] == true for all i, then the system in safe state.
Resource-Request Algorithm
After ensuring that the current state is safe, next we are now ready to
look at the Banker's algorithm itself. Resource-request algorithm determines if
a new request is safe and grants it only if it is safe to do so. Let Request[n][m]
78
indicates the number of resources of each type currently requested by
processes and Request[i] be the request vector for process Pi. If Request[i][j]
== k, then process Pi wants k instances of resource type Rj. When a request for
resources is made by process Pi, the following actions are taken:
1. If Request[i] ≤ Need[i], go to step 2. Otherwise, an error occurs.
2. If Request[i] ≤ Available, go to step 3. Otherwise, Pi must wait.
3. Allocate the resource and modify the state as follows:
Available = Available – Request[i];
Allocation[i] = Allocation[i] + Request[i] ;
Need[i] = Need[i] – Request[i] ;
The system is currently in a safe state. Indeed, the sequence <P1, P3, P4,
P2, P0> satisfies the safety criteria.
What happens if process P1 requests 1 instance of A and 2 instances of C?
79
So, Request[1] = (1, 0, 2). To decide whether this request can be immediately
granted, we first check the following condition:
Request[1] ≤ Available
That is, we check (1, 0, 2) ≤ (3, 3, 2), which is true. We then pretend that this
request has been fulfilled, and we arrive at the following new state:
80
Figure 5.6: (a) Resource allocation graph. (b) Corresponding wait-for graph
If each resource type has a single instance, then we can use a variation
of the resource-allocation graph known as a wait-for graph. A wait-for graph
can be constructed from a resource-allocation graph by eliminating the
resources and collapsing the associated edges, as shown in Figure 5.6. An arc
from Pi to Pj in a wait-for graph indicates that process Pi is waiting for a
resource that process Pj is currently holding.
81
5.6.2 Several Instances of a Resource Type
1. Let Work and Finish be vectors of length m and n, respectively. Initialize Work
= Available. For i = 0, 1, ..., n-1, if Allocation[i] 0, then Finish[i] = false;
otherwise, Finish[i] = true.
2. Find an index i such that both:
a. Finish[i] == false
b. Request[i] ≤ Work
If no such i exists, go to step 4.
3. Work = Work + Allocation[i]
Finish[i] = true
Go to step 2.
4. If Finish[i] == false for some i, 0 i < n, then the system is in a deadlocked.
Moreover, if Finish[i] == false, then process Pi is deadlocked.
1. In step 1, Banker's Algorithm sets Finish[i] = false for all i. This algorithm
sets Finish[i] = false only if Allocation[i] zero.
2. In step 4, the basic Banker's Algorithm says that if Finish[i] == true for all
i, that there is no deadlock. This algorithm is more specific, by stating
82
that if Finish[i] == false for any process Pi, then that process is specifically
involved in the deadlock which has been detected.
The system is not in deadlocked state. Indeed, the sequence <P0, P2, P3,
P1, P4> results in Finish[i] == true for all i.
Suppose that process P2 makes one additional request for an instance of
type C. The Request matrix is modified as follows:
83
5.7 Recovery from Deadlock
1. Inform the system operator and allow him to take manual intervention.
2. Terminate one or more processes involved in the deadlock.
3. Preempt resources.
1. Abort all deadlocked processes: This method clearly will break the
deadlock cycle, but at great expense.
2. Abort one process at a time until the deadlock cycle is eliminated: This
is more conservative but requires doing deadlock detection after each
step. In this case there are many factors that can go into deciding which
processes to terminate next:
a. What the process priority is?
b. How long the process has been running, and how close it is to
finishing?
c. How many and what type of resources is the process holding. Are
they easy to preempt and restore?
d. How many more resources does the process need to complete?
e. How many processes will need to be terminated?
f. Whether the process is interactive or batch?
84
5.7.2 Resource Preemption
When preempting resources to relieve deadlock, there are three
important issues to be addressed:
1. Selecting a victim: Deciding which resources to preempt from which
processes involves many of the same decision criteria outlined with
process termination.
2. Rollback: If we preempt a resource from a process, what should be done
with that process? Clearly, it cannot continue with its normal execution;
it is missing some needed resource. We must roll back the process to
some safe state and restart it from that state.
3. Starvation: How do we ensure that starvation will not occur? That is,
how can we guarantee that resources will not always be preempted
from the same process?
=====================================
85
Chapter 6
Memory management
Previously, we showed how the CPU scheduling can improve both the
utilization of the CPU and the speed of the computer's response to its users.
To increase in computer performance several processes must kept in memory;
that is, they must share memory.
The CPU can access directly only main memory and the registers built
into the processor itself. Any instructions in execution, and any data being
used by the instructions, must be in one of these direct-access storage devices.
If the data are not in memory, they must be moved there before the CPU can
operate on them.
Each process must have a separate memory space. This requires having
the ability to do the following:
1. Determining the range of legal addresses that the process may access.
2. Ensuring that the process can access only these legal addresses.
86
6.1.1 Fence
87
(a) (b)
Figure 6.1: Fence implementation: (a) Fixed fence. (b) Fence register.
Figure 6.2 A base and a limit register define a logical address space.
88
can be useful in knowing how much space is selected and in checking for
overflows into “forbidden” areas. To overcome this difficulty, a second register
is often added, as shown in figure 6.2. The second register, called a limit
register, is the offsets from the base register. Thus, the two registers are:
Figure 6.3: Hardware address protection with base and limit registers.
Important point: Erroneous addresses inside a user’s address space can still
affect that program because the base/limit checking guarantees only that each
address is inside the user’s address space. To understand this point let us
consider the following example.
89
Example: a user error might occur with two cases:
Only fetched instructions are relocated and checked with the first
register pair.
Only data accesses are relocated and checked with the second register
pair.
90
address changes at some later time, then the program will have to be
recompiled.
2. Load Time: If the location at which a program will be loaded is not
known at compile time, then the compiler must generate relocatable
code, which references addresses relative to the start of the program. If
that starting address changes, then the program must be reloaded but
not recompiled.
3. Execution Time: If a program can be moved around in memory during its
execution, then binding must be delayed until execution time. This
requires special hardware, and is the method implemented by most
modern OSs.
91
Figure 6.4 shows the various stages of the binding processes and the
units involved in each stage.
Addresses bound at compile time or load time have identical logical and
physical addresses.
Addresses created at execution time have different logical and physical
addresses.
o In this case the logical address is also known as a virtual address.
92
by a user process. As shown in Figure 6.5, an attempt by the user to address
location 346 is dynamically relocated to location 14346.
The user program deals with logical addresses and never sees the real
physical addresses. The memory-mapping hardware converts logical addresses
into physical addresses.
6-2 Swapping
93
o If it is in backing store:
If there is free memory, the dispatcher swaps in the desired
process.
If there is no free memory, it swaps out a process currently
in memory and swaps in the desired process.
The dispatcher then reloads registers and transfers control to the
selected process.
Example: assume that the size of a process is 100 MB, and the backing store is
a standard hard disk with a transfer rate of 50 MB per second. The actual
transfer of the 100 MB process to or from main memory takes:
𝑝𝑟𝑜𝑐𝑒𝑠𝑠 s𝑖𝑧𝑒 100
= = 2 sec = 2000 𝑚𝑖𝑙𝑙𝑖𝑠𝑒𝑐𝑜𝑛𝑑
𝑡𝑟𝑎𝑛𝑠𝑓𝑒𝑟 𝑟𝑎𝑡𝑒 50
94
6-3 Contiguous memory allocation
1. One for OS: usually in low memory address (at address 0).
2. One for the user processes: below OS memory.
To load several user processes into memory at the same time, we need
to consider how to allocate available memory to these processes. In
contiguous memory allocation, each process is contained in a single contiguous
section of memory.
This is shown in Figure 6.7. The system shown in Figure 6.7 allows the
following:
95
1. Protection against user programs accessing areas that they should not,
2. Programs to be relocated to different memory starting addresses as
needed,
3. The memory space devoted to the OS to grow or shrink dynamically as
needs change.
There are many different strategies for finding the best allocation of
memory to processes:
First fit: Allocate the first hole that is big enough. Searching can start
either at the beginning of the set of holes or at the location where the
previous first-fit search ended. We can stop searching as soon as we find
a free hole that is large enough.
Best fit: Allocate the smallest hole that is big enough. We must search
the entire list, unless the list is ordered by size. This strategy produces
the smallest leftover hole.
96
Worst fit: Allocate the largest hole. Again, we must search the entire list,
unless it is sorted by size. This strategy produces the largest leftover
hole, which may be more useful than the smaller leftover hole. Because
this increases the likelihood that the remaining portion will be usable for
satisfying future requests.
6.3.3 Fragmentation
97
There are two solutions for external fragmentation:
6-4 Segmentation
Each segment has a unique name. A code or data item within a segment
is addressed as the pair (name, offset), where:
98
offset is its location within the segment. That is, its distance from the
start of the segment (length).
The operating system must maintain a table of segment names and their
true addresses in memory. This segment table is used to map between logical
and physical addresses. A logical address consists of two parts:
99
1. Segment name: is used as an index to the segment table.
2. Segment offset: must be between 0 and segment length.
This translation is shown in Figure 6.9. For efficiency there is usually one
operating system segment address table for each process in execution. Two
processes that need to share access to a single segment would have the same
segment name and address in their segment tables.
1. The operating system can place any segment at any location or move
any segment to any location, even after the program begins to execute.
Because the operating system translates all address references by a
segment address table, the operating system need only update the
address in that one table when a segment is moved.
2. A segment can be removed from main memory (and stored on an
auxiliary device) if it is not being used currently. (The appearance of
memory to the user is not necessarily what exists.)
3. Every address reference pass through the operating system, so there is
an opportunity to check each one for protection.
102
a. If it is not, we trap to the operating system (logical addressing
attempt beyond end of segment).
b. When an offset is legal, it is added to the segment base to
produce the address in physical memory of the desired byte.
103
Segment 0 is 1000 bytes long and begins at location 1400.
o A reference to byte 1222 of segment 0 would result in a trap to
the OS, as this segment is only 1000 bytes long.
6.5 Paging
104
Figure 6.12: Paging hardware.
To divide the physical memory into several equal sized blocks called
frames,
To divide the programs logical memory space into blocks of the same
size called pages.
Any page from any process can be placed into any available frame.
The page table is used to maps pages to their corresponding frames. The
paging model of memory is shown in Figure 6.13. For example, page 2 of the
program logical memory is currently stored in frame 3 of physical memory.
105
Figure 6.13: Paging model of logical and physical memory.
The page size and frame size are defined by the hardware. The size of a
page is typically a power of 2, allowing addresses to be split at a certain
number of bits. If the size of the logical address space is 2m, and a page size is
2n then the high-order m - n bits of a logical address designate the page
number, and the n low-order bits designate the page offset. Thus, the logical
address is as follows:
Page number page offset
p d
m–n n
106
Figure 6.14: Paging example for a 32-byte memory with 4-byte pages.
1. Firstly, specify the page number and page offset of a given logical
address,
2. Secondly, determine the allocated frame from page table,
3. Finally, use the form:
107
3. Logical address 0 maps to physical address = 5 x 4 + 0 = 20.
What is the physical address corresponding to logical address 3?
1. Logical address 3 is in page 0, with offset 3.
2. Page 0 is allocated frame 5.
3. Logical address 3 maps to physical address = 5 x 4 + 3 = 23.
What is the physical address corresponding to logical address 4?
1. Logical address 4 is in page 1, with offset 0.
2. Page 1 is allocated frame 6.
3. Logical address 4 maps to physical address = 6 x 4 + 0 = 24.
What is the physical address corresponding to logical address 13?
1. Logical address 13 is in page 3, with offset 1.
2. Page 3 is allocated frame 2.
3. Logical address 13 maps to physical address = 2 x 4 + 1 = 9.
Figure 6.15: Free frames (a) before allocation and (b) after allocation.
108
The first page is loaded into one of the allocated frames, and the frame
number is put in the page table for this process.
The next page is loaded into another frame; its frame number is put into
the page table,
And so on (see Figure 6.15).
Some important remarks:
6.5.3 Protection
109
One bit can be added to the page table to classify the page as read-
write, read-only, read-write-execute, or some combination of these sorts
of things.
A valid-invalid bit can be added to mask off entries in the page table
that are not in use by the current process.
o If this bit is set to "valid" the associated page is in the process
logical address space and is legal page.
o If it is set to "invalid" the page is not in the process logical address
space and is illegal page.
o OS sets this bit for each page to allow or disallow access to the
page.
Example: suppose that in a system with a 14-bit address space (from 0 to 214 =
16383), we have a program that should use only addresses 0 to 10468 (need 6
pages). Given a page size of 2 KB (211), we have the situation shown in Figure
6.16.
110
Addresses in pages 0, 1, 2, 3, 4, and 5 are mapped normally through the
page table. Any attempt to generate an address in pages 6 or 7 will find that
the valid-invalid bit is set to invalid, and the computer will trap to the OS
(invalid page reference).
Notice that this scheme has created a problem. Because the program
extends only to address 10468, any reference beyond that address is illegal.
However, references to page 5 are classified as valid, so accesses to addresses
up to 12287 are valid. Only the addresses from 12288 to 16383 are invalid. This
problem is a result of the 2-KB page size and reflects the internal
fragmentation of paging.
111
Thus, to support 40 users, we need only one copy of the editor (150 KB),
plus 40 copies of the 50 KB of data space per user. The total space
required is now 150 + 40 50 = 2150 KB instead of 8,000 KB.
=========================================
112
Chapter 7
Secondary-storage management
In this chapter, we first introduce an overview of magnetic disks. We
then describe disk-scheduling algorithms, which schedule the order of disk I/O
operations to improve performance.
113
Figure 7.1 Moving-head disk mechanism.
When the disk is in use, a drive motor spins it at high speed. Most drives
rotate 60 to 200 times per second. Disk speed has two parts:
1. The transfer rate is the rate at which data flow between the drive and
the computer.
2. The positioning time sometimes called the random-access time consists
of:
a. Seek time: is the time necessary to move the disk arm to the
cylinder containing the desired cylinder.
b. Rotational latency: is the additional time necessary for the disk to
rotate the desired sector to the disk head.
Disk heads fly over the surfaces on a very thin cushion of air. If they
should accidentally contact the disk, then a head crash occurs, which normally
cannot be repaired; the entire disk must be replaced.
114
Disk drives are connected to the computer via a cable known as the I/O
Bus. Some of the common interface formats include:
115
order. Whenever a process needs disk I/O requests. These disk requests must
include the following information:
Disk address,
Memory address,
Number of sectors to transfer,
Whether the request is for reading or writing.
FCFS is simple and fair, but it is not very efficient. That is, it generally
does not provide the fastest service.
Example: consider a disk queue with requests for I/O to blocks on cylinders:
98, 183, 37, 122, 14, 124, 65, 67, in this order.
If the disk head is initially at cylinder 53, it will first move from 53 to 98, then to
183, 37, 122, 14, 124, 65, and finally to 67, for a total head movement of 640
cylinders. This schedule is diagrammed in Figure 7.2.
The moving 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 the requests for 122 and 124, the total head
movement could be decreased substantially, and performance could be
thereby improved.
116
Figure 7.2: FCFS disk scheduling
It seems reasonable to service all the requests close to the current head
position before moving the head far to service other requests. This assumption
is the basis for the shortest seek time first (SSTF) algorithm. The SSTF
algorithm selects the request with the least seek time from the current head
position.
Example: consider a disk queue with requests for I/O to blocks on cylinders:
98, 183, 37, 122, 14, 124, 65, 67, in this order.
According to SSTF algorithm:
1. Cylinder 65 is the closest request to the initial position 53.
2. Once we are at cylinder 65, the next closest request is at cylinder 67.
3. From there, the request at cylinder 37 is closer than the one at 98, so 37
is served next.
4. Continuing, we service the request at cylinder 14, then 98, 122, 124, and
finally 183 (see Figure 7.3).
117
Figure 7.3: SSTF disk scheduling
The scan algorithm is sometimes called the elevator algorithm, since the
disk arm behaves just like an elevator in a building, first servicing all the
requests going up and then reversing to service requests the other way. In scan
algorithm the disk arm starts at one end (initial position) and moves toward
the other end, servicing requests as it reaches each cylinder, until it gets to the
other end of the disk. At the other end, the direction of head movement is
reversed, and servicing continues. The head continuously scans back and forth
across the disk.
Example: consider a disk queue with requests for I/O to blocks on cylinders:
98, 183, 37, 122, 14, 124, 65, 67, in this order.
118
Assume that the disk arm is moving toward 0 from the initial position 53.
According to scan algorithm:
1. In this direction the algorithm will service 37 and then 14.
2. At cylinder 0, the arm will reverse and will move toward the other end of
the disk, servicing the requests at 65, 67, 98, 122, 124, and 183 (see
Figure 7.4).
This algorithm causes the head to move till the end of the disk even if
there are no requests to be serviced. It causes long waiting time for the
cylinders just visited by the head. As a result, the requests at the midrange are
serviced more and those arriving behind the disk arm will have to wait.
119
7.2.4 C-Scan scheduling
Both SCAN and C-SCAN move the disk arm across the full width of the
disk. Look and C-look algorithms are improved versions of the scan and C-scan
algorithms. In look scheduling, the arm goes only as far as the final request in
each direction. Then, it reverses direction immediately without going all the
way to the end of the disk. The main difference between scan scheduling and
look scheduling is:
Scan scheduling scans all the cylinders of the disk starting from one end
to the other end even if there are no requests at the ends.
Look scheduling scans all the cylinders of the disk starting from the first
request at one end to the last request at the other end.
o It does not cause the head to move till the ends of the disk when
there are no requests to be serviced.
o It provides better performance as compared to scan algorithm.
120
Figure 7.6: C-Look disk scheduling
121
Exercises
122
8. What are the five major activities of an operating system regarding file
management?
9. What are the three major activities of an operating system regarding
memory management?
10. The services and functions provided by an operating system can be
divided into two main categories. Briefly describe the two categories and
discuss how they differ.
11. It is sometimes difficult to achieve a layered approach if two components
of the operating system are dependent on each other. Identify a scenario
in which it is unclear how to layer two system components that require
tight coupling of their functionalities.
12. What is the main advantage of the layered approach to system design?
What are the disadvantages of using the layered approach?
13. Describe three general methods for passing parameters to the operating
system.
14. What is the main advantage of the microkernel approach to system
design? How do user programs and system services interact in a
microkernel architecture? What are the disadvantages of using the
microkernel approach?
15. In what ways is the modular kernel approach like the layered approach?
In what ways does it differ from the layered approach?
16. Describe the actions taken by a kernel to context-switch between
processes.
17. Describe the differences among short-term, medium-term, and long-term
scheduling.
18. Describe the differences between independent and cooperating
processes.
123
19. Describe the differences between the two methods of inter-process
communication.
20. Including the initial parent process, how many processes are created by
the program shown below?
21. Consider a system running ten I/O-bound tasks and one CPU-bound task.
Assume that the I/O-bound tasks issue an I/O operation once for every
millisecond of CPU computing and that each I/0 operation takes 10
milliseconds to complete. Also assume that the context-switching
overhead is 0.1 millisecond and that all processes are long-running tasks.
Describe the CPU utilization for a round-robin scheduler when:
a. The time quantum is 1 millisecond
b. The time quantum is 10 milliseconds
22. Why is it important for the scheduler to distinguish I/0-bound programs
from CPU-bound programs?
23. A CPU-scheduling algorithm determines an order for the execution of its
scheduled processes. Given n processes to be scheduled on one
processor, how many different schedules are possible? Give a formula in
terms of n.
124
24. What advantage is there in having different time-quantum sizes at
different levels of a multilevel queueing system?
25. Explain the differences in how much the following scheduling algorithms
discriminate in favor of short processes:
a. FCFS
b. RR
c. Multilevel feedback queues
26. Which of the following scheduling algorithms could result in starvation?
a. First-come, first-served
b. Shortest job first
c. Round robin
d. Priority
27. Consider the following set of processes, with the length of the CPU burst
given in milliseconds:
The processes are assumed to have arrived in the order P1, P2, P3, P4, Ps,
all at time 0.
a. Draw four Gantt charts that illustrate the execution of these
processes using the following scheduling algorithms: FCFS, SJF,
non-preemptive priority (a smaller priority number implies a
higher priority), and RR (quantum= 1).
b. What is the turnaround time of each process for each of the
scheduling algorithms in part a?
125
c. What is the waiting time of each process for each of these
scheduling algorithms?
d. Which of the algorithms results in the minimum average waiting
time (over all processes)?
28. Suppose that the following processes arrive for execution at the times
indicated. Each process will run for time listed. In answering the
questions, use non-preemptive scheduling, and base all decisions on the
information you have at the time the decision must be made.
a. What is the average turnaround time for these processes with the
FCFS scheduling algorithm?
b. What is the average turnaround time for these processes with the
SJF scheduling algorithm?
c. The SJF algorithm is supposed to improve performance but notice
that we chose to run process P1 at time 0 because we did not
know that two shorter processes would arrive soon. Compute
what the average turnaround time will be if the CPU is left idle for
the first 1 unit and then SJF scheduling is used. Remember that
processes P1 and P2 are waiting during this idle time, so their
waiting time may increase. This algorithm could be known as
future-knowledge scheduling.
29. Why is it important to keep the size of the critical section as small as
possible?
126
30. As a critical section can be executed by one process at a time. Other
processes must wait until the current thread must finish. Are long critical
sections minimizing the throughput?
31. What is the meaning of the term busy waiting? What other kinds of
waiting are there in an operating system? Can busy waiting be avoided
altogether? Explain your answer.
32. Show that, if the wait() and signal() semaphore operations are not
executed atomically, then mutual exclusion may be violated.
33. Illustrate how a binary semaphore can be used to implement mutual
exclusion among n processes.
34. Briefly describe the characteristics of a complete solution to the critical
section problem.
35. Briefly describe how the semaphore provides mutual exclusion of 2
processes to access their shared common data.
36. Briefly describe the solution of signal(S) and wait(S) implementation
without busy waiting.
37. Is it possible to have a deadlock involving only a single process? Explain
your answer.
38. Consider a system consisting of four resources of the same type that are
shared by three processes, each of which needs at most two resources.
Show that the system is deadlock free.
39. Write a program in C++ or Java language to implement the banker's
algorithm.
40. Modify your solution to Exercise 3 so that it is starvation-free.
41. Consider the traffic deadlock depicted in the following figure.
127
a. Show that the four necessary conditions for deadlock hold in this
example.
b. State a simple rule for avoiding deadlocks in this system.
42. Assume that there is a single-lane bridge connects the two cities A and B.
People in the two cities use this bridge to deliver their produce to the
neighboring town. The bridge can become deadlocked if a person of city A
and a person of city B get on the bridge at the same time (people of city A
are stubborn and are unable to back up.) Using semaphores, design an
algorithm that prevents deadlock. Initially, do not be concerned about
starvation (the situation in which people of city A prevent people of city B
from using the bridge, or vice versa).
43. Consider the following resource-allocation policy. Requests for and
releases of resources are allowed at any time. If a request for resources
cannot be satisfied because the resources are not available, then we
check any processes that are blocked waiting for resources. If a blocked
process has the desired resources, then these resources are taken away
from it and are given to the requesting process. The vector of resources
for which the blocked process is waiting is increased to include the
resources that were taken away. For example, consider a system with
three resource types and the vector Available initialized to (4,2,2). If
process P0 asks for (2,2,1), it gets them. If P1 asks for (1,0,1), it gets them.
128
Then, if P0 asks for (0,0,1), it is blocked (resource not available). If P2 now
asks for (2,0,0), it gets the available one (1,0,0) and one that was allocated
to P0 (since P0 is blocked). P0's Allocation vector goes down to (1,2,1),
and its Need vector goes up to (1,0,1).
a. Can deadlock occur? If you answer "yes" give an example. If you
answer "no" specify which necessary condition cannot occur.
b. Can indefinite blocking occur? Explain your answer.
44. Consider the following snapshot of a system:
129
47. Compare the memory organization schemes of contiguous memory
allocation, pure segmentation, and pure paging with respect to the
following issues:
a. External fragmentation
b. Internal fragmentation
c. Ability to share code across processes
48. What is the purpose of paging the page tables?
49. On a system with paging, a process cannot access memory that it does
not own. Why? How could the operating system allow access to other
memory? Why should it or should it not?
50. Consider a logical address space of 64 pages of 1,024 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?
51. Given five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB, and 600
KB (in order), how would the first-fit, best-fit, and worst-fit algorithms
place processes of 212 KB, 417 KB, 112 KB, and 426 KB (in order)? Which
algorithm makes the most efficient use of memory?
52. Consider the following segment table:
What are the physical addresses for the following logical addresses?
a. 0,430
130
b. 1,10
c. 2,500
d. 3,400
e. 4,112
53. Consider a logical address space of 32 pages with 1,024 words per page,
mapped onto a physical memory of 16 frames.
a. How many bits are required in the logical address?
b. How many bits are required in the physical address?
54. Suppose that a disk drive has 5,000 cylinders, numbered 0 to 4999. The
drive is currently serving a request at cylinder 143, and the previous
request was at cylinder 125. The queue of pending requests, in FIFO
order, is:
86, 1470, 913, 1774, 948, 1509, 1022, 1750, 130
Starting from the current head position, what is the total distance (in
cylinders) that the disk arm moves to satisfy all the pending requests for
each of the following disk-scheduling algorithms?
a. FCFS
b. SSTF
c. SCAN
d. LOOK
e. C-SCAN
f. C-LOOK
55. Write a program that simulates the disk-scheduling algorithms.
56. Why is rotational latency usually not considered in disk scheduling? How
would you modify SSTF, SCAN, and C-SCAN to include latency
optimization?
131
57. None of the disk-scheduling disciplines, except FCFS, is truly fair
(starvation may occur).
a. Explain why this assertion is true.
b. Describe a way to modify algorithms such as SCAN to ensure
fairness.
c. Explain why fairness is an important goal in a time-sharing system.
d. Give three or more examples of circumstances in which it is
important that the operating system be unfair in serving I/O
requests.
132