You are on page 1of 263

What is Operating System :

The primary objectives of an operating system is to make computer system


convenient to use and utilise computer hardware in an efficient manner.

• An Operating System is a system software which may be viewed as an organised


collection of software consisting of procedure's for operating a computer and
providing an environment for execution of programs.

• It acts as an interface between users and the hardware of a computer system.

• It is a system software that is mainly responsible for Resource Management. It


keeps track of the status of each resource and decides who will have a control over
computer resources, for how long and when.

NEED OF OS :
1. User interacts with the computer through operating system in order to
accomplish his task since it is his primary interface with a computer.
2. It helps users to understand the inner functions of a computer very
closely.
3. Many concepts and techniques found in operating system have general
applicability in other applications.

Computer software can be divided in to two kinds

System programs : which manage the operation of the computer


itself
Application programs :

The most fundamental of all the system programs is the operating system which
controls all the computer’s resources and provides the base upon which the application
programs can be written.

In order to shield programmers from the complexity of the hardware a layer of


software is provided on the top of the bare hardware, to manage all parts of the system
and present the user with an interface or virtual machine that is easier to understand
and program. This layer of software is the operating system.
Banking Airline Adventure Application Programs
System Reservation Games
Compilers Editors command
Interpreter System Programs
Operating Systems
Machine language
Microprogramming Hardware
Physical devices

INTERACTING WITH OPERATING SYSTEM:

There are many ways one can interact with operating system:

i. By means of Operating System Call in a program

ii. Directly by means of Operating System Commands

▪ Command Language : most operating systems provide the user with the
facility of entering commands directly via an interactive terminal.

▪ Job Control Language : Job Control Languages or JCLs are used to


define the requirements and parameters of work submitted to a batch system and
would generally be used by computer operations staff in a mainframe environment.

▪ Graphical User Interface: a Graphical User Interface or GUI provides a


means of interacting with the system using a windows and mouse driven
environment.

System Call:

• System calls provide the interface to a running program and the operating
system.
• User program receives operating system services through the set of system
calls.
• Earlier these calls were available in assembly language instructions but now
a days these features are supported through high-level languages like C,
Pascal etc., which replaces assembly language for system programming.
• The use of system calls in C or Pascal programs very much resemble pre-
defined function or subroutine calls.

Example: To know how system calls are used, let us consider a simple
program to

copy data from one file to another

In an interactive system, the following system calls will be generated by the


operating system for this:

1. Prompt messages for inputting two file names and reading it from terminal.

2. Open source and destination file.

3. Prompt error messages in case the source file cannot be open because it is
protected against access or destination file cannot be created because there
is already a file with this name.

4. Read the source file.

5. Write into the destination file.

6. Display status information regarding various Read/Write error conditions.


For example, the program may find that the end of the file has been reached
or that there was a hardware failure. The write operation may encounter
various errors, depending upon the output device (no more disk space,
physical end of tape, printer out of paper add so on).

7. Close both files after the entire file is copied.

Implementation of Program to copy a file using system calls :

# include <fcntl.h>
char buffer[ 2048 ];
int version = 1;

main(argc, argv)
int argc;
char *argv[];
{
int fdold, fdnew;

if (argc != 3 )
{
printf(“need 2 arguments for copy program\n");
exit(1);
}
fdold = open(argv[1], O_RDONLY); /* open source file read only */
if (fdold = = -1)
{
printf("cannot open file %s\n", argv[1]);
exit(1);
}
fdnew = creat(argv[2], 0666); /* create target file rw for all */
if (fdold = = -1)
{
print ( “cannot create file %s\n", argv[2]);
}
copy (fdold, fdnew);
exit(0);
}

copy (old, new)


int old, new;
{
int count;
while ((count = read(old, buffer, sizeof(buffer))) > 0)
write (new, buffer, count);
}

> copy myfile newfile


open and creat system calls return an integer called file descriptor.
LIST OF SOME AVAILABLE SYSTEM CALLS :

1. UNIX
a) File facilities

create (name, amode) Creates a new file called name with the specified access
permission given by amode.

open (name, oflag, amode) Opens the file name ; oflag specifies opening for reading,
writing, append etc; amode as above; returns a file descriptor fd
for used in other calls.

read(fd, buf, size) Reads up to size bytes form the file specified by fd (obtained
form open) into the user buffer buf.

write(fd, buf, count) Writes count bytes form the user buffer buf to the file specified
by fd.

fcntl(fd, cmd, arg) Performs a range of commands specified by cmd, on the file
fd. The parameter arg is a command – dependent argument
value.

close(fd) Close the file fd; ie. the files becomes unavailable to the
process.

lseek(fd, offset, origin) Changes the position of the read and write pointer for the file
fd. The position is expressed as offset bytes relative to an origin
which can be start or end of file, or the current pointer position.

link(f1, f2) Create another name, f2, for the file f1.

b) Process facilities

fork() Causes creation of a new process which is an exact copy of the


calling process.

exec(file, arglist) Causes a new process to be created by execution of the object


file file, overlaying the calling process. Arglist is an array of
parameters passed ot the new process. Note this this command
has several alternative formats.

exit(status) Exit causes the calling process to terminate; the value of status
is returned to the process’s parent, ie. the process which
invoked the terminating process.
kill(pid, sig) Sends a software signal identified by sig to he process identified
by pid. Signals convey the occurrence of error events to a
running process; the usual effect is that the process is aborted.
Signals are covered in Chapter 8.

pause() Pause suspends the execution of a calling process until it


receivers a signal.

signal(sig, func) Allows the calling process to control the handling of signals; a
user-supplied function identified by func can be activated on
receipt of the signal sig.

c) Miscellaneous

mount(filesys, dir, rflag) Causes a new file system (eg a floppy drive volume) specified
by filesys to be connected into the current file tree at the
directory dir. If rflag = 1, the new file system is mounted read-
only.

time(secs) Returns, and sets the parameter secs to, the number of seconds
which have elapsed since the beginning of January 1 st, 1970
GMT.

stime(secs) Sets the system time and date, expressed by the number of
seconds since January 1st, 1970 GMT.

2. MS-DOS

• MS-DOS doesnot use the term ‘system call’, but MS-DOS functions are
invoked by means of software interrupts.
• These are generated by the machine code INT instruction which, when executed,
caused the processor to be interrupted in a similar fashion to a hardware
interrupt; a range of interrupt vectors are reserved for software interrupts .

• A range of interrupts is available, but the majority of services are provided by


INT 21H.

INT 21H DOS calls

INT 10H functions for manipulating the ROM BIOS video driver.

INT 33H functions for manipulating the Microsoft Mouse driver.

INT 67H functions for management of Expanded Memory .

Interrupt 21H Functions:

An interrupt routine would be invoked by setting certain machine registers to values, which
specify the desired function and parameters. In particular, register AH is always set to the main
function number. An example of a function cal specification is given later.

Category Function No. Function Action


(hex)
Character I/O 10 Input a character from keyboard.
11 Output a character to screen.
05 Sends a character to the default printer.
File Functions 3C Create file.
3D Open file for processing.
3E Close file.
41 Delete file.
3F Read file.
40 Write file.
Directory Functions 39 Create directory.
3A Delete directory.
3B Set current directory.
47 Get current directory.
Process Management 00 Terminate process.
31 Terminate and stay resident.
4B Execute program.
Memory Management 48 Allocate memory block.
49 Release memory block.
Date and Time 2A, 2B Get, set system date.
2C, 2D Get, set system time.
Miscellaneous 30 Get MS-DOS version number.
44 I/O Control; device driver control

Examples:

The specification of function 2BH, Set System Date, is as follows:

Call Register Setting

AH 2BH Selects function.


CX year value in range 1980 to 2099.
DH month value, 1 to 12.
DL day value, 1 to 31.
Returns AL 00H if successful.
FFH if unsuccessful.

A call to this function is assembler might appear as follows:

code segment public


assume cs:code, ds:code;
org 100h
start: jmp begin
msg: db ‘Date set to 9/7/2001’,0dh,0ah,’$’
begin: mov ax,cs ; setup ds
mov ah, 2bh ; set date function request
mov cx, 2001 ; set year to 93
mov dh, 7 ; set month to 7 (July).
mov dl, 9 ; set day to 9th.
int 21h ; call interrupt MS-DOS
mov dx,offset msg ; address of message
mov ah,09h ; display string function request
int 21h ; call DOS
mov ah,00h ; terminate program function request
int 21h ; call DOS
code ends ; end of code segment
end start ; start is the entry point

The same could be implemented in C as well :

#include<dos.h>
union REGS inregs, outregs;
main()
{
inregs.h.ah = ox2b; /* function –set system date */
inregs.x.cx = 2001; /* year */
inregs.h.dh = 7; /* month */
inregs.h.dl = 9 ; /* day */
intdos(&inregs, &outregs);
printf(“Date set to 9/7/2001\n”);
if (outreg.x.cflag) /* check carry flag for error */
printf(“Error \n”);
}
1.3 EVOLUTION OF OPERATING SYSTEMS

An operating system may process its task serially (sequentially) or concurrently


(several tasks simultaneously).

Serial Processing processing of jobs one by one

Batch Processing batch processing environment it requires grouping


of similar jobs which consist of programs, data and
system commands.

Multiprogramming

▪ A single user cannot always keep CPU or I/0 devices busy at all times.
▪ Multiprogramming offers a more efficient approach to increase system
performance.
▪ In order to increase the resource utilisation, systems supporting
multiprogramming approach allow more than one job (program) to utilize CPU
time at any moment.
▪ More number of programs competing for system resources, better will be
resource utilisation.

The idea is implemented as follows. The main memory of a system contains more
than one program .

The time needed to process n programs is I + CLE _ O, where

I = the sum of the input processing times


CLE = the sum of the compile, load and execute time
O = the sum of the output processing times.

The throughput is n/t = n/(I + CLE +O).

▪ The fact that the three devices CPU, CR, and LP operate almost independently makes an
important improvement possible.
▪ We get a much more efficient system if the input and output phases overlap in time.
▪ An even better result is obtained if all three phases overlap.

Throughput of Sequential system :


n
I + O =  (ik + ok )
K=1

With
n n
--- = -----------------
t I + CLE + O

Throughput of overlapped system :

Let ik and ok represent the processing time of input and output of the kth program, and let
mk = MAX(ik+1, ok) for k = 1, ……….., n-1.

Input and output processing for n programs using overlapping can be as low as:
n-1

M = i1 +  mk + on
K=1

with a throughput of
n n
--- = ---------------
t CLE + M

Illustration :

i1 c1 o1 i2 c2 o2 i3 c3 o3 i4 c4 o4

/////// /////// /////// /////// Computer phases


i1 c1 o1 i3 c3 o3

Time saved

i2 c2 o2 i4 c4 o4

Throughput of overlapped system

TYPES OF OPERATING SYSTEM :


1. Batch Operating System

But it has two major disadvantages:


i. Non-interactive environment
ii. Off-line debugging

2. Multiprogramming Operating System

Multiprogramming has a significant potential for improving system throughput


and resource utilisation. Different forms of multiprogramming operating
system are multitasking, multiuser and timesharing

3. Real-time Operating Systems (RTOS) :

▪ A form of operating system which are used in environments where a large


number of events mostly external to computer systems, must be accepted and
processed in a short time or within certain deadlines.

▪ Examples of such applications are flight control, real time simulations etc. Real
time systems are also frequently used in military application.

4. Network Operating System (NOS)

▪ A network operating system is a collection of software and associated protocols


that allows a set of autonomous computers which are interconnected by a
computer network to be used together in a convenient and cost-effective manner.

▪ In a network operating system, the users are aware of existence of multiple


computers and can log in to remote machines and copy files from one machine
to another machine.

5. Distributed Operating System


A distributed operating system is one that looks to its users like an ordinary
centralized operating system but runs on multiple independent CPUs.

The key concept here is transparency. In other words, the use of multiple
processors should be invisible to the user.

In a true distributed system, users are not aware of where their programs are
being run or where their files are residing; they should all be handled
automatically and efficiently by the operating system.

6. Multiprocessor Operating System :

▪ A multiprocessor system has more than one processor (CPU).


▪ Load Balancing, Scheduling, Fault Tolerance, Reliability are more
complex.
▪ Used in Array Processors, Massively Parallel Processors and Super
Computers.
Concurrent Processes

Two processes are said to be concurrent if they over lap in their execution.

Precedence Graphs

Consider the following program segment, which performs some simple


arithmetic operations.

Program1 :
a : = x + y;
b : = z + 1;
c : = a – b;
w : = c + 1;
• These have data dependence and so precedence constraints,

• The point of this example is that, within a single program. There are
precedence constraints among the various statements.

Precedence Graph :

A precedence graph is a directed acyclic graph whose nodes correspond to


individual statements. An edge form node Si to node Sj means that
statement Si can be executed only after statement Si has completed
execution.
S
1

S S
2 3

S
4

S
S 6
5

S
7

Precedence graph
• S2 and S3 can be executed after S1 completes.
• S4 can be executed after S2 completes.
• S5 and S6 can be executed after S4 completes.
• S7 can execute only after S5’ S6’ and S3 complete.

Note that S3 can be executed concurrently with S2’ S4’ S5’ and S6’

Concurrency Conditions :

Qs. When can two statements in a program be executed


concurrently and still produce the same results?

The following three conditions must be satisfied for two successive


statements S1 and S2 to be executed concurrently and still produce the same
result. These conditions were first stated by Bernstein [1966] and are
commonly known as Bernstein’s conditions.

1. R(S1)  W(S2) = {}.


2. W(S1)  R(S2) = {}.
3. W (S1) W(S2) ={}.

• R(SI) = {a1’ a2’ …’ am}’ the read set for Si ’ is the set of all variables
whose values are reference in statement Si ‘ during its execution.

• W(Si) = {b1’ b2’ …’ bn}’ the write set for Si ’ is the set of all variables
whose values are changed (written) by the execution if statement S i ’
Ilustrations of READ and WRITE sets:

Example 1 : Consider the statement c := a – b.

The values of the variables a and b are used to compute the new value of c. Hence
a and b are in the read set. The (old) value of c is not used in the statement, but a new
value is defined as a result of the execution of the statement. Hence c is in the write set,
but not the read set.

R(c := a – b) = {a,b}
W(c := a – b) = {c}

Example 2 : For the statements, w := c + 1 the and write sets are:

R (w:= c + 1) = {c}
W (w:= c + 1) = {w}

Example 3 : Consider the statement read(a).

a is being read into, this its value is changing. The read and write sets are:
R (read (a)) = {}
W (read (a)) = {a}

Example 5 : S1: a := x + y
S 2: b := z + 1
S3: c := a – b

S1 and S2 statements can be executed concurrently because :

