Purpose Packages Which Are Written To Meet Specific Needs and General - Purpose Packages

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 42

OPERATING SYSTEM

CHAPTER 1

INTRODUCTION

Computer software is a set of step by step instruction called programs that enable computer
hardware to perform its tasks. Computer software can be classified or categorized as system
software and application software

Application Software handles the needs of the end-user. They can also be categorized as Special
purpose packages which are written to meet specific needs and general - purpose packages
which are used for wide variety of purpose e.g. word processors, spreadsheet e.t.c

System software manages the operation of the computer itself and consists of the Operating
systems and system utilities which are more of service software. Utility software is used to carry
out routine jobs e.g. sorting, merging e.t.c, diagnose and repair computer problem and assist In
helping the computer’s to run more efficiently. Utility programs include text editors, language
translators, linkers & loaders, sort & merge utilities and diagnostic tools

Operating Systems (OS)


Operating system is a layer of software on top of the bare hardware that is used to manage all
parts of the computer hardware and present the user with an interface or virtual machine that is
easier to understand and program. The OS perform two basic functions namely:
 OS as an extended machine/ virtual machine
 OS as a resource manager

Operating System as an Extended Machine / Virtual Machine


The operating system masks the details of the hardware from the programmer and provides the
programmer with a convenient interface for using the system. It acts as mediator, making it easier
for the programmer and for application programs to access and use those facilities and services

Operating System as a Resource Manager


Modern computers consist of processors, memories, timers, network interface, and printer among
others. The job of the o/s is to provide for an orderly and controlled allocation of processors,
memories and 1/0 devices, files among various programs competing for them.
The OS:-
 Keeps track of who is using which resource
 Grant resource requests
 Account for usage
 Mediate conflicting requests from different programs and users.

Example/ illustration
Different programs running want to print their output simultaneously on the same printer. The OS
can bring order by buffering all the output designed for the printer on the disk. When the program
is finished, OS can then copy the output from the disk file to the printer while at the same time
the other program can continue generating more output.

1
Principles of Operating Systems
OPERATING SYSTEM

History of Operating Systems

History of operating systems has been closely tied to the architecture of the computers on which
they run.

First Generation OS - Age: Vacuum Tubes and Plug boards


In these days, a single group of people designed, built, programmed, operated and maintained
each machine. All programming was in absolute machine language and often by wiring up plug
boards to control the machine’s basic functions. No operating system was used. The mode of
operation required the programmer to sign up for a block of time on sign up sheet, go to machine
room then insert his/her plug board into computer and wait hoping that no vacuum tube burn out
during the run

All problems were numerical calculation. There was the introduction of punched cards in 1950s
(the cards allowed program writing to take place via holes instead of plug boards)

Second Generation OS - Age: Transistors and Batch systems


Computers could be manufactured and sold. To run a job /program:
 Programmer would write the program on paper ( in FORTRAN or Assembly)
 Punch it on cards
 Take it to the input room and hard it to the operation
 When computer finished the job, operator go to printer, tear off the output
 Take it to output room for programmer to connect

Much time was wasted with the operators walking and the machine room. Hence the solution was
the introduction of batch system.

Batch System involved:


 Collection of a tray full of jobs in the input room
 Reading them onto a magnetic tape using a small inexpensive computer e.g. IBM 1401-
good at reading cards , coping tapes and printing out – but poor at numerical calculator
 After collection of a batch of jobs, the tape was rewound and brought to the machine
room. The tape mounted on a tape drive.
 Operator then loaded a special program (ancestor of today’s O/S) which read the 1 st job
from tape and ran it.
 Output written a second tape instead of being printed.
 O/S’s automatically read the next job from tape, after the previous job finished
 Once the whole batch was done, operator removed, I/O and replaced with next batch.
 Output tape brought to an IBM 1401 for printing OFF-LINE (i.e. not connected to the
main computer)

2nd generation-computers were mostly for scientific and engineering calculations. They were
programmed in FORTRAN and assembly language.

Advantages of batch system


 Much of the operator work was done by computer
 Increased performance since it was possible for job to start as soon as the previous job
ended.

2
Principles of Operating Systems
OPERATING SYSTEM

Disadvantages
 turn- around time can be large from user standpoint
 more difficult to debug program
 Due to lack of protection scheme, one batch job can affect pending jobs A job could
corrupt the monitor i.e. affecting pending j0b
 A job could enter an infinite loop
i.e. there is no protection scheme to prevent a job from adversely affecting others

Third Generation OS – Age: ICs and Multiprogramming


Early 1960s most computer manufactures had two distinct product lines: Word- oriented, large-
scale scientific computers used for numerical calculation in scientific and engineering fields and
Character- oriented, commercial computers widely used for tape sorting and printing by banks
and insurance companies. The solution was the creation of system/360 which was software
compatible machines ranging from 1401- sized to powerful 7094. They differed in price and
performance (maximum memory, processor speed, number of input/output devices permitted).
Program written for one machine could run on all others because they had same architecture and
instruction set. It was designed to handle scientific and commercial computing. The weakness of
was that all software had to work on all models resulting in an enormous and extra ordinary OS
two to three times larger than FMS. Consisted millions of lines of assembly language written by
thousands of programmer.

OS/360 and other 3rd generation O/S popularized several key techniques which included
multiprogramming, spooling and time-sharing.
Multiprogramming
Memory was partitioned into several pieces with a different job in each partition .While one job
was waiting for I/O to complete another job could be using the CPU; enough jobs held in memory
at once, and could keep the CPU busy nearing 100% of the time.
Spooling
It became possible to have the computer read a deck of cards to a tape, drum or disk and to write
out a tape printer while it was computing allowing job from the disk into the now –empty
partition and run. This technique is called SPOOLing:
S – Simultaneous
P – Peripheral
O – Operation
O – On
L – Line
The advantage this technique was that the output from jobs was available as soon as the job was
completed.
Time sharing – it is a variant of multiprogramming. The idea of multiprogramming was extended
to allow for multiple terminals to be connected to the computer with each in-use terminal being
associated with one or more jobs on the computer. The O/S is responsible for switching between
the jobs/processes in such a way that favored user interaction.
Fourth Generation OS – Age: Personal Computers
Computers become small and inexpensive that individuals could own one. Most software were
user friendly. Systems developed included:
a) Real-Time
b) Multiprocessor
c) Distributed
d) Network
3
Principles of Operating Systems
OPERATING SYSTEM

CHAPTER 2

OPERATING SYSTEMS STRUCTURE

Introduction
The kernel is the central component of most computer operating systems (OS). Its
responsibilities include managing the system's resources (the communication between
hardware and software components). As a basic component of an operating system, a
kernel provides the lowest-level abstraction layer for the resources (especially memory,
processors and I/O devices) that application software must control to perform its function.
It typically makes these facilities available to application processes through inter-process
communication mechanisms and system calls.

System Call
System calls basically provide the interface between the operating system and its application
programs. The heart of any operating system is the set of system calls that it can handle. These
tell what the operating system really does.

How a system call can be made


The system calls (services) provided by OS are requested by putting parameters in well-defined
places e.g. registers or stack. Special trap instruction called Kernel Call/supervisor call is
executed. The instruction switches the machine from user mode to Kernel mode, transferring
control to the OS. OS examines the parameters of the call to determine which call is to be carried
out. OS indexes into a table that contains slots to identify service procedure. Service procedure is
then called. When work is completed, a system call is finished and control is given back to user
program.

User Program
2
User Program User programs
run in
1
Kernel user mode
Call
M
A 4
I
N

M 1 3
E Service procedure O/S runs in
M Kernel mode
O
R
Y
Dispatch Table
2

1 – User program traps to the kernel 3 – O/S call service procedure


2 – O/S determines service number required 4 – Control is returned to user program

Fig. 2.1: How a system can be made

4
Principles of Operating Systems
OPERATING SYSTEM

Operating Systems Structures


There are four designs that have been tried in practice and they include:
a) Monolithic systems
b) Layered systems
c) Virtual machines
d) Client-server systems

Monolithic systems
The structure is that there is no structure. The operating system is written as a collection of
procedures, each of which can call any of the other ones whenever it needs to.
This organization suggests a basic structure for the operating system.
i) A main program that invokes the requested service procedure
ii) A set of service procedures that carry out the system calls
iii) A set of utility procedures that help the service procedures.

In this model, for each system call there is one service procedure that takes care of it. The utility
procedures do things that are needed by several service procedures, such as fetching data from
user programs (fig: 2.2).

Main
Procedure

Service
Procedure

Utility
Procedure

Fig. 2.2: A simple structure model for a monolithic system

Layered Systems
A generalization of the approach of fig. 2.2 is to organize the operating system as a hierarchy of
layers, each one constructed upon the one below it. The first system constructed in this way was
the THE built at the Technische Hogeschool Eindhoven (THE) in the Netherlands by Dijkstar
(1968) and his students. The THE system was a simple batch system for a Dutch computer. The
system had 6 layers as shown in Fig. 2.3

Layer Function
5 The operator
4 User programs
3 Input/output management
2 Operator-process communication
1 Memory and drum management
0 Processor allocation and multiprogramming

