Professional Documents
Culture Documents
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.
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.
There are many ways one can interact with operating system:
▪ Command Language : most operating systems provide the user with the
facility of entering commands directly via an interactive terminal.
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
1. Prompt messages for inputting two file names and reading it from terminal.
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.
# 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);
}
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
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.
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 .
INT 10H functions for manipulating the ROM BIOS video driver.
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.
Examples:
#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
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 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.
With
n n
--- = -----------------
t I + CLE + O
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
Time saved
i2 c2 o2 i4 c4 o4
▪ Examples of such applications are flight control, real time simulations etc. Real
time systems are also frequently used in military application.
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.
Two processes are said to be concurrent if they over lap in their execution.
Precedence Graphs
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 :
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 :
• 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:
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}
R (w:= c + 1) = {c}
W (w:= c + 1) = {w}
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
R(S1) = {x, y}
R(S2) = {z}
W(S1) = {a}
W((S2)= {b}
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
S S count := 2;
2 1 fork L1;
.
.
jo
.
in S 1;
go to L2;
L1: S2;
S L2: join count;
3
count : = count – 1;
+ if count 0 then quit;
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;
S
1
S ………… S
2 n
Sn+
1
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
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.
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:
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
• 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}
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}.
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.
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}
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.
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
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
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.
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
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 :
• 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).
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.
• 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.
• 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
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 :
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 :
• 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:
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}
R (w:= c + 1) = {c}
W (w:= c + 1) = {w}
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
R(S1) = {x, y}
R(S2) = {z}
W(S1) = {a}
W((S2)= {b}
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
S S count := 2;
2 1 fork L1;
.
.
jo
.
in S 1;
go to L2;
L1: S2;
S L2: join count;
3
count : = count – 1;
+ if count 0 then quit;
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;
S
1
S ………… S
2 n
Sn+
1
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
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
• A line Printer driver produces characters which are consumed by the Line
Printer.
Producer-Consumer Processes:
• 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.
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)
Problem Definition:
Entry Section
……
Critical Section
…….
Exit Section
Remainder Section
A solution to the Mutual Exclusion (ME) problem must satisfy the following 3 requirements
:
General structure :
begin
common variable decl.
parbegin
P0;
P1;
parend
end.
1. Algorithm 1:
Pi :
repeat
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 :
Pi : repeat
while flag[j] do skip;
flag[i] := true;
CS;
Flag[i] := false;
Remanider section ;
until false
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.
Algorithm 4 :
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:
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
critical section
lock := false;
remainder section
until false;
repeat
key := true;
repeat
Swap(lock, key)
Until key = false;
critical section
lock := false;
remainder section
until false;
N-Process Software Solutions:
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:
V(S): S: = S + 1;
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;
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:
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.
if S. value < 0
then begin
add this process to S.L;
block;
end;
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
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
A line Printer driver produces characters which are consumed by the Line Printer.
Producer-Consumer Processes:
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.
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)
Problem Definition:
……
Entry Section
……
Critical Section
…….
Exit Section
Remainder Section
A solution to the Mutual Exclusion (ME) problem must satisfy the following 3 requirements
:
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
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 :
Pi : repeat
while flag[j] do skip;
flag[i] := true;
CS;
Flag[i] := false;
Remanider section ;
until false
ME is not Ensured :
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.
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 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.
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.
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.
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.
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.
• 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:
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
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:
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
Page No.273
SYSTEM MODEL :-
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.
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.
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.
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.
• Resource instances:
• Process states:
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
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:
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
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
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)
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.
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.
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.
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).
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
0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 18 19
RR
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
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
P
P1 P2 3
0 8 12 13
P2 P3 P2 P1
P1
0 0.4 1.0 2.0 5.4 13
IDL P3 P1
E P2
0 1 2 6 14
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.
INTRODUCTION
The CPU allocation strategy is the key area of processor management and is
handled by the Operating system.
PROCESSOR SCHEDULING
• 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.
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
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.
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.
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.
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.
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.
Some performance criteria that are frequently used optimizing goal are:
CPU Utilisation:
Throughput:
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.
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)
• CPU scheduling deals with the problem of deciding which of the processes
in the ready queue to be allocated the CPU.
Scheduling Algorithm
P1 P2 P3
0 24 27 30
2. Shortest-job-First (SJF)-Scheduling
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.
Example: If there are 3 processes: P1, P2 and P3 which require the following CPU
time
P1 P2 P3 P1 P1 P1 P1
0 5 10 15 20 25 30 35
• 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 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
PROCESSOR MANAGEMENT
INTRODUCTION
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.
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.
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.
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
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.
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
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.
P1 P2 P3 P1 P1 P1 P1
0 5 10 20 25 30 35
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.
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 :
OS Monitor OS Monitor
25 K
User
Memory
25 K
25 K
30 K
30 K
15 K
:
:
Memory Fragmentation :
• Internal Fragmentation : When the job size is smaller than the block
size.
• External Fragmentation : When block size is too small to accommodate
the job.
1. Compaction
2. Hole Coalescing
3. Swapping
Fetching Strategies : Demand fetching
Anticipatory Fetching
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).
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:
• 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:
• 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 :
1 1 2 3 2 4 3 4 1 1 2 3 5 3 2
t1 t2
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.
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.
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 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.
• 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
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.
• 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.
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
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
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.
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.
• 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.
➢ 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.
• 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.
• 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.
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
Preemptive Scheduling
A scheduling discipline is preemptive if the CPU can be taken away from the process
Q. Consider the following set of processes, with th elength of the CPU burst time given in
milliseconds:
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?
Ans.
(A)
Turn around times for different processes are:
(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
(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.
P1 S=S-1;
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:
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
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.
c.Increase Max for one process: This change may lead to an unsafe state.
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
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.
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
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
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.
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.
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).
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. 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.
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
CHAPTER 1
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.
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.
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.
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.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 :-
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.
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.
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;
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
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.
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.
CHAPTER 7
b)Traffic system
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.
(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.)
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 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
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
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.
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
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 .
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
x0172327103
xx017232710
xxx01773271
xxxx0111327
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.
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
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.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.
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
2.1 The answers are the same for (a) and (b). Assume that although processor operations
cannot overlap, I/a operations can.
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.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
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.
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
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:
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.
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.
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.
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.
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:
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.
var j: 0..n-l;
key: boolean;
repeat
waiting[i] := true;
key := true;
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( ).
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.
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:
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:
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
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:
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
* * * * * *
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:
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.
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
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:
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
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:
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.
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.
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
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:
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
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
10.2 It will be useful to keep the following representation of the N tracks of a disk in mind:
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.
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
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 =
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.
R =
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:
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
10.11 Only one device at a time can be serviced on a selector channel. Thus,
FILE MANAGEMENT
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
With unspanned variable-length blocking, an average of R/2 will be wasted because of the
fitting problem, but no successor pointer is required:
b. Less than half the allocated file space is unused at any time.
CHAPTER 12
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
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
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
14.2 a. p = rk
b.
c.
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.