Purpose Packages Which Are Written To Meet Specific Needs and General - Purpose Packages
Purpose Packages Which Are Written To Meet Specific Needs and General - Purpose Packages
Purpose Packages Which Are Written To Meet Specific Needs and General - Purpose Packages
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
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 has been closely tied to the architecture of the computers on which
they 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)
Much time was wasted with the operators walking and the machine room. Hence the solution was
the introduction of batch system.
2nd generation-computers were mostly for scientific and engineering calculations. They were
programmed in FORTRAN and assembly language.
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
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
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.
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
4
Principles of Operating Systems
OPERATING SYSTEM
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
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
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
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.
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.
Kernel mode
Kernel
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.
Network
Message from
Client to server
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.
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
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.
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)
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.
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
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.
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).
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
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
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
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
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
19
Principles of Operating Systems
OPERATING SYSTEM
Illustration
C C C C C
B B B B E
A A A D D D
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.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
Memory Disk
MMU Controller
Bus
The MMU sends physical address
to the memory
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.
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 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
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.
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
23
Principles of Operating Systems
OPERATING SYSTEM
24
Principles of Operating Systems
OPERATING SYSTEM
CHAPTER 5
One of the main functions of an O/S is to control all the computer’s I/O devices.
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.
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
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.
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.
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
Interrupt handlers
Wake up driver when I/O completed
- 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.
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.
x x x x x x x
1 5 10 15 20 25 30 35
28
Principles of Operating Systems
OPERATING SYSTEM
x x x x x x x
1 5 10 15 20 25 30 35
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.
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.
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.
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
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
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.
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.
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.
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.
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.
Virtual devices
- spooling
- Buffering
- Caching
33
Principles of Operating Systems
OPERATING SYSTEM
CHAPTER 6
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.
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
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
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
Text
Object
Module
Data
Header
Relocation bits
Object
Symbols table
Module
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.
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.
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
A B C D
(a)
User directory
A B C
A A B C C C
Files
(b)
38
Principles of Operating Systems
OPERATING SYSTEM
- 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 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 >
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
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.
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
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
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
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.
Each of these single indirect blocks points to few hundred data blocks.
5.0 Reference
Tanenbaum, S.A, Woodhull, S.A (2002). Operating Systems: design and Implementation. New
Jersey: Pearson Education
42
Principles of Operating Systems