Fig. 2.3: structure of the THE operating system

A further organization of the layering concept was present in the MULTICS systems. Instead of
the layers, MULTICS was organized as a series of concentric rings with the inner ones being
more privileged than the outer ones. The advantage of the ring mechanism is that it can easily be
extended to structure user subsystems. For example, a professor could write a program to test and
5
Principles of Operating Systems
OPERATING SYSTEM

grade student programs and run this program in a ring n, with the student programs running in
ring n+1 so that they could not change their grades.

Virtual Machines
This system was based on a state observation: a time sharing system providing
Multiprogramming and an extended machine with a more convenient interface than the bare
hardware. The essence of VM/370 is to completely separate these two functions.

The heart of the system, known as the virtual machine monitor, runs on the bare hardware and
does the multiprogramming, providing not one but several virtual machines to the next layer up
(Fig.2.4). However, unlike all other operating systems, these virtual machines are not extended
machines, with files and nice features; instead, they are exact copies of the bare hardware
including kernel/user model, I/O, interrupts, and everything else the real machine has. Because
each virtual machine is identical to the true hardware, each one can run any operating system that
will run directly on the bare hardware.

Virtual 370s

System calls here

I/O instruction Trap here


CMS CMS CMS
Trap here
VM/370

370 Bare hardware

Fig. 2.4: the structure of VM/370 with CMS

CMS is a single-user, interactive system called Conversational Monitor system (CMS) for time
sharing users. When a CMS program executes a system call, the call is trapped to the operating
system in its own virtual machine. CMS then issues the normal hardware I/O instructions for
reading or doing whatever is needed to carry out the call. These I/O instructions are trapped by
VM/370, which then performs them as part of its simulation of the hardware.

By making a complete separation of the functions of multiprogramming and providing an


extended machine, each of the pieces can be much simpler, more flexible and easier to maintain.

The idea of a virtual machine is heavily used nowadays in a different context: running old MS-
DOS programs on a Pentium (or other 32-bit Intel CPU). It is used by WINDOWS, OS/2 and
other operating systems for running MS-DOS.

Client-Server Model
VM/370 gains much simplicity by moving a large part of the traditional OS code (implementing
the extended machine) into a higher layer, CMS. Nevertheless, VM/370 itself is still a complex
program because simulating a number of virtual 370s is not that simple (especially if you want to
do it reasonably efficiently)

A trend in modern OS is to take this idea of moving code up into higher layers even further and
remove as much as possible from the operating system, leaving a minimal Kernel. The usual
approach is to implement most of the OS functions in user processes. To request a service, such
6
Principles of Operating Systems
OPERATING SYSTEM

as reading a block of a file, a user process (now known as the client process) sends the request to
a server process, which then does the work and sends back the answer.

Client Client Process Terminal File Memory User mode


Process process server server server server

Kernel mode
Kernel

Client obtains service by


sending massages to server
process.
Fig. 2.5: The client - server model

In this model (Fig. 2.5), all the kernel does is handle the communication between clients and
servers.
Advantages of client-server model
 By splitting the OS up into parts, each of which only handles one facet of the system such
as file service, process service, terminal service e.t.c, each part becomes small and
manageable.
 Furthermore, because all the servers run as user-mode processes, and not kernel mode,
they do not have direct access to the hardware. As a consequence, if a bug in the file
server is triggered, the file service may crash, but this will not usually bring the whole
machine down.
 Another advantage is its adaptability to use distributed systems (Fig. 2.6). if a client
communicates with a server by sending it messages, the client need not to know whether
the message is handled locally in its own machine, or whether it was sent across a
network to a server on a remote machine. As far as the client is concerned, the same thing
happens in both cases: a request was sent and a reply came back.

Machine 1 Machine 2 Machine 3 Machine 4

Client File server Process server Terminal server

Kernel Kernel Kernel Kernel

Network
Message from
Client to server

Fig. 2.6: The client-server model in a distributed system

7
Principles of Operating Systems
OPERATING SYSTEM

CHAPTER 3

1.0 PROCESSES

What is a process?
The design of an operating system must reflect certain requirements. All multiprogramming
operating systems from single-user to multi- user are built around the concept of the process.
Most requirements that the operating system must meet can be expresses with reference to
processes:
 The operating system must interleave the execution of multiple processes, to maximize
processor utilization while providing reasonable response time.
 The operating system must allocate resources to processes in conformance with a specific
policy. ( e.g. certain functions or applications are of higher priory while at the same time
avoiding deadlock)
 The operating system may be required to support interprocess communication and user
creation of processes, both of which may aid in the structuring of applications.
There are several definitions of the term process. They include:
 A program in execution
 An instance of a program running on a computer
 The entity that can be assigned to and executed on a processor
 A unit of activity characterized by the execution of a sequence of instructions, a current
state, and an associated set of system resources.

Process states
Behavior of an individual process can be characterized by listing the sequence of instructions that
execute for that process. Such a listing is referred to as a trace of the process.

A Three-State process model


In this model (Fig. 3.1), a process may be in:
i) Running – using the CPU at that instant
ii) Ready – runnable; temporarily stopped to let another process run
iii) Blocked – unable to run until some external event happens.

Four transitions are possible among these three states as shown in Fig 3.1

Running
1 2

3 Ready
Blocked

Transition states
1 – Process blocks for input
2 – Scheduler picks another process
3 – Scheduler picks this process
4 – Input becomes available
Fig. 3.1: A Three-State Model
8
Principles of Operating Systems
OPERATING SYSTEM

A Five-State Model
The five states are as follows (Fig. 2.2):
 Running – the process that is currently being executed.
 Ready – a process that is prepared to be executed when given opportunity
 Blocked – a process that can not execute until some event occurs
 New – a process that has just been created but has not yet been admitted to the pool of
executable processes by the operating system.
 Exit – A process that has been released from the pool of executable processes by the
operating system.

Dispatch
Admit Release
New Ready Runnin Exit
Timeout g

Event
Occurs Event wait

Blocke
d
Fig. 3.2: A Five-State model

Possible transition states in the above model are as follows:


 Null – New: A new process is created to execute a program
 New – Ready: the operating system will move a process from the New state to Ready
 Ready – Running: when it is time to select a new process to run, the operating system
chooses one of the processes in the ready state.
 Running – Exit: The currently process is terminated
 Running – Ready: the scheduler picks another process; when the running process has
reached the maximum allowable time for uninterrupted execution- the process is
preempted.
 Running – Blocked: A process blocks for input – if it requests for something which it
must wait
 Blocked – Ready: When the event for which it has been waiting occurs
 Ready – Exit: In some systems, a parent may terminate a child process at any time. Also
if a parent terminates, all child processes associated with it may be terminated.
 Blocked – Exit: The comments under the preceding item apply.

N/B: The three principal states are Ready, Running, Blocked.

Suspended Processes

9
Principles of Operating Systems
OPERATING SYSTEM

A process is suspended when it is swapped out of the main memory to secondary. A process that
is not in the main memory is not immediately available for execution, whether or not it is
awaiting an event.

A process can be in a Blocked/ Suspended state when it is in secondary memory and awaiting an
event. A process can be in a Ready/Suspended state when it is in secondary memory but is
available for execution as soon as it is loaded into main memory.

A suspended process can be defined as having the following characteristics:


 The process is not immediately available for execution.
 The process may or may not be waiting for an event. If it is, this blocked condition is
independent of the suspended condition, and occurrence of the blocked event does not
enable the process to be executed.
 The process was placed in a suspended state by an agent: either itself, a parent process or
the operating system, for the purpose of preventing its execution.
 The process may not be removed in this state until the agent explicitly orders the
removal.

Reasons for process suspension:


 Swapping – the OS needs to release sufficient main memory to bring in a process that is
ready to execute
 Other OS reasons – The OS may suspend a background or utility process or a process
that is suspecting of causing a problem
 Interactive user request – a user may wish to suspend execution of a program for purpose
of debugging or in connection with the use of a resource.
 Timing – A process m be executed periodically
 Parent process request

1.1 CONCURRENCY

The central themes of operating system design are all concerned with the management of
processes and threads:
 Multiprogramming: the management of multiple processes within a uniprocessor system.
 Multiprocessor: the management of multiple processes within a multiprocessor
 Distributed processing: the management of multiple processes executing on multiple,
distributed computers.
Fundamental to all of these areas, and fundamental to operating system design, is concurrency.
Concurrency encompasses a host of design issues, including:
 Interprocess communication
 Sharing of and competing for resources
 Synchronization of the activities of multiple processes
 Allocation of processor time to processes ( scheduling)

Concurrency arises in three different contexts:


 Multiple applications
 Structured applications
 Operating system structure

Interprocess Communication
Processes frequently need to communicate with other processes. Issues related to Interprocess
Communication (IPC) include:
- How one process can pass information to another
10
Principles of Operating Systems
OPERATING SYSTEM