R(S1) = {x, y}
R(S2) = {z}
W(S1) = {a}
W((S2)= {b}

However, S2 cannot be executed concurrently with, since


W(S2)  R(S3) = {b}
Implementing Concurrency :

1. The Fork and Join Constructs


2. Concurrent Statements

1. The Fork and Join Constructs :

The fork and join instructions were introduced by Conway [1963] and
Dennis and Van Horn [1966]. They were one of the first language
notations for specifying concurrency.

S
1
S1;
fork L;
S2;
.
Fork
.
.
L : S 3;

S
S
2
3
Precedence graph for the fork construct

The fork L instruction produces two concurrent executions in a program.


➢ One execution starts at the statement labeled L, while the other is the
continuation of the execution at the statement following the fork
instruction.
➢ When the fork L statement is executed, a new computation is started at
S3. This new computation executes concurrently with the old
computation, which continues at S2.
JOIN :

• The join instruction provides the means to recombine two concurrent


computations into one.
• Each of the two computations must request to be joined with the other.
• Since computations may execute at different speeds, one may execute
the join before the other.

S S count := 2;
2 1 fork L1;
.
.
jo
.
in S 1;
go to L2;
L1: S2;
S L2: join count;
3

Precedence graph for the join construct

We need to know the number of computations which are to be joined, so


that we can terminate all but the last one. The join instruction has a
parameter to specify the number of computations to join instruction with a
parameter count has the following effect:

count : = count – 1;
+ if count  0 then quit;

where count is a non-negative integer variable, and quit is an instruction


which results in the termination of the execution. For 2 computations, the
variable count would be initialized to 2.
The join instruction must be executed atomically; that is, the concurrent
execution of two join statement is equivalent to the serial execution of
these two statement, in some undefined order.
Note that the execution of the join statement merges several concurrent
executions; hence the name join.

Consider again Program 1.

S1: a := x + y
S2: b := z + 1
S3: c := a – b
S4: w : = c + 1;

To allow the concurrent execution of the first two statements, this program
could be rewritten using fork and join instructions:

count: = 2;
fork L1;
a := x + y;
Go to L2;
L1: b: = z + 1;
L2: join count;
c := a – b;
w := c + 1;
2. Concurrent Statements:
A higher-level language construct for specifying concurrency is the
parbegin/parend statement of Dijkstra [1965a], which has the following form:
parbegin S1; S2; ……Sn parend;

• Each Si is a single statement.


• All statements enclosed between parbegin and parend can be executed
concurrently.
S
0

S
1
S ………… S
2 n

Sn+
1

Precedence graph for the concurrent statement

Earlier example written using concurrent statements :

begin
parbegin
a : = x + y;
b : = z + 1;
parend;
c : = a – b;
w : = c + 1;
end.
Illustration :
S
1

S S
2 3

S
4

S
S 6
5

S
7

Implementation of Precedence Graph :

Using FORK/JOIN Using Concurrent Statements


S1; S1;
count := 3; parbegin
fork L1; S3;
S2; begin
S4; S2;
fork L2; S4;
S5; parbegin
go to L3; S5;
L2: S6; S6;
go to L3; parend
L1: S3; end
L3: join count; parend
S7 S7
EXAMPLE :

Copy of sequential file f to another file g by using double-


buffering with s and t, this program can read from f
concurrently with writing to file g.

var f, g : file of T;
r, s: T;
count: integer;
begin
reset(f);
read(f,s);
while not eof(f)
do begin
count : = 2;
t := s ;
fork L1;
write(g,t);
go to L2;
L1: read(f,s);
L2: join count;
end;
write(g,t);
end.
Using Concurrent Statement :

var f, g : file of T;
s,t,: T;
begin
reset(f);
read(f,s);
while not eof(f)
do begin
t : = s;
parbegin
write(g,t);
read(f,s);
parend;
end;
write(g,t);
end.
Concurrent Processes

Unit now we have only considered the issue of concurrency as it relates to hardware components or
user processes. In this chapter we examine the general issue of concurrency both within a single
process and concurrency across processes.

9.1 Precedence Graphs

Consider the following program segment, Program 1, which performs some simple arithmetic
operations.

a : = x + y;
b : = z + 1;
c : = a – b;
w : = c + 1;

Suppose we want to execute some of these statements concurrently. We may have multiple
functional units (such as adders) in our processor, or multiple cpus. The “addition” and
“subtraction” might be operations on matrices or vectors, or on very large sets (union and
intersection), or on files (concatenation and difference). With multiple cpus we may be able to
execute some statements concurrently with others, reducing our total execution time.
Clearly, the statement c := a – b cannot be executed before both a and b have been assigned
values. Similarly, w := c + 1 cannot be executed before the new value of c has been computed. On
the other hand, the statements a := x + y and b:= z + 1 could be executed concurrently since neither
depends upon the other.
The point of this example is that, within a single program. there are precedence constraints
among the various statements. In the following sections, we formalize these ideas.

9.1.1 Definition

A precedence graph is a directed acyclic graph whose nodes correspond to individual statements.
An edge form node Si to node Si means that statement Si can be executed only after statement Si
has completed execution.
For example, in the precedence graph depicted in Figure 9.1, the following precedence
relations exist:

• S2 and S3 can be executed after S1 completes.


• S4 can be executed after S2 completes.
• S5 and S6 can be executed after S4 completes.
• S7 can execute only after S5’ S6’ and S3 complete.

Note that S3 can be executed concurrently with S2’ S4’ S5’ and S6’
The precedence graph must be acyclic. The graph in Figure 9.2 has the following
precedence constraints: S3 can executed only after S2 has completed and S2 can be executed only
after S3 has completed. Obviously, these constraints cannot both be satisfied at the same time.

S
1

S S
2 3

S
4

S
S 6
5

S
7

Figure 9.1 Precedence graph

9.1.2 Concurrency Conditions


When can two statements in a program be executed concurrently and still produce the same results?
That is, should there be an edge from S1 to S2 in the precedence graph corresponding to program?
Before we answer this question, let us first define some notation.

• R(SI) = {a1’ a2’ …’ am}’ the read set for Si ’ is the set of all variables whose values are reference
in statement Si ‘ during its execution.
• W(Si) = {b1’ b2’ …’ bn}’ the write set for Si ’ is the set of all variables whose values are changed
(written) by the execution if statement Si ’

To illustrate this notation, consider the statement c := a – b. The values of the variables a and b are
used to compute the new value of c. Hence a and b are in the read set. The (old) value of c is not
used in the statement, but a new value is defined as a result of the execution of the statement. Hence
c is in the write set, but not the read set.

R(c := c + 1) = {a,b}
W(c := c + 1) = {c}

For the statements, w := c + 1 the and write sets are:

R(w := c + 1) = {c}
W(w := c + 1) = {w}

The intersection of R(SI) and W(Si) need not be null. For example, in the statement x := x + 2, R(x
:= x + 2) = W(x := x + 2) = {x}.

310 Concurrent Processes

As another example, consider the statement read(a). Notice that a is being read into, this its value is
changing. The read and write sets are:

R(read(a)) = {}
W(read(a)) = {a}

The following three conditions must be satisfied for two successive statements S 1 and S2 to be
executed concurrently and still produce the same result. These conditions were first stated by
Bernstein [1966] and are commonly known as Bernstein’s condtions.

4. R(S1) W(S2) = {}.


5. W(S1) R(S2) = {}.
6. W (S1) W(S2) ={}.

As an illustration, consider S1: a := x + y and S2: b := z + 1. These two statements can be executed
concurrently becouse

R(S1) = {x,y}
R(S2) = {z}
W(S1) = {a}
W((S2)= {b}

However, S2 cannot be executed concurrently with S 3: c := a – b , since


`
W(S2) R(S3) = {b}

9.2 Specification

The precedence graph is a useful device for defining the precedence constraints of the parts of a
computation. However, a precedence graph would be difficult to use in a programming language,
since it is a two dimensional object. Other means must be provided to allow the programmer to
specify the precedence relations among the various statements in a program.

9.2.1 The Fork and Join Constructs

The fork and join instructions were introduced by Conway [1963] and Dennis and Van Horn
[1966]. They were one of the first language notations for specifying concurrency.

S
1

Fork

S S
2 3

Figure 9.3 Precedence graph for the fork construct

The fork L instruction produces two concurrent executions in a program. One execution starts at the
statement labeled L, while the other is the continuation of the execution at the statement following
the fork instruction.
To illustrate this concept, consider the following program segment:

S1;
fork L;
S2;
.
.
.
L: S3;

Part of the precedence graph corresponding to this program is presented in Figure 9.3. When the
fork L statement is executed, a new computation is started at S3. This new computation executes
concurrently with the old computation, which continues at S2.
Note that the execution of a fork statement splits one single computation into two independent
computations; hence the name fork.
The join instruction provides the means to recombine two concurrent computations into one.
Each of the two computations must request to be joined with the other. Since computations may
execute at different speeds, one may execute the join before the other. In this case, the computation
which executes the join first is terminated, while the second computation is allowed to continue. If
there were three computations to be joined, the first two to execute the join are terminated, while the
third is allowed to continue.

S S
2 1

jo
in

S
3

Figure 9.4 Precedence graph for the join construct

We need to know the number of computations which are to be joined, so that we can terminate all
but the last one. The join instruction has a parameter to specify the number of computations to join
instruction with a parameter count has the following effect:

count : = count – 1;
if count  0 then quit;
where count is a non-negative integer variable, and quit is an instruction which results in the
termination of the execution. For 2 computations, the variable count would be initialized to 2.

The join instruction must be executed atomically; that is, the concurrent execution of two join
statement is equivalent to the serial execution of these two statement, in some undefined order. The
importance of this statement will be demonstrated in Section 9.5.
To illustrate this concept, consider the following program segment:

count := 2;
fork L1;
.
.
.
S1;
go to L2;
L1: S2;
L2: join count;
Part of the precedence graph corresponding to this program is presented in Figure 9.4.
Note that the execution of the join statement merges several concurrent executions; hence the
name join.
Let us further illustrate these concepts through additional examples.
Consider again Program 1. To allow the concurrent execution of the first two statements, this
program could be rewritten using fork and join instructions:
count: = 2;
fork L1;
a := x + y;
Go to L2;
L1: b: = z + 1;
L2: join count;
c := a – b;
w := c + 1;

Now return to the precedence graph of Figure 9.1. The corresponding program using the fork and
join instructions is:

S1;
count := 3;
fork L1;
S2;
S4;
fork L2;
S5;
go to L3;
L2: S6;
go to L3;
L1: S3;
L3: join count;
S7;

Note that in Figure 9.1 there exists only one join node S7 ’ which has an in-degree of 3.
Hence, only one join statement is needed. The counter for this join is initialized to 3.

As a final example, consider a program that copies from a sequential file f to another file g.
By using double-buffering with r and s, this program can can read from f concurrently with writing
g.

var f, g: file of T;
r, s: T;
count: integer;
begin
reset(f);
read(f,r);
while not eof(f)
do begin
count : = 2;
s: = r;
fork L1;
write(g,s);
go to L2;
L1: read(f,r);
L2: join count;
end;
write(g,r);
end.

9.2.2 The Concurrent Statement

A higher-level language construct for specifying concurrency is the parbegin/parend statement of


Dijkstra [1965a], which has the following form:

Parbegin S1; S2; ……Sn parend;


Each Si is a single statement. All statements enclosed between parbegin and parend can be executed
concurrently. Thus the precedence graph which corresponds to he statement above is depicted in
Figure 9.5, where So and Sn+1 are the statements appearing just before and after the parbegin/pareend
statement, respectively. Note that statement Sn+1 can be executed only after all S i, i = 1, 2, …, n,
have completed.

Let us illustrate these concepts through additional examples. Consider again Program 1. To
allow the concurrent execution of the first

S
0

S S S
1 2 n

Sn+
1

Figure 9.5 Precedence graph for the concurrent statement

two statements, this program could be rewritten using the parbegin/parend construct:

parbegin
a : = x + y;
b : = z + 1;

parend;
c : = a – b;
w : = c + 1;

We can write the following program to correspond to the precedence graph in Figure 9.1:
S1;
parbegin
S3;
begin
S2;
S4;
parbegin
S5;
S6;
Parend;
end;
parend;
S7;
Finally, let us rewrite the program that copies a file f to another file g, using the concurrent statement.

var f, g: file of T;
r, s, T;
begin
reset(f);
read(f,r);
while not eof(f)
do begin
s : = r;
parbegin
write(g,s);
read(f,r);
parend;
end;
write(g,r);
end.
PROCESS MANAGEMENT

Process Concept :

• Process may be appreciated as the program in execution. However many


other definitions are also available for this.

• However a program is a passive entity whereas a process is an active entity.

• The key idea about a process is that it is an activity of some kind and consists
of pattern of bytes (that the CPU interprets as machine instruction, data,
register and stack).

• A single processor may be shared among several processes with some


scheduling policy being used by the processor to allocate one process and
deal locate another one.

Process hierarchies:

• Operating system needs some way to create and kill processes. When a
process is created, it creates another process(es) which in turn crates some
more process(es) and so on, thus forming process hierarchy or process tree.

• In UNIX operating system, a process is created by the fork system call and
terminated by exit.

Process States:

The lifetime of a process can be divided into several stages as states, each with
certain characteristics that describe the process. It means that as a process starts
executing, it goes through one state to another state.

Each process may be in one of the following states:

1. New -The process has been created.


2. Ready -The process is waiting to be allocated to a processor.
All ready processes (kept in queue) keeps waiting for CPU time to be allocated
by operating system in order to run. A program called scheduler which is a part
of operating system, pick-ups one ready process for execution by passing a
control to it.

3. Running- Instructions are being executed

4. Blocked/Suspended: A suspended process lacks some resource


other than the CPU.

5. Terminate: When the process finally stops. A process terminates


when it finishes executing its last statement or under some
exceptional cases by OS. A parent may terminate the execution of
one of its children for a variety of reasons, such as:
iii. The task assigned to the child is no longer required.
iv. The child has exceeded its usage of some of resources it has been
allocated.

A general form of process state diagram is illustrated:

Process State Diagram


Process Implementation

• The operating system groups all information that it needs about a particular
process into a data structure called a Process Control Block (PCB).
• It simply serves as the storage for any information for processes.

• When a process is created, the operating system creates a corresponding PCB


and when it terminates, its PCB is released to the pool of free memory
locations from which new PCBs are drawn.

• A process is eligible to compete for system resources only when it has active
PCB associated with it. A PCB is implemented as a record containing many
pieces of information associated with a specific process, including:
1. Process number: Each process is identified by its process number, called
process ID. (PID)
2. Priority.
3. Process state: Each process may be in any of these states: new, ready,
running, suspended and terminated.
4. Program counter: It indicates the address of the next instruction to be
executed for this process.
5. Registers: They include accumulator, general purpose registers, index
registers etc. Whenever a processor switches over from one process to
another process, information about current status of the old process is
saved in the register along with the program counter so that the process be
allowed to continue correctly afterwards.
6. Accounting information: It includes actual CPU time used in executing
a process.
7. I/O status information: It includes outstanding I/O requests, list of open
files information about allocation of peripheral devices to processes.
8. Processor scheduling details: It includes a priority of a process, address
to scheduling queues and any other.
Concurrent Processes

Two processes are said to be concurrent if they over lap in their execution.

Precedence Graphs

Consider the following program segment, which performs some simple


arithmetic operations.

Program1 :
a : = x + y;
b : = z + 1;
c : = a – b;
w : = c + 1;
• These have data dependence and so precedence constraints,

• The point of this example is that, within a single program. There are
precedence constraints among the various statements.

Precedence Graph :

A precedence graph is a directed acyclic graph whose nodes correspond to


individual statements. An edge form node Si to node Sj means that
statement Si can be executed only after statement Si has completed
execution.
S
1

S S
2 3

S
4

S
S 6
5

S
7

Precedence graph
• S2 and S3 can be executed after S1 completes.
• S4 can be executed after S2 completes.
• S5 and S6 can be executed after S4 completes.
• S7 can execute only after S5’ S6’ and S3 complete.

Note that S3 can be executed concurrently with S2’ S4’ S5’ and S6’

Concurrency Conditions :

Qs. When can two statements in a program be executed


concurrently and still produce the same results?

The following three conditions must be satisfied for two successive


statements S1 and S2 to be executed concurrently and still produce the same
result. These conditions were first stated by Bernstein [1966] and are
commonly known as Bernstein’s conditions.

7. R(S1)  W(S2) = {}.


8. W(S1)  R(S2) = {}.
9. W (S1) W(S2) ={}.

• R(SI) = {a1’ a2’ …’ am}’ the read set for Si ’ is the set of all variables
whose values are reference in statement Si ‘ during its execution.

• W(Si) = {b1’ b2’ …’ bn}’ the write set for Si ’ is the set of all variables
whose values are changed (written) by the execution if statement Si ’
Ilustrations of READ and WRITE sets:

Example 1 : Consider the statement c := a – b.

The values of the variables a and b are used to compute the new value of c. Hence
a and b are in the read set. The (old) value of c is not used in the statement, but a new
value is defined as a result of the execution of the statement. Hence c is in the write set,
but not the read set.

R(c := a – b) = {a,b}
W(c := a – b) = {c}

Example 2 : For the statements, w := c + 1 the and write sets are:

R (w:= c + 1) = {c}
W (w:= c + 1) = {w}

Example 3 : Consider the statement read(a).

a is being read into, this its value is changing. The read and write sets are:
R (read (a)) = {}
W (read (a)) = {a}

Example 5 : S1: a := x + y
S 2: b := z + 1
S3: c := a – b

S1 and S2 statements can be executed concurrently because :

R(S1) = {x, y}
R(S2) = {z}
W(S1) = {a}
W((S2)= {b}

However, S2 cannot be executed concurrently with, since


W(S2)  R(S3) = {b}
Implementing Concurrency :

3. The Fork and Join Constructs


4. Concurrent Statements

1. The Fork and Join Constructs :

The fork and join instructions were introduced by Conway [1963] and
Dennis and Van Horn [1966]. They were one of the first language
notations for specifying concurrency.

S
1
S1;
fork L;
S2;
.
Fork
.
.
L : S 3;

S
S
2
3
Precedence graph for the fork construct

The fork L instruction produces two concurrent executions in a program.


➢ One execution starts at the statement labeled L, while the other is the
continuation of the execution at the statement following the fork
instruction.
➢ When the fork L statement is executed, a new computation is started at
S3. This new computation executes concurrently with the old
computation, which continues at S2.
JOIN :

• The join instruction provides the means to recombine two concurrent


computations into one.
• Each of the two computations must request to be joined with the other.
• Since computations may execute at different speeds, one may execute
the join before the other.

S S count := 2;
2 1 fork L1;
.
.
jo
.
in S 1;
go to L2;
L1: S2;
S L2: join count;
3

Precedence graph for the join construct

We need to know the number of computations which are to be joined, so


that we can terminate all but the last one. The join instruction has a
parameter to specify the number of computations to join instruction with a
parameter count has the following effect:

count : = count – 1;
+ if count  0 then quit;

where count is a non-negative integer variable, and quit is an instruction


which results in the termination of the execution. For 2 computations, the
variable count would be initialized to 2.
The join instruction must be executed atomically; that is, the concurrent
execution of two join statement is equivalent to the serial execution of
these two statement, in some undefined order.
Note that the execution of the join statement merges several concurrent
executions; hence the name join.

Consider again Program 1.

S1: a := x + y
S2: b := z + 1
S3: c := a – b
S4: w : = c + 1;

To allow the concurrent execution of the first two statements, this program
could be rewritten using fork and join instructions:

count: = 2;
fork L1;
a := x + y;
Go to L2;
L1: b: = z + 1;
L2: join count;
c := a – b;
w := c + 1;
2. Concurrent Statements:
A higher-level language construct for specifying concurrency is the
parbegin/parend statement of Dijkstra [1965a], which has the following form:
parbegin S1; S2; ……Sn parend;

• Each Si is a single statement.


• All statements enclosed between parbegin and parend can be executed
concurrently.
S
0

S
1
S ………… S
2 n

Sn+
1

Precedence graph for the concurrent statement

Earlier example written using concurrent statements :

begin
parbegin
a : = x + y;
b : = z + 1;
parend;
c : = a – b;
w : = c + 1;
end.
Illustration :
S
1

S S
2 3

S
4

S
S 6
5

S
7

Implementation of Precedence Graph :

Using FORK/JOIN Using Concurrent Statements


S1; S1;
count := 3; parbegin
fork L1; S3;
S2; begin
S4; S2;
fork L2; S4;
S5; parbegin
go to L3; S5;
L2: S6; S6;
go to L3; parend
L1: S3; end
L3: join count; parend
S7 S7
EXAMPLE :

Copy of sequential file f to another file g by using double-


buffering with s and t, this program can read from f
concurrently with writing to file g.

var f, g : file of T;
s, t: T;
count: integer;
begin
reset(f);
read(f,s);
while not eof(f)
do begin
count : = 2;
t := s ;
fork L1;
write(g,t);
go to L2;
L1: read(f,s);
L2: join count;
end;
write(g,t);
end.
Using Concurrent Statement :

var f, g : file of T;
s,t,: T;
begin
reset(f);
read(f,s);
while not eof(f)
do begin
t : = s;
parbegin
write(g,t);
read(f,s);
parend;
end;
write(g,t);
end.
Inter Process Communication &
The Critical Section Problem

Producer Consumer problem is quite common in operating systems.

• A Producer process produces information that is consumed by a Consumer


process.

• A line Printer driver produces characters which are consumed by the Line
Printer.

• A Compiler may produce assembly code which is consumed by an assembler.


The assembler in turn may produce Load modules which are consumed by the
loader.

Producer-Consumer Processes:

• For maximising throughput and better system performance, approaches should


be made to allow P-C processes to run concurrently.

• Producer should produce into Buffer and Consumer should consume from
buffer.

• A Producer can produce into one buffer while the consumer is consuming from
other buffer. P-C must be synchronized so that Consumer does not try to
consume items which are not yet been produced.

Unbounded Buffer Producer-Consumer :


• No limit on the number of buffers.
• The consumer may have to wait for new items, but the producer can always
produce new items i.e. buffers are of unlimited capacity there are always
empty buffers.

Bounded Buffer Producer-Consumer : Assumes that there is a fixed number


‘n’ of buffer.

Solution to Bounded Buffer Problem :


Data Structure : 1. Circular Array ( shared Pool of Buffers)
2. Two Logical Pointers : ‘in’ and ‘out’
in points to the next free buffer.
out points to the first full buffer.
3. integer variable counter initialised to 0 .
counter is incremented everytime a new full buffer is
added to the pool and decremented whenever one is
consumed

Type item = ….;


Var buffer : array[0..n-1] of item;
In, out : 0..n-1
Nextp, nextc : item;
In := 0, out := 0;

Parbegin
Producer: begin
repeat
…..
produce an item in nextp
…..
while counter =n do skip;
buffer[in] := nextp;
in := in+1 mod n;
counter := counter + 1;
until false;
end;

Consumer: begin
repeat
…..
while counter = 0 do skip;
nextc := buffer[out];
out := out+1 mod n;
counter := counter - 1;
consume the item in nextc;
until false;
end;

parend;
This implementation may lead to wrong values, if concurrent execution is uncontrolled

e.g. If counter value is currently 5, and Producer is executing the statement counter
:= counter +1 and Consumer is executing the statement counter:= counter -1
concurrently, then the value of counter may be 4,5 or 6. ( correct answer should be 5
because one produced one consumed)

How counter may be incorrect ?

i) counter := counter +1 is implemented in machine language as


:
R1 := counter
R1 := R1 +1
Counter := R1

ii) counter := counter -1 is implemented in machine language as


:
R2 := counter
R2 := R2-1
Counter := R2

Concurrent execution of statements counter := counter +1 and


counter := counter –1 is equivalent to a sequential execution where the
lower level statements presented above are interleaved in some arbitrary order. If one
such interleaving is :

T0: Producer executes R1 := counter {R1=5}


T1: Producer executes R1 := R1+1 {R1=6}
T2: Consumer executes R2 := counter {R2=5}
T3: Consumer executes R2 := R2-1 {R2=4}
T4: Producer executes Counter := R1 {counter=6}
T5: Consumer executes Counter := R2 {counter=4}

 counter = 4 while answer should be 5.


 If T4 and T5 reversed then incorrect counter = 6.

WHY INCORRECT ? Because we allowed processes to manipulate


the variable counter concurrently.

REMEDY : We need to ensure that only one process at a


time may be manipulating the variable counter.

This observation leads us to problem of CRITCIAL SECTION (CS) and the


part of the program code where process is executing shared variable is known
as its Critical Section.

Problem Definition:

 Consider a system consisting of n cooperating processses { P1, P2,.. Pn}

 Each process has a segment of code called a Critical Section (CS)


 When one process is executing in its CS no other process is to be
allowed to execute in its CS.
 Thus the execution of CS by the processes in Mutually Exclusive in
time.

Critical Section Problem :

To design a protocol which the processes may use to cooperate.

General structure may be:


……

Entry Section
……
Critical Section
…….

Exit Section

Remainder Section

A solution to the Mutual Exclusion (ME) problem must satisfy the following 3 requirements
:

1. Mutual Exclusion : If process Pi is executing in its CS then no


other process can be executing in its CS.

2. Progress : If no process is executing in its CS and there


exist some processes that wish to enter their CS
then only those processes that are not executing in
their Remainder Sction can participate in the
decision as to who will enter the CS next, and this
selection can’t be postponed indefinitely

3. Bounded Waiting : There must exist a bound on the times that


other processes are allowed to enter their
CS after a process has made a request to enter its
CS and before that is granted.

It is assumed that each process is executing at a non zero speed, and


No assumption can be made on the relative speed of n processes.
TWO PROCESS SOFTWARE SOLUTION :

General structure :
begin
common variable decl.
parbegin
P0;
P1;
parend
end.

1. Algorithm 1:

First approach is to let the processes share a common integer variable


turn initialised to 0(or 1). If turn = i then process Pi is
allowed to execute in its CS.

Pi :
repeat

while turn <> i do skip;

C.S

turn := j ;

Remainder section
until false
Analysis:
1. Ensures only one process at a time can be in CS.
2. However it does not satisfy the progress requirement.
STRICT ALTERNATION of processes in CS.
If turn=0 and P1 wants to enter its CS, it can’t even though P0 in
remainder section.

Algorithm 2:

Problem with Algo.1 is that it fails to remember the state of each process but
remembers only which process is allowed to enter its CS.

As a solution :

var flag : array[0..1] of boolean ; (initial. to 0)

If flag[i] is true then Pi is executing in its CS.

Pi : repeat
while flag[j] do skip;
flag[i] := true;

CS;

Flag[i] := false;

Remanider section ;
until false

Analysis : 1. First checks if other process is in its CS.


2. Else Sets itself and enters CS.
3. When exits CS, resets flag to false, allowing others.
ME is not Ensured :

T0 : P0 enters the while statement and finds flag[1] = false


T1 : P1 enters the while statement and finds flag[0] = false
T2 : P1 sets flag[1] and enters CS.
T3 : P0 sets flag[0] and enters CS.

Algorithm 3:

The Problem with Algorithm 2 is that process Pi made a decision concerning the
state of Pj before Pj had the opportunity to change the state of the variable flag[j].
We can try to correct this problem. As in Algorithm 2, we still maintain the array flag.
This time, however, the setting of flag[i] = true indicates only that Pi wants to
enter the critical section.

repeat
flag[i] := true;
while flag[j] do skip;

critical section

Flag[i] := false;

remainder section ;
until false

• So, in this algorithm, we first set our flag[i] to be true, signaling that we
want to enter our critical section.

• Then we check that if the other process also wants to enter its critical section. If
so, we wait. Then we enter our critical section.
• When we exit the critical section, we set our flag to be false, allowing the
other process (if it is waiting) to enter its critical section.

In this solution, unlike Algorithm 2, the mutual–exclusion requirement is


satisfied. Unfortunately, the progress requirement is not met. To illustrate this
problem, consider the following execution sequence.

T0: P0 set flag [0] = true.


T1: P1 set flag [1] = true.

Now P0 and P1 are looping forever in their respective while statements

Algorithm 4 :

It is probably convinced that there is no simple solution to the critical section


problem. It appears that every time we fix one bug in a solution, another bug appears.
However, we now (finally) present a correct solution, due to Peterson [1981].
This solution is basically a combination of Algorithm 3 and a slight modification of
Algorithm 1.
The processes share two variables in common:

var flag : array[0..1] of boolean ;


turn : 0..1;

Initially flag[0] = flag[1] = false and the value of turn is immaterial


(but either 0 or 1). The structure of process Pi is:

repeat
flag[i] := true;
turn := j;
while (flag[j] and turn=j) do skip;

critical section
Flag[i] := false;

remainder section
until false;

To enter our critical section, we first set our flag[i] to be true, and assert that it is
the other process’ turn to enter if it wants to (turn = j). If both processes try to
enter at the same time, turn will be set to both i and j at roughly the same time.
Only one of these assignments will last; the other will occur, but be immediately
overwritten. The eventual value of turn decides which of the two processes is allowed
to enter its critical section first.

Hardware Solutions :

Many machines provide special hardware instructions that allow one to either test and
modify the content of a word, or to swap the contents of two words, in one memory
cycle. These special instructions can be used to solve the critical section problem. Let
us abstract the main concepts behind these types of instructions by defining the Test-
and Set instruction as follows:

Function Test-and Set(var target: boolen): Boolean;


begin
Test-and Set := target;
target := true;
end;

The important characteristic is that these instructions are executed atomically; that is,
in one memory cycle. Thus if two Test-and Set instructions are executed
simultaneously (each on a different cpu), they will be executed sequentially in some
arbitrary order.
If the machine supports the Test-and Set instruction, then mutual exclusion
can be implemented by declaring a boolean variable lock, initialized to false.
repeat

while Test-and Set(lock) do skip;

critical section

lock := false;

remainder section
until false;

Hardware Swap instruction as follows:

Procedure Swap (var a, b: boolean);


var temp: Boolean;
begin
temp := a:
a := b;
b := t one memory mp;
end;
If the machine support the Swap instruction, then mutual exclusion can be provided in
a similar manner. A global boolean variable lock is initialized to false. Each
process also has a local boolean variable key.

repeat
key := true;
repeat
Swap(lock, key)
Until key = false;
critical section

lock := false;

remainder section
until false;
N-Process Software Solutions:

Solves the critical section problem for ‘n’ processes.

1. Lamports Bakery Algorithm [1974]: The bakery algorithm is base upon a


scheduling algorithm commonly used in bakeries, ice cream stores, etc. Upon
entering the store, each customer receives a number. The customer with the
lowest number is served next. (Tie may be resolved)
2. Eisenberg and McGuire [1972]

Semaphores:

• The solutions to the mutual exclusion problem presented in the last section are not easy to
generalize to more complex problems.

• To overcome this difficulty, a new synchronization tool, called a semaphore, was introduced
by Dijkstra [1965a].

• A semaphore S is an integer variable that, apart from initialization, can be accessed only
through two standard atomic operations: P and V. The classical definitions of P and V are:

P(S): while S <= 0 do skip;


S : = S – 1;

V(S): S: = S + 1;

Modifications to the integer value of the semaphore in the P and V operations


are executed indivisibly. That is, when one process modifies the semaphore value, no
other process can simultaneously modify that same semaphore value. In addition, in
the case of the P(S), the testing of the integer value of S(S ≤ 0), and its possible
modification (S := S – 1) must also be executed without interruption.

Usage:
1. Semaphores can be used in dealing with the n-process critical section
problem. The n processes share a common semaphore, mutex, initialized to 1.
Each process Pi is organized as follows:

repeat

P(mutex);

critical section

V(mutex);

remainder section
until false;

3. Semaphores as Synchronization Tool:

S
1

S
2
Consider two concurrently running processes: P1 with a statement S1, and P2
with a statement S2. Suppose that we require that S2 be executed only after S1 has
completed. This scheme can be readily implemented by letting P1 and P2 share a
common semaphore synch, initialized to 0, and by inserting the statements:
S1;
V(synch); in process P1, and the statements
P(synch);
S2;
in process P2. Since synch is initialized to 0, P2 will execute S2 only after P1
has invoked V(synch), which is after S1.
Further Example:
Semaphore definition with out busy-waiting:

To implement semaphore for avoiding busy-waiting we define a semaphore as


a record.

type semaphore = record


Value: integer;
L: list of process;
end;

Each semaphore has an integer value and a list of processes. When a process must
wait on a semaphore, it is added to the list of processes. A V operation removes one
process from the list of waiting processes and awakens it.

The semaphore operations can now be defined as:

P(S): S. value := S. value – 1;

if S. value < 0
then begin
add this process to S.L;
block;
end;

V(S): S. value := S. value + 1;

if S.value ≤ 0
then begin
remove this process P from S.L;
wakeup(P);
end;
Table shows a sequence of 14 states of 6 processes invoking P and V operations on
semaphores. Initially, none of the processes is in its critical

Action Resulting Status


invoking operation running blocked value
process invoked in c. r. on s of s

1 A P(s) A 1
2 A V(s) 0
3 B P(s) B 1
4 C P(s) B C 0
5 D P(s) B C,D 0
6 E P(s) B C,D,E 0
7 B V(s) D C,E 0
8 F P(s) D C,E,F 0
9 D V(s) C,F 0
11 none of A,..,F E C,F 0
12 E V(s) F C 0
13 F V(s) C 0

P(s): if s= 1
then s 0 /* lower semaphore */
else BLOCK calling process on s
DISPATCH a ready process

V(s): if the list of processes waiting on s is nonempty


then WAKE UP a process waiting on s
else s 1 /* raise semaphore */
Classical Process Coordination Problems of Computer Science:

1. Dining Philosophers Problem


2. Bounded-Buffer Problem
3. Readers/Writers Problem
4. Cigarette Smokers Problem [Patil 1971]
5. Sleepy Barber Problem [Dijkstra 1965 a]

1. Dining Philosophers Problem


2. Bounded-Buffer Problem
3. Readers/Writers Problem
The Critical Section Problem :

Producer Consumer problem are quite common in operating systems.

A Producer process produces information that is consumed by a Consumer process.

A line Printer driver produces characters which are consumed by the Line Printer.

A Compiler may produce assembly code which is consumed by an assembler. The


assmbler in turn may produce Load modules which are consumed by the loader.

Producer-Consumer Processes:

For maximising throughput and better system performance approach should be


made to allow P-C processes to run concurrently.

Producer should produce into Buffer and Consumer should consume from
buffer.
A Producer can produce into one buffer while the consumer is consuming from other
buffer. P-C must be synchronized so that Consumer does not try to consume items
which are not yet been produced.

Unbounded Buffer P-C : No limit on the number of buffers. The consumer may have
to wait for new items, but the producer can always produce new items i.e. there are
always empty buffers.

Bounded Buffer P-C : Assumes that there is a fixed number ‘n’ of buffer.

Solution to Bounded Buffer Problem :

Data Structure : 1. Circular Array ( shared Pool of Buffers)


2. Two Logical Pointers : ‘in’ and ‘out’
in points to the next free buffer.
Out points to the first full buffer.
3. integer variable counter initialised to 0 Counter is
incremented everytime a new full buffer is added to the pool and decremented
whenver one is consumed

Type item = ….;


Var buffer : array[0..n-1] of item;
In, out : 0..n-1
Nextp, nextc : item;
In := 0, out := 0;

Parbegin
Producer: begin
repeat
…..
produce an item in nextp
…..
while counter =n do skip;
buffer[in] := nextp;
in := in+1 mod n;
counter := counter + 1;
until false;
end;

Consumer: begin
repeat
…..
while counter = 0 do skip;
nextc := buffer[out];
out := out+1 mod n;
counter := counter - 1;
consume the item in nextc;
until false;
end;

parend;
This implementation may lead to wrong Values if concurrent execution is uncontrolled

e.g. If counter value is currently 5, and Producer is executing the statement counter
:= counter +1 and Consumer is executing the statement counter:= counter -1
concurrently, then the value of counter may be 4,5 or 6. ( correct answer should 5
because one produced one consumed)

How counter may be incorrect ?

i) counter := counter +1 is implemented in machine language as


:
R1 := counter
R1 := R1 +1
Counter := R1

ii) counter := counter -1 is implemented in machine language as


:
R2 := counter
R2 := R2 +1
Counter := R2

Concurrent execution of statements counter := counter +1 and


counter := counter –1 is equivalent to a sequential execution where the
lower level statements presented above are interleaved in some arbitrary order. If one
such interleaving is :

T0: Producer executes R1 := counter {R1=5}


T1: Producer executes R1 := R1+1 {R1=6}
T2: Consumer executes R2 := counter {R2=5}
T3: Consumer executes R2 := R2-1 {R2=4}
T4: Producer executes Counter := R1 {counter=6}
T5: Consumer executes Counter := R2 {counter=4}

 counter = 4 while answer should be 5.


 If T4 and T5 reversed then incorrect counter = 6.
WHY INCORRECT ? Because we allowed processes to manipulate
the variable counter concurrently.

REMEDY : We need to ensure that only one process at a


time may be manipulating the variable counter.

This observation leads us to problem of CRITCIAL SECTION (CS) and the


part of the program code where process is executing shared variable is known
as its Critical Section.

Problem Definition:

 Consider a system consisting of n cooperating processses { P1, P2,.. Pn}

 Each process has a segment of code called a Critical Section (CS)


 When one process is executing in its CS no other process is to be
allowed to execute in its CS.
 Thus the execution of CS by the processes in Mutually Exclusive in
time.

Critical Section Problem :

To design a protocol which the processes may use to cooperate.

……

Entry Section
……
Critical Section
…….

Exit Section

Remainder Section
A solution to the Mutual Exclusion (ME) problem must satisfy the following 3 requirements
:

1. Mutual Exclusion : If process Pi is executing in its CS then no


other process can be executing in its CS.

2. Progress : If no process is executing in its CS and there


exist some processes that wish to enter their
CS, then only those processes that are not executing in their Remainder Section can
participate in the decision as to who will enter the CS next, and this selection can’t
be postponed indefinitely

3. Bounded Waiting : There must exist a bound on the times that


other processes are allowed to enter their
CS after a process has made a request to enter its CS and before
that is granted.

It is assumed that each process is executing at a non zero speed, and


No assumption can be made on the relative speed of n processes.
TWO PROCESS SOFTWARE SOLUTION :

General structure :
begin
common variable decl.
parbegin
P0;
P1;
parend
end.

2. Algorithm 1:
First approach is to let the processes share a common integer variable
turn initialised to 0(or 1). If turn = i then process Pi is
allowed to execute in its CS.

Pi :
repeat

while turn <> i do skip;

C.S

turn := j ;

Remainder section
until false

Analysis:
4. Ensures only one process at a time can be in CS.
5. However it does not satisfy the progress requirement.
STRICT ALTERNATION of processes in CS.
If turn=0 and P1 wants to enter its CS, can’t even though P0 in
remainder section.
Algorithm 2:

Problem with Algo.1 is that it fails t remember the state of each process
but remembers only which process is allowed to enter its CS.

As a solution :

var flag : array[0..1] of boolean ; (initial. to 0)

If flag[i] is true then Pi is executing in its CS.

Pi : repeat
while flag[j] do skip;
flag[i] := true;

CS;

Flag[i] := false;

Remanider section ;
until false

Analysis : 1. First checks if other process is in its CS.


2.Else Sets itself and enters CS.
6. When exits CS, resets flag to false, allowing others.

ME is not Ensured :

T0 : P0 enters the while statement and finds flag[1] = false


T1 : P1 enters the while statement and finds flag[0] = false
T2 : P1 sets flag[1] and enters CS.
T3 : P0 sets flag[0] and enters CS.
Algorithm 3:

The Problem with

2.5 DEADLOCKS
In a multiprogramming environment several processes may compete for a fixed
number of resources. A process requests resources and if the resources are not
available at that time, it enters a wait state. It may happen that it will never gain
access to the resources. Since those resources are being held by other waiting
processes. For example, take a system with one tape drive and one plotter.
Process P1 request the tape drive and process P2 requests the plotter. Both
requests are granted. Now PI requests the plotter (without giving up the tape
drive) and P2 requests the tape drive (without giving up the plotter). Neither
request can be granted so both processes enter a deadlock situation. A deadlock
is a situation where a group of processes is permanently blocked as a result of
each process having acquired a set of resources needed for its completion and
having to wait for release of the remaining resources held by others thus
making it impossible for any of the deadlocked processes to proceed.
Deadlocks can occur in concurrent environments as a .result of the uncontrolled
granting of the system resources to the requesting processes.

2.5.1 System Model


Deadlocks can occur when processes have been granted exclusive access to
devices, files and so forth. A system consists of a finite number of resources to be
distributed among a number of competing processes. The resources can be divided
into several types, each of which consists of some number of identical instances.
CPU cycles, memory space, files and I/O devices (such as printers and tape
drives) are examples of resource types. If a system has two tape drives then the
resource type tape drive has two instances.
If a process requests an instance of a resource type, any type of that resource of
class may satisfy the request. If this is not the case, then the instances are not
identical and the resource type classes have not been properly defined. For
example a system may have two printers These two printers may be defined into
same printer class, if one is not concerned about type of printers (Dot Matrix or
Laser Printer).
Whenever a process wants to utilize any resource, it must make a request for it. It
may request as many resources as it wants but it should not exceed the total
number of resources available with the system. Once the process has utilized the
resource it must release it. Therefore, a sequence of events to use a resource is:
v. Request the resource
vi. Use the resource
vii. Release the resource
If the resource is not available when it is requested then the requesting process
must wait until it can acquire the resource. In some operating system, the
process is automatically blocked when a resource request fails and awakened
when it becomes available.
The exact nature of requesting resource is highly system dependent. The
request and release of resources are implemented through system calls which
vary from one system to another. Examples of these calls are request/release
device, open/close file etc.

2.5.2 Deadlock Characterisation and Modelling


Deadlocks are undesirable features. In the most of deadlock situation process is
waiting for the release of some resource concurrently possessed by some
deadlocked process. Before we go further it would be helpful to describe some
conditions that characterise deadlock:

Figure 10: Graphic Representation of Resource Allocation


1. Mutual exclusion: Only one process at a time can use the resource. If another
process requests that resource the requesting process must be delayed until the
resource has been released.
2. Hold and wait: There must be one process that is having one resource and
waiting for another resource that is being currently held by another process.
3. No preemption: Resources previously granted cannot be forcibly taken away
from a process. They must be explicitly released by the process holding them.
4. Circular wait condition: There must be a circular chain of two or more
processes, each of which is waiting for a resource held by the next member of
the chain.

Now we will show how these four conditions can be modeled using directed
graphs. The graphs have two kinds of nodes: processes, shown as circles, and
resources, shown as squares. An arc from a resource node (square) to a process
node (circle) means that the resource is currently being held by process. In Figure
10(a) resource R1 is currently assigned to process P1. When an arc is coming from
a process to a resource it specifies that a process is waiting for a resource. In
Figure 10(b), process P2 is waiting for resource R2. In Figure 10(c) we see a
deadlock. Both processes P3 and Pn are waiting for resources which they will
never get. Process P3 is waiting for R3, which is currently held by P4. P4 will not
release R3 because it is waiting for resource R4.
To demonstrate one example, let us imagine that we have three processes: P1, P2
and P3, and three resources R 1, R2 and r3. The requests and releases of the three
processes are given in Figure (a)-(c). The operating system is free to run any
unblocked process at any instant, so it could decide to run P1 until PI finished all
its work, then run P2 to completion, and finally run P3.
If there is no competition for resources, (as we saw in the Figure 11 (a)-(c), there
will be no deadlock occurrence. Let us suppose that the request of resources is
being made in the order of Figure 11(d). If these six requests are carried out in the
same order, the six resulting resource graphs are shown in Figure 11 (e)-(f). After
a request 4 has been made, P1 locks waiting for R2 as shown in Figure 11 (h). In
the next two steps: 5 and 6, P2 and P3 also block ultimately leading to a cycle and
the deadlock situation shown in Figure 11( j ).
The operating system is not required to run the processes in any special order. In
particular, if granting a particular request might lead to deadlock, the operating
system can simply suspend the process without granting the request (i.e. just not
schedule the process) until it is safe. In fig.... , if .the operating system knew about
the impending deadlock, it could suspend P2 instead of granting it R2. By running
only P1 and P3 . we would get the requests and releases of Figure 9(k) instead of
Figure 11 (d). This sequence leads to the resource graphs of Figure 11 (I)-(q),
which do not lead to deadlock.
After step (q), process P2 can be granted R2 because P1 is finished and P3 has
everything it needs. Even if P2 should eventually block when requesting R3, no
deadlock can occur. P2 will just wait until P3 is finished.
The point to understand here is that resource graphs are a tool to notice how a
given request/release sequence leads to deadlock, we just carry out the requests
and releases step by step, and after every step check the graph to see if it contains
any cycles. If so, we have a deadlock; if not, there is no deadlock.. Resource
graphs can also be generalised to handle multiple resources of the same type.
In general, four strategies are used for dealing with deadlocks.
8. Just ignore the problem altogether.
9. Detection and recovery.
10. Prevention, by negating one of the four necessary conditions.
11. Dynamic avoidance by careful resource allocation.

2.6 SUMMARY
Process is an important concept in modem operating system. Processes provide a
suitable means for informing the operating system about independent activities
that may be scheduled for concurrent execution. Each process is represented by a
process control block (PCB). Several PCBs can be linked together to form a queue
of waiting processes. The selection and allocation of processes is done by a
scheduler. There are several scheduling algorithms. These are first-in-first-out
scheduling, Round robin scheduling, shortest job first and priority algorithm.
Cooperating process must synchronize with each other whenever they like to share
resources shared by several other processes. At most one process should be
allowed to enter the critical section of code within which particular shared variable
or data structure is updated.
Semaphores are a simple but powerful interprocess synchronization mechanism
based on this concept. We also discussed hardware support for concurrency
control in one form or another which has become an integral part of virtually all
computer architecture.
We could not discuss semaphore based solution to several synchronization
problems such as producer/consumer and readers/writers problems. Monitor
concept provides structured form of interprocess synchronization and
communication compared to semaphores. Message passing allows interprocess
communication and synchronization without the need for global variables which is
suitable for both centralized and distributed systems.
Deadlsocks are a common problem is systems where concurrent processes need
simultaneous exclusive access to a collection of shared resources. A deadlock
situation may occur if and only if four necessary conditions simultaneously hold in
the system: mutual exclusion, hold and wait, no preemption and circular wait
condition To prevent deadlocks it is essential that one of the necessary conditions
never occur.
We discussed all these issues in greater details.
Page No.273
SYSTEM MODEL :-

A system consists of a finite number of resources to be distributed among a number of


competing processes. The resources are partitioned into several types, each of which consists of some
number of identical instances. CPU cycles, memory space, files and I/O devices (such as printers,
tape drives, and card readers) are examples of resource types. If a system has two cpus, then the
resource type cpu has two instances. Similarly, the resource type printer may has five instances.

If a process requests an instance of a resource type, the allocation of any instance of


the type will satisfy the request. If this is not the case, then the instances are not identical, and the
resource type classes have not been properly defined. For example, a system may have two printers.
These two printers may be defined to be in the same resource class if no one cares which printer prints
which output. However, if one printer is on the ninth floor and the other is in the basement, then
people on the ninth floor may ;not see both printers as equivalent, and separate resource classes may
need to be defined for each printer.

A process must request a resource before using it and release the resource after using
it. A process may request as many resources as it requires to carry out its designated task. Obviously,
the number of resources requested may not exceed the total number of resources available in the
system. In other words, a process cannot request three printers if the system only has two.

Under the normal mode of operation, as process may utilize a resource only in the
following sequence:

1. Request. If the request cannot be immediately granted (for example, the resource is being
used by another process), then the requesting process must wait until it can acquire the
resource.
2. Use. The process can operate on the resource (for example, if the resource is a line printer, the
process can print on the printer).
3. Release. The process releases the resource.

The request and release of resources are system calls, as explained in Section 2.2.
1. Examples are the Request/Release Device, Open/Close File, and Allocate/Free memory system
calls. The use of resources can also only be made through system calls (for example, to read or write
a file or I/O device). Therefore, for each use, the operating system checks to make sure that the using
process has requested and been allocated the resource. A system table records whether each resource
is free or allocated, and if allocated, to which process. If a process requests a resource that is currently
allocated to another process, it can be added to a queue of processes waiting for this resource.

8.1.2 Deadlock Definition:


A set of processes is in a deadlock state when every process in the set is waiting for an event
that can only be caused by another process in the set. The events with which we are mainly concerned
here are resource acquisition and release. However, other types of events may result in deadlocks, as
shown in Chapters 9 and 13.

To illustrate a deadlock state, consider a system with three tape drives. Suppose that there are
three processes, each holding one of these tape drives. If each process now requests another tape
drive, the three processes will be in a deadlock state. Each is waiting for the event “Tape drive is
released”, which can only be caused by one of the other waiting processes. This example illustrates
a deadlock involving processes competing for the same resource type.

Deadlocks may also involve different resource types. For example, consider a system with
one printer and one card reader. Suppose that process P is holding the card reader and process Q is
holding the printer. If P now requests the printer and Q requests the card reader, a deadlock occurs.

8.2 Deadlock Characterization:

It should be obvious that deadlocks are undesirable. In a deadlock, processes never finish
executing and system resources are tied up, preventing other jobs from ever starting. Before we
discuss the various methods for dealing with the deadlock problem, it would be helpful to describe
some features that characterize deadlocks.

8.2.1 Necessary Conditions

A deadlock situation can arise if and only if the following four conditions hold simultaneously
in a system.

➢ Mutual exclusion. At least one resource is held in a non-sharable mode; that is, only one
process at a time can use the resource. If another process requests that resourse, the requesting
process must be delayed until the resource has been released.
➢ Hold and wait. There must exist a process that is holding at least one resource and is waiting
to acquire additional resources that are currently being held by other processes.
➢ No preemption. Resources cannot be preempted; that is, a resource can only be released
voluntarily by the process holding it, after the process has completed its task.
➢ Circular wait. There must exist a set {p0, p1, ………., pn} of waiting processes such that p 0
is waiting for a resource which is held by p1, p1 is waiting fkor a resource which is held by p2,
….pn-1 is waiting for a resource which is held by P0.

We emphasize that all four conditions must hold for a deadlock to occur. The circular-wait condition
implies the hold-and-wait condition, so the four conditions are not completely independent. However,
we will see (in Section 8.3) that it is quite useful to consider each condition separately.

We can see these four conditions in our river-crossing example. A deadlock occurs if and only
if two people form opposite sides of the river meet in the middle. The mutual-exclusion condition
obviously holds, since at most one person can be stepping on a stone at one time. The hold –and-wait
condition is satisfied, since each person is stepping on one stone and waiting to step on the next one.
The no-preemption condition holds, since a stepping stone cannot e forcibly removed form the person
stepping on it. Finally, the circular-wait condition holds, since the person coming from the east is
waiting on the person coming form the west, while the person coming form the west is, in turn, waiting
on the person coming from the east. Neither of the two can proceed, and each is waiting for the other
to remove their foot from one of the stepping stones.

8.2.2 Resource Allocation Graph

Deadlocks can be described more precisely in terms of a directed graph called a sstem resource
allocation graph. This graph consists of a pair G = (V,E), where V is a set of vertices and E is a set of
edges. The set of vertices is partitioned into two types P = {p1, p2, …, pn}, the set consisting of all the
processes in the system, and R= {r1, r2, …, rm}, the set consisting of all resource types in the system.

Each element in the set E of edges is and ordered pair (pi, rj) or (rj, pi), where pi, is a process
(pi  P) and rj is a resource type (rj  R). If (pi, rj)  E, then there is a directed edge from process pi
to resource type rj, implying that process pi requested an instance of resource type rj and is currently
waiting for that resource. If (rj, pi)  E, then there is a directed edge for resource type rj to process pi,
implying that an instance of resource type rj has been allocated to process pi. An edge (pi, rj) is called
a request edge, while an edge (rj, pi) is called an assignment edge.

Pictorially, we represent each process pi as a circle and each resource type rj as a square. Since
resource type rj may have more than one instance, we represent each such instance as a dot within the
square. Note that a request edge only points to the square r j, while an assignment edge must also
designate one of the dots in the square.

When process pi requests an instance of resource type r j, a request edge is inserted in the
resource allocation graph. When this request can be fulfilled, the request edge is instantaneously
transformed to an assignment edge. When the process later releases the resource, the assignment edge
is deleted.

The resource allocation graph in Figure 8.3 depicts the following situation.

• The sets P, R, and E:

P = {p1, p2, p3}


R = {r1, r2, r3, r4}
E = {(p1,r1), (p2,r3), (r1, p2), (r2, p2), (r2, p1), (r3, p3)}

• Resource instances:
o One instance of resource type r1.
o Two instance of resource type r2.
o One instance of resource type r3.
o Three instance of resource type r4.

• Process states:

o Process p1 is holding an instance of resource type r2 and is waiting for an instance of


resource type r1.

o Process p2 is holding an instance of r1 and r2 and is waiting for an instance of resource


type r3.

o Process p3 is holding an instance of r3.

Given the definition of resource allocation graph, it can be easily shown that if
the graph contains no cycles, then no process in the system is deadlocked. If , on the other hand, the
graph contains a cycle, then a deadlock may exist.

If each resource type has exactly one instance, then a cycle implies that a deadlock has
occurred. If the cycle involves only a set of resource types, each of which have only a single instance,
then a deadlock has occurred. Each process involved in the cycle is deadlocked. In this case, a cycle
in the graph is both a necessary and sufficient condition for the existence of deadlock.

If each resource type has several instances, then a cycle does not necessarily imply that a
deadlock occurred. In this case, a cycle in the graph is a necessary but not a sufficient condition for
the existence of deadlock.

To illustrate this concept, let us return to the resource allocation graph depicted in Figure 8.3.
Suppose that process p3 requests and instance of resource type r2. Since no resource instance is
available, a request edge (p3,r2) is added to the graph (Figure 8.4). At this point two minimal cycles
exist in the system:

r1 r3

P P
2 3
P
1

r2

r4

Figure 8.3 Resource allocation graph

p1 - r1 - p2 - r3 − r1 - p3 - r2 - p1

p2 - r3 - p3 - r2 - p2
.
Processes p1’ p2’ and p3’ are deadlocked. Process p2 is waiting for the resource p3’ which is held by
process p3’ on the other hand, is waiting for either process p1 or p2 to release resource p2. Meanwhile,
process p2 is waiting on process p2. In addition, process p1 is waiting for process p2 to release resource
p1.
Now consider Figure 8.5. In this example, we also have a cycle:

p2 -> r1 -> p3 - r2 -> p1

However, there is no deadlock. Observe that process p4 may release its instance of resource type r2.
That resource can then be allocated to p3, breaking the cycle.

To summarize, if a resource allocation graph does not have a cycle then the system is not in a
deadlock state. On the other hand, if there is a cycle, then the system may or may not be in a deadlock
state. This observation is important in dealing with the deadlock problem.

r1 r3
P P P
1 2 3

r2

r4

Figure 8.3 Resource allocation graph with a deadlock


8.2.3 Methods for Handling Deadlocks
Principally, there are two methods for dealing with the deadlock problem. We can use some protocol to ensure that
the system will never enter a deadlock state. Alternatively, we

Page No.273
SYSTEM MODEL :-

A system consists of a finite number of resources to be distributed among a number of


competing processes. The resources are partitioned into several types, each of which consists of some
number of identical instances. CPU cycles, memory space, files and I/O devices (such as printers,
tape drives, and card readers) are examples of resource types. If a system has two cpus, then the
resource type cpu has two instances. Similarly, the resource type printer may has five instances.

If a process requests an instance of a resource type, the allocation of any instance of


the type will satisfy the request. If this is not the case, then the instances are not identical, and the
resource type classes have not been properly defined. For example, a system may have two printers.
These two printers may be defined to be in the same resource class if no one cares which printer prints
which output. However, if one printer is on the ninth floor and the other is in the basement, then
people on the ninth floor may ;not see both printers as equivalent, and separate resource classes may
need to be defined for each printer.
A process must request a resource before using it and release the resource after using
it. A process may request as many resources as it requires to carry out its designated task. Obviously,
the number of resources requested may not exceed the total number of resources available in the
system. In other words, a process cannot request three printers if the system only has two.

Under the normal mode of operation, as process may utilize a resource only in the
following sequence:

4. Request. If the request cannot be immediately granted (for example, the resource is being
used by another process), then the requesting process must wait until it can acquire the
resource.
5. Use. The process can operate on the resource (for example, if the resource is a line printer, the
process can print on the printer).
6. Release. The process releases the resource.

The request and release of resources are system calls, as explained in Section 2.2.
1. Examples are the Request/Release Device, Open/Close File, and Allocate/Free memory system
calls. The use of resources can also only be made through system calls (for example, to read or write
a file or I/O device). Therefore, for each use, the operating system checks to make sure that the using
process has requested and been allocated the resource. A system table records whether each resource
is free or allocated, and if allocated, to which process. If a process requests a resource that is currently
allocated to another process, it can be added to a queue of processes waiting for this resource.

8.1.2 Deadlock Definition:

A set of processes is in a deadlock state when every process in the set is waiting for an event
that can only be caused by another process in the set. The events with which we are mainly concerned
here are resource acquisition and release. However, other types of events may result in deadlocks, as
shown in Chapters 9 and 13.

To illustrate a deadlock state, consider a system with three tape drives. Suppose that there are
three processes, each holding one of these tape drives. If each process now requests another tape
drive, the three processes will be in a deadlock state. Each is waiting for the event “Tape drive is
released”, which can only be caused by one of the other waiting processes. This example illustrates
a deadlock involving processes competing for the same resource type.

Deadlocks may also involve different resource types. For example, consider a system with
one printer and one card reader. Suppose that process P is holding the card reader and process Q is
holding the printer. If P now requests the printer and Q requests the card reader, a deadlock occurs.

8.2 Deadlock Characterization:


It should be obvious that deadlocks are undesirable. In a deadlock, processes never finish
executing and system resources are tied up, preventing other jobs from ever starting. Before we
discuss the various methods for dealing with the deadlock problem, it would be helpful to describe
some features that characterize deadlocks.

8.2.1 Necessary Conditions

A deadlock situation can arise if and only if the following four conditions hold simultaneously
in a system.

➢ Mutual exclusion. At least one resource is held in a non-sharable mode; that is, only one
process at a time can use the resource. If another process requests that resourse, the requesting
process must be delayed until the resource has been released.
➢ Hold and wait. There must exist a process that is holding at least one resource and is waiting
to acquire additional resources that are currently being held by other processes.
➢ No preemption. Resources cannot be preempted; that is, a resource can only be released
voluntarily by the process holding it, after the process has completed its task.
➢ Circular wait. There must exist a set {p0, p1, ………., pn} of waiting processes such that p 0
is waiting for a resource which is held by p1, p1 is waiting fkor a resource which is held by p2,
….pn-1 is waiting for a resource which is held by P0.

We emphasize that all four conditions must hold for a deadlock to occur. The circular-wait condition
implies the hold-and-wait condition, so the four conditions are not completely independent. However,
we will see (in Section 8.3) that it is quite useful to consider each condition separately.

We can see these four conditions in our river-crossing example. A deadlock occurs if and only
if two people form opposite sides of the river meet in the middle. The mutual-exclusion condition
obviously holds, since at most one person can be stepping on a stone at one time. The hold –and-wait
condition is satisfied, since each person is stepping on one stone and waiting to step on the next one.
The no-preemption condition holds, since a stepping stone cannot e forcibly removed form the person
stepping on it. Finally, the circular-wait condition holds, since the person coming from the east is
waiting on the person coming form the west, while the person coming form the west is, in turn, waiting
on the person coming from the east. Neither of the two can proceed, and each is waiting for the other
to remove their foot from one of the stepping stones.

8.2.4 Resource Allocation Graph

Deadlocks can be described more precisely in terms of a directed graph called a sstem resource
allocation graph. This graph consists of a pair G = (V,E), where V is a set of vertices and E is a set of
edges. The set of vertices is partitioned into two types P = {p1, p2, …, pn}, the set consisting of all the
processes in the system, and R= {r1, r2, …, rm}, the set consisting of all resource types in the system.

Each element in the set E of edges is and ordered pair (pi, rj) or (rj, pi), where pi, is a process
(pi  P) and rj is a resource type (rj  R). If (pi, rj)  E, then there is a directed edge from process pi
to resource type rj, implying that process pi requested an instance of resource type rj and is currently
waiting for that resource. If (rj, pi)  E, then there is a directed edge for resource type rj to process pi,
implying that an instance of resource type rj has been allocated to process pi. An edge (pi, rj) is called
a request edge, while an edge (rj, pi) is called an assignment edge.

Pictorially, we represent each process pi as a circle and each resource type rj as a square. Since
resource type rj may have more than one instance, we represent each such instance as a dot within the
square. Note that a request edge only points to the square r j, while an assignment edge must also
designate one of the dots in the square.

When process pi requests an instance of resource type r j, a request edge is inserted in the
resource allocation graph. When this request can be fulfilled, the request edge is instantaneously
transformed to an assignment edge. When the process later releases the resource, the assignment edge
is deleted.

The resource allocation graph in Figure 8.3 depicts the following situation.

• The sets P, R, and E:

P = {p1, p2, p3}


R = {r1, r2, r3, r4}
E = {(p1,r1), (p2,r3), (r1, p2), (r2, p2), (r2, p1), (r3, p3)}

• Resource instances:

o One instance of resource type r1.


o Two instance of resource type r2.
o One instance of resource type r3.
o Three instance of resource type r4.

• Process states:

o Process p1 is holding an instance of resource type r2 and is waiting for an instance of


resource type r1.

o Process p2 is holding an instance of r1 and r2 and is waiting for an instance of resource


type r3.

o Process p3 is holding an instance of r3.

Given the definition of resource allocation graph, it can be easily shown that if
the graph contains no cycles, then no process in the system is deadlocked. If , on the other hand, the
graph contains a cycle, then a deadlock may exist.

If each resource type has exactly one instance, then a cycle implies that a deadlock has
occurred. If the cycle involves only a set of resource types, each of which have only a single instance,
then a deadlock has occurred. Each process involved in the cycle is deadlocked. In this case, a cycle
in the graph is both a necessary and sufficient condition for the existence of deadlock.

If each resource type has several instances, then a cycle does not necessarily imply that a
deadlock occurred. In this case, a cycle in the graph is a necessary but not a sufficient condition for
the existence of deadlock.

To illustrate this concept, let us return to the resource allocation graph depicted in Figure 8.3.
Suppose that process p3 requests and instance of resource type r2. Since no resource instance is
available, a request edge (p3,r2) is added to the graph (Figure 8.4). At this point two minimal cycles
exist in the system:

r1 r3

P P P
1 2 3

r2

r4

Figure 8.3 Resource allocation graph


p1 - r1 - p2 - r3 − r1 - p3 - r2 - p1

p2 - r3 - p3 - r2 - p2
.
Processes p1’ p2’ and p3’ are deadlocked. Process p2 is waiting for the resource p3’ which is held by
process p3’ on the other hand, is waiting for either process p1 or p2 to release resource p2. Meanwhile,
process p2 is waiting on process p2. In addition, process p1 is waiting for process p2 to release resource
p1.
Now consider Figure 8.5. In this example, we also have a cycle:

p2 -> r1 -> p3 - r2 -> p1

However, there is no deadlock. Observe that process p4 may release its instance of resource type r2.
That resource can then be allocated to p3, breaking the cycle.

To summarize, if a resource allocation graph does not have a cycle then the system is not in a
deadlock state. On the other hand, if there is a cycle, then the system may or may not be in a deadlock
state. This observation is important in dealing with the deadlock problem.

r1 r3

P P P
1 2 3

r2

r4

Figure 8.3 Resource allocation graph with a deadlock


8.2.5 Methods for Handling Deadlocks
Principally, there are two methods for dealing with the deadlock problem. We can use some protocol to ensure that
the system will never enter a deadlock state. Alternatively, we

2.4 INTERPROCESS COMMUNICATION AND SYNCHRONIZATION

2.4.1 Basic Concepts of Concurrency


Concurrent Process: We discussed the concept of a process earlier in this unit.
The operating system consists of a collection of such processes which are
basically two types:
Operating system processes: Those that execute system code and the rest being
user processes those that execute user's code. All of these processes can
potentially execute in concurrent manner. Concurrency refers to a parallel
execution of a program.
A concurrent program specifies two or more sequential programs (a sequential
program specifies sequential execution of a list of statements) that may be
executed concurrently as parallel processes. For example, an airline reservation
system that involves processing transactions from many terminals has a natural
specifications as a concurrent program in which each terminal is controlled by its
own sequential process. Even when processes are not executed simultaneously, it
is often easier to structure as a collection of cooperating sequential processes
rather than as a single sequential program.
A simple batch operating system can be viewed as 3 processes -a reader process,
an executor process and a printer process. The reader reads cards from card reader
and places card images in an input buffer. The executor process reads card images
from input buffer and performs the specified computation and store the result in an
output buffer. The printer process retrieves the data from the output buffer and
writes them to a printer Concurrent processing is the basis of operating system
which supports multiprogramming.
The operating system supports concurrent execution of a program without
necessarily supporting elaborate form of memory and file management. This form
of operation is also known as multitasking. Multiprogramming is a more general
concept in operating system that supports memory management and file
management features, in addition to supporting concurrent execution of programs.

2.4.2 Basic Concepts of Interprocess Communication and Synchronization


In order to cooperate concurrently executing processes must communicate and
synchronize. Interprocess communication is based on the use of shared variables
(variables that can be referenced by more than one process) or message passing.
Synchronization is often necessary when processes communicate. Processes are
executed with unpredictable speeds. Yet to communicate one process must
perform some action such as setting the value of a variable or sending a message
that the other detects. This only works if the events perform an action or detect an
action are constrained to happen in that order. Thus one can view synchronization
as a set of constraints on the ordering of events. The programmer employs a
synchronization mechanism to delay execution of a process in order to satisfy such
constraints.
To make this concept more clear, consider the batch operating system again. A
shared buffer is used for communication between the leader process and the
executor process. These processes must be synchronized so that, for example, the
executor process never attempts to read data from the input if the buffer is empty.
The next sections are mainly concerned with these two issues.

2.4.3 Mutual Exclusion


Processes frequently need to communicate with other processes. When a user
wants to read from a file, it must tell the file process what it wants, then the file
process has to inform the disk process to read the required block.
Processes that are working together often share some common storage that one
can read and write. The shared storage may be in main memory or it may be a
shared file. Each process has segment of code, called a critical section, which
accesses shared memory or files. The key issue involving shared memory or
shared files is to find way to prohibit more than one process from reading and
writing the shared data at the same time. What we need is mutual exclusion -
some way of making sure that if one process is executing in its critical section, the
other processes will be excluded from doing the same thing. Now we present
algorithm to support mutual exclusion. This is applicable for two processes only.
Module Mutex
var
P1busy, P2busy: boolean;
Process PI
begin
while true do
begin
P1busy:= true;
while P2busy do {keeptesting};
critical.-,section;
P1busy:= false;
other_P1busy_Processing
end {while}
end; {P1}
Process P2;
begin
while true do
begin
P2busy:= true;
while P2busy do {keeptesting};
critical.-,section;
P2busy:= false;
other_P2busy_Processing
end {while}
end; {P2}
{Parent process}
begin (mutex)
P1busy:= false;
P2busy:= false;
Initiate PI, P2
end (mutex)
Program 1: Mutual Exclusion algorithm

PI first sets P1busy and then tests P2busy to determine what to do next.
When it finds P2busy to be false, process PI may safely proceed to the
Critical section knowing that no matter how the two processes may be
interleaved, process P2 is certain to find P2busy set and to stay away
from the critical section. The single change ensures mutual exclusion.
But consider a case where PI wishes to enter the critical section and sets
P1busy to indicate the fact. If process P2 wishes to enter the critical
section at the same time and pre-empts process P1 just before P1 tests
P2busy.
Process P2 may set P2busy and start looping while waiting for P1busy to
become false. When control is eventually returned to Process P1, it finds
P2busy set and starts looping while it waits for P2busy to become false.
And so both processes are looping forever, each awaiting the other one
to clear the way. In order to remove this kind of behaviour, we must add
another requirement to occur in our algorithm. When more than one
process wishes to enter the critical section, the decision to grant entrance
to one of them must be made in finite time.

2.4.4 Semaphores
In the previous section, we discussed the mutual exclusion problem. The
solution we presented did not solve all the problems of mutual exclusion.
The Dutch mathematician Dekker is believed to be the first to solve the
mutual exclusion problem. But its original algorithm works for two
processes only and it cannot be extended beyond that number.
To overcome this problem, a synchronization tool called semaphore was
proposed by Dijkstra which gained wide acceptance and implemented in
several commercial operating system through system calls or as built-in
functions. A semaphore is a variable which accepts non-negative integer
values and except for initialisation may be accessed and manipulated
through two primitive operations - wait and signal (originally defined as
P and V respectively). These names come from the Dutch words
Problem (to test) and Verogen (to

increment). The two primitives take only argument as the semaphore


variable, and may be defined as follows:
a. Wait(s):
while S <= 0 do (keep testing) S: = S-1;
wait operation decrements the value of semaphore variable as soon as it
would become non-negative.
b. Signal(s) S: S+1
Signal operation increments the value of semaphore variable.
Modifications to the integer value of the semaphore in the wait and signal
operations are executed indivisibly. That is, when one process modifies the
semaphore no other process can simultaneously modify the same semaphore
value. In addition in the case of wait(s), the testing of the integer value of S (S
<= 0) and its possible modification (S :=S-1) must also be executed without
any interruption.
Program 2 demonstrates the functioning of semaphores. In this program, there
are 3 processes to trying to share a common resource which is being protected
by a binary semaphore (bsem). (A binary semaphore is a variable which
contains only values of 0 and 1) by enforcing its use in mutually exclusive
fashion. Each process ensures the integrity of its critical section by opening it
with a WAIT operation and closing with a SIGNAL operation on the related
semaphore, bsem in our example. This way any number of concurrent
processor might share the resource provided each of these process use wait and
signal operation.
We also present a table (figure 8) showing the run time behaviour of three
processes and functioning of semaphore. Each column of the table show the
activity of a particular process and the value of a semaphore after certain action
has been taken on this process.
The parent process in the program first initialises binary semaphore variable
bsem to 1 indicating that the source is available. As shown in the table (figure
8) at time TI no process is active to share the resource. But at time T2 all the
three processes become active and want to enter their critical sections to share
the resource by running the wait operation. At T2, the bsem is decremented to
0 which indicates that some processes has been given permission to enter the
critical section. At time T3, we find that it is P3 which has been given some
permission. One important thing is to be noted that only one process is allowed
by semaphore at a time to the critical section.
Once PI is given the permission, it prevents other processes P2 & P3 to read
the value of bsem as 1 till the wait operation of PI decrements bsem to 0. This
is why wait operation is executed without interruption.
After grabbing the control from semaphore PI starts sharing the resource which
is depicted at time T3. At T4, PI executes signal operation to release the
resource and comes out of its critical section. As shown in the table that the
value of bsem becomes 1 since the resource is now free.
The two remaining processes P2 and P3 have an equal chance to compete the
resource. In our example, process P3 become the next to enter the critical
section and to use the shared resource. At time T7, process P3 releases the
resource and semaphore variable bsem again becomes 1. At this time, the two
other processes PI and P2 will attempt to compete for the resource and they
have equal chance to get access.
In our example, it is P2 which gets the chance but it might happens one of the three
processes could have never got the chance. There are number of problems related to
concurrency control which can be tackled easily with semaphore. These so called
problems are Bounded Buffer Problem, the readers/writers problem.
Module Sem-mutex var bsem : semaphore; {binary semaphore)
process P1;
begin
while true do
begin
wait (bsem)
Critical_section
Signal (bsem)
The rest_of P1_Processing
end (which)
process P2;
begin
while true do
wait (bsem)
critical-section;
signal (BSEM)
Rest.p2-Processing
end; (P2)

process P3;
begin
while true do
wait (bsem)
critical-section;
signal (bsem)
Rest. P3 Processing
end; (P3)
(Parent process)
begin (sem-rnutex)
bsem:= 1 (free)
initiate P1, P2, P3
end; (Mutux)

Program 2. Mutual Exclusion with Semaphore


Process status/ activity
bsem Process sharing
Time P1 P2 P3 1= FREE resources; attempting
0=BUSY to enter
T1 - - - 1 -;-
Wait Wait Wait
T2 0 -;P1, P2, P3
(bsem) (bsem) (bsem)
Critical
T3 Waiting Waiting 0 P1; P2, P3
section
T4 Signal Waiting Waiting 1 -; P2, P3
Rest-P1- Critical
T5 Waiting 0 P3; P3
Processing section
Wait Critical
T6 Waiting 0 P3; P2, P1
(bsem) section
Wait signal
T7 Waiting 1 -; P2, P1
(bsem) (bsem)
Wait Critical Rest-P3
T8 0 P2; P1
(bsem) section processing
2.4.5 Hardware Support for Mutual Exclusion
Semaphore solve the mutual exclusion problem in a simple and natural way.
However it requires indivisibility of operations to function properly. In this section
we will discuss the implementation of semaphore at hardware level.

Test-and-Set Instruction: The test and set (TS) instruction is intended for direct
hardware support of mutual exclusion. It is designed to allow only one process
among several concurrent processes to enter in its critical section.
The basic idea of this mechanism is to Set the global control variable to 0 (FREE)
indicating that the shared resource is available for access. Each process wishing to
access this resource is to execute the TS instruction with a control variable as an
operand to get permission. In principle, the TS takes one operand, the address of
the control variable or a register that may act as a semaphore. Now let us see how
wait operation on the semaphore variable S is implemented through TS instruction
set if it is available at supporting hardware (S is passed by the process competing
for the resource).
Wait-
TS S; request for entering into critical section
s
BREF
WAIT_S; repeat until granted enter to critical
REE
section
Let us assume that S is initially set to O(Zero) i.e. resource is available. When S is
set to 1, it enters the BUSY state. Suppose there are several processes executing
wait - S to access the resource protected by a global semaphore variable S. When
the TS instruction is executed on behalf of the first process, the global variable S
is set to 1 (BUSY), therefore, the resource can be accessed now. Since S was
initially set to 0 prior to execution of TS instruction, the second instruction
BRNFREE WAIT_S will not be executed. All other processes upon executing the
TS instruction find S set to 1 and continue looping (testing). As usual the process
using the resource is supposed to release it and reset S to 0. When S is reset one of
the processes looping on S (competing for resources) gets permission to access the
resource. The indivisible part of TS makes certain that only one among several
competing process, gets the permission.
The essence of the TS instruction is the indivisible testing of the global variable
and its subsequent setting to BUSY. Therefore, other instructions that allow the
contents of a variable to be tested and subsequently set as an indivisible operation
are also candidates for implementation of mutual exclusion.
TS instruction is simple to use and also it does not affect interrupts or the
operating system while performing its job and it may be used in a multiprocessor
environment But there are also some drawbacks with this mechanism. Suppose P1
is in critical section. Another process P2 which is of higher priority than P2
preempts P1 and attempts to use the same resource. This will cause P2 looping on
the control variable awaiting its resetting but the resetting will be done only by PI
which is preempted.
Two processes can become deadlocked in such a situation. One solution to deal
with such a situation is not to preempt any process within its critical section by its
contenders. In that case operating system has to keep track of the nature of
instruction being executed by user process which is rarely implemented because of
overhead imposed on operating system.
As a consequence the possibility of preemption of a process within its critical
section is a serious problem in a system that rely on hardware implementation of
mutual exclusion by means of the TS instruction.
2.5 DEADLOCKS
In a multiprogramming environment several processes may compete for a fixed
number of resources. A process requests resources and if the resources are not
available at that time, it enters a wait state. It may happen that it will never gain
access to the resources. Since those resources are being held by other waiting
processes. For example, take a system with one tape drive and one plotter.
Process P1 request the tape drive and process P2 requests the plotter. Both
requests are granted. Now PI requests the plotter (without giving up the tape
drive) and P2 requests the tape drive (without giving up the plotter). Neither
request can be granted so both processes enter a deadlock situation. A deadlock
is a situation where a group of processes is permanently blocked as a result of
each process having acquired a set of resources needed for its completion and
having to wait for release of the remaining resources held by others thus
making it impossible for any of the deadlocked processes to proceed.
Deadlocks can occur in concurrent environments as a .result of the uncontrolled
granting of the system resources to the requesting processes.

2.5.1 System Model


Deadlocks can occur when processes have been granted exclusive access to
devices, files and so forth. A system consists of a finite number of resources to be
distributed among a number of competing processes. The resources can be divided
into several types, each of which consists of some number of identical instances.
CPU cycles, memory space, files and I/O devices (such as printers and tape
drives) are examples of resource types. If a system has two tape drives then the
resource type tape drive has two instances.
If a process requests an instance of a resource type, any type of that resource of
class may satisfy the request. If this is not the case, then the instances are not
identical and the resource type classes have not been properly defined. For
example a system may have two printers These two printers may be defined into
same printer class, if one is not concerned about type of printers (Dot Matrix or
Laser Printer).
Whenever a process wants to utilize any resource, it must make a request for it. It
may request as many resources as it wants but it should not exceed the total
number of resources available with the system. Once the process has utilized the
resource it must release it. Therefore, a sequence of events to use a resource is:
iii. Request the resource
iv. Use the resource
v. Release the resource
If the resource is not available when it is requested then the requesting process
must wait until it can acquire the resource. In some operating system, the
process is automatically blocked when a resource request fails and awakened
when it becomes available.
The exact nature of requesting resource is highly system dependent. The
request and release of resources are implemented through system calls which
vary from one system to another. Examples of these calls are request/release
device, open/close file etc.

2.5.2 Deadlock Characterisation and Modelling


Deadlocks are undesirable features. In the most of deadlock situation process is
waiting for the release of some resource concurrently possessed by some
deadlocked process. Before we go further it would be helpful to describe some
conditions that characterise deadlock:

Figure 10: Graphic Representation of Resource Allocation


5. Mutual exclusion: Only one process at a time can use the resource. If another
process requests that resource the requesting process must be delayed until the
resource has been released.
6. Hold and wait: There must be one process that is having one resource and
waiting for another resource that is being currently held by another process.
7. No preemption: Resources previously granted cannot be forcibly taken away
from a process. They must be explicitly released by the process holding them.
8. Circular wait condition: There must be a circular chain of two or more
processes, each of which is waiting for a resource held by the next member of
the chain.

Now we will show how these four conditions can be modeled using directed
graphs. The graphs have two kinds of nodes: processes, shown as circles, and
resources, shown as squares. An arc from a resource node (square) to a process
node (circle) means that the resource is currently being held by process. In Figure
10(a) resource R1 is currently assigned to process P1. When an arc is coming from
a process to a resource it specifies that a process is waiting for a resource. In
Figure 10(b), process P2 is waiting for resource R2. In Figure 10(c) we see a
deadlock. Both processes P3 and Pn are waiting for resources which they will
never get. Process P3 is waiting for R3, which is currently held by P4. P4 will not
release R3 because it is waiting for resource R4.
To demonstrate one example, let us imagine that we have three processes: P1, P2
and P3, and three resources R 1, R2 and r3. The requests and releases of the three
processes are given in Figure (a)-(c). The operating system is free to run any
unblocked process at any instant, so it could decide to run P1 until PI finished all
its work, then run P2 to completion, and finally run P3.
If there is no competition for resources, (as we saw in the Figure 11 (a)-(c), there
will be no deadlock occurrence. Let us suppose that the request of resources is
being made in the order of Figure 11(d). If these six requests are carried out in the
same order, the six resulting resource graphs are shown in Figure 11 (e)-(f). After
a request 4 has been made, P1 locks waiting for R2 as shown in Figure 11 (h). In
the next two steps: 5 and 6, P2 and P3 also block ultimately leading to a cycle and
the deadlock situation shown in Figure 11( j ).
The operating system is not required to run the processes in any special order. In
particular, if granting a particular request might lead to deadlock, the operating
system can simply suspend the process without granting the request (i.e. just not
schedule the process) until it is safe. In fig.... , if .the operating system knew about
the impending deadlock, it could suspend P2 instead of granting it R2. By running
only P1 and P3 . we would get the requests and releases of Figure 9(k) instead of
Figure 11 (d). This sequence leads to the resource graphs of Figure 11 (I)-(q),
which do not lead to deadlock.
After step (q), process P2 can be granted R2 because P1 is finished and P3 has
everything it needs. Even if P2 should eventually block when requesting R3, no
deadlock can occur. P2 will just wait until P3 is finished.
The point to understand here is that resource graphs are a tool to notice how a
given request/release sequence leads to deadlock, we just carry out the requests
and releases step by step, and after every step check the graph to see if it contains
any cycles. If so, we have a deadlock; if not, there is no deadlock.. Resource
graphs can also be generalised to handle multiple resources of the same type.
In general, four strategies are used for dealing with deadlocks.
6. Just ignore the problem altogether.
7. Detection and recovery.
8. Prevention, by negating one of the four necessary conditions.
9. Dynamic avoidance by careful resource allocation.

2.6 SUMMARY
Process is an important concept in modem operating system. Processes provide a
suitable means for informing the operating system about independent activities
that may be scheduled for concurrent execution. Each process is represented by a
process control block (PCB). Several PCBs can be linked together to form a queue
of waiting processes. The selection and allocation of processes is done by a
scheduler. There are several scheduling algorithms. These are first-in-first-out
scheduling, Round robin scheduling, shortest job first and priority algorithm.
Cooperating process must synchronize with each other whenever they like to share
resources shared by several other processes. At most one process should be
allowed to enter the critical section of code within which particular shared variable
or data structure is updated.
Semaphores are a simple but powerful interprocess synchronization mechanism
based on this concept. We also discussed hardware support for concurrency
control in one form or another which has become an integral part of virtually all
computer architecture.
We could not discuss semaphore based solution to several synchronization
problems such as producer/consumer and readers/writers problems. Monitor
concept provides structured form of interprocess synchronization and
communication compared to semaphores. Message passing allows interprocess
communication and synchronization without the need for global variables which is
suitable for both centralized and distributed systems.
Deadlsocks are a common problem is systems where concurrent processes need
simultaneous exclusive access to a collection of shared resources. A deadlock
situation may occur if and only if four necessary conditions simultaneously hold in
the system: mutual exclusion, hold and wait, no preemption and circular wait
condition To prevent deadlocks it is essential that one of the necessary conditions
never occur.
We discussed all these issues in greater details.

2.7 MODEL ANSWERS

Check Your Progress 1


10. The concept of a process is either directly or indirectly available in
practically all operating system supporting multiprogramming feature.
Even high level languages like Ads and Modula incorporate this
mechanism for the management of concurrent processes. A process is an
instance of a program in execution. It is the smallest piece of work that is
individually schedulable by an operating system. Process used somewhat
interchangeably with task, has been given many definitions. Some of
these are:
• An asynchronous activity
• Entity to which processors are assigned.
• Which is manifested by the existence of "Porcess Control Block"
in the operating system.
Many other definitions have been given. There is no universally agreed
upon definition but the "Instance of a program in execution" seems to be
most frequently used.
11. Concurrent processing or concurrent execution of processes is usually
beneficial in single user environments as well. for example, in a
workstation environment, multiprocesses operating system receipt of
network broadcasts. Concurrently with other user activities, support of
multiple active windows, concurrent printing etc. The drawback is
increased complexity, overhead, and resource requirements of
multiproces operation. However, this is not a big problem in single user
environment where CPU and most other resources are lying unutilized
most of the time.

Check Your Progress 2


9. There are usually 3 different types of schedulers that are existing in a complex
operating systems: long-term, medium-term and short-term schedulers.
The primary objective of the long term scheduler is to provide a
balanced mix of jobs such as CPU bound and 110 bound, to the short-
term scheduler. It keeps the resource utilisation at the desired level. For
example, when the CPU utilisation is low, it may admit more jobs in the
ready queue for the allocation of CPU time. Conversely, when the
utilisation factor becomes high and it may reduce the number of
admitted jobs.
10. No model answer.
11. (a) FCFS is non-preemptive algorithm.
(b) Shortest-job-first and priority based algorithm may be either
preemptive or non-preemtpive.
(c) Round robin is a preemptive algorithm.
Check Your Progress 3
1. When only the process executing the critical section (or critical region) should
be allowed to access the shared resources and all the other processes computing
for the shared resources should be excluded from doing so until the completion
of the critical section. This is often referred to as mutual exclusion. Whereby a
single process temporarily prevents all others from using a shared resource
during the critical operation that might otherwise adversely affect the systems
integrity.
Conforcing mutual exclusion is one of the key problems in concurrent
programming. Many solutions have been developed; some software
solutions and some hardware solutions; some requiring voluntary
cooperations among processes, and some demanding rigid adherence to
strict protocols.
A semaphore mechanism supports two primitive operations that operate on a
special type of semaphore variable bsem in our example (Program 2)
The semaphore variable can assume nonnegative integer value and can be
accessed manipulate by SIGNAL & WAIT operation only. A binary,
semaphore bsem is used to protect the shared resource by enforcing its use
in a mutually exclusive manner. Each process (in our example there are 3
processes) ensures the integrity of its critical section by opening it with a
WAIT operation and closing it with SIGNAL operation. In this manner,
several concurrent processes might compete for the same resource safely
provided each of them uses WAIT & SIGNAL operations.
2. No model answer.
3. (i) Today the trend is towards multiprocessing and massive parallelim.
(ii) It is difficult to determine manually what activities can and cannot be
performed in parallel.
(iii) Parallel program are much more difficult to debug than sequential
program. Interaction between processes can be complex.
4. No model answer.
CPU SCHEDULING

Q.1 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 possible
different schedules are there? Give a formula in terms of n.

ANS.1 There are three types of schedulings for a process such as:-
1 Long term scheduling or job scheduling.
2 Middle term scheduling.
3 Short term scheduling .
So if number of processes are n then the number of different schedules are:-
3*n.

Q.2 Define the difference between preemptive and non preemptive scheduling.
State why strict nonpreemptive scheduling is unlikely to be used
Ina computer center.

Ans.2 The difference is as follows:


Preemptive Scheduling:-The scheduling in which the process leaves
The CPU before completion based on certain conditions is known as
preemptive scheduling.These certain conditions are priority and time
quantum during which the process is allowed to have CPU .Examples
Are shortest job first(SJF),round robin(RR).

Non preemptive scheduling:- In it, once the process gets the CPU
does not leave it untill it is finished making rest of the processes wait
for their turn.The example is first come first serve( FCFS).

In a computer center it may happen that long


processes make wait a number of small processes resulting in greater
average wait timewhich is highly undesirable.

Q.3 Consider the following set of processes,with the length of CPU-


burst
time given in milliseconds:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2

The processes are assumed to have arrived in the order P1,P2,P3,P4,P5


all at time 0.
a.Draw four Gantt charts illustrating the execution of these processes
using FCFS, SJF, a nonpreemptive priority(a smaller priority number
implies a higher priority),and RR(quantum=1)scheduling.
b.What is the turn around time of each process for each of the scheduling
algorithms in part a?
c.What is the waiting time of each process for each of the scheduling
algorithms in part a?
d.Which of the schedules in part aresults inthe minimal average waiting
time (over all processes)?

P P P4 P5
P1 2 3

0 10 11 13 14 19

FCFS

P P3 P5 P1
P1 4
0 1 2 4 9 19
SJF

P2 P5 P1 P3 P4
0 1 6 16 18 19

PRIORITY

P1 P2P3 P4P5 P1P3 P5 P1 P5 P1 P5 P1P5 P1 P1 P1 P1 P1

0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 18 19

RR

Ans.b: For FCFS :


TAT(P1) 10
TAT(P2) 11
TAT(P3) 13
TAT(P4) 14
TAT(P5) 19
Avg. TAT (10+11+13+14+19)/5 =13.4 ms

For SJF :
TAT(P1) 19
TAT(P2) 1
TAT(P3) 4
TAT(P4) 2
TAT(P5) 10
Avg. TAT (19+1+4+2+10)/5=7.2 ms

For PRIORITY :
TAT(P1) 16
TAT(P2) 1
TAT(P3) 18
TAT(P4) 19
TAT(P5) 6
Avg.TAT (16+1+18+19+6)/5=12 ms

For RR :
TAT(P1) 19
TAT(P2) 2
TAT(P3) 7
TAT(P4) 4
TAT(P5) 14
Avg.TAT (19+2+7+4+14)/5=9.2 ms

Ans. c For FCFS:


W(P1) 0
W(P2) 10
W(P3) 11
W(P4) 13
W(P5) 14
Avg. W (0+10+11+13+14)/5=9.6 ms

For SJF :
W(P1) 10
W(P2) 0
W(P3) 2
W(P4) 1
W(P5) 5
Avg. W (10+0+2+1+5)/5=3.6 ms

For PRIORITY :
W(P1) 6
W(P2) 0
W(P3) 16
W(P4) 18
W(P5) 1
Avg. W (6+0+16+18+1)/5=8.2 ms

For RR :
W(P1) 9
W(P2) 1
W(P3) 5
W(P4) 3
W(P5) 9
Avg. W (9+1+5+3+9)/5=5.4 ms

Ans. d Thus we see that minimum average waiting time calculates


in the case of SJF where it is equal to 3.6 ms.
Q.4 Suppose that the following processes arrive for execution at the times
Indicated. Each process will run the listed amount of time. In answering the
quest ions,use nonpreemptive scheduling and base all decisions on the infor_
mation you have at the time the decision must be made.
Process Arrival Time Burst Time
P1 0.0 8
P2 0.4 4
P3 1.0 1

a. What is the average turn around time for these processes


with the FCFS scheduling algorithm?
b.What is average turn around time for these processes
with the SJF scheduling algorithm?
c.The SJF algorithm is supposed to improve performance
but notice that we choose to run process P1 at time 0 because we did not know
that two shorter processes would arrive soon.Compute what the average TAT
will be if the CPU is left idle for the first one 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.

A. 4: The Gantt chart for FCFS as follows:

P
P1 P2 3
0 8 12 13

We know that TAT is waiting time+execution time.


TAT(P1)=0+8=8
TAT(P2)=(8-0.4)+(12-8)=7.6+4=11.6
TAT(P3)=(12-1)+(13-12)=11+1=12
Avg. TAT=(8+11.6+12)/3=10.53 ms

Ans. b Gantt chart for SJF is as follows :

P2 P3 P2 P1
P1
0 0.4 1.0 2.0 5.4 13

Waiting time for P1=(5.4 – 0.4)=5,Execution time =8


Waiting time for P2=1,Execution time =4
Waiting time for P3=0,Execution time =1
Hence,
TAT(P1)=(5+8)=13
TAT(P2)=(1+4)=5
TAT(P3)=(0+1)=1
Avg. TAT=(13+5+1)/3=6.33 ms

Ans. c In this case the Gantt chart will be as follows :

IDL P3 P1
E P2
0 1 2 6 14

Waiting Time for P1=6.0


Waiting Time for P2=2- 0.4=1.6
Waiting Time for P3=0.0
TAT(P1)=6.0+8.0=14.0
TAT(P2)=1.6+4.0=5.6
TAT(P3)=0.0+1.0=1.0
Avg. TAT=(14.0+5.6+1.0)/3=6.86 ms

Q.5 Consider the following preemptive priority-scheduling algorithm


Based on dynamically changing priorities.Larger priority numbers
Imply higher priority. When aprocess is waiting for the CPU,its priority
Changes at the rate of a;when it is running its priority changes at a rate b.
All the processes are given a priority 0 when they enter ready queue.The
Parameters a and b can be set to give many different scheduling algorithms.
What is the algorithm that results from b>a>0?

Ans. 5 The algorithm that results from b>a>0 is FCFS because the
Process having CPU has greater rate of priority increment than those at
Queue and due to this reason they will always enjoy higher priority
And can not be preempted before execution.

Q. 6 Explain the differences in the degree to which the following


Scheduling algorithms discriminate in favor of short processes :
a. FCFS
b. RR
c. Multilevel feedback queues
Ans. 6 FCFS :It does not care whether the process is long or short.
Once the CPU is assigned to the process CPU is not left till the process is
completed .

RR :If the quantum time is such that it is nearly equal to the


burst times of incoming processes, then this algorithm favors the short
processes .Because they will not have to wait for their subsequent chances
of CPU allocation. This will happen if quantum size is well below the CPU
burst times of short jobs.

Multilevel feedback queue :This greatly supports short processes because


the queues are arranged in increasing time quantum sizes .Thus short jobs
will be executed first and longer processes will be fedback in subsequent
queues with greater time quantum. When there is a process whose CPU
burst time is greater than the highest time quantum (i.e.penultimate queue)
then this process is sent to last queue where the processes are given CPU
on FCFS basis.
PROCESSOR MANAGEMENT

INTRODUCTION

In modern computer systems, several programs are allowed to reside in


computer's memory. Each program is supposed to be allocated CPU time for its
completion.

The CPU allocation strategy is the key area of processor management and is
handled by the Operating system.

PROCESSOR SCHEDULING

• Scheduling is a fundamental operating system function. All computer resources


are scheduled before use.

• Since CPU is one of the primary computer resources, its scheduling is central to
operating system design.
Scheduling refers to a set of policies and mechanisms supported by operating
system that controls the order in which the work to be done is completed. A
scheduler is an operating system program (module) that selects the next job to be
admitted for execution.

The main objective of scheduling are as follows:

1. Be Fair
2. Maximise Throughput
3. Maximise Number of Interactive Users
4. Minimise Overheads
5. Be Predictive
6. Balance Resource Use
7. Avoid Indefinite Postponement
8. Enforce Priority
9. Give Preferences to processes holding Key resources
10. Give better service to process exhibiting desirable behavior
11. Degrade gracefully under heavy loads
Process & Schedulers :
Types of Schedulers

Three types of schedulers in terms objectives, operating environment and


relationship to other schedulers :

1. Long Term Scheduler (Less Frequently)


2. Medium Term Scheduler (Frequently)
3. Short Term Scheduler (Very Frequently)

1. Long term scheduler :

a) Sometimes it is also called job scheduling. This determines which job


shall be admitted for immediate processing.

b) There are always more processes than it can be executed by CPU


operating System. These processes are kept in large storage devices like
disk for later processing. The long term scheduler selects processes
from this pool and loads them into memory.

c) The short term scheduler (also called the CPU scheduler) selects from
among the processes in memory which are ready to execute and assigns
the CPU to one of them.
d) The long term scheduler executes less frequently.

e) If the average rate of number of processes arriving in memory is equal


to that of departing the system then the long- term scheduler may need
to be invoked only when a process departs the system. Because of
longer time taken by CPU during execution, the long term scheduler
can afford to take more time to decide which process should be selected
for execution.

f) It may also be very important that long term scheduler should take a
careful selection of processes i.e. processes should be combination of
CPU and I/O bound types.

Generally, most processes can be put into any of two categories: CPU bound
or I/O bound

• If all processes are I/O bound, the ready queue will always be empty and the
short term scheduler will have nothing to do.

• If all processes are CPU bound. no process will be waiting for I/O operation and
again the system will be unbalanced. Therefore, the long term scheduler provides
good performance by selecting combination of CPU bound and I/O bound
process.

2. Medium term scheduler:

a) Most of the processes require some I/O operation. In that case, it may become
suspended for I/O operation after running a while.

b) It is beneficial to remove these process (suspended) from main memory to hard


disk to make room for other processes.

c) At some later time these process can be reloaded into memory and continued
where from it was left earlier. This is said to be Context Switching. The process
is swapped in and swap out by the medium term scheduler. (Context Switching)
d) The medium term scheduler has nothing to do with the suspended processes. But
the moment the suspending condition is fulfilled, the medium term scheduler get
activated to allocate the memory and swap in the process and make it ready for
commenting CPU resources.

e) In order to work properly, the medium term scheduler must be provided with
information about the memory requirement of swapped out processes which is
usually recorded at the time of swapping and stored in the related process control
block.

f) In term of the process state transition diagram the medium term scheduler
controls suspended to ready transition of swapped processes.

3. The short term scheduler:

a) It allocates processes belonging to ready queue to CPU for immediate


processing.

b) Its main objective is to maximize CPU requirement.

c) Compared to the other two scheduler it is more frequent.

d) It must select a new process for execution quite often because a CPU executes a
process only for few millisecond before it goes for I/O operation.
e) Often the short term scheduler executes at least once very 10 millisecond. If it
takes 1 millisecond to decide to execute a process for 10 millisecond, the
1/(10+1) = 9% of the CPU is being wasted simply for scheduling the work.
Therefore. it must be very fast.

f) In terms of the process state transition diagram it is in charge of ready to running


state transition
Scheduling and Performance Criteria

Some performance criteria that are frequently used optimizing goal are:

12. CPU Utilisation


13. Throughput
14. Turnaround Time
15. Waiting Time
16. Response Time

CPU Utilisation:

• How mush time the CPU is utilised/Busy.


• CPU must not be idle.
• The key idea is that if the CPU is busy all the time, the utilisation factor
of all the components of the system will be also high.

Throughput:

• It refers to the amount of work completed in a unit of time (Number of


outputs per unit time).
• One simplest way to measure throughput is by means of the number of
processes that are completed in a unit of time.
• The higher the number of processes, the more work apparently being done
by the system.

Turnaround Time:

• It is the time interval, from the time of submission of a job to the time of
its completion.

• It may be the sum of the periods spent waiting to get into memory, waiting
in the ready queue, CPU time and I/O operations etc.

Waiting Time:
• In multiprogramming operating system several jobs reside at a time in
memory. CPU executes only one job at a time. The rest of jobs wait
for the CPU.

• The waiting time is the time a job has to wait in the ready queue to get
the job completed.

• Waiting Time may be = TAT-Actual CPU time

Response time:

• The Response time is the time a job has to wait in the ready queue to
get the CPU for the first time. (first response)

• It is most frequently considered in time sharing and real time operating


system.

Processor (CPU) Scheduling Algorithms

• CPU scheduling deals with the problem of deciding which of the processes
in the ready queue to be allocated the CPU.

• A major division among scheduling algorithms is that whether they support


pre-emptive or non-preemptive scheduling discipline.

▪ A scheduling discipline is non-preemptive if once a process has been


given the CPU, the CPU cannot be taken away from that process. A
scheduling discipline is pre-emptive if the CPU can be taken away.
Queuing System Model:

• Arrivals are independent Random events (follow Poisson distribution). Number


of arrivals during a given interval time depends only on the length of interval
and not on the history.

Scheduling Algorithm

1. First-Come-First-Served (FCFS) Scheduling

• Its implementation is straightforward which is maintained by FIFO (First-in-


First-out) queue.

• Once a process has the CPU, it runs to completion.

Consider the following example :

Process Execution/Burst time


P1 24
P2 3
P3 3

If processes P1, P2 and P3 arrive in order P1,P2 and P3.


The Turn Around Times (TATs) are :

P1 P2 P3
0 24 27 30

The TAT for P1 : 24


The TAT for P2 : 27
The T AT for P3 : 30
Average TAT : (24+27+30)/3 = 27

2. Shortest-job-First (SJF)-Scheduling

• A different approach to CPU scheduling is the shortest-job-First where the


scheduling of a job or a process is done on the basis of its having shortest
execution time.

• If two processes have the same CPU time, FCFS is used.

As an example consider the following set of processes units of time.


Process CPU time
P1 5
P2 10
P3 8
P4 3

Using Shortest job first scheduling, these processes would be scheduled in the
P4-P1 -P3-P2 order.

Waiting time is
0 + 3 + 8 + 16 / 4 = 27/4 = 6.75 units of time.

If we were using the FCFS scheduling then the average waiting time will be =
(0 + 5 + 15 + 23)/4 = 43/4 = 10.75 units of time.
Shortest job first (SJF) may be implemented in either non- preemptive or
preemptive varieties (SJN, SRT).

In either case, whenever the SJF scheduler is invoked, it searches the ready queue
to find the job or the process with the shortest execution time. The difference
between the two (preemptive or non-Preemptive) SJF scheduler lies in the
condition that lead to invocation of the scheduler and consequently the frequency
of its execution.

3.Round Robin Scheduling:

• This is one of the oldest, simplest and widely used algorithm.


• The round robin scheduling algorithm is primarily used in a time-sharing and
a multi-user system where the primary requirement is to provide reasonably
good response times and in general to share the system fairly among all
system users.
• Basically the CPU time is divided into time slices.
• Each process is allocated a small time-slice quantum (from 10-100
millisecond) while it is running.
• No process can run for more than one time slice when there are others waiting
in the ready queue. If a process needs more CPU time to complete after
exhausting one time slice, it goes to the end of ready queue to await the next
allocation.

Example: If there are 3 processes: P1, P2 and P3 which require the following CPU
time

Process Burst time/Execution time


P1 25
P2 5
P3 5

Thus the resulting round robin schedule is:

P1 P2 P3 P1 P1 P1 P1
0 5 10 15 20 25 30 35

Context switching is the key consideration.

Round-robin scheduling is often regarded as a fair scheduling discipline. It is also


one of the best known scheduling disciplines for achieving good and relatively
evenly distributed response time.

4. Priority based Scheduling:

• A priority is associated with each process and the scheduler always picks up the
highest priority process for execution from the ready queue.

• Equal priority processes are scheduled FCFS. The level of priority may be
determined on the basis of resource requirements, processes characteristics and
its run time behaviour.

Highest Response Ratio Next (HRN):

• Brinch Hansen developed HRN strategy that corrects some of the


weaknesses of SJF, SJN etc., such as excessive favouratism toward short
new jobs

• It is a Non-preemptive scheduling in which the priority of each job is a


function not only of the Service Time but also of the amount of time the job
has been waiting for service.

Dynamic Priority in HRN

Priority = (Time Waiting + Service Time) / Service Time

• Once a job gets CPU it runs to completion

Multiple-level-Queue (MLQ) scheduling :

processes are classified into different groups.


interactive processes (foreground) and
batch processes (background)

could be considered as two types of processes because of their different response


time requirements, scheduling needs and priorities.

A multi queue scheduling algorithm partitions the ready queue into separate queues.
Processes are permanently assigned to each queue, usually based upon properties
such as memory size or process type. Each queue has its own scheduling algorithm.
The interactive queue might be scheduling by a round-robin algorithm while batch
queue follows FCFS.
As an example of multiple queues scheduling one simple approach to partitioning
of the ready queue into system processes, interactive processes and batch processes
which creates a three ready queues

Figure 7: Multiple queue Scheduling


Each queue is serviced by some scheduling discipline best suited to the type of
processes stored in the queue. There are different possibilities to manage queues.
One possibility is to assign a time slice to each queue, which it can schedule among
different processes in its queue. Foreground processes can be assigned 80% of CPU
whereas background processes are given 20% of the CPU time.
The second possibility is to execute the high priority queue first. No process in the
batch queue, for example could run unless the queue for system processes and
interactive processes were all empty. If an interactive process entered the ready
queue while a batch process is running, the batch process would be pre-empted.

PROCESSOR MANAGEMENT

INTRODUCTION

The notion of a process is central to the understanding of Operating System's


functioning. Everything is centered around this concept and it is very important
to understand and appreciate this concept right from the beginning.
A process is basically a program while it is being executed. A process is a
running program with some specific tasks to do. For example in UNIX
Operating System, Shell or command interpreter is also a process,
performing the task of listening to whatever is typed on a terminal. Every time
we run a command, shell runs that commands as a separate process. When a
process is created, it requires several resources such as CPU time, memory,
files, stack, registers to run the program.

In a time shared system, several user's program reside in computer's memory.


Each user is allocated only a fraction of CPU time for his/her program. When
CPU executes a program or command, a process is created. When one process
stops running because it has taken its share of CPU time, another process starts.

When a process is temporarily suspended, information about it is stored in a


memory location so that its execution could start from the same location where
from it was suspended. In many operating system, all the information about
each process is stored in a Process table. Operating system also provides
several system calls to manage processes.

These system calls are mainly to create and kill processes. In UNIX operating
system, for example when a user types a command to compile a C-language program,
shell creates a new process to run the compiler. When the compilation is over, it
executes a system call to terminate itself.

2.2 PROCESS CONCEPT

From the previous discussion on a process one should understand a difference


between a program and a process, although it is marginal. A program is a
passive entity whereas a process is an active entity. The key idea about a
process is that it is an activity of some kind and consists of pattern of bytes
(that the CPU interprets as machine instruction, data, register and stack). A
single processor may be shared among several processes with some scheduling
policy being used by the processor to allocate one process and deal locate
another one.
Process hierarchies: Operating system needs some way to create and kill
processes. When a process is created, it creates another process(es) which in
turn crates some more process(es) and so on, thus forming process hierarchy or
process tree.
In UNIX operating system, a process is created by the fork system call and
terminated by exit.

Process States: The lifetime of a process can be divided into several stages as
states, each with certain characteristics that describe the process. It means that
as a process starts executing, it goes through one state to another state.

Each process may be in one of the following states:


New -The process has been created.
Ready -The process is waiting to be allocated to a processor. Processor
comes to this state immediately after it is created.
All ready processes (kept in queue) keeps waiting for CPU time to be allocated
by operating system in order to run. A program called scheduler which is a
part of operating system, pick-ups one ready process for execution by passing a
control to it.
Running- Instructions are being executed. When a process gets a control from
CPU plus other resources, it starts executing. The running process may require
some I/O during execution. Depending on the particular scheduling policy of
operating system, it may pass its control back to that process or it (operating
system) may schedule another process if one is ready to run.
Suspended: A suspended process lacks some resource other than the CPU.
Such processes are normally not considered for execution until the related
suspending conditions is fulfilled. . The running process become suspended by
invoking I/O routines whose result it needs in order to proceed.
Terminate: When the process finally stops. A process terminates when it
finishes executing its last statement. At that point in time, the process may
return some data to its parent process. Sometimes there are additional
circumstances when termination occurs. A process can cause the termination of
another process via an appropriate system call.
A general form of process state diagram is illustrated in Figure 1.

Figure 1: Process State Diagram


A parent may terminate the execution of one of its children for a variety of
reasons, such as:
xvii. The task assigned to the child is no longer required.
xviii. The child has exceeded its usage of some of resources it has been
allocated.

Process Implementation
The operating system groups all information that it needs about a particular
process into a data structure called a Process Control Block (PCB). It simply
serves as the storage for any information for processes. When a process is
created, the operating system creates a corresponding PCB and when it
terminates, its PCB is released to the pool of free memory locations from
which new PCBs are drawn. A process is eligible to compete for system
resources only when it has active PCB associated with it. A PCB is
implemented as a record containing many pieces of information associated
with a specific process, including:
• Process number: Each process is identified by its process number, called
process ID.
• Priority.
• Process state: Each process may be in any of these states: new, ready,
running, suspended and terminated.
• Program counter: It indicates the address of the next instruction to be
executed for this process.

Registers: They include accumulator, general purpose registers, index registers


etc. Whenever a processor switches over from one process to another process,
information about current status of the old process is saved in the register along
with the program counter so that the process be allowed to continue correctly
afterwards. The process is shown in figure 2.

Accounting information: It includes actual CPU time used in executing a process.


I/O status information: It includes outstanding I/O requests, list of open files
information about allocation of peripheral devices to processes.
Processor scheduling details: It includes a priority of a process, address to
scheduling queues and any other.

2.3 PROCESSOR SCHEDULING


Scheduling is a fundamental operating system function. All computer resources
are scheduled before use. Since CPU is one of the primary computer resources, its
scheduling is central to operating system design.
Scheduling refers to a set of policies and mechanisms supported by operating
system that controls the order in which the work to be done is completed. A
scheduler is an operating system program (module) that selects the next job to be
admitted for execution. The main objective of scheduling is to increase CPU
utilisation and higher throughput. Throughput is the amount of work
accomplished in a given time interval. CPU scheduling is the basis of operating
system which supports multiprogramming concepts. By having a number of
programs in computer memory at the same time, the CPU may be shared among
them. This mechanism improves the overall efficiency of the computer system by
getting more work done in less time. The advantage of multiprogramming is
explained in the previous unit.
In this section we describe the roles of three types of schedulers encountered in
operating system. After this discussion, we will touch upon various performance
criterias that schedulers may use in maximizing system performance and finally
we will present several scheduling algorithms.

2.3.1 Types of Schedulers


In this subsection we describe three types of schedulers: long term, medium term
and short term schedulers in terms of its objectives, operating environment and
relationship to other schedulers in a complex operating system environment.
Long term scheduler: Sometimes it is also called job scheduling. This determines
which job shall be admitted for immediate processing.

There are always more processes than it can be executed by CPU operating
System. These processes are kept in large storage devices like disk for later
processing. The long term scheduler selects processes from this pool and loads
them into memory. In memory these processes belong to a ready queue. Queue is
a type of data structure which has been discussed in course 4. Figure 3 shows the
positioning of all three type of schedulers. The short term scheduler (also called
the CPU scheduler) selects from among the processes in memory which are ready
to execute and assigns the CPU to one of them. The long term scheduler executes
less frequently.
If the average rate of number of processes arriving in memory is equal to that of
departuring the system then the long- term scheduler may need to be invoked only
when a process departs the system. Because of longer time taken by CPU during
execution, the long term scheduler can afford to take more time to decide which
process should be selected for execution. It may also be very important that long
term scheduler should take a careful selection of processes i.e. processes should be
combination of CPU and I/O bound types. Generally, most processes can be put
into any of two categories: CPU bound or I/O bound. If all processes are I/O
bound, the ready queue will always be empty and the short term scheduler will
have nothing to do. If all processes are CPU bound. no process will be waiting for
I/O operation and again the system will be unbalanced. Therefore, the long term
scheduler provides good performance by selecting combination of CPU bound and
I/O bound process.

Medium term scheduler: Most of the processes require some I/O operation. In
that case, it may become suspended for I/O operation after running a while. It is
beneficial to remove these process (suspended) from main memory to hard disk to
make room for other processes. At some later time these process can be reloaded
into memory and continued where from it was left earlier. Saving of the suspended
process is said to be swapped out or rolled out. The process is swapped in and
swap out by the medium term scheduler.

The medium term scheduler has nothing to do with the suspended processes. But
the moment the suspending condition is fulfilled, the medium term scheduler get
activated to allocate the memory and swap in the process and make it ready for
commenting CPU resources. In order to work properly, the medium term
scheduler must be provided with information about the memory requirement of
swapped out processes which is usually recorded at the time of swapping and
stored in die related process control block. In term of the process state transition
diagram (figure 1) the medium term scheduler controls suspended to ready
transition of swapped processes.
The short term scheduler: It allocates processes belonging to ready queue to
CPU for immediate processing. Its main objective is to maximize CPU
requirement. Compared to the other two scheduler it is more frequent It must
select a new process for execution quite often because a CPU executes a process
only for few millisecond before it goes for I/O operation. Often the short term
scheduler executes at least once very 10 millisecond. If it takes 1 millisecond to
decide to execute a process for 10 millisecond, the 1/(10+1) = 9% of the CPU is
being wasted simply for scheduling the work. Therefore. it must be very fast.
In terms of the process state transition diagram it is in charge of ready to running
state transition

2.3.2 Scheduling and Performance Criteria


Some performance criterias that are frequently used by schedulers to maximize
system performance. These are:

19. CPU Utilisation


20. Throughput
21. Turnaround Time
22. Waiting Time
23. Response Time

CPU Utilisation: The key idea is that if the CPU is busy all the time, the
utilisation factor of all the components of the system will be also high.
Throughput: It refers to the amount of work completed in a unit of time. One
way to measure throughput is by means of the number of processes that are
completed in a unit of time. The higher the number of processes, the more
work apparently being done by the system. But this approach is not very useful
for comparison because this is dependent on the characteristics and resource
requirement of the process being executed. Therefore to compare throughput of
several scheduling algorithms it should be fed the process with similar
requirements.
Turnaround Time: It may be defined as interval from the time of submission
of a process to the time of its completion. It is the sum of the periods spent
waiting to get into memory, waiting in the ready queue, CPU time and I/O
operations.
Waiting Time: In multiprogramming operating system several jobs reside at a
time in memory. CPU executes only one job at a time. The rest of jobs wait for
the CPU. The waiting time may be expressed as turnaround time, less the
actual processing time i.e. waiting time = turnaround time - processing time.
But the scheduling algorithm affects or considers the amount of time that a
process spends waiting in a ready queue. Thus rather than looking at
turnaround time waiting time is usually the waiting time for each process.
Response time: It is most frequently considered in time sharing and real time
operating system. However its characterises differs in the two systems. In time
sharing system it may be defined as interval from the time the last character of
a command line of a program or transaction is entered to the time the last result
appears on the terminal. In real time system it may be defined as interval from
the time an internal or external event is signaled to the time the first instruction
of the respective service routine is executed.
One of the problems in designing schedulers and selecting a set of its
performance criterias is that they often conflict with each other. For example,
the fastest response time in time sharing and real time system may result in low
CPU utilisation. Throughput and CPU utilisation may be increased by
executing the large number of processes, but then response time may suffer.
Therefore, the design of a scheduler usually requires balance of all the different
requirements and constraints.

2.3.3 Scheduling Algorithms

CPU scheduling deals with the problem of deciding which of the processes in
the ready queue to be allocated the CPU. There are several scheduling
algorithms which we will examine in this section.
A major division among scheduling algorithms is that whether they support
pre-emptive or non-preemptive scheduling discipline. A scheduling
discipline is non-preemptive if once a process has been given the CPU, the
CPU cannot be taken away from that process. A scheduling discipline is pre-
emptive if the CPU can be taken away.
Preemptive scheduling is more useful in high priority process which requires
immediate response. For example in Real time system the consequence of missing
one interrupt could be dangerous.
In non-preemptive systems, jobs are made to wait by longer jobs, but the treatment
of all processes is fairer. The decision whether to schedule preemptive or not
depends on the environment and the type of application most likely to be
supported by a given operating system.
First-Come-First-Served (FCFS) Scheduling

Its implementation is also straightforward which is maintained by FIFO (First-in-


First-out) queue. Once a process has the CPU, it runs to completion.

Figure 5: First-in-First-out Scheduling

A FCFS scheduling is non preemptive which usually results in poor performance.


As a consequence of non preemption, there is a low rate of component utilisation
and system throughput. Short jobs may suffer considerable turnaround delays and
waiting times when CPU has been allocated to longer jobs. Consider the following
example of two processes:
Process Execution time
P1 20
P2 4
If both processes P1 and P2 arrive in order P1 -P2 in quick succession, the
turnaround times are 20 and 24 units of time respectively (P2 must wait for P1
to complete) thus giving an average of 22 (20+24)/2) units of time. The
corresponding waiting times are 0 and 20 units of time with average of 1 0 time
units. However, when the same processes arrive in P2-P 1 order, the turn
around times are 4 and 24 units of time respectively giving an average of 14
(4+24)12) units of time and the average waiting time is(0+4)/2)=2. This is a
substantial reduction. This simple example explain how short jobs may be
delayed in FCFS scheduling algorithm.
Shortest-job-First (SJF)-Scheduling
A different approach to CPU scheduling is the shortest-job-First where the
scheduling of a job or a process is done on the basis of its having shortest
execution time. If two processes have the same CPU time, FCFS is used. As an
example consider the following set of processes units of time.
Process CPU time
P1 5
P2 10
P3 8
P4 3
Using Shortest job first scheduling, these processes would be scheduled in the
P4-P1 -P3-P2 order. Waiting time is
0 + 3 + 8 + 16 / 4 = 27/4 = 6.75 units of time. If we were using the FCFS
scheduling then
the average waiting time will be
= (0 + 5 + 15 + 23)/4 = 43/4 = 10.75 units of time.
Shortest job first (SJF) may be implemented in either non- preemptive or
preemptive varieties. In either case, whenever the SJF scheduler is invoked, it
searches the ready queue to find the job or the process with the shortest
executive lime. The difference between the two (preemptive or non-
Preemptive) SJF scheduler lies in the condition that lead to invocation of the
scheduler and consequently the frequency of its execution.
SJF scheduling is an optimal scheduling algorithm in terms of minimizing the
average waiting time of a given set of processes. It would always schedule the two
process P1 and P2 discussed in the FCFS scheduling section. When both are
available in a ready queue in the shorter-longer order and thus achieve set of lower
waiting time.
SJF scheduling algorithm works optimally only when the exact future execution
times of jobs or processes are known at the time of scheduling. In the case of
short-time scheduling and preemption, even more detailed knowledge of each
individual CPU burst is required. In other words, the optimal performance of SJF
scheduling is dependent upon future knowledge of the process/job behaviour. This
comes on a way to effective implementation of SJF scheduling in practice,
because there is difficulty in estimating future process behaviour reliably except
for very specialised deterministic cases.
The occurrence of this problem is SJF scheduling can be tackled through sorting
of ready list of processes according to the increasing values of their remaining
execution times. This approach can also improve the schedulers' performance by
removing the search process of shortest processes. However, insertion into a
sorted list are generally more complex if the list is to remain sorted after inserting.

Round Robin Scheduling: This is one of the oldest, simplest and widely used
algorithm. The round robin scheduling algorithm is primarily used in a time-
sharing and a multi-user system where the primary requirement is to provide
reasonably good response times and in general to share the system fairly among
all system users. Basically the CPU time is divided into time slices.
Each process is allocated a small time-slice (from 10-100 millisecond) while it is
running. No process can run for more than one time slice when there are others
waiting in the ready queue. If a process needs more CPU time to complete after
exhausting one time slice, it goes to the end of ready queue to await the next
allocation (Figure 6). Otherwise, if the running process releases a control to
operating system voluntarily due to I/O request or termination, another process is
scheduled to run.

Figure 6: Round Robin Scheduling

Round robin scheduling utilises the system resources in an equitable manner.


Small process may be executed in a single time-slice giving good response time
whereas long processes may require several time slices and thus be forced to pass
through ready queue a few times before completion. For example there are 3
processes: P1, P2 and P3 which require the following CPU time

Process Burst time/Execution time


P1 25
P2 5
P3 5
If we use a time-slice of 5 units of time, then PI gets the first 5 units of time.
Since it requires another 20 units of time, it is pre-empted after the first time
slice and the CPU is given to the next process i.e. P2. Since P2 just needs 5
units of time, it terminates as time-slice expires. The CPU is then given to the
next process P3. Once each process has received one time slice. The CPU is
returned to PI for an additional time-slice. Thus the resulting round robin
schedule is:

P1 P2 P3 P1 P1 P1 P1
0 5 10 20 25 30 35

Implementation of round robin scheduling requires the support of a dedicated


timer. The timer is usually set to interrupt the operating system wherever a time
slice expires and thus force the scheduler to be involved. Processing the interrupt
to switch the CPU to another process requires saving all the registers for the old
process and then loading the registers for the new process.
This task is known as context switching. The scheduler itself simply stores the
context of the running process, moves it to the end of the ready queue, and
despatches the process at die head of ready queue. Whenever the running process
surrenders control to the operating system before expiration of its time-slice, the
scheduler is again invoked to despatch a new process to CPU.
Round-robin scheduling is often regarded as a fair scheduling discipline. It is also
one of the best known scheduling disciplines for achieving good and relatively
evenly distributed response time.

Priority based Scheduling: A priority is associated with each process and the
scheduler always picks up the highest priority process for execution from the
ready queue. Equal priority processes are scheduled FCFS. The level of priority
may be determined on the basis of resource requirements, processes characteristics
and its run time behaviour.
A major problem with a priority based scheduling is indefinite blocking of a lost
priority process by a high priority process. In general, completion of a process
within finite time cannot be guaranteed with this scheduling algorithm.
A solution to the problem of indefinite blockage of low priority process is
provided by aging priority. Aging priority is a technique of gradually increasing
the priority of processes (of low priority) that wait in the system for a long time.
Eventually, the older processes attain high priority and are ensured of completion
in a finite time.

Multiple-level-Queue (MLQ) scheduling processes are classified into different


groups. For example, interactive processes (foreground) and batch processes
(background) could be considered as two types of processes because of their
different response time requirements, scheduling needs and priorities.
A multi queue scheduling algorithm partitions the ready queue into separate
queues. Processes are permanently assigned to each queue, usually based upon
properties such as memory size or process type. Each queue has its own
scheduling algorithm. The interactive queue might be scheduling by a round-robin
algorithm while batch queue follows FCFS.
As an example of multiple queues scheduling one simple approach to partitioning
of the ready queue into system processes, interactive processes and batch
processes which creates a three ready queues
Figure 7: Multiple queue Scheduling
Each queue is serviced by some scheduling discipline best suited to the type of
processes stored in the queue. There are different possibilities to manage queues.
One possibility is to assign a time slice to each queue, which it can schedule
among different processes in its queue. Foreground processes can be assigned
80% of CPU whereas background processes are given 20% of the CPU time.

The second possibility is to execute the high priority queue first. No process in the
batch queue, for example could run unless the queue for system processes and
interactive processes were all empty. If an interactive process entered the ready
queue while a batch process is running, the batch process would be pre-empted.
Memory Management
Jobs/Programs need memory which has to be allocated by Operating
System.

Memory View :

i. Memory as a single unit


ii. Memory as collection of various units.

OS Monitor OS Monitor

25 K
User
Memory
25 K

25 K

Single partition Many Partitions

1. Fixed partition Multiprogramming : Partition sizes are fixed


30 K
50K 10K 30 K

30 K

30 K

2. Variable size partition Multiprogramming : Partitions are of different


size

…………. 15K 10K 35K


35 K
10K

15 K

:
:

Memory Fragmentation :

Memory wastage due to improper arrangements etc.

• Internal Fragmentation : When the job size is smaller than the block
size.
• External Fragmentation : When block size is too small to accommodate
the job.

Strategies to avoid fragmentation :

1. Compaction
2. Hole Coalescing
3. Swapping
Fetching Strategies : Demand fetching
Anticipatory Fetching

Placement strategies : First Fit


Best Fit
Worst Fit

Replacement Strategies :
Virtual Storage and its Management :

• Normally associated with the ability to address a storage space much larger
than that available in the memory storage.

• All virtual storage systems have the attribute that the address developed by
running programs are not necessarily those address available in primary
storage. (Disassociation of address).

• Achieved through Address Translation Unit (ATU). Usually available as


Hardware .

• Virtual Memory is divided in blocks and information is stored block by block.


If blocks are equal size called pages, if Variable sizes Segments.

• This dynamic address translator can be implemented using a page table. In


most systems, this table is maintained in the main memory. It will have one
entry for each virtual page of the virtual address space.
Paging :
Secondary Memory is divided into Pages.
Primary Memory is divided into Pageframes.
EXAMPLE : Design a mapping scheme with the following
specifications

• Virtual address space = 32K words


• Virtual memory size = 8K words
• Page size = 2K words
• Secondary memory address = 24 bits

SOLUTION
32K words can be divided into 16 virtual pages with 2K words per page, as follows:
VIRTUAL ADDRESS PAGE NUMBER
0-2047 0
2048-4095 1
4096-6143 2
6144-8191 3
8192-10239 4
10240-12287 5
12288-14335 6
14336-16383 7
16384-18431 8
18432-20479 9
20480-22527 10
22528-24575 11
24576-26623 12
26624-28671 13
28672-307119 14
30720-32767 15

Since there are 8K words in the main memory, 4 Pageframes with 2K words per frame
are available:

PHYSICAL ADDRESS PAGE FRAME NUMBER


0-2047 0
2048-4095 1
4096-6143 2
6144-8191 3

• Since there are 32K addresses in the virtual space, 15 bits are required for the virtual address.
• Because there are 16 virtual pages, the page map table will contain 16 entries.
• The 4 most-significant bits of the virtual address can used as an index to the page map table, and
the remaining 11 bits of the virtual address can be used as the displacement to locate a word within
the page frame.

• Each entry of the page table is 32 bits long. This obtained as follows:

1 bit for determining whether the page table is in main memory or not
(residence bit).
2 bits for main secondary memory address
24 bits for secondary memory address
5 bits for future use. (Unused at the moment)
32 bits total

Assume the virtual address generated is 0111 000 0010 1101. From this,
compute the following:

Virtual page number = 710


Displacement = 4310

• From the page-map table entry corresponding to the address 0111, the page
can be found in the main memory (the page resident bit is 1).

• The required virtual page is mapped to main memory page frame number 2.
Therefore, the actual physical word is the 43rd word in the second page frame
of the main memory.
PAGE FAULT and ITS HANDLING :
So far, a page reference by a program assumed to be found in the main memory.
In practice, this is not necessarily true. When a page needed by a program is not
assigned to the main memory, a page fault has occurred. A page fault is actually an
interrupt, and when this interrupt occurs, control is transferred to a service routine of
the operating system called the page-fault handler. The sequence of activities
performed by the page-fault handler are summarized as follows:

• The secondary memory address of the required page p is located from the page
table.
• Page p from the secondary memory is transferred into one of the available
main memory frames by performing a block-move operation.
• The page table is updated by entering the frame number where page p is loaded
and by setting the residence bit to 1 and change bit to 0.

When a page-fault handler completes its task, control is transferred to the user
program, and the main memory is accessed again for the required data or
instruction. All these activities are kept hidden from a user.
Pages are transferred to main memory only at specified times. The policy that
governs this decision is known as the fetch policy.

PAGE REPLACEMENT :

• Similarly, when a page is to be transferred from the secondary memory to main


memory; all frames may be full. At this point, one of the frames has to be
removed from the main memory to provide room for an incoming page. The
frame to be removed is selected using a replacement policy. The performance
of a virtual memory system is dependent upon the fetch and replacement
strategies.
Fetch strategies :
Demand Paging : Bring the page only when the page is demanded.
(Page fault)

Anticipatory Paging : Bring the page in anticipation.

Principal of Locality of Reference : In a short period of time


addresses referenced by a program are clusters around a
particular region of the address space.

P.J.Denings Working Set Model may be used for this.

The working set of a program W(m,t) is defined as the set of m most


recently needed pages by the program at some instant of time t. The parameter
m is called the window of the working set.

For example, consider the stream of references:

1 1 2 3 2 4 3 4 1 1 2 3 5 3 2

t1 t2

From this figure, determine that:

W(4, t1) = {2,3}


W(4, t2) = {1,2,3,}
W(5, t2) = {1,2,3,4}

In practice, the working set of program varies slowly with respect to


time. Therefore, the working set of a program can be predicated ahead of time.
PAGE REPLACEMENT ALGORITHMS :
1. First In First Out (FIFO)
2. Optimal (OPT)
3. Least Recently Used (LRU)

1. FIRST IN FIRST OUT (FIFO) :

Consider the following stream of page requests

2, 3, 2, 4, 6, 2, 5, 6, 1, 4, 6

HIT RATIO for this stream using the FIFO replacement policy, assuming the
main memory can hold 3 page frames and initially all of them are vacant.

• The primary advantage of the FIFO algorithm is its simplicity. This algorithm can
be implemented by using a FIFO queue.

• FIFO policy gives the best result when page references are made in a strictly
sequential order.
• However, this algorithm fails if a program. loop needs a variable introduced at the
beginning.
• Another difficulty with the FIFO algorithm is it may give anomalous results.
BELADY’S ANOMALY:

Intuitively, one may feel that an increase in the number of page frames will also
increase the hit ratio. However, with FIFO, it is possible that when the page frames
are increased, there is a drop in the hit ratio. Consider the following stream of
requests:

1, 2, 3, 4, 5, 1, 2, 5, 1, 2, 3, 4, 5, 6, 5

• Assume the main memory has 4 page frames; then using the FIFO policy there is a
hit ratio of 4/15.
• However, if the entire computation is repeated using 5 page frames, there is a hit
ratio of 3/15.
• This surprising result was first observed by L.A. Belady in 1969, and for this
reason is often referred to as Belady’s anomaly.

2. OPTIMAL PAGE REPLACEMENT :

When there is a need to replace a page, choose that page which may not be
needed again for the longest period of time in the future.

• The decision made by the optimal replacement policy is optimal because it makes a
decision based on the future evolution.
• It has been proven that this technique does not give any anomalous results when the
number of page frames is increased.

• However, it is not possible to implement this technique because it is impossible to


predict the page references well ahead of time.

• Despite this disadvantage, this procedure is used as a standard to determine the


efficiency of a new replacement algorithm.

Since the optimal replacement policy is practically unfeasible, some method that
approximates the behavior of this policy is desirable. One such approximation is the
least recently used (LRU) policy.

3. LEAST RECENTLY USED (LRU):

According to the LRU policy, the page that is selected for replacement is the
page that has not been referenced for the longest period of time.

From the results of the example, we observe that the performance of the LRU policy
is very close to that of the optimal replacement policy. Also, the LRU obtains better
result than the FIFO because it tries to retain the are used recently.

Now, let us summarize some important features of the LRU algorithm.


• In principle, the LRU algorithm is similar to the optimal replacement policy except
that it looks backward on the time axis.

• If the request stream is first reversed and then the LRU policy is applied to it. The
result obtained is equivalent to the one that is obtained by the direct application of
the optimal replacement policy to the original request stream.

• It has been proven that the LRU algorithm is a stack algorithm. A page-replacement
algorithm is said to be a stack algorithm if the following condition holds:
Pt (n)  Pt (n + 1)

In the preceding relation the quantity Pt(i) refers to the set of pages in the main memory whose total
capacity is I frames at some time t. This relation is called the inclusion property. One can easily
demonstrate that FIFO replacement policy is not a stack algorithm. This task is left as an exercise.

• The LRU policy can be easily implemented using a stack. Typically, the page numbers of the
request stream are stored in this stack. Suppose that p is the page numbers being referenced. If p
is not in the stack, then p is pushed into the stack. However, if p is in the stack, p is removed
from the stack and placed on the top of the stack. The top of the stack always holds the most
recently referenced page number, and the bottom of the stack always holds the least-recent page
number. To see this clearly, consider Figure 5.45. in which a stream of page references and the
corresponding stack instants are shown. The principal advantage of this approach is that there is
no need to search for the page to be replaced because it is always the bottom most element of
the stack. This approach can be implemented using either software or macrocodes. However,
this method takes more time when a page number is moved from the middle of the stack.
• Alternatively, the LRU policy can be implemented by adding an age register to each entry of the
page table and a virtual clock to the CPU. The virtual clock is organized so that it is incremented
after each memory reference. When a page is referenced, its age register is loaded with the
contents of the virtual clock. The age register of page holds the time at which that page was
most recently referenced. The least-recent page is that page whose age register value is
minimum. This approach requires and operating system to perform time-consuming
housekeeping operations. Thus the performance of the system may be degraded.
• To implement these methods, the computer system must provide adequate hardware support.
Incrementing the virtual clock using software takes more time. Thus the operating speed of the
entire systems that do not provide enough hardware support. To get around this problem, some
replacement policy is employed that will approximate the LRU policy.
• The LRU policy can be approximated by adding an extra bit called an activity bit to each entry
of the page table. Initially all activity bits are cleared to 0. When a page is referenced, its activity
bit is set to 1. Thus this bit tells whether or not the page is used. Any pager whose activity bit is
0 may be a candidate for replacement. However, the activity bit cannot determine how many
times a page has been referenced.
• More information can be obtained by adding a register to each page table entry. To illustrate this
concept, assume a 16-bit register has been added to each entry of the page table. Assume that
the operating system is allowed to shift the contents of all the registers 1 bit to the right at
regular intervals. With one right shift, the most-significant bit position becomes vacant. If it is
assumed that the activity bit is used to fill this vacant position, some meaningful conclusions
can be derived. For example, if the content of a page register is 000016, then it can be concluded
that this page was not in use during the last 16 time-interval periods. Similarly, a value FFFF16
for page register indicates that the page should have been referenced at least once in the last 16
time-interval periods. If the content of a page register is FFOO16 and the content of another one
is OOFO16, the former was used more recently.
• If the content of a page register is interpreted as an integer number, then the least-recent page
has a minimum page register value and can be replaced. If two page registers hold the minimum
value, then either of the pages can be evicted, kor one of them can be chosen on a FIFO basis.
• The lager the size of the page register, the more time is spent by the operating system in the
update operations. When the size of the page register is 0, the history of the system can only be
obtained via the activity bits. If the proposed replacement procedure is applied on the activity
bits alone, the result is known as the second-chance replacement policy.
• Another bit called a dirty bit may be appended to each entry of the page table. This bit is
initially cleared to 0 and set ot 1 when a page is modified.
• This bit can be used in two different ways:
• The idea of dirty bit reduces the swapping overhead because when the dirty bit of a page to
be replaced is zero, there is no need to copy this page into the secondary memory, and it can
be overwritten by an incoming page. A dirty bit can be used in conjunction with any
replacement algorithm.
• A priority scheme can be set up for replacement using the values of the dirty and activity
bits, as described next.

IMPLEMENTING LRU :

Method Strategy

using a stack page numbers of the request stream are stored in this stack,
If p is not in the stack, then p is pushed into the stack
However, if p is in the stack, p is removed from the stack
and placed on the top of the stack. The top of the stack
always holds the most recently referenced page number,
and the bottom of the stack always holds the least-recent
page number.
adding an age The virtual clock is organized so that it is incremented
register entry of page after each memory reference. The age register of page
table and a virtual holds the time at which that page was most recently
clock to the CPU referenced

approximated by Initially all activity bits are cleared to 0. When a page is


adding an extra bit referenced, its activity bit is set to 1. Any page whose
called an activity bit activity bit is 0 may be a candidate for replacement
to each entry of the
page table

MORE STRATEGIES :

• Another bit called a dirty bit may be appended to each entry of the page table. This
bit is initially cleared to 0 and set to 1 when a page is modified.

• This bit can be used in two different ways:

• The idea of dirty bit reduces the swapping overhead because when the dirty bit
of a page to be replaced is zero, there is no need to copy this page into the
secondary memory, and it can be overwritten by an incoming page. A dirty bit
can be used in conjunction with any replacement algorithm.

• A priority scheme can be set up for replacement using the values of the dirty
and activity bits.

Priority Level Activity Bit Dirty Meaning


Bit
0 0 0 Neither used nor modified
1 0 1 Not recently used but
modified
2 1 0 Used but not modified
3 1 1 Used as well as dirty

NRU : Not Recently Used


LFU : Least Frequently Used
MFU : Most Frequently Used
Fetch strategies :
Demand Paging : Bring the page only when the page is demanded.
(Page fault)

Anticipatory Paging : Bring the page in anticipation.

Principal of Locality of Reference : In a short period of time addresses


referenced by a program are clusters around a particular region of the address
space.
P.J.Denings Working Set Model may be used for this.

The working set of a program W(m, t) is defined as the set of m most recently needed pages
by the program at some instant of time t. The parameter m is called the window of the working set.
For example, consider the stream of references shown in Figure 5.38:

1 1 2 3 2 3 4 3 4 1 1 2 3 5 3 2

m=4

m=4
t1 m=5 t2

Figure 5.38 Strea of Page References

From this figure, determine that:


W(4, t1) = {2,3}
W(4, t2) = {1,2,3,}
W(5, t2) = {1,2,3,4}

In general, the cardinality of the set W(O, t) is zero, and the cardinality of the set W(00, t) is equal
to the total number of distinct pages in the program. Since m + 1 most-recent page references
include m most-recent page references:
#[(W(m + 1, t)]  # [W(m, t)]

In this equation, the symbol # ids used t indicate the cardinality of the set W(m, t). When m
is varied from 0 to 00, the quantity #W(m, t) continuously increases #W(m, t) is a monotonically
increasing function of m. The relationship between m and #W(m, t) is shown in Figure 5.39.
In practice, the working set of program varies slowly with respect to time. There-fore, the
working set of a program can be predicated ahead of time.

EXAMPLE

Consider the following screen of page requests.


2, 3, 2, 4, 6, 2, 5, 6, 1, 4, 6
Determine the hit ratio it for this stream using the FIFO replacement policy. Assume the main memory
can hold 3 page frames and initially all of them are vacant.

SOLUTION
The hit ratio computation for this situation is illustrated in Figure 5.42.

From Figure 5.42, it can be seen that the first two page references cause page faults. However.
There is a hit with the third reference because the required page (page 2) is already in the main
memory. After the first four references, all main memory frames are completely used. In the fifth
reference, page 6 is required. Since this page is not in the main memory, a page fault occurs.
Therefore, page 6 is fetched from the secondary memory. Since there are no vacant frames in the
main memory, the oldest of the current main memory pages is selected for replacement. Page 6 is
loaded in this position. All other data tabulated in this figure are obtained in the same manner. Since
9 out of 11 references generate a page fault, the hit ratio is 2/11.

The primary advantage of the FIFO algorithm is its simplicity. This algorithm can be implemented
by using a FIFO queue. FIFO policy gives the best result when page references are made in a
strictly sequential order. However, this algorithm fails if a program. loop needs a variable
introduced at the beginning. Another difficulty with the FIFO algorithm is it may give anomalous
results.
Intuitively, one may feel that an increase in the number of page frames will also increase the
hit ratio. However, with FIFO, it is possible that when the page frames are increased, there is a drop
in the hit ratio. Consider the following stream of requests:

1, 2, 3, 4, 5, 1, 2, 5, 1, 2, 3, 4, 5, 6, 5

Assume the main memory has 4 page frames; then using the FIFO policy there is a hit ration
of 4/15. However, if the entire computation is repeated using 5 page frames, there is a hit ratio of 3/15.
This computation is left as an exercise. This surprising result was first observed by L: A. Belady in
1969[3], and for this reason is often referred to as Belady;s anomaly.
Another replacement algorithm of theoretical interest is the optimal replacement policy. This
idea is due to L. A. Belady:
When there is a need to replace a page, choose that page which may not be needed again
for the longest period of time in the future.

The following numerical example explains this concept.

EXAMPLE 5.11

Using the optimal replacement policy. calculate the hit ratio for the stream of page references
specified in Example. Assume the main memory has three frames and initially all of them are vacant.

SOLUTION

The hit ratio computation for this problem is shown in Figure 5.43.

From Figure 5.43 it can be seen that the first two page references generate page faults. There
is a hit with the sixth page reference, because the required page (page 2) is found in the main memory.
Consider the fifth page reference. In this case, page 6 is required. Since this page is not in the main
memory, it is fetched from the secondary memory. Now, there are no vacant page frames. This means
that one of the current pages in the main memory has to be selected for replacement. Choose page 3
for replacement because this page is not used for the longest period of time. Page 6 is loaded into this
position. Following the same procedure, other entries of this figure can be determined. Since 6 out of
11 page references generate a page fault, the hit ratio is 5/11.

The decision made by the optimal replacement policy is optimal because it makes a decision based
on the future evolution. It has been proven that this technique does not give any anamolous results
when the number of page frames is increased. However, it is not possible to implement this technique
because it is impossible to predict the page references well ahead of time. Despite this disadvantage,
this procedure is used as a standard to determine the efficiency of a new replacement algorithm. Since
the optimal replacement policy is practically unfeasible, some method that approximates the behavior
of this policy is desirable. One such approximation is the least recently used (LRU) policy.
According to the LRU policy, the page that is selected for replacement is that page that has
not been referenced for the longest period of time. To see how this idea works, examine Example
5.12.

EXAMPLE 5.12
Solve Example 5.11 using the LRU policy.

SOLUTION
The hit ratio computation for this problem is shown in Figure 5.44.
In the figure we again notice that the first two references generate a page fault, whereas the
third reference is a hit because the required page is already in the main memory. Now, consider what
happens when the fifth reference is made. This reference requires page 6, which is not in the memory.
Also, we need to replace one of the current pages in the main memory because all frames are
felled. According to the LRU policy, among pages 2,3, and 4, page 3 is the page that is least
recently referenced. Thus we replace this page with page 6. Following the same reasoning the other
entries of Figure 6.44 can be determined. Note that 7 out of 11 references generate a page fault;
therefore, the hit ratio is 4/11. From the results of the example, we observe that the performance of the
LRU policy is very close to that of the optimal replacement policy. Also, the LRU obtains better
result than the FIFO because it tries to retain the are used recently.
Now, let us summarize some important features of the LRU algorithm.

• In principle, the LRU algorithm is similar to the optimal replacement policy except that it looks
backward on the time axis. Note that the optimal replacement policy works forward on the time
axis.
• If the request stream is first reversed and then the LRU policy is applied to it. The result
obtained is equivalent to the one that is obtained by the direct application of the optimal
replacement policy to the original request stream.
• It has been proven that the LRU algorithm is a stack algorithm. A page-replacement algorithm is
said to be a stack algorithm if the following condition holds:
Pt (n)  Pt (n + 1)

In the preceding relation the quantity Pt(i) refers to the set of pages in the main memory whose total
capacity is I frames at some time t. This relation is called the inclusion property. One can easily
demonstrate that FIFO replacement policy is not a stack algorithm. This task is left as an exercise.

• The LRU policy can be easily implemented using a stack. Typically, the page numbers of the
request stream are stored in this stack. Suppose that p is the page numbers being referenced. If p
is not in the stack, then p is pushed into the stack. However, if p is in the stack, p is removed
from the stack and placed on the top of the stack. The top of the stack always holds the most
recently referenced page number, and the bottom of the stack always holds the least-recent page
number. To see this clearly, consider Figure 5.45. in which a stream of page references and the
corresponding stack instants are shown. The principal advantage of this approach is that there is
no need to search for the page to be replaced because it is always the bottom most element of
the stack. This approach can be implemented using either software or macrocodes. However,
this method takes more time when a page number is moved from the middle of the stack.
• Alternatively, the LRU policy can be implemented by adding an age register to each entry of the
page table and a virtual clock to the CPU. The virtual clock is organized so that it is incremented
after each memory reference. When a page is referenced, its age register is loaded with the
contents of the virtual clock. The age register of page holds the time at which that page was
most recently referenced. The least-recent page is that page whose age register value is
minimum. This approach requires and operating system to perform time-consuming
housekeeping operations. Thus the performance of the system may be degraded.
• To implement these methods, the computer system must provide adequate hardware support.
Incrementing the virtual clock using software takes more time. Thus the operating speed of the
entire systems that do not provide enough hardware support. To get around this problem, some
replacement policy is employed that will approximate the LRU policy.
• The LRU policy can be approximated by adding an extra bit called an activity bit to each entry
of the page table. Initially all activity bits are cleared to 0. When a page is referenced, its activity
bit is set to 1. Thus this bit tells whether or not the page is used. Any pager whose activity bit is
0 may be a candidate for replacement. However, the activity bit cannot determine how many
times a page has been referenced.
• More information can be obtained by adding a register to each page table entry. To illustrate this
concept, assume a 16-bit register has been added to each entry of the page table. Assume that
the operating system is allowed to shift the contents of all the registers 1 bit to the right at
regular intervals. With one right shift, the most-significant bit position becomes vacant. If it is
assumed that the activity bit is used to fill this vacant position, some meaningful conclusions
can be derived. For example, if the content of a page register is 000016, then it can be concluded
that this page was not in use during the last 16 time-interval periods. Similarly, a value FFFF16
for page register indicates that the page should have been referenced at least once in the last 16
time-interval periods. If the content of a page register is FFOO16 and the content of another one
is OOFO16, the former was used more recently.
• If the content of a page register is interpreted as an integer number, then the least-recent page
has a minimum page register value and can be replaced. If two page registers hold the minimum
value, then either of the pages can be evicted, kor one of them can be chosen on a FIFO basis.
• The lager the size of the page register, the more time is spent by the operating system in the
update operations. When the size of the page register is 0, the history of the system can only be
obtained via the activity bits. If the proposed replacement procedure is applied on the activity
bits alone, the result is known as the second-chance replacement policy.
• Another bit called a dirty bit may be appended to each entry of the page table. This bit is
initially cleared to 0 and set ot 1 when a page is modified.
• This bit can be used in two different ways:
• The idea of dirty bit reduces the swapping overhead because when the dirty bit of a page to
be replaced is zero, there is no need to copy this page into the secondary memory, and it can
be overwritten by an incoming page. A dirty bit can be used in conjunction with any
replacement algorithm.
• A priority scheme can be set up for replacement using the values of the dirty and activity
bits, as described next.

DISK MANAGEMENT

 When ever a process needs I/O to or from the disk, it issues a system call to the
Operating System.

The request specifies the following necessary information :


1. Is this an input or output operation?
2. The disk address (drive, cylinder, surface, block)
3. The memory address
4. The amount of information to be transferred ( byte or word count)

• If the desired disk drive and controller is available, the request can be serviced
immediately.

• However, while the drive or controller is serving one request, any additional
requests, will need to be queued.

• For Multi Programming system with many processes, the disk queue may often
be non-empty.

• Thus when a request is complete OS must pick a new request from the queue
and service it.

• A Disk service requires that the head be moved to the desired track, then a wait
for latency, and finally the transfer of data.

Disk Scheduling Strategies :

1. First Come First Served Scheduling (FCFS):

• The algorithm is easy to program and fair.


• May not provide best (average) service.

Sequence : 53, 98, 183, 37, 122, 14, 124, 65 and 67

Total head movement of 640 tracks.

2. Shortest Seek Time First ( SSTF) :

➢ SSTF selects the request with the minimum seek time from the current head
position.
Start 53 , 65, 37, 14, 98, , 122, 124 and finally 183.

Total Head movement = 236

3. SCAN (ELEVATOR) Scheduling :

• Recognition of the dynamic nature of the request queue leads to the SCAN
algorithm.
• The read/write head starts at one end of the disk, and moves toward the other
end, servicing requests as it reaches each track, until it gets to the other end of
the disk.

• At other end, the direction of the head movement is reversed and servicing
continues.

• The head continuously scans the disk from end to end.

Need to know the direction of the head movement. ( moving towards 0


or towards 190)

If a request arrives in the Q just in front of head, it will be serviced


immediately. If just behind the head then complete wait.
4. Circular Scan ( C-SCAN) Scheduling :

• C-SCAN is a variant of SCAN designed to provide a more uniform wait time.


• As with SCAN it also moves the head from one end of the disk to the other,
servicing requests as it goes.

• When it reaches the other end, however it immediately returns to the beginning
of the disk, without servicing any requests on the return trip.

• C-SCAN essentially treats the disk as if it were circular, with the last track
adjacent to the first one.

LOOK (SCAN intelligently) :

1. LOOK
2. C-LOOK
• Both SCAN and C-SCAN always move the head from one end of the disk
to the other.

• More commonly, the head is only moved as far as the last request in each
direction.
• As soon as there are no requests in the current direction, the head movement
is reversed.
Chapter 5

CPU Scheduling

Q. 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 possible different
schedules are there? Give a formula in terms of n.

Ans. Since n processes have to be scheduled these processes can be arranged or scheduled
in n! ways.(using permutation).
Hence the number of possible different schedules are=
n.(n-1).(n-2)…1

Q. Define the difference between preemptive and nonpreemptive scheduling.State why


strict nonpreemptive scheduling is unlikely to be used in a computer center.

Ans. Nonpreemptive scheduling


Under nonpreemptive scheduling, once the CPU has been allocated to a process, the process
keeps the CPU until it releases the CPU either by terminating or switching to the waiting
state.

Preemptive Scheduling
A scheduling discipline is preemptive if the CPU can be taken away from the process

Nonpreemptive scheduling is unlikely to be used in a computer center because in a


computer center jobs with different priorities arrive at different times and is necessary to
service the higher priority job first.

Q. Consider the following set of processes, with th elength of the CPU burst time given in
milliseconds:

Process Burst Time Priority

P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2

The processes are assumed to have arrived in the order P1,P2,P3,P4,P5 all at time 0.

(a) What is the turn around time for each process for FCFS, SJF, A nonpreemptive
priority and RR.

(b) What is the waiting time of each process for the above algorithms?

(c) Which of the schedule results in minimal average waiting time?

Ans.
(A)
Turn around times for different processes are:

FCFS SJF Nonpremptive priority RR

P1=10 P1=19 P1=16 P1=19


P2=11 P2=1 P2=1 P2=2
P3=13 P3=4 P3=18 P3=7
P4=14 P4=2 P4=19 P4=4
P5=19 P5=9 P5=6 P5=14

(B)
Waiting time for different processes are:

FCFS
P1=0, P2=10, P3=11,P4=13,P5=14

SJF
P1=9,P2=0,P3=2,P4=1,P5=4

Nonpreemptive priority
P1=6,P2=0,P3=16,P4=18,P5=1

RR

P1=11,P2=1,P3=3,P4=3,P5=4
(c) Average waiting time

FCFS= 9.6
SJF = 5.2
NPP =14.2
RR = 4.4

Q. Suppose that the following processes arrive for execution at the times indicated

Process Arrival Time Burst Time


P1 0.0 8
P2 0.4 4
P3 1.0 1

(a) What is the average turn around time for these processes with FCFS?
(b) What is the average turn around time for these processe with SJF?

Ans.

(a)
turn around time for
P1=8-0=8
P2=12-0.4=11.6
P3=13-1=12
Average tat= 10.53

(b)
same as above

CHAPTER 6

PROCESS SYNCHRONIZATION
Q. Continuosly testing a variable waiting for some value to appear is called busy waiting. It should
be avoided because it wastes CPU time. Other type of wait can be wait for input-output and
waiting for interrupt.

Yes, busy waiting can be avoided by making the process blocked until the variable gets the specified
value.

Q. If Pi is in critical section and Pk has already chosen its number then the number of Pk will be

number[k]=max(A0,………………,Ai,……….,An-1)+1;

Because Pi is already is in critical section so number of Pi and Pk may be same if both had
simultaneous access to code.Now if both have the same number,then the name of Pk must be greater
than Pi because then only the condition of mutual exclusion can be satisfied.

Q. This algorithm satisfies all the requirements of the critical section in the following manner-

Mutual exclusion :Here mutual exclusion is ensured by variable turn.Even if the flag of both
the process are true, then the turn variable decides who goes in critical section.

Progress :Since every process first gives other process a chance by making itself false after waiting
for some time.

Bounded wait : Bounded wait is ensured by making the process itself false.

Q. The wait signal is given by

Po while S<=0 do no-op;

P1 S=S-1;

The signal operation is given by

P2 S:=S+1;
If both the operation are not atomic then it is possible that in the middle of one updation by one
process ,can go undone if it is interrupted for updation by another process. This may lead to
inconsistent state of shared variable.

CHAPTER 7

DEADLOCK

Ans. Examples of deadlock that are not related to computer system environment are as follows:

a.Traffic jam on a bridge.

b.If you are afresh passsout, seeking for a job and a job is available but requires
experience,then neither of them get satisfied.

c.In a hall when mob from both the sides wants to use the gate (i.e.exit and entrance is
same) then neither of them can get in or get out.

Ans .No, since there are no other process to share the resources.

Ans. A.In this problem the four crossing can be considered as the shared variable. The
four conditions for deadlock indeed hold for the system in the following manner:

a.Mutual Exclusion: Here a car, which depicts the condition of mutual exclusion, holds
each crossing.
b. Hold and Wait: Since the car on each cross point is holding the space and waiting for
another car to move.

c.No pre-emption: No car can be moved out of the cross point when it is in the middle.

d.Circular Wait: This condition is depicted in the figure itself. Each car at the junction is
waiting other to move.

Ans. B. A deadlock in the system can be avoided if a car arriving at a particular cross-
point, checks that no other vehicle is arriving at that point before moving across.

OR

Allot a certain amount of time for the traffics in parallel direction to move. Then allow
the traffic perpendicular to above and parallel to each other to move for the same amount
of time.

Ans. An unsafe state is the state that does not give any guarantee that the process will
finish. The following is the demonstration that the state B is not safe

Has Max Has Max

A 3 9 4 9

B 2 4 2 4

C 2 7 2 7

It is worth noting that an unsafe state is not a deadlock state. Starting at the second figure
configuration the system can run for a while. In fact one process can even complete.
Furthermore it is possible that A might release a resource before asking for anymore,
allowing C to complete and avoiding deadlock together.
Ans. a.Increase Available : This change can be made safely.

b. Decrease Available: This change may lead to an unsafe state.

c.Increase Max for one process: This change may lead to an unsafe state.

d. Decrease Max for one process: This change is safe.

e. Increase the number of process: This change may lead to an deadlock.

f.Decrease the number of process:This change is safe.

Ans. The following is the proof:

m=number of resources avaible

n=number of process

Since initially the CPU has to check all the processes atleast once for their needs, in worst
case it leads to n search operations and if it finds out the process, it makes m transactions

=>n X m

Now it has to go through the processes once again to satisfy rest of the processes that
makes

=>n X n X m

Ans. The processes are as given below:

Need Available Allocated

A 2 4 1

B 2 1
C 2 1

In the worst case each process gets 1 resource as shown above and one left over can be
given to any one of the processes and thus can be completed.

Ans. a.The arguments for installing the deadlock avoidance algorithm are-It is cost-
effective because it saves money that is spend on deadlock recovery and more jobs can
be done in the 30% idle time.

b.The arguments against installing the deadlock avoidance algorithm are-More


turnaround time and more execution time is required.

Ans. a.The contents of matrix need are

NEED
A B C D

P0 0 0 0 0

P1 0 7 5 0

P2 10 0 2

P3 0 0 2 0
P4 0 6 4 2

b.The sequence for safe state can be shown as:

P0->P2->P3->P1->P4

c.Yes , the request can be granted because available is greater than need 0 4 2 0.
CHAPTER 8

MEMORY MANAGEMENT

Q. Explain the difference between logical & physical addresses.

Ans. An address generated by the CPU is commonly referred to as logical address. An


address seen by the memory unit is commonly referred to as physical address.

Q. Explain the difference between external & internal fragmentation.

Ans. External fragmentation exists when enough total memory space exists to satisfy a
request, but it is not contiguous. If the allocated memory is slightly larger than the requested
memory as the overhead to keep the track of a hole of the size equal to the difference
between the two is larger than the hole itself. This difference between the allocated & the
requested memory is internal fragmentation.

Q. Explain the following allocation algorithms:


a. First-fit
b. Best-fit
c. Worst-fit

Ans. The most common strategies used to select a free hole from the set of available holes.
a. First-fit: Allocate the first hole that is big enough. Searching can start at the either
end of the set of holes.
b. Best-fit: Allocate the smallest hole that is big enough. We must search the entire list,
unless the list is sorted by size.
c. Worst-fit: Allocate the largest hole .We must search the entire list, unless the list is
sorted by size.

Q. When a process rolled out of memory, it loses its ability to use the CPU. Describe another
situation where a process loses its ability to use the CPU, but where the process does not get
rolled out.

Ans. If a process enters busy wait state it loses its ability to use the CPU, but does not get
rolled out.
Q. Given memory partitions of 100K,500K,200K,300K,600K(in order),how would each of the
First-fit,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?

Ans. The Best-fit algorithm makes the most efficient use of memory.The processes are
assigned as
212 K in 300K, 417K in 500K, 112K in 200K, 426K in 600K

Q. Consider a system where a program can be separated into two parts: code & data .The
CPU knows whether it wants an instruction or data. Therefore, two base-limit register pairs
are provided : one for instruction & one for data. The instruction base-limit register pair is
automatically read-only, so programs can be shared among different users. Discuss the
advantages & disadvantages of this scheme.

Ans. The instructions can be shared among different users & only one copy of the instruction
is needed in the physical memory. Each user’s page table maps onto the same physical copy.
Each user has its own copy of data .thus a lot of physical memory is saved. The
disadvantage of such a scheme is that the use inverted page tables have difficulty
implementing shared memory.

Q.Why are page sizes always powers of 2?

Ans. The selection of a power of 2 as a page size makes translation of a logical address into
a page number & page offset particularly easy.If the size of logical address space is 2 m ,& a
page size is 2n addressing units,then the high order m-n bits of a logical address designate
the page number,& the n low-order bits designate the page offset.

Q. 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 logical address?
b. How many bits are there in the physical address?

Ans.
a. The logical address is of 13 bits 3 to represent the 8 page frames & 10 to locate the
1024 words in a page.
b. The physical address is of 15 bits, 5 to represent the 32 page frames & 10 for the
displacement within the page(1024).

Q. Consider a paging system with page table stored in the memory.


a. If memory reference takes 200ns, how long does a paged memory reference
take?

Ans.a. A paged memory reference involves two references to memory per logical address : first is
the memory reference to page table which takes 200ns & when the frame no. is obtained it
can be used to access the required byte in the memory which takes another 200ns. Hence a
total of 400ns is taken.

b. If we add associative registers and 75% of all page table references are found in
the associative registers, what is the effective memory reference time?

Ans. b. If entries are in page table entry, memory reference time


is 200ns,else two memory references are required leading to a time
of 400ns.

Effective access time = 0.75 * 200 + 0.25 * 400


= 250ns

Q. Why are segmentation & paging sometimes combined into one?

Ans. Paging & segmentation are combined into one to improve upon the shortcomings of both
models. Paging eliminates external fragmentation which is a serious problem in
segmentation and paging combined with segmentation makes allocation problem trivial
and also reduces internal fragmentation caused in paging due to the variable sized
partitions available in segmentation.

Q.Describe a mechanism by which one segment could belong to the address space
of two different processes?

Ans. Since there is only one physical copy of the segment, it must refer to itself in the same way
for the address space of two different processes it must have a unique segment number.

Q. Consider the following segment table:


Segment Base Length
0 219 600
1 2300 14
2 90 100
3 1327 580
4 1952 96
What are the physical addresses for the following logical addresses?

Ans.
a) 0,430
Segment Base = 219
Offset = 430
Physical address = 219+430
= 649
b) 1,10
Segment Base = 2300
Offset = 10
Physical address = 2300+10
= 2310

c) 2,500
Segment Base = 90
Offset = 500
Physical address = Trap as offset exceeds length of segment

d) 3,400
Segment Base = 1327
Offset = 400
Physical address = 1327+400
= 1727

e) 4,112
Segment Base = 1952
Offset = 112
Physical address = Trap as offset exceeds length of segment

Q. Consider the Intel address translation scheme:Describe all the steps that are taken
by the Intel 80386 in translating a logical address into a physical address.

Ans. The 80386 uses segmentation with paging for memory management.
The maximum number of segments per process is 16K & each segment can be as large
as 4GB. The page size is 4KB. The logical address is a pair of (selector, offset) where the
selector is a 16-bit number. Offset is a 32-bit no.The m/c has 6 segment registers,allowing 6
segments to be addressed at any time by a process. It has 8-byte microprogram registers to
hold the corresponding the descriptors from either the LDT ot GDT. This cache lets 386
avoid having to read the descriptor from memory for every memory reference.

Q. In the IBM/370, memory protection is provided through the use of keys. A key is a 4-bit
quantity. Each 2K block of memory has a key associated with it. A store operation is
allowed only if both keys are equal, or if either is zero. Which of the following memory
management techniques could be used successfully with this hardware?
a) Bare machime
b) Single user system
c) Multiprogramming with a variable no. of processes.
d) Multiprogramming with a fixed no. of processes.
e) Paging
f) Segmentation

Ans. No. of blocks = 24


Hence, multiprogramming with a fixed no. of processes & paging could be used
with this hardware successfully.

CHAPTER 1

1) Three main purposes of an operating system are :


a) Convenience for the user,
b) Efficient operation of computer system and
c) Facilitate the use of hardware.

2) Four steps that are necessary to run a program on a completely


dedicated machine are :
a) Compilation of program,
b) Linking,
c) Loading and
d) Execution.

3) Spooling has a direct beneficial effect on the performance of the


system. For the cost of some disk space and few tables, the
computation of one job can overlap with the I/O of other job. Thus
spooling can keep both the CPU and I/O devices utilized at much
higher rate.

5) The advantage of multiprogramming is the full utilization of CPU.


The CPU does not sit idle in a multiprogramming environment.

6) Some of the design decisions that are made in operating system


mainframes may be appropriate for smaller systems. E.g. file
protection may not seem necessary on a personal machine. Secondly
decrease in the cost of computer hardware from mainframe to
personal computer system.

7) a) Batch: - A pool of job is kept on the disk. This job pool allows
the operating system to select which job to run next to increase CPU
utilization.
b) Interactive: - Interactive computer systems provide on-line
communication between the user and the system. The user gives instructions
to the O.S. and receives an appropriate response.

c) Time Sharing: - These systems were developed to provide interactive


use of computer systems at a reasonable cost.

d) Real Time: - A real time system is used when there are rigid time
requirements on the operation of a processor or the flow of data.

e) Distributed :- In this system there are a number of processors. Each


processor has its own local memory. The processors communicate with
one another through communication lines.

8) When by wasting the resources we can get better CPU utilization,


cost effectiveness, fast processing then that type of system is not
wasteful. Because on the expense of resources we can get these
things.

9) When a single user has a lot of processes to execute at the same time
then it is desirable to use real time system rather than a personal
computer for a single user.

10) In symmetric multiprogramming each processor runs an


identical copy of the O.S. and these copies communicate with one
another as needed. While in asymmetric multiprogramming each
processor is assigned a specific job. Master processor controls the
system.
Advantages: - 1) Performance increases.
2) Reliability
3) Cost effectiveness
Disadvantages:- Complex System
11) There are a variety of ways for which distributed systems are
desirable: -
a) resource sharing
b) computation speed up
c) reliability
d) communication

Chapter 2

CHAPTER 3:

Ans 3.1 The five major activities of an operating system regarding process management
are:-
i) The creation and deletion of both user and system processes.
ii) The suspension and resumption of processes.
iii) The provision of mechanism for process synchronization.
iv) The provision of mechanism for process communication.
v) The provision of mechanism for deadlock handling.

Ans 3.2 Three major activities of an operating system regarding to memory


management are:-
i) Keep track of which part of memory are currently being used and by whom.
ii) Decide which process are to be loaded into memory when memory space becomes
available.
iii) Allocates and deallocates memory space as needed.

Ans 3.3 Three major activities of an operating system regarding to secondary storage
are:-

i) Free-space management.
ii) Storage allocation.
iii) Disk scheduling.

Ans 3.4 The five major activities of an operating system regarding to file management
are :-

i) The creation and deletion of files.


ii) The creation and deletion of directories.
iii) The support of primitives for manipulating files and directories.
iv) The mapping of files into secondary storage.
v) The backup of file on stable (non-volatile) storage media.

Ans 3.5 Command Interpreter:- Command interpreter is an interface


between the user and the operating system. It is program that takes the
command from the user and executes it.

Ans 3.6 Five services provided by operating system are


i) Program Execution:- The system must be able to load a program into memory and run
it and program must be able to somehow complete execution.
ii) I/O Operations :- If a running process requires some I/O operation the the operating
system provide some means to complete it’s I/O operation.
iii) File System Manipulation :- A program should be able to read and write file and can create
and delete files by name.
iv) Communication :- There are many circumstances when two processes need to
communicate. They can communicate by using shared memory or message passing.
v) Error Detection :- The operating system must be aware of possible error, viz. error in the
CPU and the memory hardware, in I/O devices, power failure etc.
Ans 3.7 System Call :- System call provide an interface between the
process and the operating system. System call provide special facilities
to the processes so that they can issue commands to operating, and OS
to carries out that command. System calls can be divided into following
categories :
i) Process control.
ii) File manipulation.
iii) Device manipulation
iv) Information maintenance
v) Communication

Ans 3.8 System program:- System program provides a more convenient


environment for program development and execution.

Ans 3.9 The main advantage of layered approach is modularity. The layers are selected such that
each uses the function and services of only the layer below. This approach simplifies
debugging and system verification. Each layer is implemented using only there operations
provided by lower level layer. A layer need not to worry how there functions are carried
out.

Chapter 4 Processes

Q1. The major complication than concurrent programming adds to an operating system is
synchronization among processes, protection and firmer compartmentalization.

Q2. The long-term scheduler selects processes from the pool (a mass storage device where they are
kept for later execution) and them into the memory for the execution.
The short-term scheduler selects the process from the memory that is ready to execute and
allocates the CPU to one of them.
The key idea behind the medium term scheduler is that sometimes it can be advantages to
remove processes from memory and thus o reduce the degree of multiprogramming.

Q4. The two advantages of threads of multiprocessors are:


1. With multiple processes, each process has its own program counter, registers and address
space. This is useful when the jobs performed are unrelated. It is more efficient on process
having multiple threads serve the same purpose.
2. A multiple thread process uses fewer resources than multiple redundant processes.
The major disadvantage of thread is that all thread can access every address in the task. A
thread can be read or write over any other thread’s stack. It does not provide protections
between threads.

Q5. The resource used by the process that has created the thread or being used by a thread. It differs
from the resources given to a process as a process can either use the parent resource or it can take it
directly from the operating system.

Q6. a. Context switch among threads: It requires a register set switch, no overhead is
required. No separate address space is required, no memory management work to be done. Thus
blocking a thread and witching to another thread is efficient.

b. Context switch among processes: It requires saving the state of old process and
loading the saved state for the new process. It is pure overhead. Its speed varies from system to
system.

Q7. Kernel level threads are implemented by a system calls whereas a user level threads are
implemented in user level libraries.
The user level threads are better in thread switching, it does not need to call operating
system, and to cause an interrupt to kernel, switching is done very quickly.
The user level thread has disadvantage if the kernel is single threaded, then any user level
thread execute a system call will cause the entire task to wait until the call returns.

Q8. To utilize all n buffers we use an counter initialize to 0.

var counter: int


var n;
type item =…;
var buffer : array[0..n-1] of item;
in , out : 0..n-1;
Procedure:
repeat
…….
Produce an item in nextp;
…….
while counter=n do no op;
buffer[in]=nextp;
in=in +1 mod n;
counter =counter +1;
until false;

Consumer:
repeat
while counter =0 do no op;
nextc=buffer[out];
out =out +1 mod n;
counter = counter-1;
…….
Consume item in nextc;
…….
until false;

CHAPTER 5 CPU SCHEDULING


5.1 Total no. Of process to be scheduled on single processor=n
Total no. ways to schedule all the process=n!

5.2 Under non-preemptive scheduling, once the CPU has been allocated to a process, the process
keeps until it releases the CPU either by terminating or by switching to a waiting state. But in
the case of preemptive scheduling, CPU can be deallocated from a process to allocate CPU to
another high priority process even in the midst of execution of the first process.
Suppose in case of non-preemptive scheduling one big process is given the CPU
ahead of several shorter processes. So the average waiting time and turnaround
time of all other processes increases more than the turn around time for decreases
for the bigger process. So system throughput and efficiency degrades. So to
improve system efficiency, strictly non-preemptive scheduling is unlikely to be
used in a computer center.

5.3
Process Burst time Priority
P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2
FCFS : Gantt chart

P1 P2 P3 P4 P4
0 10 11 13 14 19
average turn around time=(10+11+13+14+19)/5=13.4 ms
average waiting time= (0+10+11+13+14)/5= 9.6 ms

SJF: Gantt chart


0 P2 P4 P3 P5 P1 1 2
4 9
19
average turn around time=(1+2+4+9+19)/5=7 ms
average waiting time= (0+1+2+4+9)/5= 3.1 ms
non preemptive priority: Gantt chart
P2 P5 P1 P3 P4
0 1 6 16 18 19

average turn around time=(1+6+16+18+19)/5=12 ms


average waiting time= (0+1+6+16+18)/5= 8.2 ms
SJF results in minimum average time.

Process Arrival time Burst time


P1 0.0 8
P2 0.4 4
P3 1.0 1
5.4
a) FCFS :
Gantt chart
P1 P2 P3
0 8 12 13
average turn around time=(8+12+13)/3=11 ms
b)
SJF:
Gantt chart
P1 P3 P2
0 8 9 13
average turn around time=(8+9+13)/3=10 ms
c)
SJF:
Gantt chart
P3 P2 P1
1 2 6 14
average turn around time=(2+6+14)/3=7.3 ms

5.5 a) If we have two pointers to the same process in the ready queue than, each process
will be given two times slots to complete for one time quantum each.
b) Advantage is that this will require low memory space for ready queue. Disadvantage
is that it will increase turn around time for larger jobs. Thus degrading system
efficiency.

5.6 Main advantage in having different time quantum sizes on different levels of
multilevel queuing system is that each queue gets a certain portion of the CPU time,
which it can schedule among various processes in its queue. Higher priority jobs will
get larger time slice while low priority processes will get low time slice so turn around
time for higher priority processes will decrease while it may increase for low priority
processes but overall system efficiency is improved.

5.7 a) in case of >>0, the priority of the running process decreases at a rate larger
than the rate at which priority of processes in ready queue increases, in this case
algorithm works as FCFS algorithm. The process which comes, will be served first.
b) In case of <<0, the priority of processes in ready queue increases at a rate higher
than the priority of running process decreases so this algorithm works as LIFO. The
jobs which comes last are served first and if there is a process that is running when new
process arrives the running process will be preempted and new process will get the
CPU.

5.8 a) Priority & SJF: Smallest job has highest priority in case of SJF scheduling.
b) Multilevel feedback queue & FCFS: all feedback queues are implemented in FCFS
order.
c) Priority & FCFS: first process has highest priority.
d) RR & SJF: the job that has the shortest burst time will complete its execution than
other.

5.9) The I/O bound program will have lower CPU burst time. So the I/O bound program
will be executed before any CPU bound program, because CPU bound program have
higher CPU burst.
This algorithm will not permanently starve the CPU bound program, because at one
time when all the I/O bound program will wait for the I/O and there is no I/O bound
program in the queue that is waiting for CPU, then all CPU bound program will
execute.

5.10 a) For FCFS: in FCFS if short programs are put at the end of the queue than the
average waiting time will increase, because shorter jobs have to wait longer.
b) Round-Robin: the process, which has the lowest CPU burst, will be executed first.
If quantum is small then average waiting time decreases.
c) Multilevel feedback Queue: in this every queue is ordered in FCFS order.

CHAPTER 6 Process Synchronization


Ans 6.1:- In busy waiting the Process continues to loop in the entry section, while waiting to
enter its critical section, while any process is executing in its critical section.
The other kinds of waiting are:
1) When a process is waiting in the ready queue to allocate to the CPU.
2) A process can wait for an interrupt or an I/O execution to be completed.
3) A process can wait for an child process to complete and return control to the parent
process.
No busy waiting cannot be eliminated altogether. At most we can remove busy waiting
from entry to the critical section of application program and limit it to the critical
sections of P and V statements. Even if we use other software soln like critical region
or monitors, they are also implemented using semaphores and thus there will be small
amount of busy waiting in them.

Ans 6.2:- The proof is as follows:


Tw1 - Pi reads choosing [k] for the last time for j=k in its first wait. So we have
choosing [k]= False at Tw1.
Tw2 - Pi begins its final execution for j=k of the second while loop. We have
Tw1<Tw2.
Tk1 - Pk enters the beginning of repeat loop.
Tk2 - Pk finishes calculating number [k].
Tk3 - Pk sets choosing [k] to false, we have Tk1<Tk2<Tk3
Since at Tw1 choosing [k]= False we have either:
Tw1<Tk1 or Tk3<Tw1
For the first case we have number [i]<number [k] hence
(number[i],i)<(number[k],k)
For the second case we have:
Tk2<Tk3<Tw1<Tw2. therefore Tk2<Tw2
This means that Pi has read the current value of number [k]. Moreover as Tw2 is the
moment at which the final execution of the second while for j=k takes place we have:
(number [i],i)<(number[k],k)
Hence the following property is proved.

Ans 6.3:- The proof is as follows:


1) Mutual Exclusion - Note that a process Pi can enter its critical section if flag[j] is
False or turn=i. Suppose both process are executing in their critical section then
flag[i]=flag[j]=True. This means that both cannot successfully execute their if
statement as turn can process which executes the if condition successfully will set its
flag to false and allowing other process to enter C.S. This condition will persist until
other process exits C.S and sets The turn so that the other can enter its C.S. Thus
Mutual Exclusion is preserved.
2) To show the progress we note that the process can be prevented from entering its
critical section only if it is stuck in the loop:
while turn=j do no-op
This loop is the only one. Suppose both processes set their flag simultaneously to True
then if turn =i then Pi will enter critical section and if turn=j then Pj will enter critical
section. If Pj is note ready to enter its C.S then flag[j]=False and Pi can enter its critical
section. However once Pi exits its critical section it will set the turn=j and flag[i]=False
allowing the Pj to enter its critical section as it do not change the value of turn while
executing while loop. Thus progress is proved.
3) From the above statements we see that if both process try to enter the C.S
simultaneously then only one process can enter and other will wait while executing
while turn=j do no-op. When the process exits form C.S it will set turn=i and
flag[j]=False thus allowing other to enter C.S. Thus Pi will be able to enter C.S after at
most one entry by Pj. Thus proving bounded waiting.
Ans 6.5:- The system clock calculate the time on the basis of the timer interrupts generated by
the clock If w disable the interrupts then the system clock will tend to slow down.
Having a separate hardware time-of-the-day clock that is independent of the operating
system could minimize such effects.

Ans 6.6:- the execution of wait and signal will be done not in a single step. It will be a
combination of step as shown below:
Wait(s) Signal(s)
S1: register1=s S4: register2=s
S2: register1=register1-1 S5: register2=register2+1
S3: s=register1 S6: s=register2
If wait and signal are not executed atomically, suppose two processes executed wait(s).
We take s=1. Since Wait is not atomic consider following interleave sequence of
execution:
T0: process1 execute S1 (s=1)
T1: process1 execute S2 (s=1)
T2: process2 execute S1 (s=1)
T3: process2 execute S2 (s=1)
T4: process1 execute S3 (s=0)
T5: process2 execute S3 (s=0)
As we can see both the process will see the value of s as 1 and assume that no other
process is executing in the C.S and enter their C.S. Thus Mutual Exclusion will be
violated.
If signal is not atomic then the value of s will be inconsistent. Since the semaphore is
used to implement M.E hence its inconsistent value is undesirable. Thus if both wait
and signal are not atomic then mutual exclusion may be violated.

Ans 6.7:- The soln. to the sleeping barber problem is:


We use a variable state, which denotes the state of barber. It can take two values: wake
and sleep. We also use three semaphores mutex, customer and barber. We keep the no.
of customer waiting to be processed in the chair variable. Initially state=sleep,
customer=1, barber=0, mutex=1,and chair=n
The customer process is as follows:
begin
wait(mutex)
if chair=0
then begin
signal(mutex)
exit
end
if chair=n and state=sleep
then barber=wake
else chair=chair-1
signal(mutex)
wait(customer)
signal(barber)
end
The barber process is as follows:
repeat
while state=sleep do no-op
wait(barber)
process the customer
wait(mutex)
if(chair=n)
then begin
barber=sleep
signal(customer)
signal(mutex)
end
else begin
chair=chair+1
signal(customer)
signal(mutex)
end
until False

Ans 6.8:- The soln to cigarette-smoker problem is as follows:


We use a semaphore agent to signal agent. Semaphore mutex is used to provide mutual
exclusion. We use semaphores S1 and S2 to signal the resources produced by agent.
var agent, mutex, S1,S2: semaphore
S1=0;
S2=0;
mutex=0;
agent=0;
The agent process is as follows:
repeat
signal(S1);
signal(S2);
signal(mutex);
wait(agent);
until False
The code for process Pi is as follows:
repeat
wait(mutex);
if(Pi needs S1) then begin
wait(S1);
if(Pi needs S2) then begin
wait(S2);
Pi consumes S1 and S2
signal(agent);
end
else begin
signal(S1);
signal(mutex);
end
end if
else signal(mutex);
end if
until False

CHAPTER 7

Q.1.) Three examples that aren't related to computer are-

a)Two trains come toward each other on same track.

b)Traffic system

c)Dining philospher problem

Q.2.) It isn't possible to have deadlock having only single process.Because by


considering single process the three conditions i.e. Mutual exclusion ,No preemption
& Hold & Wait conditions are satisfied but the fourth on i.e. Circular Wait isn't
satisfied because for Circular Wait at least two processes are required.

Q.3.) By considering the resource i.e. cpu time, memory & disk space deadlock will
never occur . Because memory & bdisk space are the sharable resources which
violates the mutual exclusion condition.

Q.4.)
a.)
(i) In this problem, the four roads crossing each other are the resources
which are nonsharable hence M.E. condition is satisfied.

(ii) Secondary Hold & Wait condition is satisfied because one vehicle if
covers the road, doesn't leave that untill it is given a track to proceed.

(iii) No preemption condition is also satisfied because once a road is


covered by a vehicle then it will not leave the road untill t is gven a full path.

(iv) Circular Wait condition is also satisfied .One vehicle is waiting for
other vehicle to leave the track which is also waiting.

(b.) To avoid deadlock in this system is to use traffic modulation using traffic
light.

Q.5.) If a process is in unsafe state, then this state will not necessarily lead to
deadlock state. Beacuse if a process shows it's max. need then it isn't necessary that it
will complete after it gets it's all or max. resource . the process may complete after
getting some or all of the resources.

Q.6.) If deadlock is considered by the Banker's algo. then to avod the deadlock, two
changes can be made safely.The two changes can increase available(new resurces
added) & decrease maximum for one process.

Q.7.) In the safety algo. , the second step loopes 'n' times for each 'i' ;then there are
n*n operations to check all the processes i.e. for i = 1,2,3,........... n ; i.e. the statement
finish(i) = false ; for all 'i' will have n*n opereations while for each 'i' the statement
need(i) <= work will have 'n' operations. Since there are n*n operations for finding
an 'i' & there are 'm' operations to find the resource restriction.Hence there will be
total m*n*n operations.

Q.8.) Given that there are four resources of the same type which are shared by three
processes.So this situation will be deadlock free, because in the worst case, if we
assign one resource to all the three processes then one resource is free.So if we assign
that resource to any process then that process will free all it's resource & then that
resource will become available for all other processes.

Q.13.)

ALLOCATION MAX. AVAILABLE

A B C D A B C D A B C D

P0 0 0 1 2 0 0 1 2 1 5 2 0

P1 1 0 0 0 1 7 5 0

P2 1 3 5 4 2 3 5 6

P3 0 6 3 2 0 6 5 2

P4 0 0 1 4 0 6 5 6

(a) Content of matrix Need :-

A B C D

P0 0 0 0 0

P1 0 7 5 0
P2 1 0 0 2

P3 0 0 2 0

P4 0 6 4 2

(b) The system is in a safe state, because after leaving the resource of p0,p2 &
p3 , the need for p1 & p4 will also be satisfied. Hence the won't be deadlocked & the
seq. that won't lead to deadlock is p0,p2,p3,p4,p1. Hence at present the system isn't
deadlocked.

(c) If a request from process p1 arrives for(0,4,2,0) then the request can be
immediately granted because it wont leave the system in a deadlocked state.

Chapter 9
Virtual Memory
Sol 1).
If the process tries to use a page that was not bought into memory, then access to that page marked
invalid causes a page fault trap. The procedure for handling this page fault is as follows:
a). We check an internal table (usually kept with PCB) for this process, to determine whether the
reference was valid or invalid memory access.
b). If the reference was invalid we terminate the process. If it was valid but we have not bought
in that page, we now page in that later.
c). We find a free frame.
d). We schedule a disk operation to read the desired page into the newly allocated frame.
e). When the disk read is complete, we modify the internal table kept with the process and the
page table to indicate that the page is now in memory.
f). We restart the instruction that was interrupted by the illegal address trap. The process can now
access the page as though it has always been in memory.

Sol 2).
A) Lower bound on the number of page faults = number of frames = m
B) Upper bound on the number of page faults = max length of reference string = p
Sol 3)
Given that virtual = 232 Byte
Given physical memory = 218 Byte
Page size = 4096 Byte
Then number of pages = 232/4096 = 220
Number of frames = 218/4096 = 64
Virtual 11123456 is equivalent to page number 2715 and offset 2816
This 2715th page will be mapped on frame number 15 (2715 Mod 64) with the offset 2816.

Sol 4)
a) Stack: This programming technique will be good enough for a demand paging. The page to
be replaced is kept on the top of the stack and is replaced when needed.
b) Hash Symbol Table:
c) Sequential Seach: This programming technique will be not good for a demand paging. The List
will be searched sequentially to find out the victim page it will take a lot of time.
d) Binary search: Better than sequential, as it needs less searching comparision than the sequential
search.
Sol 5)
Given that page fault service time if the replaced page is not modified = 8 msec
Page fault service time if the replaced page is modified =20 msec
Service time if empty page is available=8 msec

Probability of modified page= 0.70


Then page fault service time = (0.7 * 20 + 0.3 *8) msec =16.4 106 nsec
Suppose max acceptable page fault rate = p
Then
200 > (1-p)*100 + p*16400000
Hence p < 0.00000614

Sol 6)
The replacement policies from “BAD” to “PERFECT” according to their page fault rate are:
FIFO Replacement
LRU Replacement
Second chance algorithm
Optimum Replacement

Only FIFO Replacement get suffers from Belady’s anomaly.

Sol 7)
Benefits of virtual memory: The memory is seen much more greater than the actual main
memory to the programmer. More than one program can be run at the same time to increase CPU
utilization. Less Input/Output will be needed

Cost Paid: Extra Hardware will be needed. Complexity will increase. Substantially decrease
performance when it is used carelessly.

Sol 8)
a) Installing a faster CPU will not increase the CPU utilization. Because it takes the references
from the paging disk which is constant. Hence CPU utilization will decrease.
b) Installing a bigger paging disk will increase the CPU Utilization, as there will more processes
in the memory, which will increase degree of multiprogramming.
c) Degree of multiprogramming will increase the CPU utilization, as there will be more
processes running at the same time.
d) Increasing the page size will decrease CPU utilization, as there will be fewer pages hence
fewer processes in the memory at the same time. Decreasing degree of multiprogramming will
decrease CPU utilization.
e) Decrease in degree of multiprogramming will decrease the CPU utilization, as there will be
fewer processes running at the same time.
f) Installing a bigger main memory will increase the CPU Utilization, as there will more
processes in the memory, which will increase degree of multiprogramming.
g) Installing a faster harddisk will lessen the time required to bring the page into the memory
from the disk, hence CPU will have to wait less for desired page increasing CPU utilization.

Sol 10)
B) For the programming code
For j:= 1 to 100 do
For i:= to 100 do
A [j][i]: =0;

If we assume address technique Row major then the address A[1][1] will be 200, A[1][2] will
be 201, A[2][1] will be 300 and so A[100][100] will be 9999.
Given that page size is 200. So the addresses for the above code will be in sequence
200, 201, ……..300, 301………………..9999
The reference string for above addresses will be 2,3,4…51. All pages are distinct hence using
LRU there will be 51 page faults.

A) For the programming code


For i:= 1 to 100 do
For j:= to 100 do
A [j][i]: =0;
If we assume address technique Row major then the address A[1][1] will be 200, A[1][2] will
be 201, A[2][1] will be 300 and so A[100][100] will be 9999.
Given that page size is 200. So the addresses for the above code will be in sequence
200, 300, ……..9900, 201,301,………………..9901,202,302,……9902,….9999
The reference string for above addresses will be 2,3,4…51,2,3,4…51,2,3,4…51 and so on for
100 times. All pages are distinct hence using LRU for each value of I there will be 51 page
faults resulting in total 51 *100=5100 page faults.

Sol 11)
Reference String is 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6
 Algo.

Frame 1 2 3 4
Numbers→

LRU Replacement 20 18 15 10
FIFO Replacement 20 18 16 12
Optimal Replacement 20 15 11 8

SOLUTIONS TO CHAPTER 1 PROBLEMS

1. An operating system must provide the users with an extended (i .e., virtual) machine, and it
must manage the I/O devices and other system resources.
2. Multiprogramming is the rapid switching of the CPU between multiple processes in memory
.It is commonly used to keep the CPU busy while one or more processes are doing I/O.
3. Input spooling is the technique of reading in jobs, for example, from cards, onto the disk, so
that when the currently executing processes are finished, there will be work waiting for the
CPU. Output spooling consists of first copying printable files to disk before printing them,
rather than printing directly as the output is generated. Input spooling on a personal computer
is not very likely, but output spooling is.
4. The prime reason for multiprogramming is to give the CPU something to do while waiting
for I/O to complete .If there is no DMA, the CPU is fully occupied doing I/O, so there is
nothing to be gained (at least in terms of CPU utilization) by multiprogramming. No matter
how much I/O a program does, the CPU wi11 be 100 percent busy.
5. Second generation computers did not have the necessary hardware to protect the operating
system from malicious user programs.
6. Choices (a), (c), and (d) should be restricted to kernel mode.
7. Personal computer systems are always interactive, often with only a single user. Mainframe
systems nearly always emphasize batch or time-sharing with many users. Protection is much
more of an issue on mainframe systems, as is efficient use of all resources.
8. The file will be executed.
9. It is often essential to have someone who can do things that are normally forbidden. For
example, a user starts up a job that generates an infinite amount of output. The user then logs
out and goes on a three-week vacation to London. Sooner or later the disk will fill up, and the
super-user will have to manually kill the process and remove the output file. Many other such
examples exist.
10. Yes it can, especially if the system is a message passing system.
11. The process table is needed to store the state of a process that is currently suspended, either
ready or blocked. It is not needed in a single process system because the single process is
never suspended.
12. Block special files consist of numbered blocks, each of which can be read or written
independently of all the other ones. It is possible to seek to any block and start reading or
writing. This is not possible with character special files.
13. When the first DEL comes in, an interrupt is generated to the shell. The interrupt procedure
then re-enables the SIGINT signal. If a second DEL (SIG INT signal) comes in before the
shell has been able to run and re- enable the SIGINT signal, the system sees an interrupt that
has not been enabled for been caught and kills the shell.
14. The read works normally. User 2's directory entry contains the i-node of the file, so its
reference count is nonzero and the file is not removed when user 1 removes it. Only when
both of them have removed it does it actually vanish.
15. If an ordinary user could set the root directory anywhere in the tree, he could create a file
etclpasswd in his home directory, and then make that the root directory .He could then execute
some command, such as su or login that reads the password file, and trick the system into
using his password file, instead of the real one.
16. When a user program writes on a file, the data does not really go to the disk. It goes to the
buffer cache. The update program issues SYNC calls every 30 seconds to force the dirty
blocks in the cache onto the disk, in order to limit the potential damage that a system crash
could cause.
SOLUTIONS TO CHAPTER 2 PROBLEMS

1. You could have a register containing a pointer to the current process table entry. When I/O
completed, the CPU would store the current machine state in the current process table entry.
Then it would go to the interrupt vector for the interrupting device and fetch a pointer to
another process table entry (the service procedure). This process would then be started up.
2. A race condition is situations in which two (or more) process are about to, perform some
action. Depending on the exact timing, one or other goes first. If one of the processes goes
first, everything works, but if another one goes first, a fatal error occurs.
3. Yes, it still works, but it still is busy waiting, of course.
4. Yes it can. The memory word is used as a flag, with 0 meaning that no one is using the critical
variables and 1 meaning that someone is using them. Put a 1 in the register, and swap the
memory word and the register. If the register contains a 0 after the swap, access has been
granted. If it contains a 1, access has been denied. When a process is done, it stores a 0 in the
flag in memory.
5. To do a semaphore operation, the operating system first disables interrupts. Then it reads the
value of the semaphore. If it is doing a DOWN and the semaphore is equal to zero, it puts the
calling process on a list of blocked processes associated with the semaphore. If it is doing an
UP, it must check to see if any processes are blocked on the semaphore. If one or more
processes are blocked, one of then is removed from the list of blocked processes and made
runnable. When all these operations have been completed, interrupts can be enabled again.
6. Associated with each counting semaphore are two binary semaphores, M, used for mutual
exclusion, and B, used for blocking. Also associated with each counting semaphore is a
counter that holds the number of UPs minus the number of DOWNs, and a list of processes
blocked on that semaphore. To implement DOWN, a process first gains exclusive access to
the semaphores, counter, and list by doing a DOWN on M. It then decrements the counter. If
it is zero or more, it justs does an UP on M and exits. If M is negative, the process is put on
the list of blocked processes. Then an UP is done on M and a DOWN is done on B to block
the process. To implement UP, first M is DOWNed to get mutual exclusion, and then the
counter is incremented. If it is more than zero, no one was blocked, so all that needs to be
done is to UP M. If, however, the counter is now negative or zero, some process must be
removed from the list. Finally, an UP is done on B and M in that order.
7. With round robin scheduling it works. Sooner or later L will run, and eventually it will leave
its critical section. The point is, with priority scheduling, L never gets to run at all; with round
robin, it gets a normal time slice periodically, so it has the chance to leave its critical section.
8. It is very expensive to implement. Each time any variable that appears in a predicate on which
some process is waiting changes, the run-time system must re-evaluate the predicate to see if
the process can be unblocked. With the Hoare and Brinch Hansen monitors, processes can
only be awakened on a SIGNAL primitive.
9. The employees communicate by passing messages: orders, food, and bags in this case. In
MINIX terms, the four processes are connected by pipes.
10. It does not lead to race conditions (nothing is ever lost) , but it is effectively busy waiting.
11. If processes blocked by doing WAITs on condition variables without a linked list, then if
several UPs were done first, they would cause SIGNALs to be done and lost. Unlike
semaphores, the condition variables are not counters. They do not store SIGNALS for future
use.
12. If a philosopher blocks, neighbors can later see that he is hungry by checking his state, in test,
so he can be awakened when the forks are available.
13. The change would mean that after a philosopher stopped eating, neither of his neighbors could
be chosen next. With only two other philosophers, both of them neighbors, the system would
deadlock. With 100 philosophers, all that would happen would be a slight loss of parallelism.
14. Variation 1: readers have priority. No writer may start when a reader is active. When a new
reader appears, it may start immediately unless a writer is currently active. When a writer
finishes, if readers are waiting, they are all started, regardless of the presence of waiting
writers. Variation 2: Writers have priority. No reader may start when a writer is waiting. When
the last active process finishes, a writer is started, if there is one, otherwise, all the readers (if
any) are started. Variation 3: symmetric version. When a reader is active, new readers may
start immediately. When a writer finishes, a new writer has priority, if one is waiting. In other
words, once we have started reading, we keep reading until there are no readers left. Similarly,
once we have started writing, all pending writers are allowed to run.
15. It will need nT sec.
16. If a process occurs multiple times in the list, it will get multiple quanta per cycle. This
approach could be used to give more important processes a larger share of the CPU.
17. The CPU efficiency is the useful CPU time divided by the total CPU time. When Q ≥ T, the
basic cycle is for the process to run for T and undergo a process switch for S. Thus (a) and (b)
have an efficiency of T /(S + T). When the quantum is shorter than T, each run of T will
require T / Q process switches, wasting a time ST / Q. The efficiency here is then which
reduces to Q/(Q + S), which is the answer to (c). For (d), we just substitute Q for S and find
that the efficiency is 50 percent. Finally, for (e), as Q -+ 0 the efficiency goes to 0.
18. For round robin, during the first 10 minutes each job gets 115 of the CPU . At the end of 10
minutes, C finishes. During the next 8 minutes, each job gets of the CPU, after which time D
finishes. Then each of the three remaining jobs gets 1/3 of the CPU for 6 minutes, until B
finishes, and so on, The finishing times for the five jobs are 10, 18, 24, 28, and 30, for an
average of 22 minutes. For priority scheduling, B is run first. After 6 minutes it is finished.
The other jobs finish at 14, 24, 26, and 30, for an average of 18.8 minutes. If the jobs run in
the order A through E, they finish at 10, 16, 18, 22, and 30, for an average of 19.2 minutes.
Finally, shortest job first yields finishing times of 2, 6, 12, 20, and 30, for an average of 14
minutes.
19. The first time it gets 1 quantum. On succeeding runs it gets 2, 4, 8, and 15, so it must be
swapped in 5 times.
20. Shortest job first is the way to minimize average response time.
21. The sequence of predictions is 40, 30, 35, and now 25.
22. Two-level scheduling is needed when memory is too small to hold all the ready processes.
Some set of them is put into memory, and a choice is made from that set. From time to time,
the set of in-core processes is adjusted. This algorithm is easy to implement and reasonably
efficient, certainly a lot better than say, round robin without regard to whether a process was
in memory or not.
23. This pointer makes it easy to find the place to save the registers when a process switch is
needed, either due to a system call or an interrupt.
24. When a clock or keyboard interrupt occurs, and the task that should get the message is not
blocked, the system has to do something strange to avoid losing the interrupt. With buffered
messages this problem would not occur.
25. While the system is adjusting the scheduling queues, they can be in an inconsistent state for a
few instructions. It is essential that no interrupts occur during this short interval, to avoid
having the queues accessed by the interrupt handler while they are inconsistent. Disabling
interrupts prevents this problem.
26. When a RECEIVE is done, a source process is specified, telling who the receiving process is
interested in hearing from. The loop checks to see if that process is among the process that are
currently blocked trying to send to the receiving process. Each iteration of the loop examines
another blocked process to see who it is.
SOLUTIONS TO CHAPTER 3 PROBLEMS

1. In the figure, we see a controller with two devices. The reason that a single controller is
currently expected to handle multiple devices is to eliminate the need for having a controller
per device. If controllers become almost free, then it will be simpler just to build the controller
into the device itself. This design will also allow multiple transfers in parallel, and thus give
better performance.
2. Possibly. If most files are stored in logically consecutive sectors, it might be worthwhile
interleaving the sectors to give programs time to process the data just received, so that when
the next request was issued, the disk would be in the right place. Whether this is worth the
trouble depends strongly on the kind of programs run and how uniform their behavior is.
3. The rotation time is 200 msec. Reading all the sectors in order requires 1/2 rotation to get
sector 0 and 2.75 rotations to get the data (after sector 7 is read, the transfer is finished). Thus
3.25 rotations are needed, for 650 msec. Reading 4K in 650 msec is 6302 bytes/sec. For a no
interleaved disk, it takes 300 msec to read 4K, which is 13,653 bytes/sec. Interleaving reduces
the capacity to 6302/13653 or 0.46 of its former capacity .
4. Each line required 1200 x 7 = 8400 samples/sec. At 5.7 microsec per sample, each line took
up 47.88 msec of DM-11 time. Twenty lines would take up 957.6 msec of DM-11 time per
second of real time, so the. DM-11 could handle at most 20 lines, leaving it idle about 4 percent
of the time.
5. A packet must be copied four times during this process, which takes 4.1 msec. There are also
two interrupts, which account for 2 msec. Finally, the transmission time is 0.83 msec, for a
total of 6.93 msec per 1024 bytes. The maximum data rate is thus 147,763 bytes/sec, or about
12 percent of the nominal 10 megabit/sec network capacity. (If we include protocol over-
head, the figures get even worse.)
6. Device independence means that files and devices are accessed the same way, independent of
their physical nature. Systems that have one set of calls for writing on a file, but a different
set of calls for writing on the con- sole (terminal) do not exhibit device independence.
7. (a) Device driver.
(b) Device-independent software.
(c) Device driver.
(d) Device-independent software.
(e). User-level software.

8. If the printer were assigned as soon as the output appeared, a process could tie up the printer
by printing a few characters and then going to sleep for a week.
9. Neither changes leads to deadlock. There is no circular wait in either case.
10. If the system had two or more CPUs, two or more processes could run in parallel, leading to
diagonal trajectories.
11. A request from Suzanne is unsafe, but one from Marvin is safe.
12. No. D can still finish. When it finishes, it returns enough resources to allow E (or A) to finish,
and so on.
13. With three processes, each one can have two drives. With four processes, the distribution of
drives will be (2, 2, 1, 1), allowing the first two processes to finish. With five processes, the
distribution will be (2, 1, 1, 1, 1), which still allows the first one to finish. With six processes,
each holding one tape drive and wanting another, one we have a deadlock. Thus for n < 6 the
system is deadlock-free.
14. There are states that are neither safe nor deadlocked, but which lead to deadlocked states. As
an example, suppose we have four resources: tapes, plotters, printers, and punches, as in the
text, and three processes competing for them. We could have the following situation.
Has Needs Available
A: 2000 1020 01 2 1
B: l000 013 1
C: 012 1 1010
This state is not deadlocked because many actions can still occur, for example, A
can still get two printers. However, if each process asks for its remaining requirements,
we have a deadlock.
15. Yes. Suppose all the mailboxes are empty. Now A send& to B and waits for a reply, B sends
to C and waits for a reply, and C sends to A and waits for a reply. All the conditions for
deadlock are now fulfilled.
16. To avoid circular wait, number the resources (the accounts) with their account numbers. After
reading an input line, a process locks the lower- numbered account first, then when it gets the
lock (which may entail waiting), it locks the other one. Since no process ever waits for an
account lower than what it already has, there is never a circular wait, hence never a deadlock.
17. Comparing a row in the matrix to the vector of available resources takes m operations. This
step must be repeated on the order of n times to find a process that can finish and be marked
as done. Thus marking a process as done takes on the order or mn steps. Repeating the
algorithm for all n processes means that the number of steps is then mn2.
18. If both programs ask for Woofer first, the computers will starve with the endless sequence:
request Woofer, cancel request, request Woofer, cancel request, etc. If one of them asks for
the doghouse and the other asks for the dog, we have a deadlock, which is detected by both
parties and then broken, but it is just repeated on the next cycle. Either way, if both computers
have been programmed to go after the dog or the doghouse first, either starvation or deadlock
ensues. There is not really much difference between the two here. In most deadlock problems,
starvation does not seem serious because introducing random delays will usually make it very
unlikely. That approach does not work here.
19. The POSITION field is not need for character devices, since they are not randomly
addressable.
20. (a) 10 + 12 + 2 + 18 + 38 + 34 + 32 = 146 cylinders = 876msec.
(b) 0 + 2 + 12 + 4 + 4 + 36 +2 = 60 cylinders = 360 msec.
(c) 0 + 2 + 16 + 2 + 30 + 4 +4 = 58 cylinders = 348 msec.
21. Not necessarily. A UNIX program that reads 10,000 blocks issues the requests one at a time,
blocking after each one is issued until after it is completed. Thus the disk driver sees only one
request at a time; it has no opportunity to do anything but process them in the order of arrival.
Harry should have started up many processes at the same time to see if the elevator algorithm
worked.
22. It is like a subroutine (procedure) .Each time the user part invokes the kernel part, the kernel
starts out in the same place. It does not remember where it was last time.
23. Two msec 60 times a second is 120 msec/sec, or 12 percent of the CPU.
24. Tabs and line feeds on hard copy terminals may also need delays.
25. After a character is written to an RS232 terminal, it takes a (relatively) long time before it is
printed. Waiting would be wasteful, so interrupts are used. With memory mapped terminals,
the character is accepted instantly, so interrupts make no sense.
26. At 110 baud, we have 10 interrupts/sec, with is 40 msec or 4 percent of the CPU. No problem.
At 4800 baud, we have 480 interrupts/sec, which takes 1920 msec. In other words, it cannot
be done. The system can handle at most 250 interrupts/sec.
27. Scrolling the window requires copying 65 lines of 80 characters or 5200 characters. Copying
1 character (12 bytes) takes 6 microsec, so the whole window takes 31.2 msec. Writing 80
characters to the screen takes 4 msec, so scrolling and displaying a new line takes 35.2 msec
for 80 characters. This is only 7273 characters/sec, or 22.7 kilobaud, barely faster than 19.2
kilobaud. For color, everything takes four times as long, so we only get 5683 baud.
28. Escape characters make it possible to output backspaces and other characters that have special
meaning to the driver, such as cursor motion.
29. Suppose the user inadvertently asked the editor to print thousands of lines. Then he hits DEL
to stop it. If the driver did not discard output, output might continue for several seconds after
the DEL, which would make the user hit DEL again.
30. The microprocessor inside the terminal has to move all the characters up one line by just
copying them. Viewed from the inside, the terminal is memory mapped. There is no easy way
to avoid this organization unless special hardware is available.
31. The 25 lines of characters, each 8 pixels high, requires 200 scans to draw. There are 60 screens
a second, of 12,000 scans/sec. At 63.6 microsec/scan, the beam is moving horizontally 763
msec per second, leaving 237 msec for writing in the video RAM. Thus the video RAM is
only available 23.7 percent of the time .

SOLUTIONS TO CHAPTER 4 PROBLEMS

1. First fit takes 20K, 10K, 18K. Best fit takes 12K, 10K, and 9K. Worst fit takes 20K, 18K, and
15K. Next fit takes 20Kt 18Kt and 9K.
2. Originally we have 3-way multiprogramming. Adding another megabyte gives us 5-way
multiprogramming. The idle time decreases from 0.63 to 0.65, which also means that the
throughput goes from 0.79 to 0.92, an increase of 16 percent.
3. From the unused memory rule, the fraction of memory in holes is 0.4/2.4 or 1/6. Thus we have
167K in holes and 833K of data. It takes 416.5 msec to copy this, so 416.5/500 = 42 percent
of the CPU time is spent compacting.
4. The 5K split breaks the 256K buddy into pieces of size 8K, 8K, 16K, 32K, 64K, and 128K.
The first 8K piece is taken. Then the 32K and 64K pieces are taken. The 20K request requires
splitting up the 64K piece into two 32K buddies, one of which is taken. What is left over is
8K at address 8K, 16K at address 16K, 32K at address 96K, and 64K at address 128K.
5. The average segment size is 10K, and the average hole size is 5K. From the unused memory
rule, .we see that the fraction of memory in holes is 0.5/2.5 or 20 percent.
6. (a) 8112 (b) 4100 (c) 24684
7. They built an MMU and inserted it between the 8086 and the bus. Thus all 8086 physical,
addresses went into the MMU as virtual addresses. The MMU then mapped them onto
physical addresses, which went to the bus.
8. Three-bit process numbers means that the hardware supports 8 processes, each with an 8M
virtual address space. The total virtual address space is thus 64M. With 4K pages, we have a
total of 16,384 pages, thus 16K words for the page pointers (at 1 word/pointer). Doubling the
segment size just means that their will now be half as many segments, each twice as big. The
number of pages remains the same, since it just depends on the total virtual address space.
9. A page fault every k instructions adds an extra overhead of n/k microsec to the average, so the
average instruction takes 1 + n /k microsec.
10. The reference string is 1(I), 12(D); 2(I), 15(D); 2(I), l5(D); 10(I); 10(I); 15(D); 10(1).The code
(I) indicates an instruction reference, whereas (D) indicates a data reference. Semicolons give
the instruction boundaries.
11. The effective instruction time is 100h + 500(1 -h), where h is the hit rate. If we equate this
formula with 200 and solve for h we find that h must be at least 0.75.
12. NRU removes page 0. FIFO removes page 2. LRU removes page 1. Second chance removes
page 0.
13. The page frames for FIFO are as follows:

x0172333300
xx017222233
xxx01777722
xxxx0111177

The page frames for LRU are as follows:

x0172327103
xx017232710
xxx01773271
xxxx0111327

FIFO yields 6 page faults; LR U yields 7.

14. The counters are:


Page 0: 0110110
Page 1: 01001001
Page 2: 00110111
Page 3: 10001011
15. The seek plus rotational latency is 40 msec. For 2K pages, the transfer time is 1.25 msec, for
a total of 41.25 msec. Loading 32 of these page will take 1.32 sec. For 4K pages, the transfer
time is doubled to 2.5 msec, so the total time per page is 42.5. Loading 16 of these pages takes
0.68 sec.
16. The PDP-11 paging drum had the advantage of no rotational latency, This saved half a rotation
each time memory was written to the drum,
17. The text is eight pages, the data is five page, and the stack; is four page. The program does
not fit because it needs nine pages. With a 512-byte page, the situation is different. Here the
text is 64 pages the data is 33 pages, and the stack is 31 pages, for a total of 128 512-byte
pages. Which, fits. With the small page size it is ok, but not with the large one.
18. The program is getting 15,000 page faults, each of which 2 msec of extra processing time.
Together, the page fault overhead is 30 sec. This mean that of the 60 sec used, half was spent
on page fault overhead, and half on running the program. If we run the program with twice as
much memory we get half as memory page faults, and only 15 sec of page fault overhead so
the total run time will be 45 sec.
19. When a program is started up, it gets a fixed allocation that it can never change. The operating
system has to know how much to give it. The information is in the header. If the programmer
is not sure how much is needed, he needs something like chmem to be able to try different
values.
20. If the function address were stored in the memory manager, the memo manager would need
16 words per process table slot to hold the pointer even though it is very unlikely that every
process would enable all 16 sign, to be caught. The scheme used only takes up memory for
process that exist, which are fewer than the number of process table entries.
SOLUTIONS TO CHAPTER 5 PROBLEMS

1. Use file names such as /usr/ast/file. While it looks like a hierarchical p name, it is really just
a single name containing embedded slashes.
2. You can go up and down the tree as often as you want using “..”. Some of the many paths are:
/etc/passwd
/./etc/passwd
/././etc/passwd
/./././etc/passwd
/etc/../passwd
/etc/../etc/../etc/passwd
/etc/../etc/../etc/.. /etc/passwd
/etc/../etc/../etc/.. /etc/../etc/passwd
3. The bit map requires B bits. The free list requires DF bits. The free list requires fewer bits if
DF < B. Alternatively, the free list is shorter if F/B < 1lD, where F /B is the fraction of blocks
free. For 16-bit disk addresses, the free list is shorter if 6 percent or less of the disk is free.
4. For (a), searching the FAT is going to require a large number of disk accesses, so UNIX will
be much faster. For (b), using the FA T will be faster, since it requires no disk accesses,
whereas UNIX will need disk accesses for indirect blocks.
5. Since the path is a relative path, we know that the i-node for the current working directory is
already in memory. Therefore, we need one disk reference to fetch the current working
directory itself (assuming it fits in one disk block), and one more to fetch the i-node of the
file. A total of two disk references are needed.
6. When a file is removed from a directory, it is important to know if it is still in use in another
directory .It would be very expensive to search the entire directory tree to find out. Having the
count in the i-node makes this search unnecessary.
7. A single-indirect block holds 512 pointers, so a double-indirect block is good for 5122 blocks.
However, files cannot be anywhere near this large because with 16-bit disk addresses, a disk
can have at most 64K lK blocks. Files cannot be larger than their devices, so the maximum
file size here is 64K blocks, or 64M bytes. (Actually, it is somewhat smaller, since the disk
also needs i-nodes, etc.)
8. Sharing would be possible, but not easy. Each directory entry contains the number of the first
disk block of the file. If a file appeared in several directories, each one would just have the
same initial disk block. The hard part would be to keep track of how many directory entries
were pointing to a given file so the system would know when to remove a file. In UNIX this
information is provided by the link count in the i-node. Not having i-nodes, the best we could
do would be to introduce a new table, indexed by disk block, giving the link count. This new
table would need as many entries as the FAT, although 8 bits per entry should be enough.
9. A file system that is correct should have a 1 in one of the tables and a 0 in the other. That is
not the case here. Blocks 1 and 6 are missing. This wastes two blocks of storage, but is not
serious. Far worse is that block 5 is in use and also in the free list. If not corrected in time, the
same block may appear in two files, which is disastrous.
10. Many UNIX files are short. If the entire file fit in the same block as the i- node, only one disk
access would be needed to read the file, instead of two, as is presently the case. Even for
longer files there would be a gain, since one fewer disk accesses would be needed.
11. The time needed is h + 40 x (1 -h ).The plot is just a straight line.
12. The time per block is built up of three components: seek time, rotational latency, and transfer
time. In all cases the rotational latency plus transfer time is the same, 125 msec. Only the seek
time differs. For 13 cylinders it is 78 msec; for 2 cylinders it is 12 msec. Thus for randomly
placed files the total is 203 msec, and for clustered files it is 137 msec.
13. If done right, yes. While compacting, each file should be organized so that all of its blocks
are consecutive, for fast access.
14. No matter what the size of the disk block, a file of that size will use the double-indirect block.
The file therefore needs three blocks: the data block, the double-indirect block, and a single-
indirect block pointed to by the double-indirect block.
15. The client machine does (a) and (c) .The server does (b).
16. It should read the entire password all the time, even if it sees early on that the password is
wrong. That way, a page fault will always occur.
17. Nothing (except calm the assistant) .The password encryption algorithm is public. Passwords
are encrypted by the login program as soon as they are typed in, and the encrypted password
is compared to the entry in the password file.
18. If users could MOUNT file systems, they could become super-user as follows. A user makes
a file system containing a shell owned by the root (the super- user) and having the SETUID
bit on. He then mounts this file system and executes the shell. Presto. He is now running a
shell as super-user.
19. No, it does not. The student can easily find out what the random number for his super user is.
This information is in the password file unencrypted. If it is, 0003, for example, then he just
tries encrypting potential passwords as Susan 0003, Boston0003, IBMPC0003, etc. If another
user has password Boston0004, he will not discover it, however.
20. If all the machines can be trusted, it works ok. If some cannot be trusted, the whole scheme
breaks down, because an untrustworthy machine could send a message to a trustworthy
machine asking it to carry out some command on behalf of the super-user. The machine
receiving the message has no way of telling if the command really did originate with the super-
user, or with a student.
21. From a security point of view, it would ideal. Used blocks sometimes are exposed, leaking
valuable information. From a performance point of view, it would generate a large number of
additional disk writes, thus degrading performance. On the large Burroughs mainframes, files
designated as critical are erased when released, but ordinary files are not. This is a reasonable
compromise.
22. To make a file readable by everyone except one person, access control lists are the only
possibility. For sharing private files, access control lists or capabilities can be used. To make
files public, access control lists or the rwx bit mechanism are easy to use. It may also be
possible to put a capability for the file or files in a well-known place in a capability system.
23. It resembles the MUL TICS ring mechanism. The numbers are the ring numbers, and
processes cannot access objects in inner rings (lower numbers). However, one essential
difference with our proposal is that the ring mechanism is used to protect the various
procedures and objects within a single process from one another. It is not used to keep Steve
from reading Mitch's files. Access control lists are used for that purpose.
24. If the capabilities are used to make it possible to have small protection domains, no, otherwise
yes. If an editor, for example, is started up with only the capabilities for the file to be edited
and its scratch file, then no matter what tricks are lurking inside the editor, all it can do is read
those two files. On the other hand, if the editor can access all of the user's objects, then Trojan
Horses can do their dirty work, capabilities or not.
25. Elinor is right. Having two copies of the i-node in the table at the same time is a disaster,
unless both are read-only. The worst case is when both are being updated simultaneously.
When the i-nodes are written back to the disk, whichever one gets written last will erase the
changes made by the other one, and disk blocks will be lost.
26. If a file that is currently open is removed, it will have these values. When it is closed, the file
will be removed.
27. See Fig. 5-30 for the file system layout. It is the same here, except that only two i-node blocks
are needed. This leaves 354 blocks over for the data and directories.
28. The super-block tells how many i-nodes there are. By scanning the i-nodes, it is possible to
see which are in use and which are not. This allows the i- node bit map to be fully
reconstructed.
29. From the super-block, it is possible to locate all the i-nodes. Given all the i-nodes, all the disk
blocks (zones) can be found, making it possible to reconstruct the zone bit map .
30. As far as opening the files goes, the processing is the same. The only difference in reading
them is how the current position within the file is converted to a disk address. For a block
special file, the file position is also the position on the disk. Block 0 in the file is block 0 on
the disk. For an ordinary file, the zone number for the current position must be looked up in
the i-node, something not needed for special files.
31. Hashing is done to speed up finding the block. It is not essential.

(Above Tannenbaum Sol.)

Stallings Manual

CHAPTER l
COMPUTER SYSTEM OVERVIEW

1.2 a. Input from the teletype is stored in INPR. The INPR will only accept data
from the teletype when FGI = 0. When data arrives, it is stored in INPR, and FGI is
set to 1. The CPU periodically checks FGI. If FGI = 1, the CPU transfers the contents
of INPR to the AC and sets FGI to 0.
When the CPU has data to send to the teletype, it checks FGO. If FGO = 0, the
CPU must wait. If FGO = 1, the CPU transfers the contents of the AC to OUTR and
sets FGO to 0. The teletype sets FGI to 1 after the word is printed.

b. The discipline described in (a) is very wasteful. The CPU, which is much faster than
the teletype, must repeatedly check FGI and FGO. If interrupts are used, the teletype
can issue an interrupt to the CPU whenever it is ready to accept or send data. The IEN
register can be set by the CPU (under program control) to enable or disable interrupts.

1.5 Define

Ci = Average cost per bit, memory level i Si = Size of memory level i


Ti = Time to access a word in memory level i
Hi = Probability that a word is in memory level i and in no memory of higher level
Bi = Time to transfer a block of data from memory level (i + 1) to memory level i

Let cache be memory level 1; main memory be memory level 2, and so on, for a total of N
levels of memory. Then

The derivation of Ts is more complicated. We begin with the result from probability theory
that:

Expected value of x =

We can write
Then, we need to realize that if a word is in MI (cache), it is read immediately. If it is in M 2 but not
MI, then the block of data containing the word is transferred from M2 to M1, and then the word is
read. Thus

T2 == Bl + T1

And

T3 = B2 + T2 == Bl + B2 + T1

Generalizing,

So

But

Finally

1.6 a. Cost = Cm X 8 X 106 == 8 X 103 = $80


b. Cost = Cc X 8 X 106 = 8 X 104 = $800

1.7 If a processor is held up in attempting to read or write memory, usually no damage occurs
except a slight loss of time. However, a DMA transfer may be to or from a device that is
receiving or sending data in a stream (e.g., disk or tape), and cannot be stopped. Thus, if the
DMA module is held up (denied continuing access to main memory), data will be lost.

1.8 Let us ignore data read/write operations and assume the processor only fetches instructions.
Then the processor needs access to main memory once every microsecond. The DMA module
is transferring characters at a rate of 1200 characters per second, or one every 833 μs. The
DMA therefore "steals" every
833rd cycle. This slows down the processor approximately 1/833 x100% = 0.12%

1.9 a. The processor can only devote 5% of its time to I/O. Thus the maximum I/O
instruction execution rate is 105 x 0.05 = 500 instructions per second. The I/O transfer
rate is therefore 2500 words/second.

b. The number of machine cycles available for DMA control is

105(0.05 x 5 + 0.95 x 2) -2.15 x 105

If we assume that the DMA module can use all of these cycles, and ignore any
setup or status-checking time, then this value is the maximum I/O transfer rate.

1.10 Yes, if the stack is only used to hold the return address. If the stack is also used to pass
parameters, then the scheme will work only if it is the control unit that removes parameters,
rather than machine instructions. If parameters are removed by machine instructions, the
processor would need both a parameter and the PC on top of the stack at the same time.
CHAPTER 2

OPERA TNG SYSTEMS OVERVIEW

2.1 The answers are the same for (a) and (b). Assume that although processor operations
cannot overlap, I/a operations can.

1 Job: TAT= NT Processor utilization = 50%


2 Jobs: TAT= NT Processor utilization = 100%
4 Jobs: TAT= (2N- l)NT Processor utilization = 100%

2,3 I/O-bound programs use relatively little processor time and are therefore favored by the
algorithm. However, if a processor-bound process is denied processor time for a
sufficiently long period of time, the same algorithm will grant the processor to that process
since it has not used the processor at all in the recent past. Therefore, a processor-bound
process will not be permanently denied access.

2.4 Refer to Problem 1.5. We have the equation

2.5 With time sharing, the concern is turnaround time. Time-slicing is preferred because it gives
all processes access to the processor over a short period of time. In a batch system, the concern
is with throughput, and the less context switching, the more processing time is available for
the processes. Therefore, policies that minimize context switching are favored.
CHAPTER 3

PROCESS DESCRIPTION AND CONTROL

3.1 a. A separate queue is associated with each wait state. The differentiation of waiting processes
into queues reduces the work needed to locate a waiting process when an event occurs that
affects it. For example, when a page fault completes, the scheduler know that the waiting
process can be found on the Page Fault Wait queue.

b. In each case, it would be less efficient to allow the process to be swapped out while in this
state. For example, on a page fault wait, it makes no sense to swap out a process when we are
waiting to bring in another page so that it can execute.

c. The state transition diagram can be derived from the following state transition table:

Next State
Current State Currently Computable Computable Variety of Variety of
Executing (resident) (outswapped) wait states wait states
(resident) (outswapped)
Currently Rescheduled Wait
Executing
Computable Scheduled Outswap
(resident)
Computable Inswap
(outswapped)
Variety of wait Event Outswap
states satisfied
(resident)
Variety of wait Event
states satisfied
(outswapped)

3.2 The following example is used in [PINK89] to clarify their definition of block and suspend:

Suppose a process has been executing for a while and needs an additional magnetic
tape drive so that it can write out a temporary file. Before it can initiate a write to tape, it must
be given permission to use one of the drives. When it makes its request, a tape drive may not
be available, and if that is the case, the process will be placed in the blocked state. At some
point, we assume the system will allocate the tape drive to the process; at that time the process
will be moved back to the active state. When the process is placed into the execute state again
it will request a write operation to its newly acquired tape drive. At this point, the process will
be move to the suspend state, where it waits for the completion of the current write on the tape
drive that it now owns.

The distinction made between two different reasons for waiting for a device be useful to the
operating system in organizing its work. However, it is substitute for a knowledge of which
processes are swapped out and which processes are swapped in, this latter distinction is a
necessity and must be reflected in some fashion in the process state.

3.4 Penalize the Ready, suspend processes by some fixed amount, such as one or priority levels,
so that a Ready, suspend process is chosen next only if it has a higher priority than the highest-
priority Ready process by several levels of priority.

3.6 a. With j < i, a process running in Di is prevented from accessing objects in Dj.
Thus, if Dj contains information that is more privileged or is to be kept more
secure than information in Di, this restriction is appropriate. However, this security
policy can be circumvented in the following way. A process running in Dj could read
data in Dj and then copy that data into Di. Subsequently, a process running in Di could
access the information.

b. An Approach to dealing with this problem, known as a trusted system, is discussed in


Chapter 14.

3.8 This technique is based on the assumption that an interrupted process A will continue to run
after the response to an interrupt. But, in general, an interrupt may cause the basic monitor to
preempt a process A in favor of another process now necessary to copy the execution state of
process A from the location associated with the interrupt to the process description associated
with A. The machine might as well have stored them there in the first place. Source: [BRIN73].

3.9 Yes cause more state information must be saved to switch from one process to another.

3.10 Since there are circumstances under which a process may not be preempted (i.e., it is executing
in kernel mode), it is impossible for the operating system to d rapidly to real-time
requirements.
CHAPTER 4

CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZA TION

4.1 b. The read coroutine reads the cards and passes characters through a one
character buffer, rs, to the squash coroutine. The read coroutine also passes the extra
blank at the end of every card image. The squash coroutine need known nothing about
the 80-character structure of the input; it simply looks for double asterisks and passes
a stream of modified characters to the print coroutine via a one-character buffer, sp.
Finally, print simply accepts an incoming stream of characters and prints it as a
sequence of 125-character lines.

4.3 a. On casual inspection, it appears that tally will fall in the range 50 ≤ tally ≤ 100
since from a to 50 increments could go unrecorded due to the lack of mutual exclusion.
The basic argument contends that by running these two processes concurrently we
should not be able to derive a result lower than the result produced by executing just
one of these processes sequentially. But consider the following interleaved sequence
of the load, increment, and store operations performed by these two processes when
altering the value of the shared variable:

1. Process A loads the value of tally, increments tally, but then loses the processor (it
has incremented its register to 1, but has not yet stored this value.
2. Process B loads the value of tally (still zero) and performs forty-nine complete
increment operations, losing the processor after it has storied the value 49 into the
shared variable tally.
3. Process A regains control long enough to perform its first store operation
(replacing the previous tally value of 49 with 1) but is then immediately forced to
relinquish the processor.
4. Process B resumes long enough .to load 1 (the current value of tally) into its
register, but then it too is forced to give up the processor (note that this was B's
final load).
5. Process A is rescheduled, but this time it is not interrupted and runs to completion,
performing its remaining 49 load, increment, and store operations, which results
in setting the value of tally to 50.
6. Process B is reactivated with only one increment and store operation to perform
before it terminates. It increments its register value to 2 and stores this value as the
final value of the shared variable.

Some thought will reveal that a value lower than .2 cannot occur. Thus, the proper
range of final values is 2 ≤ tally ≤ 100.

b. For the generalized case of N processes, the range of final values is 2 ≤ tally ≤ (N x 50),
since it is possible for all other processes to be initially scheduled and run to completion
in step (5) before Process B would finally destroy their work by finishing last.
Source: [RUDO90].

4.5 Consider the case in which turn equals 0 and P(1) sets blocked[l] to true and then finds
blocked[0] set to false. P(0) will then set blocked[0] to true, find turn = 0, and enter its critical
section. P(l) will then assign 1 to turn and will also enter its critical section.

4.6 a. Process P1 will only enter its critical section if flag[0] = false. Only P1 may
modify flag[1], and P1 tests flag[0] only when flag[l] = true. It follows that when P1
enters its critical section we have:

(flag[l] and (not flag[0])) = true

Similarly, we can show that when P0 enters its critical section:

(flag[l] and (not flag[0])) = true

b. Case 1: A single process P(i) is attempting to enter its critical section. It will find flag[l-
i] set to false, and enters the section without difficulty.
Case 2: Both process are attempting to enter their critical section, and turn = 0 (a
similar reasoning applies to the case of turn = 1). Note that once both processes enter
the while loop, the value of turn is modified only after one process has exited its critical
section.

subcase 2a: flag[0] = false. P1 finds flag[0] = 0, and can enter its critical
section immediately.

subcase 2b: flag[0] = true. Since turn = 0, P0 will wait in its external loop for
flag[l] to be set to false (without modifying the value of flag[0].
Meanwhile, P1 sets flag[l] to false (and will wait in its internal loop because
turn = 0). At that point, P0 will enter the critical section.

Thus, if both processes are attempting to enter their critical section, there is no deadlock.

4.7 It doesn't work. There is no deadlock; mutual exclusion is enforced; but starvation is possible
if turn is set to a non-contending process.

4.8 a. With this inequality, we can state that the condition in lines 4-5 is not satisfied
and Pi can advance to stage j+ 1. Since the act of checking the condition is not a
primitive, equation (1) may become untrue during the check: some Pr may set q[r] =
j; several could do so, but as soon as the first of them also modifies turn[j], Pi can
proceed (assuming it tries; this assumption is present throughout the proof, but will be
kept tacit from now on). Moreover, once more than one additional process joins stage
j, Pi can be overtaken.
b. Then either condition (1) holds, and therefore Pi precedes all other processes;
or turn[j] ≠ i, with the implication the Pi is not the last, among all processes currently
in lines 1-6 of their programs, to enter stage j. Regardless of how many processes
modified turn[j] since Pi did, there is a lost one, Pr, for which the condition is its line
5 is true. This process makes the second line of the lemma hold (Pi is not alone at stage
j). Note that it is possible that Pi proceeds to modify q[i] on the strength of its finding
that condition (1) is true, and in the meantime another process destroys this condition,
thereby establishing the possibility of the second line of the lemma.

c. The claim is void for. j = 1. For j = 2, we use Lemma 2: when there is a


process at stage 2, another one (or more) will join it only when the joiner leaves behind
a process in stage 1. That one, so long as it is alone there cannot advance, again by
Lemma 2. Assume the Lemma holds for stage j-l; if there are two (or more) at stage j,
consider the instant that the last of them joined in. At that time, there were (at least)
two at stage j-l (Lemma 2), and by the induction assumption, all preceding stages were
occupied. By Lemma 2, none of these stages could have vacated since.

d. If stages 1 through j-l contain at least one process, there are N- (j-1) left at
most for stage j. If any of those stages is "empty" Lemma 3 implies there is at most
one process at stage j.

e. From the above, stage N-l contains at most two processes. If there is only one
there, and another is at its critical section, Lemma 2 says it cannot advance to enter its
critical section. When there are two processes at stage N- 1, there is no process left to
be at stage N (critical section), and one of the two may enter its critical section. For
the one remaining process, the condition in its line 5 holds. Hence there is mutual
exclusion.

There is no deadlock: There is one process the precedes all others or is with
company at the highest occupied stage, which it was not the last to enter, and for such
a process the condition of its line 5 does not hold.

There is no starvation. If a process tries continually to advance, no other


process can pass it; at worst, it entered stage 1 when all others were in their entry
protocols; they may all enter stage N before it does-but no more.
Source: [HOFR90].

4.9 a. When a process wishes to enter its critical section, it is assigned a ticket
number. The ticket number assigned is calculated by adding one to the largest of the
ticket numbers currently held by tile processes waiting to enter their critical section
and the process already in its critical section. The process with tile smallest ticket
number has the highest precedence for entering its critical section. In case more than
one process receives tile same ticket number, tile process with the smallest ticket
number enters its critical section. When a process exits its critical section, it resets its
ticket number to zero.

b. If each process is assigned a unique process number, then there is a unique, trict
ordering of processes at all times. Therefore, deadlock cannot occur.

c. To demonstrate mutual exclusion, we first need to prove the following lemma:


if Pi is in its critical section, and Pk has calculated its number[k] and is attempting to
enter its critical section, tilen the following relationship holds:

(number[i],i) < ( number[k],k)

To prove the lemma, define the following times:

Tw1 Pi reads choosing[k] for the last time, for j = k, in its first wait, so we have
choosing[k] = false at Tw1.

Tw2 Pi begins its final execution, for j = k, of the second while loop. We therefore
have Tw1 < Tw2.
Tkl Pk enters the beginning of the repeat loop.
Tk2 Pk finishes calculating number[k].
Tk3 Pk sets choosing[k] to false. We have Tkl < Tk2 < Tk3.

Since at Twl, choosing[k] = false, we have either Twl < Tkl or Tk3 < Twl. In the first
case, we have number[i] <number[k], since Pi was assigned its number prior to Pk;
this satisfies the condition of the lemma.
In the second case, we have Tk2 < Tk3 < Tw1 < Tw2, and therefore Tk2 <
Tw2. This means that at Tw2; Pi has read the current value of number[k]. Moreover,
as Tw2 is the moment at which the final execution of the second while for j = k takes
place, we have (number[i], i) < ( number[k], k), which completes the proof of the
lemma.

It is now easy to show the mutual exclusion is enforced. Assume that Pi is in


its critical section and Pk is attempting to enter its critical section. Pk will be unable
to enter its critical section, as it will find number[i] ≠ 0 and ( number[i], i) < (
number[k], k).

d. A solution is proposed in" A Note on Lamport's Mutual Exclusion Algorithm,"


by Woo, Operating Systems Review, October 1990. To determine precedence, compute the
difference between two ticket numbers. If the difference is greater than or equal to n, the
process with the larger ticket number has precedence; otherwise the process with the smaller
ticket number has precedence. To test if the process with ticket number A has precedence over
a process with ticket number B, the inequality (A < B) should be replaced by ( | A -B |I ≥ n)
(A < B) where is the exclusive-or operation.
The following changes are made to the algorithm. The assignment of number[i] becomes

number[i] := (MODMAX(number[0], number[1], …...., number[n-l] + 1) MOD 2n;

After leaving the critical section, number[i] is assigned the value -1, and the test in the second
while loop is number[j] ≠ -1. The notation (a,b) < (c,d) is now defined as ((| a -c | ≥ n) (a <
c) OR (a = c and b < d). The function MODMAX is defines as follows:

function MODMAX (number: integer) : integer;


begin
while number[m] = -1 do x := x + 1; {x is initialized to 0}
max := number[x]; {establish temporary maximum}
for k := x + 1 to n -1 do

I if ((| max -number[k] | ≥ n) (max < number[k])) OR (number[k] > -1)


then max := number[k]; ;
return max;
end;

MODMAX checks each element of the array number. If number[k] = -1, process k
does not want to enter its critical section. Otherwise, if number[k] has lower precedence than
the temporary max, max is updated. The function returns the ticket number that has the lowest
precedence for entering the critical section.

4.11 The following program is provided in [SILB9l]:

var j: 0..n-l;
key: boolean;
repeat
waiting[i] := true;
key := true;

while waiting[i] and key do key := testset(lock);


waiting[i] := false;
< critical section >
J := i + 1 mod n;
while G ≠ i) and (not waiting [j]) do j := j + 1 mod n;
if j = i then lock := false
else waiting := false;
< remainder section >
until false;

The algorithm uses the common data structures


var waiting: array [0..n-l] of boolean
lock: boolean

These data structures are initialized to false. When a process leaves its critical section, it scans
the array waiting in the cyclic ordering (i + 1, i + 2, ..., n -1,0, ..., i -1). It designates thefirst
process in this ordering that is in the entry section (waiting [j] = true) as the next one to enter
the critical section. Any process waiting to enter its critical section will thus do so within n -
1 turns.

4.14 Move the else line, which appears just before the end line in Wait to just before the
end line in Signal. For a discussion, see " A Correct Implementation of General emaphores;"
by Hemmendinger, Operating Systems Review, July 1988.

4.15 The code has a major problem. The V(passenger-released) in the car code can unblock
a passenger blocked on P(passenger-released) that is NOT the one riding in the car that did
the V( ).

4.15 This solution is from [BEN82].

program producerconsumer;
var n: integer;
s: (*binary*) semaphore (:= 1);
delay: (*binary*) semaphore (:= 0);
procedure producer;
begin
repeat
produce;
waitB(s);
append;
n := n + 1;
if n=0 then signalB(delay);
signalB(s)
forever

end;
procedure consumer;
begin
repeat
waitB(s);
take;
n := n -1;
if n = -1 then
begin
signalB(s);
waitB( delay );
waitB(s)
end;
consume;
signalB(s)
forever
end;
begin (*main program*)
n := 0;
parbegin
producer; consumer
parend
end.

4.18 a. If the buffer is allowed to contain n entries, then the problem is to distinguish
an empty buffer from a full one. Consider a buffer of six slots, with only one entry, as
follows:

out in

Then, when the one element is removed, out = in. Now suppose that the buffer is one
element shy of being full:

D E A B C
in out

Here, out = in + 1. But then, when an element is added, in is incremented by 1 and out
= in, the same as when the buffer is empty.

4.20 a. Change receipt to an array of semaphores all initialized to 0 and use enqueue2,
queue2, and dequeue2 to pass the customer numbers.

b. Change leave-b-chair to an array of semaphores all initialized to 0 and use


enqueue1(custnr), queue1, and dequeuel(b-cust) to release the right barber.

Figure 1 shows the program with both of the above modifications. Note: The barbershop
example in the book and Problems 4.19 and 4.20 are based on the following article, used with
permission:

Hilzer, P. "Concurrency with Semaphores." SIGSCE Bulletin, September 1992.


program barbershop2;
var max-capacity: semaphore (:= 20);
sofa: semaphore (:= 4);
barber-chair, coord: semaphore (:= 3);
mutexl, mutex2, mutex3: semaphore (:=1);
cust-ready, payment: semaphore (:= 0);
finished, leave-b-chair, receipt: array[1..50] of semaphore (:=0);
count: integer;

procedure customer; procedure barber; procedure cashier;


var custnr: integer; var b-cust: integer; var b-cust: integer;
begin begin begin
wait(max-capacity); repeat repeat
enter shop; wait(cust-ready); wait(payment); wait(mutexl);
wait(mutex2); wait(mutex3);
count := count + 1; dequeuel(b-cust); dequeue2(c-cust);
custnr := count; signal(mutex2); signa1(mutex3); signal(mutexl);
wait(coord); wait(coord);
wait(sofa); cut hair; accept pay;
sit on sofa; signal(coord); signal(coord);
wait(barber-chair); signal(finished[b-custJ); signal(receipt[c-cust]);
get up from sofa; wait(leave-b-chair[custnr]); forever
signal(sofa); signal(barber-chair); end;
sit in barber chair; forever
wait(mutex2); end;
enqueuel (custnr);
signal( cust-ready ) ;
signal(mutex2);
wait(finished[custnr]);
signal(1eave-b-chair[custnr]);
pay;
wait(mutex3);
enqueue2(custnr);
signa1(payment);
signa1(mutex3);
wait(receipt[ custnr ]);
exit shop;
signal(max-capacity)
end;

begin (*main program*)


count := 0;
parbegin
customer; ...50 times; ...customer;
barber; barber; barber;
cashier
parend
end.
Figure 1 A Fair Barbershop, with Modifications

4.19 There is an array of message slots that constitutes the buffer. Each process maintains a linked
list of slots in the buffer that constitute the mailbox for that process. The message operations
can implement as:

send (message, dest)


wait (mbuf) wait for message buffer available
wait (mutex) mutual exclusion on message queue
acquire free buffer slog
copy message to slot
link slot to other messages
signal (dest.sem) wake destination process
signal (mutex) release mutual exclusion

receive message
wait (own.sem) wait for message to arrive
wait (mutex) mutual exclusion on message queue
unlink slot from own.queue
copy buffer slot to message
add buffer slot to freelist
signal (mbuf) indicate message slot freed
signal (mutex) release mutual exclusion

where mbuf is initialized to the total number of message slots available; own and dest refer to the
queue of messages for each process, and are initially zero. Source: [THEA83].

4.20 This solution is taken from [TANE87]. The synchronization process maintains a counter and
a linked list of waiting processes for each semaphore. To do a W AIT or SIGNAL, a process
calls the corresponding library procedure, wait or signal, which sends a message to the
synchronization process specifying both the operation desired and the semaphore to be used.
The library procedure then does a RECEIVE to get the reply from the synchronization process.

When the message arrives, the synchronization process checks the counter to see if the
required operation can be completed. SIGNALs can always complete, but
WAITs will block if the value of the semaphore is 0. If the operation is allowed, the
synchronization process sends back an empty message, thus unblocking the caller. If,
however, the operation is a WAIT and the semaphore is 0, the synchronization process enters
the caller onto the queue and does not send a reply. The result is that the process doing the
WAIT is blocked, just as it should be. Later, when a SIGNAL is done, the synchronization
process picks one of the processes blocked on the semaphore, either in FIFO order, priority
order, or some other order, and sends a reply. Race conditions are avoided here because the
synchronization process handles only one request at a time.
CHAPTER 5

CONCURRENCY: DEADLOCK AND STARVATION

5.1 a.
0000
0750
6622
2002
0320

b. Running the banker's algorithm, we see processes can finish in the order p1, p4, p5,
p2, p3.

c. Change available to (2,0,0,0) and p3's row of "still needs" to (6,5,2,2). Now p1, p4, p5
can finish, but with available now (4,6,9,8) neither p2 nor p3's "still needs" can be
satisfied. So it is not safe to grant p3's request.

5.2 It is unrealistic: don't know max demands in advance, number of processes can change over
time, number of resources can change over time (something can break). Most OS's ignore
deadlock. But SunOS only lets the superuser use the last process table slot.

5.3 a. A deadlock is a state in which all resource units are reserved while one or
more processes are waiting indefinitely for more units. But, if all 4 units are reserved,
at least one process has acquired 2 units. Consequently, that process will be able to
complete its work and release both units, thus enabling another process to continue.

b. Using terminology similar to that used for the banker's algorithm, define
claim[i] = total amount of resource units needed by process i; allocation[i] = current
number of resource units allocated to process i; and deficit[i] = amount of resource
units still needed by i. Then we have:

In a deadlock situation, all resource units are reserved:

and some processes are waiting for more units indefinitely. But from the two preceding equations, we
find
This means that at least one process j has acquired all its resources (deficit[j] = 0) and will be
able to complete its task and release all its resources again, thus ensuring further progress in
the system. So a deadlock cannot occur.

5.4 a. When a philosopher finishes eating, he allows his left neighbor to proceed
if possible, then permits his right neighbor to proceed. The solution uses an array,
state, to keep track of whether a philosopher is eating, thinking, or hungry (trying to
acquire forks). A philosopher may move only into the eating state if neither neighbor
is eating. Philosopher is neighbors are defined by the macros LEFT and RIGHT.

b. This counterexample is due to [GING90]. Assume that philosophers P0, P1, and P3 are
waiting with hunger while philosophers P2 and P4 dine at leisure. Now consider the
following admittedly unlikely sequence of philosophers' completions of their suppers.

EATING HUNGRY
4 2 0 1 3
2 0 1 3 4
3 0 1 2 4
0 2 1 3 4
4 2 0 1 3

Each line of this table is intended to indicate the philosophers that are presently eating and
those that are in a state of hunger. The dining philosopher listed first on each line is the one who
finishes his meal next. For example, from the initial configuration, philosopher P4 finishes eating
first, which permits P0 to commence eating. Notice that the pattern folds in on itself and can repeat
forever with the consequent starvation of philosopher P1.

5.5 a. Assume that the table is in deadlock, i.e., there is a nonempty set D of
philosophers such that each Pi in D holds one fork and waits for a fork held by
neighbor. Without loss of generality, assume that Pj (E D is a lefty. Since Pj clutches
his left fork and cannot have his right fork, his right neighbor Pk never completes his
dinner and is also a lefty. Therefore, Pk (E D.
Continuing the argument rightward around the table shows that all philosophers in D
are lefties. This contradicts the existence of at least one righty. Therefore deadlock is
not possible.

b. Assume that lefty Pj starves, i.e., there is a stable pattern of dining in which Pj never eats.
Suppose Pj holds no fork. Then Pj's left neighbor Pi must continually hold his right fork
and never finishes eating. Thus Pi is a righty holding his right fork, but never getting his
left fork to complete a meal, i.e., Pi also starves. Now Pi's left neighbor must be a rightly
who continually holds his right fork. Proceeding leftward around the table with this
argument shows that all philosophers are (starving) righties. But Pj is a lefty: a
contradiction. Thus Pj must hold one fork.
As Pj continually holds one fork and waits for his right fork, Pj's right neighbor Pk
never sets his left fork down and never completes a meal, i.e., Pk is also a lefty who starves.
If Pk did not continually hold his left fork, Pj could eat; therefore Pk holds his left fork.
Carrying the argument rightward around the table shows that all philosophers are
(starving) lefties: a contradiction. Starvation is thus precluded.
Source: [GING90].
CHAPTER 6

MEMORY MANAGEMENT

6.2 Let s and h denote the average number of segments and holes, respectively. The
probability that a given segment is followed by a hole in memory (and not by another
segment) is 0.5, because deletions and creations are equally probable in equilibrium. so
with s segments in memory , the average number of holes must be s/2. It is intuitively
reasonable that the number of holes must be less than the number of segments because
neighboring segments can be combined into a single hole on deletion.
CHAPTER 7

VIRTUAL MEMORY

7.1 a. split binary address into virtual page number and offset; use VPN as index into
page table; extract page frame number; concatenate offset to get physical memory
address
b. (i) 1052 = 1024 + 28 maps to VPN 1 in PFN 7, 7 x 1024+28 = 7196
(ii) 2221 = 2 x 1024 + 173 maps to VPN 2, page fault
(iii) 5499 = 5 x 1024 + 379 maps to VPN 5 in PFN 0, 0 x 1024+379 = 379

7.2 a. PFN 3 since loaded longest ago at time 20


b. PFN 1 since referenced longest ago at time 160
c. PFN 1 since most desirable since R and M are both 0
c. clear R in PFN 3 (oldest loaded), clear R in PFN 2 (next oldest loaded), victim
PFN is 0 since R=0
d. replace the page in PFN 3 since VPN 3 (in PFN 3) is used furthest in the
future
e. there are 6 faults, indicated by *

* * * * * *
4 0 0 0 2 4 2 1 0 3 2
pages in 3 4 0 0 0 2 4 2 1 0 3 2
memory in 0 3 4 4 4 0 2 4 2 1 0
LRU order 2 0 3 3 0 0 4 2 1
1 2 4 2

7.3 9 and 10 page transfers, respectively. This is referred to as "Belady's anomaly ,”and was
reported in "An Anomaly in Space-Time Characteristics of Certain Programs Running in a
Paging Machine," by Belady et al, Communications of the ACM, June 1969.

7.6 The principal advantage is a savings in physical memory space. This occurs for two
reasons:(1) a user page table can be paged in to memory only when it is needed. (2) The
operating system can allocate user page tables dynamically, creating one only when the
process is created.
Of course, there is a disadvantage: address translation requires extra work.

7.7 The machine language version of this program, loaded in main memory starting at address 4000, might
appear as:

4000 (R1) ← ONE Establish index register for i


4001 (R1) ← n Establish n in R2
4002 compare R1, R2 Test i > n
4003 branch greater 4009
4004 (R3) ← B(R1) Access B[i] using index register R1
4005 (R3) ← (R3) + C(R1) Add C[i] using index register R1
4006 A(R1) ← (R3) Store sum in A[i] using index register
Rl
4007 (R1) ← (R1) + ONE Increment i
4008 branch 4002
6000-6999 storage for A
7000- 7999 storage for B
8000-8999 storage for C
9000 storage for ONE
9001 storage for n

The reference string generated by this loop is

494944(47484649444)1000

consisting of over 11,000 references, but involving only five distinct pages. Source:
[MAEK87].

7.8 By problem 6.2, we know that the average number of holes is s/2, where s is the number of
resident segments. Regardless of fit strategy, in equilibrium, the average search length is s /4.

7.9 A criticism of the best-fit algorithm is that the space remaining after allocating a block of the
required size is so small that in general it is of no real use. The worst fit algorithm maximizes
the chance that the free space left after a placement will be large enough to satisfy another
request, thus minimizing the frequency of compaction. The disadvantage of this approach is
that the largest blocks are allocated first; therefore a request for a large area is more likely to
fail.

7.10 The S/370 segments are fixed in size and not visible to the programmer. Thus, none of the
benefits listed for segmentation are realized on the S/370, with the exception of protection.
The P bit in each segment table entry provides protection for the entire segment.

7.11 The processor hardware sets the reference bit to 0 when a new page is loaded into the frame,
and to 1 when a location within the frame is referenced. The operating system can maintain a
number of queues of page-frame tables. A page-frame table entry moves from one queue to
another according to how long the reference bit from that page frame stays set to zero. When
pages must be replaced, the pages to be replaced are chosen from the queue of the longest-
life nonreferenced frames.

7.12 [PIZZ89] suggests the following strategy. Use a mechanism that adjusts the value of Q at each
window time as a function of the actual page fault rate experienced during the window. The
page fault rate is computed and compared with a system-wide value for "desirable" page fault
rate for a job. The value of Q is adjusted upward (downward) whenever the actual page fault
rate of a job is higher (lower) than the desirable value. Experimentation using this adjustment
mechanism showed that execution of the test jobs with dynamic adjustment of Q consistently
produced a lower number of page faults per execution and a decreased average resident set
size than the execution with a constant value of Q (within a very broad range). The memory
time product (MT) versus Q using the adjustment mechanism also produced a consistent r and
considerable improvement over the previous test results using a constant value of Q.
CHAPTER 8

UNIPROCESSOR SCHEDULING

8.3 We will prove the assertion for the case in which a batch of n jobs arrive at the same time,
and ignoring further arrivals. The proof can be extended to cover later arrivals.

Let the service times of the jobs be

Then, n users must wait for the execution of job 1; n -1 users must wait for the execution of
job 2, and so on. Therefore, the average response time is

If we make any changes in this schedule, for example by exchanging jobs j and k (where j <
k), the average response time is increased by the amount

In other words, the average response time can only increase if the SPN algorithm is not
used. Source: [BRIN73].

8.5 The first equation is identical to equation (3), so the parameter a provides an exponential
smoothing effect. The parameter b is a delay variance factor (e.g., 1.3 to 2.0). A smaller value
of b will result in faster adaptation to changes in the observed times, but also more fluctuation
in the estimates.
A sophisticated analysis of this type of estimation procedure is contained in Applied
Optimal Estimation, edited by Gelb, M.I.T. Press, 1974.

8.6 First, the scheduler computes the response ratios at time t + rl + r2 + r3, when all three jobs
will have been finished (see figure). At that time, job 3 will have the smallest response ratio
of the three: so the scheduler decides to execute this job last and proceeds to examine jobs 1
and 2 at time t + rl + r2, when they will both be finished. Here the response ratio of job 1 is
the smaller, and consequently job 2 is selected for service at time t. This algorithm is repeated
each time a job is completed to take new arrivals into account. Note that this algorithm is not
quite the same as highest response ratio next. The latter would schedule job 1 at time t.
Intuitively, it is clear that the present algorithm attempts to minimize the maximum response
ratio by consistently postponing jobs that will suffer the least increase of their response ratios.
1

t1 t2 t3 t

8.7 This proof, due to P. Mondrup, is reported in [BRIN73]. Consider the queue at time t
immediately after a departure and ignore further arrivals. The waiting jobs are numbered 1 to
n in the order in which they will be scheduled:

job: 1 2 … i … n
arrival time: tl t2 … ti … tn
service time: rl r2 … ri … rn

Among these we assume that job i will reach the highest response ratio before its departure.
When the jobs 1 to i have been executed, time becomes
Ti = t + rl + r2 + ...+ ri

and job i has the response ratio

The reason for executing job i last in the sequence 1 to i is that its response ratio will be the
lowest one among these jobs at time Ti:

Ri(Ti) = min [ R1(Ti), R2(Ti), ..., Ri(Ti) ]


Consider now the consequences of scheduling the same n jobs in any other sequence:

job: a b … i … z
arrival time: ta tb … tj … tz
service time: ra rb … rj … rz

In the new sequence, we select the smallest subsequence of jobs, a to j, that contains all the
jobs, 1 to i, of the original subsequence (This implies that job j is itself one of the jobs 1 to i). When
the jobs a to j have been served, time becomes

Tj = t + ra + tb + ...+ rj

and job j reaches the response ratio

Since the jobs 1 to i are a subset of the jobs a to j, the sum of their service times Ti -t must be
less than or equal to the sum of service time Tj -t. And since response ratios increase with
time, Ti ≤ Tj implies

Rj(Tj) ≥ Rj(Ti)

It is also known that job j is one of the jobs 1 to i, of which job j has the smallest response
ratio at time Ti. The above inequality can therefore be extended as follows:

Rj(Tj) ≥ Rj(Ti) ≥ Ri(Ti)

In other words, when the scheduling algorithm is changed, there will always be a job j that
reaches response ratio Rj(Tj), which is greater than or equal to the highest response ratio
Ri(Ti) obtained with the original algorithm.
Notice that this proof is valid in general for priorities, which are non- decreasing
functions of time. For example, in a FIFO system, priorities increase linearly with waiting
time at the same rate for all jobs. Therefore, the present proof shows that the FIFO algorithm
minimizes the maximum waiting time for a given batch of jobs.

8.8 Before we begin, there is one result that is needed, as follows. Assume that an item with
service time s has been in service for a time h. Then, the expected remaining service time E
[T /T>h] = s. That is, no matter how long an item has been in service, the expected remaining
service time is just the average service time for the item. This result, though counter to
intuition, is correct, as we now show.

Consider the exponential probability distribution function:

F(x) = Pr[X ≤ x] = 1 -e-μx

Then, we have Pr[X > x] = e--μx. Now let us look at the conditional probability that X is
greater than x + h given that X is greater than x:

Thus the probability distribution for service time given that there has been service of duration
x is the same as the probability distribution of total service time. Therefore the expected value
of the remaining service time is the same as the original expected value of service time.
With this result, we can now proceed to the original problem. When an item arrives
for service, the total response time for that item will consist of its own service time
plus the service time of all items ahead of it in the queue. The total expected
response time has three components.

➢ Expected service time of arriving process = s


➢ Expected service time of all processes currently waiting to be served. This value is
simply w x s, where w is the mean number of items waiting to be served.
➢ Remaining service time for the item currently in service, if there is an item currently
in service. This value can be expressed as p x s, where p is the utilization and therefore
the probability that an item is currently in service and s, as we have demonstrated, is
the expected remaining service time.
Thus, we have

8.9 Let us denote the time slice, or quantum, used in round robin scheduling as δ. In this problem,
δ is assumed to be very small compared to the service time of a process. Now, consider a
newly arrived process, which is placed at the end of the ready queue for service. We are
assuming that this particular process has a service time of x, which is some multiple of δ:

x=mδ

To begin, let us ask the question, how much time does the process spend in the queue before
it receives its first quantum of service. It must wait until all q processes waiting in line ahead
of it have been serviced. Thus the initial wait time = q δ, where q is the average number of
items in the system (waiting and being served). We can now calculate the total time this
process will spend waiting before it has received x seconds of service. Since it must pass
through the active queue m times, and each time it waits q δ seconds, the total wait time is as
follows:

Wait time = m (q δ)

Then, the response time is the sum of the wait time and the total service time

Rx = wait time + service time


= qx + x = (q + 1) x

Referring to the queuing formulas in Appendix A, the mean number of items in the system, q,
can be expressed as

Thus,

8.10 First, we need to clarify the significance of the parameter 1'. The rate at which items arrive at
the first box (the "queue" box) is 1. Two adjacent arrivals to the second box (the "service"
box) will arrive at a slightly slower rate, since the second item is delayed in its chase of the
first item. We may calculate the vertical offset y in the figure in two different ways, based on
the geometry of the diagram:

which therefore gives

The total number of jobs q waiting or in service when the given job arrives is given by:

independent of the scheduling algorithm. Using Little's formula (see Appendix A):

Now let W and Vx denote the mean times spent in the queue box and in the service box by a
job of service time x. Since priorities are initially based only on elapsed waiting times, W is
clearly independent of the service time x. Evidently we have

Rx = W + Vx

From problem 9.9, we have

By taking the expected values of Rx and Sx, we have R = W + V. We have already developed
the formula for R. For V, observe that the arrival rate to the service box is 1', and therefore
the utilization is r`. Accordingly, from our basic M /M / l formulas, we have
which yields the desired result for Rx.

8.11 Only as long as there are comparatively few users in the system. When the quantum is
decreased to satisfy more users rapidly two things happen: (1) processor utilization decreases,
and (2) at a certain point, the quantum becomes too small to satisfy most trivial requests. Users
will then experience a sudden increase of response times because their requests must pass
through the round- robin queue several times. Source: [BRIN73].

8.12 If a process uses too much processor time, it will be moved to a lower-priority queue. This
leaves I/O-bound processes in the higher-priority queues.
CHAPTER 10

I/O MANAGEMENT AND DISK SCHEDULING

10.2 It will be useful to keep the following representation of the N tracks of a disk in mind:

0 1 … j-1 … N-j … N-2 N-1

a. Let us use the notation Ps [j/t] = Pr [seek of length j when head is currently
positioned over track t]. Recognize that each of the N tracks is equally likely to be
requested. Therefore the unconditional probability of selecting any particular track
is 1/N. We can then state:

In the former case, the current track is so close to one end of the disk (track 0
or track N- 1) that only one track is exactly j tracks away. In the second case,
there are two tracks that are exactly j tracks away from track t, and therefore
the probability of a seek of length j is the probability that either of these two
tracks is selected, which is just 2/N.

b. Let Ps [K] = Pr [seek of length K, independent of current track position].


Then:

From part (a), we know that Ps[K/t] takes on the value l/N for 2K of the tracks,
and the value 2/N for (N -2K) of the tracks. So
d. This follows directly from the last equation.

10.4 Define
Ai = Time to retrieve a word from memory level i
Hi = Probability that a word is in memory level i and no higher-level memory
Bi = Time to transfer a block of data from memory level (i + 1) to memory level i
Let cache be memory level 1; main memory, memory level 2, and so on, for total of N levels
of memory. Then, we can write

Now, recognize that if a word is in M1 (cache), it is read immediately. If it is M2 but not in


M1, then a block of data is transferred from M2 to M1 and the read. Thus

A2 = B1 + A1

Further

A3 = B2 + A2 = B1 + B2 + A1

Generalizing,

So

But

Finally

10.5 a. The middle section in Figure 10.9b is empty. Thus, this reduces to the strategy of
Figure 10.9a.

b. The old section consists of one block, and we have the LRU replacement policy.

10.6 The density can be expressed as 1600 characters per inch. Thus the transfer is 1600 x 120 =
192,000 characters per second.

10.7 b. First, let us determine how long it takes to read one physical block plus the gap
to the next block.

Size of physical block = (30 logical records per physical record) x (120 b per
logical record) = 3600 bytes
Length of physical block =

Length of gap = 0.6 in

Transfer time for one block plus gap = seconds

The number of blocks on the tape can be computed as

Thus, the time to read a full tape is 10,105 x 0.02875 = 291 seconds

c. With a blocking factor of 30, the tape holds 10,105 physical records an
10,105 = 303,150 logical records.

d. The effective transfer rate, with a blocking factor of 30:

R =

e. Capacity = 303,150 records x 120 bytes/record = 36,378,000 bytes

10.8 Each sector can hold 4 records. The required number of sectors is 303,150/4 = 75,788 sectors.
This requires 75,788/96 = 790 tracks, which in turn requires 790/110 = 8 surfaces.

10.9 There are 512 bytes/sector. Since each byte generates an interrupt, there are 512 interrupts.
Total interrupt processing time = 2.5 x 512 = 1280 μs. The time to read one sector is:

((60 sec/min) / (360 rev /min)) / (96 sectors/track)


= 0.001736 sec = 1736 μs

Percentage of time processor spends handling I/O:

(100) x (1280/1736) = 74%

10.10 With DMA, there is only one interrupt of 2.5 μs. Therefore, the percentage of time the
processor spends handling I/O is

(100) x (2.5/1736) = 0.14%

10.11 Only one device at a time can be serviced on a selector channel. Thus,

Maximum rate = 800 + 800 + 2 x 6.6 + 2 x 1.2 + 10 x 1 = 1625.6 KBytes/sec


CHAPTER 11

FILE MANAGEMENT

11.1 Fixed blocking: F = largest integer ≤

When records of variable length are packed into blocks, data for marking the record
boundaries within the block has to be added to separate the records. When spanned records
bridge block boundaries, some reference to the successor block is also needed. One possibility
is a length indicator preceding each record. Another possibility is a special separator marker
between records. In any case, we can assume that each record requires a marker, and we
assume that the size of a marker is about equal to the size of a block pointer [WEID87] For
spanned blocking, a block pointer of size p to its successor block may be included in each
block, so that the pieces of a spanned record can easily be retrieved. Then we have

Variable-length spanned blocking: F =

With unspanned variable-length blocking, an average of R/2 will be wasted because of the
fitting problem, but no successor pointer is required:

Variable-length unspanned blocking F =

11.2 a. log2 N/F

b. Less than half the allocated file space is unused at any time.
CHAPTER 12

NETWORKING AND DISTRIBUTED PROCESSING

12.2 a. The probability that it will take exactly i attempts to transmit a frame successfully is
pi-1
(l -p). Thus

N=

b. N=

where f(i) is the total number of frames transmitted if the original frame must be transmitted
i times. This can be expressed as

f(i) = 1 + 4(i -1) = 4i -3

Substituting yields

N=

12.3 Perhaps the major disadvantage is the processing and data overhead. There is processing
overhead since as many as seven modules are invoked to move data from one application
through the communications software to another application. There is data overhead because
of the appending of multiple headers, one for each layer. Another possible disadvantage is
that there must be at least one protocol standard per layer. With so many layers, it takes a long
time to develop and promulgate the standards.

12.4 The network layer could be split into two sublayers, one dealing with issues relating to the
attachment to and use of a single network, and one relating to the routing of data across
multiple networks.
The session and transport layers are both involved in providing end-to-end service to
the OSI user, and could easily be combined.

12.5 The problem is discussed in [KLEI78]. The answer is no. There is no way to be assured that
the last message gets through, except by acknowledging it. Thus, either the acknowledgment
process continues forever, or one army has to send the last message and then act with
uncertainty.

12.6 a. No. This would violate the principle of separation of layers. To layer (N – 1), the
N-level PDU is simply data. The (N – 1) entity does not know about the internal format
of the N-level PDU. It breaks the PDU into segments and reassembles them in the
proper order.

b. Each N-level PDU must retain its own header, for the same reason given in (a).
CHAPTER 13

DISTRIBUTED PROCESS MANAGEMENT

13.3 Pi can save itself the transmission of a Reply message to Pj if Pi has sent a Request message
but has not yet received the corresponding Release message.

13.4 a. If a site i, which has asked to enter its critical section, has received a response from all
the others, then (1) its request is the oldest (in the sense defined by the timestamp
ordering) of all the requests that may be waiting; and (2) all critical sections requested
earlier have been completed. If a site j has itself sent an earlier request or if it was in
its critical section, it would not have sent a response to i.

13.5 The algorithm makes no allowance for resetting the times tamping clocks with respect to each
other. For a given process, Pi for example, clock is only used to update, on the one hand,
request [i] variables in the other processes by way of request messages, and, on the other
hand, token [i] variables, when messages of the token type are transferred. So the clocks are
not used to impose a total ordering on requests. They are used simply as counters that record
the number of times the various processes have asked to use the critical section, and so to find
whether or not the number of times that Pi has been given this access, recorded as the value
of token [i], is less than the number of requests it has made, known to Pj by the value of
requestj [i]. The function max used in the processing associated with the reception of requests
results in only the last request from Pj being considered if several had been delivered out of
sequence.

13.6 a. Mutual exclusion is guaranteed if at anyone time the number of variables token-present
that have the value true cannot exceed 1. Since this is the initial condition, it suffices
to show that the condition is conserved throughout the procedure. Consider first the
prelude. The variable for Pi, which we write token-presenti, changes its value from
false to true when Pi receives the token. If we now consider the postlude for process
Pj that has issued the token, we see that Pj has been able to do so only if token-presentj
had the value true and Pj had changed this to false before sending the token.

b. Suppose that all the processes wish to enter the critical section but none of them has
the
token, so they are all halted, awaiting its arrival. The token is therefore in transit. It
will after some finite time arrive at one of the processes, and so unblock it.

c. Fairness follows from the fact that all messages are delivered within a finite time of
issue. The postlude requires that Pi transfers the token to the first process Pj, found in
scanning the set in the order j = i+l, i+2, ..., n, 1, ..., i-l, whose request has reached Pi;
if the transmission delays for all messages are finite (i.e., no message is lost), all the
processes will learn of the wish for some Pj to enter the critical section and will agree
to this when its turn comes.
CHAPTER 14

SECURITY
14.1 a. T = -seconds = 63.5 hours 2

b. Expect 13 tries for each digit. T = 13 x 4 = 52 seconds.

14.2 a. p = rk

b.

c.

14.3 a. T = (21 x 5 x 21)2 = 4,862,025 1

b. P = T ≈ 2 x 10-7

14.4 Drake is not authorized to read the string directly, so the no-read-up rule will prevent this.
Similarly, Drake is not authorized to assign a security level of sensitive to the back-pocket
file, so that is prevented as well.

14.5 The purpose of the "no write down" rule, or *-property is to address the problem of Trojan
horse software. With the *-property, information cannot be compromised through the use of
a Trojan horse. Under this property, a program operating on behalf of one user cannot be used
to pass information to any user having a lower or disjoint access class.

14.6 In a military environment, an increased level of traffic to and from a particular commander
might serve as a predictor of troop movements. If the adversary can tap into the network at
the point of connection of the commander's system the network, this traffic can be observed
even with both end-to-end and link- by-link encryption.

14.8 The central points should be highly fault-tolerant, should be physically secured, and should
use trusted hardware/software.

14.10 a. Since KUa and KRa are inverses, the value KRa can be checked to validate that Pa
was
correctly supplied: Simply take some arbitrary block X and verify that X = D KRa
[EKUa [X] ].

b. Since the file / etc publickey is puplicly readable, an attacker can guess P (say P') and
compute KRa' = Dp' [ Ep [KUa] ]. now he can choose an arbitrary block Y and
check to see if Y = DKRa [ EKUa [Y] ]. If SO, it is highly probable that P' = P.
Additional blocks can be used to verify the equality .
14.11 Yes.

14.12 Without the salt, the attacker can guess a password and encrypt it. If ANY of the users on a
system use that password, then there will be a match. With the salt, the attacker must guess a
password and then encrypt it once for each user, using the particular salt for each user.

14.13 It depends on the size of the user population, not the size of the salt, since the attacker
presumably has access to the salt for each user. The benefit of larger salts is that the larger the
salt, the less likely it is that two users will have the same salt. If multiple users have the same
salt, then the attacker can do one encryption per password guess to test all of those users.

14.14 a. There are 264 ≈ 1.8 x 1019 possible passwords. At a rate of 6.4 x 106 encryptions per
second, all passwords can be encrypted in a time of 2.8 x 1012 seconds.

14.15 a. The program will loop indefinitely once all of the executable files in the system are
infected.

You might also like