- Making sure two or more processes do not get into each other’s way when engaging in
critical activities.
- Proper sequencing when dependencies are present: if process A produces data and
process B prints it, B has to wait until A has produced some data before starting to print.
Some key terms related to concurrency
• Race Condition: situation where processes are reading or writing some shared data and
final results depends on who runs precisely when; it occurs when multiple processes or
threads read and write data items so that the final results depends on the order of
execution of instructions in the multiple processes.
- Race conditions are avoided by use of mutual exclusion
• Mutual exclusion: way of making sure that if one process is using a shared variable or
file, the other processes will be excluded from doing the same thing i.e. no two processes
are ever in their critical section at the same time.
• Critical section/region – that part of the program where the shared memory is accessed.
• Starvation – a situation in which runnable process is overlooked indefinitely by the
scheduler; although it is able to proceed, it is never chosen
• Livelock – a situation in which two or more processes continuously change their state in
response to changes in the other processes) without doing any useful work.

1.1.1 Concurrency: Mutual exclusion


Mutual exclusion with Busy Waiting
Various proposals for achieving mutual exclusion include:
a) Disabling interrupts
b) Lock variables
c) Peterson’s solutions
d) The TSL(Test and Set Lock) Instruction
Disabling Interrupts
This is the simplest solution where by each process’s interrupts are disabled just after entering its
critical region and re-enable the m just before leaving it. With interrupts disabled, no clock
interrupts can occur. The CPU is only switched from process to process as result of clock or other
interrupts. (NB: is a hardware solution)
Disadvantage:
 The power to turn off the interrupts is given to the user processes. Suppose that one of
them did it and never turned them on again, that could be the end of the system.
 Further more, if the system is a multiprocessor, disabling interrupts affects only the CPU
that executed the disable instruction.
Lock Variables
This is a software solution. Consider having a single, shared, (lock) variable, initially 0. When a
process wants to enter its critical region, it first tests the lock. If the lock is 0, the process sets it to
1 and enters the critical region. If the lock is already 1, the process just waits until it becomes 0.
Thus, a 0 means that no process is in its critical region, and a 1 means that some process is in its
critical region.
Strict Alternation
A program fragment written in C; a proposed solution to the critical region problem.
While (TRUE) { While (TRUE) {
while (turn! = 0) /* wait */; While (turn! = 1) /* wait */;
critical_region(); critical_region();
turn = 1; turn = 0;
noncritical_region(); noncritical_region();
} }
(a) (b)
11
Principles of Operating Systems
OPERATING SYSTEM

Fig 3.4: A proposed solution to the critical region problem.


In the Fig 3.4, the integer variable turn, initially 0, keeps track of whose turn it is to enter the
critical region and examine or update the shared memory. Initially, process 0 inspects turn, finds
it to be 0, and enters its critical region. Process 1 also finds it to be 0 and therefore sits in a tight
loop continually testing to see when it becomes 1. When process 0 leaves the critical region, it
sets turn to 1, to allow process 1 to enter its critical region.
This solution requires that the two processes strictly alternate in entering there critical regions.
Peterson’s Solution
This solution combines the idea of taking turns with the idea of lock variables and warning
variables. It is also software solution. It does not require strict alternation
Special machine Instruction approach - The TSL Instruction
This proposal requires a little help from the hardware. Many computers especially those designed
with multiple processors have TSL.
How it works:
It reads the contents of the memory word into register and then stores a nonzero value at that
memory address. The operations of reading the word and storing into it are guaranteed to be
indivisible – no other processor can access the memory word until the instruction is finished. The
CPU executing the TSL instruction looks the memory bus to prohibit other CPUs from accessing
memory until it is done.
Advantages
The se of a special machine instruction to enforce mutual exclusion has a number of advantages:
 It is applicable to any number of processes on either a single processor or multiple
processors sharing main memory.
 It is simple and therefore easy to verify
 It can be used to support multiple critical sections; each critical section can be defined by
it own variable
Disadvantage
 Busy waiting is employed: Thus, when a process is waiting for access to a critical section,
it continues to consume processor time; when a process wants to enter its critical region,
it checks to see if the entry is allowed. If not, the process just sits in a tight loop waiting
until it is; i.e. continuously testing a variable until some value appears. This approach
wastes CPU time. ( busy waiting is also employed in Peterson’s solution, Lock variable,
Interrupt disabling, Strict alternation)
 Starvation is possible: When a process leaves a critical section and more than one process
is waiting, the selection of waiting process is arbitrary. Thus some process could
indefinitely be denied access.
 Deadlock is possible

Concurrency: synchronization
Sleep and Wakeup
Note: Because of the drawback pf both the software and hardware solutions just outline, other
mechanism which are operating system and programming language mechanisms can be used to
provide concurrency. This mechanism / Interprocess Communication Primitives that block
instead of wasting processor time when they are not allowed to enter their critical regions. One of
the simplest is the pair SLEEP and WAKEUP. SLEEP is a system call that causes the caller to
block, thus be suspended until another process wakes it up. The WAKEUP call has one
parameter, the process to be awakened.

Producer-Consumer Problem
Consumer-Producer problem (also known as the bounded buffer problem) illustrates how these
primitives can be used.

12
Principles of Operating Systems
OPERATING SYSTEM

Two processes share a common, fixed-size buffer. One of them, the producer, puts information
into the buffer, and the other one, the consumer, takes it out. Trouble arises when the producer
wants to put a new item in the buffer, but it is already full. The solution is for the producer to go
to sleep, to be awakened when the consumer removed one or more items. Similarly, if the
consumer wants to remove an item from the buffer and sees the buffer empty, it goes to sleep
until the producer puts something in the buffer and wakes it up. To keep track of the number of
items in the buffer, a variable, count, is used. If the maximum number of items the buffer can
hold is N, the producer’s code will first test to see if count is N, if it is, the producer will go to
sleep; if not, the producer will add an item and increment count. The consumer’s code is similar:
first test count to see if it is 0. If it is, go to sleep; if it is nonzero, remove an item and decrement
the count. Each of the processes also tests to see if the other should be sleeping, and if not, wakes
it up.
This approach sounds simple enough, but it leads to race condition. It can occur because access to
count is unconstrained. The following situation could possibly occur. The buffer is empty and the
consumer has just read count to see if it is 0. At that instant, the scheduler decides to stop running
the consumer temporarily and start running the producer. The producer enters an item in the
buffer, increments count, and notices that it is now 1. The producer calls wakeup to wake the
consumer up. Unfortunately, the consumer is not yet logically asleep, so the wakeup signal is lost.
When the consumer next runs, it will test the value of count it previously read, find it to be 0, and
go to sleep. The producer will fill up the buffer and also goes to sleep. Both will sleep forever.
The essence of the problem here is that a wakeup sent to a process that is not (yet) sleeping is
lost.
Semaphores
To solve the above problem, Dijkstra (1965) suggested using integer variable to count the number
of wakeups saved for future use. A new variable type called a semaphore was introduced. A
semaphore could have the value 0, indicating that no wakeups were saved, or some positive value
if one or more wakeups were pending. With the operating system briefly disabling all interrupts
while it is testing the semaphore, updating it, and putting the process to sleep, if necessary. If
multiple CPUs are being used, each semaphore should be protected by a lock variable, with the
TSL instruction used to make sure that only one CPU at a time examines the semaphore.
Semaphore can be used in two different ways:
 Mutual exclusion – it is designed to guarantee that only one process at a time will be
reading or writing the buffer and the associated variables.
 Synchronization – to guarantee that certain event sequences do or do not occur. In this
case, they ensure that the producer stops running when the buffer is full, and the
consumer stops running when it is empty. This use is different from mutual exclusion.
Monitors
Semaphores provide a primitive yet powerful and flexible tool for enforcing mutual exclusion and
coordinating processes however, with semaphores, deadlocks (to be discusses later) can easily
occur. Further more, it may be difficult to produce a correct program using semaphores. To make
it easier to write correct programme, Hoare (1974) and Hansen (1975) proposed a higher level
synchronization primitive called a monitor.
A monitor is a collection of procedures, variables, and data structures that are all grouped
together in a special kind of module or package. Processes may call the procedures in a monitor
whenever they want to, but they cannot directly access the monitor’s internal data structures from
procedures, declared outside the monitor.
Monitors are programming language construct, so the compiler knows they are special and can
handle calls to monitor procedures differently from other procedure calls.

13
Principles of Operating Systems
OPERATING SYSTEM

They have important property that makes them useful for achieving mutual exclusion: only one
process can be active in a monitor at any instant. It is up to the compiler to implement the mutual
exclusion on monitor entries.
A monitor supports synchronization by the use of condition variables that are contained within
the monitor and accessible only within the monitor. Condition variables are a special data type in
monitors, which are operated on by two functions:
 WAIT: suspend execution of the calling process on condition – thus this action causes the
calling process to block. The monitor is now available for use by another process.
 SIGNAL: resume execution of some process blocked after a WAIT on the same
condition.
Note: the monitor WAIT and SIGNAL operations are different from those for semaphore. If a
process in a monitor signals and no task is waiting on the condition variable, the signal is lost.
The condition variables are not counters, they do not accumulate signals for later use the way
semaphores do.
Disadvantages:
 With monitors, you need a language that has them built in. a few languages have them,
but they are rare.
 They were designed to for solving the mutual exclusion problem on one or more CPU
that all have access to a common memory. (Same with semaphores).
Massage passing
Semaphores are too low level synchronization and monitors are not usable except in a few
programming languages. Also, none of them provide for information exchanges between
machines. When processes interact with one another, two fundamental requirements must be
satisfied: synchronization and communication. Processes need to be synchronized to enforce
mutual exclusion; cooperating processes may need to exchange information. One approach to
providing both of these functions is message passing.
A further advantage of message passing is that it lends itself to implementation in distributed
systems as well as in shared-memory multiprocessor and uniprocessor systems.

The actual function of message passing is normally provided in the form of a pair of primitives
SEND and RECEIVE, which, like semaphores and unlike monitors, are system calls rather than
language constructs. As such, they can easily be put into library procedures, such as

Send (destination, message)


Receive (source, message)
A process sends information in the form of a message to another process designated by a
destination. A process receives information by executing the receive primitive, indicating the
source and the message.
Design Issues for Message Passing Systems
Messaging passing have many challenging problems and design issues that do not arise with
semaphores or monitors, especially if the communicating processes are on different machines
connected by a network.

 Lost message – to guard against lost message, a special acknowledgment message can
used. If the sender has not received the acknowledgement within a certain time interval, it
retransmits the message.
 `Addressing – it is necessary to have a way of specifying in the send primitive which
process is to receive the message. Similarly, most implementations allow a receiving
14
Principles of Operating Systems
OPERATING SYSTEM

process to indicate the source of message to be received. When data structures called
mailboxes are used, the address parameters in SEND and RECEIVE calls are mailboxes
and not processes. (a mailbox is a place to buffer a certain number of messages, typically
specified when the mailbox is created). When a process tries to send a message to
mailbox that is full, it is suspended until a message is removed from that mailbox,
making room for a new one.
 Dealing with the question of how processes are named, so that the process specified in a
SEND or RECEIVE call is unambiguous.
 Authentication – dealing with how the client can tell that he is communicating with the
real file server and not with an imposter.
 Queuing discipline – the simplest is first-in-first-out, but this may not be sufficient if
some messages are more urgent than others. An alternative is to allow the specifying of
message priority, on the basis of message type or designation by the sender. Another
alternative is to allow the receiver to inspect the message queue and select which message
to receive next.

1.2 PROCESS SCHEDULING


When more than one process is runnable, the operating system must decide which one to run first.
The part of the operating system that makes this decision is called the scheduler; the algorithm it
uses is called the scheduling algorithm. The scheduler is concerned with deciding policy, not
providing mechanism.
What constitutes a good scheduling algorithm?
i) Fairness – make sure each process gets its fair share of the CPU.
ii) Efficiency – keep the CPU busy 100 percent of the time.
iii) Response time – minimize response time for interactive users.
iv) Turnaround – minimize the time batch users must wait for output.
v) Throughput – maximize the number of jobs processed per hour.
Preemptive scheduling is the strategy of allowing processes that are logically runnable to be
temporary suspended.
Non preemptive scheduling is run to completion method of the early batch system.
Scheduling algorithms
 Round robin scheduling
 Priority scheduling
 Shortest job first (SJF)
 Multiple queues
 Guaranteed scheduling
 Lottery scheduling
 Real-Time scheduling
 Two-level scheduling

Round Robin Scheduling


It is one of the oldest, simplest, fairest and widely used. Each process is assigned a time interval
called its quantum, which it is allowed to run. If the process is still running at the end of the
quantum, the CPU is preempted and given to another process. If the process has blocked or
finished before the quantum has elapsed, the CPU switching is done. Round robin is easy to
implement. The scheduler maintains a list of runnable processes (Fig 3.7(a)) when the process
uses up its quantum; it is put on the end of the list (Fig3.7 (b)).

Current Next Current


process process process
15
Principles of Operating Systems
OPERATING SYSTEM

B F D G A F D G A B

(a) (b)

Fig. 3.7: Round robin scheduling. (a) The list of runable processes. (b) the list of runnable
processes after B uses up its quantum.

Limitations:
 the length of the quantum. Switching from one process to another requires a certain
amount of time for doing administration. Setting the quantum too short causes too many
process switches and lowers the CPU efficiency, but setting it too long may cause poor
response to short interactive requests.
Priority Scheduling
Round robin scheduling makes the implicit assumption that all processes are equally important.
At university the pecking order may deans first then professors, secretaries, janitors and finally
students. The need to take external factors into account leads to priority scheduling.

Even on a pc with a single owner there may be multiple processes some more important than
others. For example a daemon process sending electronic mail in the background should be
assigned a lower priority than a process displaying a video film on the screen in real time.

To prevent high priority from running indefinitely the scheduler may decrease priority of the
currently running process at each clock tick(i.e. at each clock interrupt).

Priorities can be assigned to processes statically or dynamically. On military computers processes


started by generals might begin at priority 100, processes started by colonels at 90, majors at 80,
captains at 70, lieutenants at 60, and so on. Alternatively at commercial computer center high
priority jobs might cost 100 dollars an hour, medium priority 75 dollars an hour, and low priority
50 dollars an hour.

Shortest job first


When several equally important jobs are sitting in the input queue waiting to be started the
scheduler should use shortest job first. Look at fig.2-24. Here we find four jobs A, B, C, and D
with run times of 8, 4, 4, and 4 minutes respectively. By running them in that order the
turnaround time for A is 8 minutes, for B is 12 minutes, for C is 16 minutes, and for D is 20
minutes for an average of 14 minutes.

1.3 DEADLOCKS
Definition – a set of process is deadlocked if each process in the set is waiting for an event that
only another process in the set can cause.
Resources
A resource is anything that can only be used by a single process at any instant.
Computer systems are full of resources that can only be used by one process at a time. Examples
of these resources include: Plotters, CD-ROM readers, CD-ROM recorders e.t.c.
Types of Resources
i) Preempt able resources – one that can be taken away from the process owning it with
no ill effect e.g. memory
ii) Non-preempt able resources – one that can not be taken away from its current owner
without causing the computation to fail. Examples: printers e.t.c
16
Principles of Operating Systems
OPERATING SYSTEM

Deadlocks involve non-preemptive resources


The conditions for Deadlock
1. Mutual exclusion. Only one process may use a resource at a time. No process may access
a resource unit that has been allocated to another.
2. Hold and wait. A process may hold allocated resource while waiting assignment of other
resources.
3. No preemption. No resources can be forcibly removed a process holding it.
4. Circular wait. A closed chain processes exists such that each process holds at least one
resource needed by the next process in the chain

Deadlock Prevention
Mutual Exclusion
In general the first of the four listed conditions cannot be disallowed. If access to a resource
requires mutual exclusion must be supported by the operating system. Some resources such as the
files may allow multiple access for reads but only exclusive access for writes.
Hold and Wait
The hold and wait condition can be prevented by requiring that a process request all of its
required resources at one time and blocking the process until all requests can be granted
simultaneously.
No Preemption
This condition can be prevented in several ways. First if a process certain resource is denied a
further request that process must release its original resource and if necessary request them again
together with the additional resources. Alternatively if a process requests resource that is
currently held by anther process the operating system may preempt the second process and
require it to release its resources. This latter scheme would prevent deadlock only if no two
processes possessed the same priority.
Circular Wait
The circular wait condition can be prevented by defining a linear ordering of a resource type. If a
process has been allocated resources of a type R, then it may be subsequently request only those
resources of types following R in the ordering.

17
Principles of Operating Systems
OPERATING SYSTEM

CHAPTER 4

2.0 MEMORY MANAGEMENT

Memory manager – part of the OS that manages the memory hierarchy.

Functions of memory manager


 Keep track of which parts of memory are in use and which parts are not in use
 Allocate memory to processes when they need it and deallocate it when they are done.
 Manage swapping between main memory & desk when main memory is too small to hold
all the processes.
Basic Memory Management
Allocation Techniques
Memory management systems can be divided into two categories:-
 Those that move processes back and forth between main memory and disk during
execution.
 Those that don’t page or swap.
2.1 Mono-programming without swapping or paging
It is one of the simplest memory management scheme i.e. only one programme/process can run at
a time, sharing the memory between that program and the OS.

Operating Device drivers in


User System in ROM
Program ROM

User program
User
Program
Operating Operating system
system RAM in RAM
0 0 0
(a) (b) (c)

Fig 4.1: Simple ways of organizing memory with an operating system and one user process

(a) Operating system may be at the bottom of memory in RAM


(b) ROM at the top of memory
(c) The device drivers may be at the top of memory in a ROM and the rest of the system in
RAM below.
(Used by small MS-DOS systems)
2.2 Multiprogramming with fixed partitions
Multiprogramming increases the CPU utilization.
How multiprogramming is achieved:
- Memory is divided into partitions
- When a job arrives, it is put into the input queue for the partition to hold it.
- Since the partitions are fixed, any space in a partition not used by a job is lost.

18
Principles of Operating Systems
OPERATING SYSTEM

Multiple input
queues
Partition 4 Partition 4
700k
Single input
Partition 3 queue Partition 3

400k
Partition 2
Partition 2

Partition 1 Partition 1
100k
Operating Operating
system system
0
(a) (b)

(a) fixed memory partition with separate input queues for each partition
Disadvantages: when the queues for a large partition is empty but the queue for a small
partition is free e.g. partition 3&1

(b) fixed memory partitions with a single input queue


- Whenever a partition is free, the job closest to the front of the queue that fits in it could
be loaded into the empty partition & run.

Note: Sometimes there is not enough main memory to hold all current active processes,
excess processes must be kept on disk and be brought in to run dynamically. Two general
approaches to memory management strategies can be used:
 Swapping
 Virtual memory

2.3 Swapping
Swapping consists of bringing in each process in its entirety, running it for a while, then putting it
back on the disk. Fig. 4.3

(a) Only process A is in memory.


(b) Process B is created and swapped in from disk
(c) Process C is created and swapped in form disk
(d) A terminates or is swapped out to disk
(e) D comes in
(f) B goes out
(g) E comes in

19
Principles of Operating Systems
OPERATING SYSTEM

Illustration

C C C C C

B B B B E

A A A D D D

O/S O/S O/S O/S O/S O/S O/S


(a) (b) (c) (d) (e) (f) (g)

Fig. 4.3: Memory allocation changes as processes come into memory and leave it. Shaded region
are unused memory.

Memory compaction: a technique used when multiple holes created by swapping are combined
into one big one by moving all the processes downward as far as possible.

Overlays
Years ago, programs that were too big to fit in the available memory were split into pieces called
overlays.

Overlay 0 would run and when it was done, it would call another overlay. The overlays were kept
in the disk and swapped in and out of memory by the O/S dynamically as needed.

Swapping overlays was done by the system but splitting the program had to be done by the
programmer. This was time consuming and boring hence there was needed to come up a way
where by the whole job s turned over to the computer hence coming up of virtual memory.

2.4 Virtual Memory


Allows programs to run even when they are only partially in main memory. Virtual memory
systems used a technique called paging.

2.4.1 Paging
On any computer, there exists a set of memory addresses that programs produce. Addresses can
be generated using:
 indexing
 base registers
 Segment registers e.t.c.
The program-generated addresses are called virtual addresses and form the virtual space
For computers without virtual memory, the virtual address is put directly onto the memory bus.
When virtual memory is used, the virtual addresses go to a Memory Management Unit (MMU)., a
chip or collection of chips that maps the virtual addresses onto the physical memory addresses as
shown below (Fig. 4.4)

20
Principles of Operating Systems
OPERATING SYSTEM

CPU Card

The CPU sends


Virtual addresses
CPU
To the MMU

Memory Disk
MMU Controller

Bus
The MMU sends physical address
to the memory

Fig. 4.4: The position and function of the MMU

Page Tables
The purpose of the page table is to map virtual pages onto page frames

Virtual address
space
60k-64k x
x Virtual page
x
x
44k-48k 7
x
Physical memory
5 address
x
28k-32k 28k-32k
x
x
3
16k-20k 4 16k-20k
0
page frame
6
1
0k-4k 0k-20k
2

Fig. 4.5 Relation between virtual addresses and physical memory addresses given by the page
table.

Pages – virtual address space is divided up in units called pages


Page frames – are the corresponding units in the physical memory

21
Principles of Operating Systems
OPERATING SYSTEM

Pages and page frames are always exactly the same size. With 64k of virtual address space and
32k of physical memory, we have 16 virtual pages & 8 page frames. Transfers between memory
and disk are always in units of pages.

Example
When the program tries to access addressb0, using the instruction, MOVE REG, 0
The virtual address 0 is sent to the MMU. The MMU sees that this virtual address falls in page
0(0 to4095) which according to its mapping is page 2(8192 to 12287). It thus transforms the
address to 8192 and outputs address 8192 onto the bus. The memory board just sees the request
for reading or writing address 8192 which it honors. i.e. the MMU has effectively mapped all
virtual address between 0 and 4095 onto physical addresses 8192 to 12287.

The virtual address is split into:


 a virtual page number (high-order bits)
 an offset (lower-order bits)

The virtual page number is used as index into the page table to find the entry for that virtual
page. From the page table entry, the page frame number (if any) is found. The page frame number
is attached to the high-order end of the offset, replacing the virtual page number to form a
physical address that can be sent to the memory.

outing
1 1 00 0 0 0 0 0 0 0 0 1 0 0 physical
address
(21580)

15 000 0
14 000 0
13 000 0
12 000 0
11 111 1
10 000 0
Page frame 9 101 1 12-bit offset
8 000 0 copied directly from
7 000 0 input to output
6 000 0
5 011 1
4 100 1
3 000 1
2 110 1 110
1 001 1
0 010 1 present/
Virtual page = 2 absent bit
is used as an
into the
page frame

0 01 0 00 0 00 00 0 01 0 0 incoming
virtual
address
(8196)

Fig. 4.6: the internal operation of the MMU with 1664k pages
22
Principles of Operating Systems
OPERATING SYSTEM

Issues to be considered when using page tables


(a) The page table can be extremely large
(b) The mapping must be fast

Multilevel Page Tables


Multilevel page table method is used to avoid keeping all the page tables in memory all the time
hence solving the problem of having huge page tables in memory all the time

Associative Memory (Translation Look side Buffer)


In most paging schemes, the page tables are kept in memory due to there large size hence
affecting the performance.
- most programs tend to make a large number of references to a small number of pages i.e.
only a small fraction of the page table entries are heavily read; the rest are barely read.

Solution: equipping computers with a small hardware device for mapping virtual addresses to
physical; addresses without going through the page table. The device is TLB/ associative memory
is usually inside the MMU. The TLB entries have protection codes for reading and executing.

How TLB functions


- When a virtual address is presented to MMU for translation, H/W checks to see if its
virtual page number is present in the TLB by comparing it to all entries simultaneously.
- If a varied match is found & the access does not violet the protection bits, the page frame
is taken directly from TLB, without going to the page table.
- When the virtual page number is not in TLB, the MMU detects the miss and does an
ordinary page table lookup. It then evicts one of the entries from the TLB and replaces it
with the page table entry just looked up.

Page Replacement Algorithms


When a page fault occurs, the O/S has to choose a page to remove from memory to make room
for the page that has to b brought in.
Examples of page replacement algorithms
 The Optimal Page Replacement Algorithm
 The Not Recently used Page Replacement Algorithm
 The First-In, First-Out (FIFO) Page Replacement Algorithm
 The Second Chance Page Replacement Algorithm
 The Clock Page Replacement Algorithm
 The Least Recently Used (LRU) Page Replacement Algorithm

2.4.2 Segmentation
Segments – Completely independent address spaces. Each segment consists of linear sequence of
addresses, from 0 to some maximum. Each segment can grow or shrink independently without
affecting each other. A segment is a logical entity

20k
16k 16k
12k Symbol 12k 12k Parse 12k
8k 8k Source 8k tree 8k
table Call
4k 4k text 4k 4k stack
Constants
0k 0k 0k 0k 0k

Segment 0 Segment 1 Segment 2 Segment 3 Segment 4

23
Principles of Operating Systems
OPERATING SYSTEM

Comparison of paging and segmentation

Consideration Paging Segmentation


Need the programmer to be aware that
this technique is being used? No Yes
How many linear address spaces are 1 Many
there?
Can procedures and data be
distinguished and separately protected? No Yes
Can tables whose size fluctuates be No Yes
accommodated easily?
Is sharing of procedures between users No Yes
facilitated?
Why was this technique invented? To get a large linear To allow programs and
space without having to data to be broken up into
buy more physical logically independent
memory address spaces & to aid
sharing & protection

Segmentation and Paging


If the segments are large, it may be inconvenient to keep them in main memory in their entirety,
leading to the ides of paging them so that only those pages that are actually needed have to be
around.

Systems with paged segments include:


 MULTICS
 Intel Pentium

24
Principles of Operating Systems
OPERATING SYSTEM

CHAPTER 5

3.0 DEVICE I/O MANAGEMENT

One of the main functions of an O/S is to control all the computer’s I/O devices.

Objectives of device I/O Management


 O/S must issue commands to the devices, catch interrupts and handle errors
 Should provide an interface between the devices and the rest of the system that is simple
and easy to use. ( the interface should be the same for all device s ( device independence)

3.1 Principles of I/O Hardware

I/O devices categories:


 Block devices
 Character devices
Block device – is one that stores information in fixed – size blocks, each one with its own
address. Its essential property is that it is possible to read or write each block independently of all
the other ones. Example of a block device is Disks
Character device – it delivers or accepts a stream of characters without regard to any block
structure. It is not addressable and does not have any seek operations. Examples include: printers,
network interfaces, mile e.t.c.

Device Controllers
I/O units consist of a mechanical component and an electronic component. The electronic
component is called the device controller or adapter. The mechanical part is the device itself. The
controller card usually has a connector on it, into which a cable leading to the device itself can be
plugged. The operating system nearly always deals with the controller.

Disk drives Printer


Controller
Device interface

CPU Memory Disk Printer Other


controller controller controllers

Fig. 5.1: a model for connecting the CPU, memory, controllers and I/O devices
Most small computers use the single bus model (above) for communication between the CPU and
the controllers. In large mainframes, use multiple buses and specialized I/O computers called I/O
channels. Most controllers especially those for block devices support Direct Memory Access
(DMA)
I/O Controllers
 Clock
 keyboard
 hard disk
 floppy disk
 printers
25
Principles of Operating Systems
OPERATING SYSTEM

3.2 Principles of I/O Software

Goals of the I/O Software

Device independence – A key concept in the design of I/O S/W. means that it should be possible
to write programs tat can read files on a floppy disk, on a hard disk, on a CD-ROM, without
having to modify the programs for each different device type.

Uniform naming – Is closely related to device independence. The name of a file or device should
simply be a string or an integer and not depend on the device in anyway.

Error handling - Error should be handled as close to the H/W as possible. If the controller
discovers a read error, it should try to correct the error itself if it can. If it can’t then the device
driver should handle it. (Many errors are transient e.g. read errors caused by specks of dust on the
read head). Only if the lowers layers are not able to deal with the problem should the upper layers
be told about it.

Synchronous (blocking) versus asynchronous (Interrupt-driven) transfers.


Most physical I/O is asynchronous – the CPU starts the transfer and goes off to do something else
until the interrupt arrives. User programs are much easier to write if the I/O operations are
blocking – after a READ command the program is automatically suspended until the data are
available in the buffer. It is the O/S that makes operations that are actually interrupt-driven look
blocking to the user programs.

Sharable versus dedicated devices


Some I/O devices e.g. disks can be used by many users at the same time. Others e.g. tape drivers
have to be dedicated to a single user until that user is finished. It is the O/S to be able to handle
both shared and dedicated devices.

I/O Software Layers


These goals can be achieved in a comprehensible and efficient way by structurig the I/O S/W in
four layers:
 Interrupt handlers (bottom)
 Device drivers
 Device – independent operating system software
 User-level software (top)

Interrupt handlers
The best way to hide from interrupts is to have every process starting an I/O operation block until
the I/O has completed and the interrupt occurs.

Device Drivers
All device-dependent code goes in the device drivers. Each device driver handles one device type.
Each controller has one or more device registers used to give it commands. The device drivers
issue that commands and check that they are carried out properly. Generally, the job of a device
driver is to accept abstract requests from the device-independent – software above it and see to it
that the request is executed.

Device- independent I/O Software


Although some of the I/O S/W is device specific, a large fraction of it is device independent. The
boundary between the drivers and the device independent software is system dependent, because

26
Principles of Operating Systems
OPERATING SYSTEM

some functions that could be done in a device-independent way may be done in the drivers, for
efficiency or other reasons.
Functions of device-independent I/O software
- uniform interfacing for device drivers
- device naming
- device protection
- providing a device-independent block size
- buffering
- storage allocation on block devices
- allocation and releasing dedicated devices
- error reporting

User-space I/O Software


Although most of the I/O software is within the O/S, a small potion of it consists of libraries
linked together with user programs. User-level I/O software consists:
- Library procedures
- Spooling system
Spooling – a way of dealing with dedicated I/O devices in a multiprogramming system. Example
of a spooler device is the printer.

Summary of I/O System


Layer Function

I/O I/O reply


request User processes Make I/O call; format I/O; spooling

Device-independent Naming; protection; blocking; buffering; allocation


software

Device drivers Setup device registers; check status

Interrupt handlers
Wake up driver when I/O completed

Hardware Perform I/O operation

Fig 5.2: Layers of the I/O system & there functions

- When a user program tries to read a block from a file, the O/S is invoked to carry out the
call;
- the device-independent S/W looks in the block cache, if the needed block is not there, it
calls the device driver;
- The device driver issues the request to the hardware;
- The process is then blocked until the disk operation has been completed;
- When the disk is finished, the H/W generates the interrupt.
o The interrupt handler is run to discover what has happened i.e. which device
wants attention at the moment
o It the extracts the status from the device and wakes up the sleeping process to
finish off the I/O request and let the user process continue.
27
Principles of Operating Systems
OPERATING SYSTEM

3.3 Disks

Disk hardware
All real disks are organized into cylinders, each containing as many tracks as there are heads
stacked vertically. The tracks are divided into sectors with 8-32 in floppy disks and up to several
in hard disks. Simplest designs have same number of sectors on each track. All sectors contain
the same number of bytes. Sectors close to the outer rim of the disk will be physically longer than
those close to the hub. Time to read or write each sector is the same. Data density is higher on the
innermost cylinder.

Disk software
Factors determining time required to read or write a disk block:
 the seek time ( time to move the arm to the proper cylinder)
 the rotation delay ( time for the proper sector to rotate under the head)
 the actual data transfer time

For most disks, the seek time dominates the other two times, reducing the seek time can improve
system performance substantially.

Disk devices are prone to errors, some kind of error check, s checksum or a cyclic redundancy
check is always recovered along which the data sector on the disk.

Disk Arm Scheduling Algorithms


 First-Come, First-Served (FCFS) scheduling algorithm
 Shortest Seek First (SSF) disk scheduling algorithm
 Elevator algorithm

First-Come, First-Served (FCFS)


The disk driver accepts requests one at a time and carries them out in that order. When the current
request is finished, the disk driver has a choice of which request to handle next, using FCFS, it
would go next to the first cylinder.

Shortest Seek First (SSF)


It handles the closest request. It minimizes seek time. It cuts the total arm motion almost in a half
compared to FCFS.

Disadvantage: with a heavily loaded disk, the arm will tend to stay in the middle if the disk of the
time, so requests at either extreme will have to wait until there is no request near the middle.

Initial position Pending requests

x x x x x x x
1 5 10 15 20 25 30 35

28
Principles of Operating Systems
OPERATING SYSTEM

Fig. 5.6: Shortest Seek First (SSF) Dist scheduling algorithm


Elevator Algorithm
To reconcile the conflicting goals of efficiency and fairness in SSF, the arm in this algorithm
keeps moving in the same direction until there are no more outstanding requests in that direction
then they switch directions. The algorithm requires the software to maintain 1 bit: the current
direction bit, UP or DOWN. When a request finishes, the disk or elevator driver checks the bit. If
it is UP, the arm or cabin is moved to the next highest pending request, if no request pending at
higher positions , the direction bit is reversed and when the bit is set to DOWN, the move is to the
next lowest request position if any.

Initial position Pending requests

x x x x x x x
1 5 10 15 20 25 30 35

Fig. 5.7: The elevator algorithms for scheduling disk request

Comparisons of the three algorithms doing the same scheduling/task

Algorithm Cylinder Order/Sequence Arm Motions Total Cylinder


First-Come First- (initial position, 11)
Served 1,36,16,34,9,12 10,35,20,18,25,3 111
( FCFS)

Shortest Seek First 12,9,16,1,34,36 1,3,7,15,33,2 61


(SSF)

Elevator Algorithm 12,16,34,36,9,1 1,4,18,2,27,8 60

RAM Disks
RAM disk driver is a block device driver. It can be used to provide access to any part of memory.
It is primary use is to allow a part of memory to be reserved for use like an ordinary disk. It
doesn’t provide permanent storage but one file have been copied to this area, they can be accesses
quickly.

Advantages over real disk


- Simpler in that it just uses a preallocated portion of the main memory for storing the
blocks .
- No seek or rotational delay i.e. has instant access making it suitable for storing programs
or data that are frequently accesses.
- They are simpler in error handling than read disk hence real disk are subject to a wide
variety of errors.
29
Principles of Operating Systems
OPERATING SYSTEM

Error Handling
Errors common in real disks:
 Programming error ( e.g. request for nonexistent sector)
 Transient checksum error (e.g. caused by dust on the head)
 Permanent checksum error ( e.g. disk block physically damaged)
 Seek error ( e.g. the arm sent to cylinder 6 but it went to7)
 Controller error ( e.g. controller refuses to accept animals)
Seek errors are caused by mechanical problems in the aim

Track-at-a-Time Caching
The time required to seek a new cylinder is usually much more than the rotational delay, and
always much more than the transfer time, i.e. once the driver has gone to the trouble of moving
the arm somewhere, it hardly matters whether it reads one sector or a whole track. Some disk
drives take advantage of this property by making maintaining a secrete tract0at-a-time cache,
unknown to the device independent s/w. if a sector that is in the cache is needed, no disk transfer
is required.

Disadvantage of track-at-a-time caching


- software complexity
- buffer space needed
- the transfers from the cache to the calling progam will have to be done by the CPU using
a programmed loop, rather than letting the DMA(Direct Memory Access) hardware do
the job.

Some controllers do track-at-a-time caching in their own internal memory, transparent to the
driver, so that the transfer between the controller and memory can use DMA

Note: The controller and the driver are in a good position to read and write entire tracks in one
command, but the device-independent S/W cannot, because it regards of disks as a linear
sequence of blocks, without regards to how they are divided up into tracks and cylinder.

Floppy Disk Handling


Floppy disk driver is longer and more complicated than hard disk driver. Floppy disk driver
doesn’t have multiple types of controller of controller to support like in hard disk driver. Disk
controllers of IBM PC family are supported by a single S/W driver. Floppy disks speed and data
capacity are capacity are too limited as compared to hard disk. They are important in distribution
of new S/W and for backup. Floppy disk driver does not use the SSF or elevator algorithm. It is
sequential.

Characteristics of a floppy disk drive that complicate its drivers are:


 Removable media
 Multiple disk formats
 Motor control
3.4 Clocks
Also called timers. They are essential to the operation of any timesharing system for a variety of
reasons. They maintain the time and prevent a process from monopolizing the CPU, among other
things. The clock software takes a form of a device driver although the clock is neither a block
device like a disk nor character device like a terminal.

30
Principles of Operating Systems
OPERATING SYSTEM

Clock hardware
Two types of clock are commonly used in computers:
 the simpler clocks are tied to the 110 x 220 Volts power line and cause an interrupt on
every voltage cycle at 50 or 60HZ
 Programmer clocks – it is built out of three components i.e.
- a crystal oscillator
- a counter
- a holding register ( as shown below)

Crystal oscillator

Counter is decremented at
each pulse

Holding register – used to load


the counter
Programmable Clock

At least one such circuit is usually found in any computer providing a synchronizing signal to the
computer’s various circuits. When the counter gets to zero, it causes a CPU interrupt.

Clock Software
All the clock hardware does is to generate interrupts at known intervals. Everything else
involving time must be done by the S/W, the clock driver

Functions of the clock driver


 maintaining the time of day/ real time
 Preventing processes from running longer than they are allowed to
 Accounting for CPU usage
 Handling the ALARM system call made by user processes
 Providing watchdog timers for parts of the system itself
 Doing profiling, monitoring and statistics gathering.

Maintaining the time of day/real time


- Requires incrementing a counter at each clock tick
- Watching for the number of bits in real time can be done in three approaches;
1. using 64 bit counter although expensive
2. maintaining the time of day in seconds rather than in ticks using subsidiary centre
to count ticks until a whole second has been accumulated
3. county in tick

Preventing process from running longer than they are allowed


Whenever a process is started the scheduler should initialize a county to the value of that process
quality counter by
When it gets to zero the clock diver calls the scheduler to set up another process.

Accounting for CPU usage


The most accurate way is to start a second timer distance from the main system timer whenever a
process is started when the process is stopped the timer can be read to tell how long the process
has run.

31
Principles of Operating Systems
OPERATING SYSTEM

Another way is to maintain a pointer to the process table entry for the current running process is a
global variable.

Handling the alarm system call made by user process


A process can request that the operating system give it a warning after a certain interval. The
warning is usually a signal interrupt message.
Networking is or example of application requiring such ways a packet not acknowledged within a
certain time interval must be retransmitted.
Computer aided instructions is another application where a student not providing a response
within a certain time.

Watchdog timers
The o/p need to set timers called watchdog timer , hard disk driver the wake up called is
scheduled each time the disk controller is sent a command list.
Mechanism used by the clock to handle watchdog timer is the same as for user signal the only
difference instead of a signal the clock diver calls a procedure supplied by the call.

Profiling
Some o/s provide a mechanism which a user program can have the system build up a histogram
of its program counter so it can see whenever it is spending its time.
At every tick the diver checks to see if the current power is being profiled.

3.5 Terminals
Every general purpose computer has one or more terminals used to
communicate.

Terminals hardware
Can be divided into three categories based on how the o/s communicates with them. They
include:
 memory- mapped terminals
 terminals that face via a serial communication line using
 Terminals connected the computer via a network.

Memory mapped terminal


Consist of a keyboard and a display both of which are hardwired on to the computer
They are all integral part of the computers themselves.
These terminals are interface via a special memory called a Video RAM which forms part of the
computers address space and is addressed by the CPU the same was as the rest of the memory.

Video controller:-pulls character codes out of the video RAM and generates the video signal used
to drive the display (monitor)

RS-232 Terminals
Are devices containing a keyboard and a display that communicate using a serial interface one bit
at a time. They use a 9-pin or 25-pin connector of which one pin is used for transmitting data one
for receiving and one is grounded. Others are for various control functions. RS-232 terminals are
commonly used to communicate with remote computers using a modem and telephone line.

X Terminals – Network interface


Runs M.I.T’s X window system. Usually X terminals talk to the main computer over Ethernet.
An X terminal is a computer that runs the X software. X server- the program inside the X
terminal that collects input from the keyboard or mouse and accepts commands from a remote
computer. It communicates over the network with X client running on some remote host.
32
Principles of Operating Systems
OPERATING SYSTEM

Its job is to coordinate input from the mouse, keyboard and x client and update the display
accordingly. Its job is to coordinate input from the mouse, keyboard and X client and update the
display accordingly.

Terminal Software

Input software
The basic job of keyboard driver is to collect input from the keyboard and pass it to user
programs when they read from the terminals.

Task of keyboard driver


- Collect characters
- After receiving the character, it processes it

Output software
The methods used by drivers for RS-232 terminals are different from drivers for memory-mapped
terminals.

RS-232 Terminals
The method that is commonly used is to have output buffers associated with each terminal. When
programs write to the terminal, the output is first copied to the buffers. After all the output has
been copied to the buffers (or the buffers are full) the first character is output and the driver goes
to sleep. When interrupt comes in, the next character is output and so on. Each character to be
output is just sent a cross the line to the terminal.

Memory-mapped terminals
Character to be printed are extracted one at a time from user space and put directly in the video
RAM. The driver must also deal with the issues of cursor positioning.

Ways of scrolling the screen:


 Software scrolling – data moved in the memory
 Hardware scrolling – data not moved in the memory

Virtual devices
- spooling
- Buffering
- Caching

33
Principles of Operating Systems
OPERATING SYSTEM

CHAPTER 6

4.0 FILE MANAGEMENT

4.1 Introduction
All computer applications need to store and retrieve information while a process is running; it can
store a limited a mount of information within its own address space. The storage capacity is
restricted to the size of the virtual address space.

Problems of keeping information within a process’ address space:


 In some application e.g. banking airline reservation e.t.c the size is small
 When the process terminates, the information is lost
 It is frequently necessary for multiple processes to access (parts of) the information at the
same time. If information is stored in the address space of a single process only that
process can access it.
The essential requirements for long-term information storage are:
1. It must be possible to store a very large amount of information
2. the information must survive the termination of the process using it
3. must processes must be able to access the information concurrently

Solution
Store information in disks and other external media in units called Files. Information stored in
files must be persistent, not affected by process creation and termination. A file should only
disappear when its owner explicitly removes it.

Files are managed by the operating system. File system is part of the operating system that deals
with files

4.2 File system


a) File naming
b) Structure
c) Types
d) Attributes
e) Operation

File naming
Files are an abstraction mechanism. They provide away to store information on the disk and read
it back later. This must be done in such away as to shield the user from the details of how and
where the information is stored and how the disks actually work. Characteristic of any abstraction
mechanism is the way the objects being managed are named. Rules of files naming vary
somewhat from system to system but all O/S allow strings of one to eight letters as legal file
names. Some permit digits and special character and names as long as 255 characters. Some file
systems distinguish between upper case letters and lower case letters e.g. UNIX others do not e.g.
MS-DOS. Many O/S support two-part file names e.g. prog.c the part following the period is
called the file extension and usually indicates something about the file

Example:
Extension Meaning
File.back Backup file
File.c C source program
File.gif Compuserve Graphical Interchange Format image
File.txt General text file

34
Principles of Operating Systems
OPERATING SYSTEM

File Structure
File can be structured in any of several ways. They include:
a) Byte sequence
b) Record sequence
c) Tree

1 byte 1 record Ant Fox Pig

Cat Cow Dog Goat Lion Owl Pony Rat Worm

Hen Ibis Lamb

(a) (b) (c)

Byte sequence (fig a)


- unstructured sequence of bytes
- the O/S doesn’t know or care what is in the file. All it sees are bytes hence providing
maximum flexibility. Any meaning must be imposed by user-level programs. UNIX and
MS-DOS use this approach

Record sequence (fig b)


A file is a sequence of fixed –length records, each with some internal structure. File being a
sequence of records, the idea is that the read operation returns one record and write operations
overwrite or appends one record. This approach has been faced out.

Tree (fig c)
In this organization a file consists of a tree of records not necessary all the same length, each
containing a key field in a fixed position in the record. The tree is sorted on the key field, to
allow rapid searching for a particular key. Basic operation here is to get the record with a specific
key. New records can be added to the file, with the O/S and not the user, deciding where to place
them. Used I large mainframe computers and also some commercial data processing.

File Types
Many operating systems support several types of files
Types:
 Regular files - are ones that contain user information
 Directories – are system files for maintaining the structure of the file system
 Character special – are related to input/output and used to model serial I/O devices e.g.
terminals, printers and networks
 Block special files – used to model disks

UNIX and MS-DOS have regular files and directories. UNIX has character and block special
files.

35
Principles of Operating Systems
OPERATING SYSTEM

Regular files
They are generally either ASCII files or binary files.
 ASCII files – consists of line text. Advantage of ASCII files is that they can be displayed
and printed as is, and can be edited with an ordinary text editor. If large numbers of
programs use ASCII files for input and output, it is easy to connect the output of one
program to the input of another
 Binary files- they usually have some internal structure. The O/S executes a file if it has
proper format (fig a). It has five sections:
- header
- text data
- relocation bits
- symbol table
Header: starts with a magic number, identifying the file as an executable file. Followed, 16-bit
integers giving the sizes of the various process of the file, the address at which the execution
starts and some flag bits. Following the header are the text and data of the program itself which
are loaded into memory and relocation bits. Symbol table is used for debugging.

16 bits
Module
Magic number name
Header
Text size Date
Data size
Owner
Object
BSS size
Module
Symbol table size Protection

Entry point Size


Header
Flags

Text
Object
Module
Data
Header
Relocation bits
Object
Symbols table
Module

(a) An executable file (b) An archive


 Binary file as an archive – it consists of a collection of library procedures (modules)
compiled but not linked. Each one is prefaced by header telling its name, creation data,
owner, protection code and size

File Access
 Sequential access – in this system, a process could read all bytes or records in a file in
order, starting at the beginning. Sequential files are convenient when the storage medium
is magnetic tape rather than disk
 Random access files – bytes or records can be read in any order. Essential for many
application e.g. database systems. Two methods are used for specifying where to start
reading:
o Every READ operation gives the position in the file to start reading at.
o SEEK operation, provided to set the current position

36
Principles of Operating Systems
OPERATING SYSTEM

File Attributes
Ever file has a name and its data. All O/S associate other information with each file e.g. the date
and time the file was created and file’s size. These extra items are referred to as the file’s
attribute. Lists of attributes vary from system to system.

Examples

Field Meaning
Protection Who can access the file and in what way
Password Password needed to access the file
Owner Current owner
Record length Number of bytes in a record
Time of last access Date and time the file was accessed last

File operation
Files exist to store information and allow it to be retrieved later. Different systems provide
different operations to allow storage and retrieval.

Systems calls relating to files

 CREATE – the file is created with no data. The purpose of the call is to announce that the
file is coming and to set some of the attributes
 DELETE – when the file is no longer needed, it has to be deleted to free up disk space
 OPEN – before using a file, a process must open it. The call allows the system to fetch
the attribute and list of disk addresses into main memory for rapid access on later calls.
When all the accesses are finished, the attribute and disk addresses are no longer needed,
so the file should be closed to free up internal table space.
 READ – Data are read from file
 WRITE – Data are written to the file, usually at the current position
 APPEND – is a restricted form of WRITE. It can only add data to the end of the file
 SEEK – For random access files, a method is needed to specify from where to take the
data
 GET ATTRIBUTES – processes often need to read file attributes to do their work
 SET ATTRIBUTES – some attributes are user settable and can be changed after the file
has been created
 RENAME – it frequently happens that user needs to change the name of an existing file,
hence use of this system call.

4.3 DIRECTORIES
To keep track of files, file systems normally have directories, which in many systems, are
themselves files.

Hierarchical Directory systems


A directory typically contains a number of entries, one per file. These can be achieved in two
ways:
 Each entry contains the file name, the file attributes and the disk addresses where data are
stored.
 Directory entry holds the file name and a pointer to another data structure where the
attribute and disk addresses are found.

37
Principles of Operating Systems
OPERATING SYSTEM

When a file is opened, the O/S searches its directory until it finds the name of the file to be open.
It then extracts the attributes and disk address either directly from the directory entry or from the
data structure pointed to, and puts them in a table in main memory. All subsequent references to
the file use the information in main memory.

File system designs


The number of directories varies from system to system. The file system design includes:
a) Single directory shared by all users
b) One directory per user
c) Arbitrary tree per user

Single directory shared by all users


Is the simplest design where the system maintains a single directory containing all the files of all
users. If many users choose the same file names, conflicts and confusion makes the system
unworkable. This models was used by the 1st microcomputers O/S but is rarely used any more.

Root directory - Directory

- File
A B C D

(a)

One directory per user


This design eliminates name conflicts among users. It is not satisfactory for users with a large
number of files. Users would want to group their files together in logical ways which can be
achieved in this design.
Root directory

User directory
A B C

A A B C C C

Files

(b)

38
Principles of Operating Systems
OPERATING SYSTEM

Arbitrary tree per user


With this approach, each user can have as many directories as are needed so that files can be
grouped together in natural and logical ways

- Root directory

A B C - User directory

C
A B B B C

B C C

C C C C
User subdirectories

(c)

Path Names
When the file system is organized as a directory tree some way is needed for specific file names.
Two different methods are commonly used:
- absolute path name
- relative path name

Absolute path name


Each is given an absolute path name consisting of the path from the root directory the file
Example: A/word/assign/o.s/ex1

Absolute path names always start at the root directory and are unique.
In ms. DOS, components of the path are separated by / in UNIX / and in MULTICS >

Relative path name


Used in conjunction within the concept of the working directory (current directory)

Directory operations

- CREATE - PATH
- DELETE - DISPLAY
- OPENDDIR - RENAME
- CLOSEDIR
- READDIR – returns the next entry in an open directory
- LINK –allows file to appear in more than the directory
- UNLINK –directory entry is removed.

39
Principles of Operating Systems
OPERATING SYSTEM

4.4 FILE SYSTEM IMPLEMENTATION

Implementing files
Most important issue in implementing file storage is keeping track of which disk blocks go with
which file various methods are used in different O/S

File allocation
 Contiguous allocation
 Linked list allocation

Contiguous allocation
Each file is stored as a contiguous block of data in the disk. In a disk with 1k blocks, a 50k file
would be allocated 50 consecutive blocks

Advantage
Excellent performance because the entire file can be read from the disk in a single operation.
Simple to implement because keeping the files blocks one is reduced to remembering one
number.

Disadvantage
Not feasible unless the maximum file size is known at the time the file is created.
Disk fragmentation- space is wasted that might otherwise have been used.

Linked list allocation


Keep each file as linked list of disk blocks. The first word of each block is used as a pointer to the
next one. The rest of the block is data.

File File File File File


Physical block 0 block 1 block 2 block 3 block 4
block
4 7 2 10 12

File File File File


Physical block o block 1 block 2 block 3
block
6 3 11 14
Fig (a) – storing a file as a linked list of disk blocks

Advantage
Unlike contiguous allocation every disk block can be used. No space is lost to disk fragmentation.
It’s sufficient for the directory entry to merely store the disk address of the first block.

Disadvantage
 Random access is slow
 Pointer takes up a few bytes hence reducing the amount of data storage in a block.

40
Principles of Operating Systems
OPERATING SYSTEM

Linked list allocation using an index


Both disadvantages of linked allocation can be eliminated by taking the pointer word from each
disk block and putting it in a table or index in memory.
Example: in fig (a) file A uses disk blocks 4, 7,2,10, and 12 and B uses 6, 3, 11, and 14 in that
order. Using the table of fig (b), we can start with block 4 and follow the chain to the end. Same
with B, start with block 6.

Physical
block
0
1
2 10
3 11
4 7 - file A starts her
5
6 3 - file B starts her
7 2
8
9
10 12
11 14
12 0
13
14 0
15 - unused block

Fig (b) – Linked list allocation using a table in main memory

Advantages
 The entire block is available for data
 Random access is much easier -The chain is entirely in memory so it can be followed without
making any disk references
 Also sufficient for the directory entry to keep a single integer (the starting block number) and
still be able to locate all the blocks, no matter how large the file is.
MS-DOS uses this method for disk allocation

Disadvantages
The entire table must be in memory all the time to make it work .For speed in lookup they should
be 4 bytes. The table will take up 1.5 or 2 megabytes all the time.

I-nodes
This method of keeping track of which track of which blocks belong to which files, by
associating each file with a little table called i-node(index –node) which uses the attribute and
disk addresses of the files blocks.

41
Principles of Operating Systems
OPERATING SYSTEM

Addresses of data
blocks
Single double indirect triple indirect
i- node indirect block block block
Attributes

disk
addresses

Fig (c) an I-node

The first few disks addresses are stored in the 1-node itself for small files all information in the 1-
node, which is tech from disk to main memory when file is opened.

For larger files, one of the i-node is the address of disk called single indirect blocks.

Single indirect blocks - contains additional disk addresses. If it is not enough another address
called double indirect block is used

Double indirect block - contains the address of a block that contains list of single indirect
blocks.

Triple indirect blocks – used when the above is not enough.

Each of these single indirect blocks points to few hundred data blocks.

5.0 Reference

Stallings, W(2005). Operating Systems (Internals and Design Principles).


New Jersey: Pearson Education

Tanenbaum, S.A, Woodhull, S.A (2002). Operating Systems: design and Implementation. New
Jersey: Pearson Education

42
Principles of Operating Systems

You might also like