04 DCBC 830 Efb 418
04 DCBC 830 Efb 418
SYLLABUS
UNIT III Process Scheduling: Basic Concepts-Scheduling Criteria-Scheduling Algorithms (FCFS, SJF
& Round Robin only) - Algorithm Evaluation. Synchronization: Back ground-The Critical Section
Problem-Peterson’s SolutionSynchronization Hardware-Mutex Locks-Semaphores-The Dining-
Philosopher’s Problem.
Text Book: OPERATING SYSTEMS CONCEPTS- Abraham Silberschatz, Peter B Galvin, Gerg Gagne-
NINTH EDITION (2015 INDIA EDITION)-WILEY
Reference Book: THE MINIX BOOK OPERATING SYSTEMS DESIGN AND IMPLENTATION-Third
Edition-ANDREW S.TANENBAUM and ALBERT S WOODHULL. – PEARSON
UNIT I
1.1 INTRODUCTION
An operating system can be considered as computer program (software) that manages the
computer hardware and acts as an intermediary between the computer user and the
hardware. With the help of an operating system environment, a user can do things in a
convenient and efficient manner.
Kernel is the program running at all times on the computer. System programs are
associated with the operating system but are not necessarily forms part of the kernel.
Application programs include all programs not associated with the operation of the
system.
Different operating systems are available under various environments as listed below:
Mainframe operating systems: designed to optimize utilization of hardware.
Personal computer (PC) operating systems: designed to support user friendly
applications, business applications and computer games.
Functions of Operating System
Booting the computer
Performing basic computer tasks like managing the peripheral devices
Provides file management like manipulating storing, retrieving and saving
Handling system resources like computer's memory, and printer
Handling errors by applying error prevention methods.
Providing user interface like Graphical User Interface (GUI)
Operating System Management Tasks
Application allows standard communication between software and computer. The User
interface allows communicating with computer. Other tasks are:
Processor management arranges the tasks into order and pairing them into manageable
size before processing by CPU.
Memory management coordinates data to and from Random Access Memory (RAM)
and applies techniques like paging and segmentation.
Device management provides interface between connected devices.
Storage management directs permanent data storage.
Security prevents unauthorized access to programs and data.
Control over system performance considers the request for a service and response from
the system.
Job accounting keeps track of time and resources used by various jobs and users.
Error detecting by producing traces, error messages, debugging and error detecting.
Coordination between other softwares and users Coordinating and assigning compilers,
interpreters, assemblers and other software to the various users of the computer systems.
Multiprocessor Systems
It has several processors that share a common physical memory.
Execution of several tasks by different processors concurrently.
If possible, system divides task into many subtasks and then these subtasks can be
executed in parallel in different processors.
Provides higher computing power and speed.
All processors operate under single operating system.
Enhanced performance is achieved
Increases the system's throughput without speeding up the execution of a single task.
Multiprocessor Systems: Multiprocessor systems are also known as parallel systems have
two or more processors in close communication, sharing the computer bus, the clock,
memory, and peripheral devices. Three important advantages of Multiprocessor systems are
Increased throughput - more work is carried out in less time.
Low cost – since it shares peripheral devices there is much reduction in cost. Also
processors share data stored on a disk thereby reducing requirement of storage media.
Increased reliability – since many processors are used, failure of a single processor
would only slow down the system rather than halting altogether thereby increasing
reliability of a computer system.
The two types of multiple-processor systems are:
Asymmetric multiprocessing - each processor is assigned a specific task. One main
processor controls the system; the other processors get instructions from it or have
predefined tasks. The main processor schedules and allocates work to the remaining
processors.
Symmetric multiprocessing (SMP) - each processor has its own set of registers and
cache memory but all processors share common physical memory. Each processor
performs all tasks within the operating system. All processors are main peers. Figure 1.2
illustrates a typical SMP architecture.
Latest CPU design includes multiple computing cores on a single chip. Such
multiprocessor systems are termed multicore. Recent development is blade servers in
which multiple processor boards, I/O boards, and networking boards are placed in the
same chassis. Each blade-processor board boots independently and runs its own
operating system.
Fig.1.2 SMP Architecture
Clustered Systems: Multiple CPUs are gathered together. Composes of two or more
individual systems / nodes joined together. Each node may be a single processor system
or a multicore system. Clustered computers share storage and are closely linked via a
Local Area Network (LAN). Clustering provides high reliability - service will continue
even if one or more systems in the cluster fail. Clustering can be structured
asymmetrically or symmetrically.
In asymmetric clustering, one machine is in standby mode while the other is running the
applications. The standby host machine monitors the active server. If the server fails, the
standby host becomes the active server.
In symmetric clustering, two or more hosts are running applications and are monitoring
each other. This structure is obviously more efficient, as it uses all of the available
hardware. More than one application must be available to run.
Normally, main memory is too small to accommodate all jobs. So, the jobs are kept
initially on the disk in the job pool consisting of all processes residing on disk awaiting
main memory allocation. The operating system simply switches between jobs. As long
as at least one job needs to execute, the CPU is never idle. Thus in Multi-programmed
systems various system resources like CPU, memory, and peripheral devices are utilized
effectively, even without providing for user interaction with the computer system.
0 Operating
system
1 Job 1
2 Job 2
. …
. …
. …
. …
max Job n
Fig. 1.3 Multiprogramming system memory
A time-shared operating system allows many users to share the computer simultaneously.
Since , only a little CPU time is needed for each user. As the system switches rapidly
from one user to the next, each user is under the impression that the entire computer
system is dedicated to his/her use, even though it is being shared among many users.
User process
Calls Return
system from
call system
call
Trap mode Return
bit =0 mode bit =
1
Kernel
Execute
system call
Fig 1.4 Dual mode operation
At system boot time, the hardware starts in kernel mode. The operating system is then
loaded and starts user applications in user mode.
Whenever a trap or interrupt occurs, the hardware switches from user mode to kernel
mode (that is, changes the state of the mode bit to 0).
Whenever the operating system gains control of the computer, it is in kernel mode. The
system always switches to user mode (by setting the mode bit to 1) before passing
control to a user program.
Timer
For the operating system to maintain control over the CPU a timer can be used. A timer
can be set to interrupt the computer after a specified period. The period may be fixed (for
example, 1/60 second) or variable (for example, from 1 millisecond to 1 second). A
variable timer is generally implemented by a fixed-rate clock and a counter.
Setting the Timer: The operating system sets the counter. Every time the clock ticks, the
counter is decremented. When the counter reaches 0, an interrupt occurs.
Before turning over control to the user, the operating system ensures that the timer is set
to interrupt.
If the timer interrupts, control transfers automatically to the operating system.
Advantage: Preventing a user program from running too long.
Time-sharing systems used a timer and scheduling algorithms to cycle processes rapidly
through the CPU, giving each user a share of the resources. Time sharing systems are
used on desktop computers, laptops, servers, and even mobile computers, but frequently
all the processes are owned by the same user (or a single user and the operating system).
It applied the simple fact that it can perform different tasks at the same time.
Client–Server Computing: Systems that act as server systems satisfy the requests generated
by client systems. A client–server system, has the general structure depicted in Figure 1.5.
Client 1
SERVER
NETWORK Client 2
Client 3
Peer-to-Peer Computing (P2P): All nodes within the system are considered peers, that is
each node may act as either a client or a server, depending on whether it is requesting or
providing a service. Services can be provided by several nodes distributed throughout the
network.
To participate in a peer-to-peer system, a node must initially join the network of peers. Once
a node has joined the network, it can provide services. Service providing methods are,
- Using centralized lookup service table
- Broadcasting request for service
Centralized lookup service : When a node joins a network, it registers its service with a
centralized lookup service on the network. Any node desiring a specific service contacts this
centralized lookup service to determine which node provides the service and communicates
with it for service.
Broadcasting request: The peer acting as a client must discover which node provides a
desired service by broadcasting a request for the service to all other nodes in the network.
The node (or nodes) providing that service responds to the peer making the request. To carry
out this, a discovery protocol must be provided that allows peers to discover services
provided by other peers in the network. Figure 1.6 illustrates P2P scenario.
***
UNIT II
System Services
o Protection and Security Service
o Resource allocation
o Accounting
User interface: Different types of interface are available.
o Command-line interface: (CLI), which uses text commands and a method for
entering them.
o Batch interface(BI): A Batch file with commands and directives to control
those commands are the contents of a batch files which are executed when
needed.
o Graphical User Interface (GUI): A window system with a pointing device like
mouse to direct I/O for choosing and selecting and a keyboard to enter text.
Program execution: The capability of the system to load a program into memory and
to execute it. The program must be able to end its execution if necessary.
I/O operations: While executing a program it may require performing I/O operations
from a file or an I/O device. The operating system must perform such I/O operations.
File-system services: Read, write. Create and delete operations are common activity
in a computing system. Searching for a given file, listing file information, accessing
rights to users of files and directories are other activities.
Communication services: Exchange of information among processes takes place on
the same computer or between processes on different computer systems connected in
a network. Such communications might happen through a shared memory or message
passing.
Error detection: Common errors that may occur are
- CPU and memory hardware errors - memory error, power failure
- I/O devices error – network connection failure, parity error on disk, out of paper in
printer
- User program error - arithmetic overflow, trying to access an illegal memory
location
Each type of error needs an appropriate action to ensure proper functioning. Under
certain conditions error detection is possible.
Resource allocation: Resources must be allocated to multiple users or multiple jobs
running at the same time. Operating system efficient management of different types of
resources like allocating
o CPU cycles
o main memory
o file storage
o I/O devices like printers, USB storage drives.
Accounting: Keeps track of which users use how much and what kinds of computer
resources for the purpose of billing and knowing usage statistics.
Protection and security: Information that are stored in a multiuser or networked
computer system needs protection and security. During concurrent processes
execution, interfering into another should not be allowed. Securing the system
requires each user to authenticate to the system, usually by means of a password, to
gain access to system resources.
Sequence of system
Source File Destination File
calls
User application
System Call
User Mode
System Call Interface
Kernel Mode
OS Kernel
Application Programs
KERNEL
Layered Structure
Layer n …
Layer 1
Layer 0
hardware
2.6 PROCESS MANAGEMENT
Process, which is a program in execution. A process is the unit of work in
a modern time-sharing system. A system has operating system processes
for executing system code and user processes for executing user code.
Both the processes can be executed concurrently. By switching the CPU
between processes, the operating system can make the computer more
efficient.
Waiting
PROCESS STATE
PROCESS NUMBER
PROGRAM
COUNTER
REGISTERS
MEMORY LIMITS
LIST OF OPEN
FILES
…..
Fig 2.7 Process Control Block
CPU registers. The registers vary in number and type, depending on
the computer architecture. They include accumulators, index
registers, stack pointers, and general-purpose registers, plus any
condition-code information. Along with the program counter, this
state information must be saved when an interrupt occurs, to allow
the process to be continued correctly afterward.
CPU-scheduling information. This information includes a process
priority, pointers to scheduling queues, and any other scheduling
parameters.
Memory-management information. This information may include
such items as the value of the base and limit registers and the page
tables, or the segment tables, depending on the memory system used
by the operating system.
Accounting information. This information includes the amount of
CPU and real time used, time limits, account numbers, job or
process numbers, and so on.
I/O status information. This information includes the list of I/O
devices allocated to the process, a list of open files, and so on. In
brief, the PCB simply serves as the repository for any information
that may vary from process to process.
THREADS
Processes are used to group resources together and threads are the entities
scheduled for execution on the CPU.
A thread is a single sequence stream within in a process also
called as lightweight processes.
A single thread of control allows the process to perform only one
task at a time.
A thread can be in any of several states
o Running
o Blocked
o Ready
o Terminated.
Each thread has its own stack.
A thread has
o program counter (PC)
o register set
o stack space.
Threads are not independent of one other like processes
Threads shares their code section, data section, OS resources with
other threads. Only one thread active (running) at a time.
Threads within a process execute sequentially.
If one thread is blocked, another thread can run.
Scheduling Queues
Job Queue:
o All processes when enters into the system are stored in the job
queue. From the job queue, the Job Processor, selects
processes and loads them into the memory for execution.
Primary aim of the Job Scheduler is to maintain a good degree
of Multiprogramming.
Ready Queue:
o Processes in the Ready state are placed in the ready queue.
Device Queue:
o Processes waiting for a device to become available are placed
in device queues.
Context Switch
A context switch is the process of storing and restoring the state of a
process or thread so that execution can be resumed from the same point at
a later time. The core of the operating system performs the following
context switching activities with regard to processes including threads on
the CPU:
Suspending the progression of one process and storing the
CPU's state (i.e., the context) for that process in memory,
Retrieving the context of the next process from memory and
restoring it in the CPU's registers
Returning to the location indicated by the program counter in order
to resume the process.
A context switch can also be described as the kernel suspending the
progression of a process execution of one process on the CPU and
resuming execution of some other process that had previously been
suspended. Modes in Context switching
Kernel Mode: Kernel mode is a privileged mode of the CPU in
which only the kernel runs and which provides access to all memory
locations and all other system resources. Context switches can occur
only in kernel mode.
User Mode: All other programs, including applications, initially
operate in user mode.
Operations on Processes
The two major operatins performed on process are
Process creation
Process Termination
Process Creation
Parent process create children processes, which, in turn create other
processes, forming a tree of processes
Resource sharing methods
o Parent and children share all resources
o Children share subset of parent‘s resources
o Parent and child share no resources
Execution modes
o Parent and children execute concurrently
o Parent waits until children terminate
Address space representation
o Child duplicate of parent
o Child has a program loaded into it
Process Termination
Exit - Process executes last statement and asks the operating system
to delete it
o Output data from child to parent (via wait)
o Process‘ resources are deallocated by operating system
Abort - Parent may terminate execution of children processes under
the following situations
o Child has exceeded allocated resources
o Task assigned to child is no longer required
o If parent is exiting
o Some operating system do not allow child to continue if its
parent terminates
o All children terminated - cascading termination
Process A
Shared memory
Process B
Kernel
Indirect communication: The messages are sent to and received from mailboxes, or
ports.
- Each mailbox has a unique identification.
- Two processes can communicate only if they have a shared mailbox.
- A mailbox may be owned either by a process or by the operating system.
The send() and receive() operations are defined as follows:
o send(A, message)—Send a message to mailbox A.
o receive(A, message)—Receive a message from mailbox A.
Its properties are,
- A link is established only if both members of the pair have a shared mailbox.
- A link may be associated with more than two processes.
- Between each pair of communicating processes, a number of different links may
exist, with each link corresponding to one mailbox.
Synchronous or asynchronous communication
Message passing may be either synchronous and asynchronous also known as blocking
or nonblocking.
• Blocking send. The sending process is blocked until the message is received.
• Nonblocking send. The sending process sends the message and resumes operation.
• Blocking receive. The receiver blocks until a message is available.
• Nonblocking receive. The receiver retrieves either a valid message or a null.
***
UNIT III
PROCESS STATE
Processes can be any of the following states :
New - The process is in the stage of being created.
Ready - The process has all the resources available that it needs to run, but the CPU is
not currently working on this process's instructions.
Running - The CPU is working on this process's instructions.
Waiting - The process cannot run at the moment, because it is waiting for some
resource to become available or for some event to occur.
Terminated - The process has completed.
PROCESS SCHEDULING
The act of determining which process in the ready state should be moved to the running state
is known as Process Scheduling. The prime aim of the process scheduling system is to keep
the CPU busy all the time and to deliver minimum response time for all programs. To achieve
this, the scheduler must apply appropriate rules for swapping processes IN and OUT of CPU.
Scheduler categories:
Non pre-emptive scheduling. When the currently executing process gives up the
CPU voluntarily.
Pre-emptive scheduling. When the operating system decides to favour another
process, pre-empting the currently executing process.
SCHEDULING QUEUES
All processes when enters into the system are stored in the job queue.
Processes in the Ready state are placed in the ready queue.
Processes waiting for a device to become available are placed in device queues.
There are unique device queues for each I/O device available.
Types of Schedulers
The three types of schedulers available are:
1. Long Term Scheduler :
Long term scheduler runs less frequently. Long Term Schedulers decide which program must
get into the job queue. From the job queue, the Job Processor, selects processes and loads
them into the memory for execution. Primary aim of the Job Scheduler is to maintain a good
degree of Multiprogramming. An optimal degree of Multiprogramming means the average
rate of process creation is equal to the average departure rate of processes from the execution
memory.
2. Short Term Scheduler :
This is also known as CPU Scheduler and runs very frequently. The primary aim of this
scheduler is to enhance CPU performance and increase process execution rate.
3. Medium Term Scheduler :
During extra load, this scheduler picks out big processes from the ready queue for some time,
to allow smaller processes to execute, thereby reducing the number of processes in the ready
queue.
OPERATIONS ON PROCESS
Process Creation
Through appropriate system calls, such as fork or spawn, processes may create other
processes. The process which creates other process, is termed the parent of the other process,
while the created sub-process is termed its child.
Each process is given an integer identifier, termed as process identifier, or PID. The parent
PID (PPID) is also stored for each process.
On a typical UNIX systems the process scheduler is termed as sched, and is given PID 0. The
first thing done by it at system start-up time is to launch init, which gives that process PID 1.
Further Init launches all the system daemons and user logins, and becomes the ultimate
parent of all other processes.
A child process may receive some amount of shared resources with its parent depending on
system implementation. To prevent runaway children from consuming all of a certain system
resource, child processes may or may not be limited to a subset of the resources originally
allocated to the parent.
There are two options for the parent process after creating the child :
Wait for the child process to terminate before proceeding. Parent process makes
a wait() system call, for either a specific child process or for any particular child
process, which causes the parent process to block until the wait() returns. UNIX shells
normally wait for their children to complete before issuing a new prompt.
Run concurrently with the child, continuing to process without waiting. When a
UNIX shell runs a process as a background task, this is the operation seen. It is also
possible for the parent to run for a while, and then wait for the child later, which
might occur in a sort of a parallel processing operation.
Process Termination
By making the exit(system call), typically returning an int, processes may request their own
termination. This int is passed along to the parent if it is doing a wait(), and is typically zero
on successful completion and some non-zero code in the event of any problem.
Processes may also be terminated by the system for a variety of reasons, including :
The inability of the system to deliver the necessary system resources.
In response to a KILL command or other unhandled process interrupts.
A parent may kill its children if the task assigned to them is no longer needed i.e. if
the need of having a child terminates.
If the parent exits, the system may or may not allow the child to continue without a
parent (In UNIX systems, orphaned processes are generally inherited by init, which
then proceeds to kill them.)
When a process ends, all of its system resources are freed up, open files flushed and closed,
etc. The process termination status and execution times are returned to the parent if the parent
is waiting for the child to terminate, or eventually returned to init if the process already
became an orphan.
The processes which are trying to terminate but cannot do so because their parent is not
waiting for them are termed zombies. These are eventually inherited by init as orphans and
killed off.
THREAD
A thread is a flow of control within a process. Two kinds of threads are
- User-level threads: Visible to the programmer and are unknown to the kernel. These
threads are faster to create and manage. Thread libraries provide the application
programmer with an API for creating and managing threads. Three primary thread
libraries are in common use: POSIX Pthreads,Windows threads, and Java threads.
- Kernel-level threads: the operating-system kernel supports and manages kernel-level
threads. To create this thread, intervention from the kernel is required.
A multi-threaded process contains several different flows of control within the same address
space. The benefits of multithreading include
- increased responsiveness to the user
- resource sharing within the process
- cost effectiveness
- more efficient use of multiple processing cores
THREAD MODELS
- The many- to-one model maps many user threads to a single kernel thread.
- The one-to-one model maps each user thread to a corresponding kernel thread.
- The many-to- many model multiplexes many user threads to a smaller or equal
number of kernel threads.
CPU SCHEDULING
It is the basis of multi-programmed operating systems. By switching the CPU among
processes, the operating system can make the computer more efficient. Multiprogramming
environment have some process running at all times thereby maximizing CPU utilization.
Since several processes are kept in memory at one time, when one process has to wait, the
operating system takes the CPU away from that process and gives it to another process.
Scheduling of this kind can be called as CPU scheduling. Almost all computer resources can
be scheduled before use.
CPU SCHEDULER
Whenever the CPU becomes idle, the operating system must select one of the processes in the
ready queue to be executed. The selection process is carried out by the short-term
scheduler, or CPU scheduler. The scheduler selects a process from the processes in memory
that are ready to execute and allocates the CPU to that process applying a scheduling
algorithm.
Some of the scheduling algorithms :an be implemented as a FIFO queue, a priority queue, a
tree, or simply an unordered linked list. The records in the queues are generally process
control blocks (PCBs) of the processes.
Preemptive Scheduling
CPU-scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state (for example, as the
result of an I/O request or an invocation of wait() for the termination of a child process
2. When a process switches from the running state to the ready state (for example, when an
interrupt occurs)
3. When a process switches from the waiting state to the ready state (for example, at
completion of I/O)
4. When a process terminates.
When a process in ready state can be selected for execution then it is preemptive scheduling.
A timer is needed for preemptive scheduling. A preemptive scheduling can result in race
conditions when data are shared among several processes.
CPU-scheduling function has a dispatcher module that gives control of the CPU to the
process selected by the short-term scheduler. This function involves the following:
• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that program
The dispatcher is invoked during every process switch. The time it takes for the dispatcher to
stop one process and start another running is known as the dispatch latency.
Non-Preemptive Scheduling:
When scheduling takes place only under circumstances where a new process must be
selected for execution then, the scheduling scheme is called as nonpreemptive or
cooperative. Under non- preemptive scheduling, once the CPU has been allocated to a
process, the process keeps the CPU until it releases the CPU either by terminating or by
switching to the waiting state.
Many criteria have been suggested for comparing CPU-scheduling algorithms. Important
criteria include the following:
• CPU utilization – Keeping the CPU as busy as possible. CPU utilization in a real system
ranges from 40% to 90% for a lightly loaded system to a heavily loaded system respectively.
• Throughput – It is a unit for measure of work done by the CPU. The number of processes
that are completed per unit time is called throughput. It may vary depending on the length of
the process. For long processes, throughput may be one process per hour and for short
transactions, it may be even many processes per second.
• Turnaround time - The time taken to execute a particular process is the turnaround time.
The time interval is from the time of submission of a process to the time of completion.
Turnaround time includes the following:
- Time spent waiting to get into memory.
- Waiting time in the ready queue.
- Executing time on the CPU.
- Performing I/O related operations.
• Waiting time - Waiting time is the sum of the periods spent waiting in the ready queue. It is
only the amount of time that a process spends waiting in the ready queue.
• Response time – Response time is a measure of the time from the submission of a request
to the first response is has produced. In general, it is the time taken to start responding and
not the time it takes to output the response.
Working functionality:
When a process enters the ready queue, its PCB is linked onto the tail of the queue.
When the CPU is free, it is allocated to the process at the head of the queue.
The running process is then removed from the queue.
The code for FCFS scheduling is simple to write and understand. Processes are dispatched
according to their arrival time on the ready queue. Being a non-preemptive discipline, once a
process has a CPU, it runs to completion.
Example: Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds:
Process CPU Burst
Time P1 20 P2 5 P3
2
CPU Burst
Time P1 20 P2 5 P3
2
P1 20 P2 5 P3 2
P1 20 P2 5 P3 2
20 P2 5 P3 2
P2 5 P3 2
P2 5 P3 2
5 P3 2
P3 2
P3 2
2
P1 P2 P3
P2 P3
P3
3.4 SHORTEST-JOB-
FIRST SCHEDULING
In Shortest-Job-First (SJF)
scheduling algorithm each
process is associated with the
length of the process‘s next
CPU burst. The process that
has the smallest next CPU
burst is allocated. Example of
SJF scheduling, consider the
following set of processes,
with the length of the CPU
burst given in milliseconds:
Process CPU Burst
Time P1 16 P2 18 P
3 17 P4 13
CPU Burst
Time P1 16 P2 18 P
3 17 P4 13
P1 16 P2 18 P3 17
P4 13
P1 16 P2 18 P3 17
16 P2 18 P3 17 P4
13
P2 18 P3 17 P4 13
P2 18 P3 17 P4 13
18 P3 17 P4 13
P3 17 P4 13
P3 17 P4 13
17 P4 13
P4 13
P4 13
13
Completion Time 13
29 46 54 P4 P1 P3
P2
29 46 54 P4 P1 P3 P
2
46 54 P4 P1 P3 P2
54 P4 P1 P3 P2
P4 P1 P3 P2
P4 P1 P3 P2
P1 P3 P2
P3 P2
P2
Working functionality
- The CPU scheduler
allocates the CPU to
each process for a
time interval of up to
1 time quantum.
- The CPU scheduler
picks the first process
from the ready queue,
sets a timer to
interrupt after 1 time
quantum, and
dispatches the
process.
- CPU burst < 1
quantum time:
o If the process
has a CPU
burst of less
than 1 time
quantum, the
process itself
will release the
CPU
automatically.
o The scheduler
will then
proceed to the
next process in
the ready
queue.
- CPU burst > 1
quantum time:
o If the CPU
burst of the
currently
running
process is
longer than 1
time quantum,
the timer will
go off and will
cause an
interrupt to the
operating
system.
o A context
switch will be
executed, and
the process
will be put at
the tail of the
ready queue.
o The CPU
scheduler will
then select the
next process in
the ready
queue.
P1 21 P2 2 P3 3
21 P2 2 P3 3
P2 2 P3 3
P2 2 P3 3
2 P3 3
P3 3
P3 3
3
3 5 8 11 14 17 20 21
P1 P2 P3 P1 P1 P1
P1 P1
5 8 11 14 17 20 21
8 11 14 17 20 21 P1
P2 P3 P1 P1 P1 P1
11 14 17 20 21 P1 P
2 P3 P1 P1 P1 P1 P1
14 17 20 21 P1 P2 P
3 P1 P1 P1 P1 P1
17 20 21 P1 P2 P3 P
1 P1 P1 P1 P1
20 21 P1 P2 P3 P1 P
1 P1 P1 P1
21 P1 P2 P3 P1 P1 P
1 P1 P1
P1 P2 P3 P1 P1 P1 P
1 P1
P1 P2 P3 P1 P1 P1 P1
P1
P2 P3 P1 P1 P1 P1 P1
P3 P1 P1 P1 P1 P1
P1 P1 P1 P1 P1
P1 P1 P1 P1
P1 P1 P1
P1 P1
P1
The average waiting time for this schedule is P1 waits for 5 milliseconds, P2 waits for 3
milliseconds, and P3 waits for 7 milliseconds.
Thus, the average waiting time is 15/3 = 5 milliseconds.
The performance of the RR algorithm depends on the size of the time quantum. If the time
quantum is large, it behaves like FCFS policy. If the time quantum is small, it needs large
number of context switches.
Deterministic Modeling
Deterministic modeling is one type of analytic evaluation. Analytic evaluation uses the given
algorithm and the system workload to produce a formula or number to evaluate the
performance of the algorithm for that workload. It takes a particular predetermined workload
and defines the performance of each algorithm for that workload.
For example, evaluation can be carried out for FCFS, SJF and RR, given the same set of
workload, (i.e. the number of processes and the processing time are same for all) and
determining the minimum average waiting time.
- Deterministic modeling is simple and fast.
- Given exact numbers for input, it gives exact results to compare the algorithms.
- Can be mainly used for evaluating scheduling algorithms.
Queuing Models
Queuing network analysis - The computer system can be described as a network of servers.
Each server has a queue of waiting processes. The CPU is a server with its ready queue, and
the I/O system is with its device queues. Knowing arrival rates and service rates, we can
compute utilization, average queue length, average wait time, and so on. This analysis is
called queuing-network analysis.
As an example, let n be the average queue length and ‗α‘ be the average waiting time in the
queue, and let β be the average arrival rate for new processes in the queue. During the time
‗α‘ that a process waits, β x α new processes will arrive in the queue. If the system is in a
steady state, then the number of processes leaving the queue must be equal to the number of
processes that arrive. Thus, n = β x α. This equation, known as Little’s formula, is useful for
any scheduling algorithm and arrival distribution. This formula can be used to compute one
of the three variables if the other two are known.
Queueing analysis can be useful in comparing scheduling algorithms, with some limitations.
Queueing models are often only approximations of real systems.
Simulations
Running simulations is by programming a model of the computer system. Software data
structures represent the major components of the system.
Simulator: The simulator has a variable representing a clock. As time goes on, the simulator
modifies the system state to reflect the activities of the devices, the processes, and the
scheduler. As the simulation executes, statistics that indicate algorithm performance are
gathered and printed. The data to drive the simulation can be generated using the following
methods:
- random-number generator: The data for simulation can be collected using a random-
number generator. It is programmed to generate processes, CPU burst times, arrivals,
departures, etc., according to probability distributions. The results define the
distribution of events in the real system; this distribution can then be used to drive the
simulation.
- Trace tapes: Trace tapes are created by monitoring the real system and recording the
sequence of actual events. We then use this sequence to drive the simulation. Trace
tapes provide an excellent way to compare two algorithms on exactly the same set of
real inputs. This method can produce accurate results for its inputs.
In general, analytic methods use mathematical analysis to determine the performance of an
algorithm. Simulation methods determine performance by simulating the scheduling
algorithm on a sample of processes and computing the resulting performance. Normally,
simulation can provide an approximation of actual system performance. The only reliable
technique for evaluating a scheduling algorithm is to implement the algorithm on an actual
system and monitor its performance in a real-world environment.
A situation, where several processes access and manipulate the same data concurrently and
the outcome of the execution depends on the particular order in which the access takes place,
is called a race condition.
Process Synchronization was introduced to handle problems that arose while multiple process
executions. Some of the problems are listed below:
Critical Section Problem
The Dining-Philosophers Problem
The Bounded-Buffer Problem
The Readers-Writers Problem
3.8 CRITICAL SECTION PROBLEM
A Critical Section is a code segment that accesses shared variables and has to be executed as
an atomic action. In a group of cooperating processes, at a given point of time, only one
process must be executing its critical section. If any other process also wants to execute its
critical section, it must wait until the first one finishes. The various sections in a process code
are as follows:
Entry section: The code preceding the critical section, and which controls access to
the critical section, is termed the entry section.
Critical Section: The code between the entry and exit section.
Exit Section: The code following the critical section is termed the exit section.
Remainder section: The rest of the code not included in either the critical section or
the entry or exit sections is termed the remainder section.
The general structure of a typical process can be illustrated as follows:
do
Entry section
Critical
section
Exit section
Remainder
section
} while (true);
A solution to the critical section problem must satisfy the following three conditions:
Mutual Exclusion: Among a group of cooperating processes, only one process can
be in its critical section at a given point of time.
Progress: If critical section has no process and other threads wants to execute its
critical section then any one of these threads must be allowed to get into its critical
section.
Bounded Waiting: There exists a bound, or limit, on the number of times that other
processes are allowed to enter their critical sections before that request is granted.
Two general approaches that are used to handle critical sections are,
Preemptive kernels: A preemptive kernel allows a process to be preempted while it is
running in kernel mode. Preemptive kernels are especially difficult to design for SMP
architectures, since it is possible for two kernel-mode processes to run simultaneously
on different processors.
Nonpreemptive kernels: Only one process is active in the kernel at a time. A
nonpreemptive kernel is free from race conditions on kernel data structures. It does
not allow a process running in kernel mode to be preempted; a kernel-mode process
will run until it exits kernel mode, blocks, or voluntarily yields control of the CPU.
flag[i] = true;
turn = j;
while (flag[j] && turn == j);
Critical section
flag[i] = false;
Remainder section
} while (true);
Setting a lock is possible only if no other process has already set it. Some hardware solutions
can be provided.
The first approach is to prevent a process from being interrupted while in their critical
section. This approach is taken by non preemptive kernels. This approach is not suitable
for multiprocessor environments.
The second approach is to provide some atomic operations that guarantees to operate as a
single instruction, without interruption. The "Test and Set", is one such operation that
simultaneously sets a boolean lock variable and returns its previous value. TestandSet( )
can be defined as follows:
do
while (TestandSet(&lock))
;
Critical section
lock = false;
Remainder section
} while (true);
This hardware solution is difficult for ordinary programmers to access, especially on multi-
processor machines. Also it is often platform-dependent.
As the resource is locked while a process executes its critical section no other process can
gain access to it. It is a software API equivalent called mutex locks or mutexes. (meaning
mutual exclusion) when used acquires a lock prior to entering a critical section, and
releases it when exiting.
do
acquire lock;
Critical section
release lock;
Remainder section
} while (true);
release( )
{
available = true;
}
Busy waiting: It is a state in which a process is in its critical section, any other process trying
to enter its critical section must loop continuously in the call to acquire( ). Busy waiting
wastes CPU cycles.
Spinlock: When a mutex lock is set, the process ―spins‖ while waiting for the lock to become
available. When locks are expected to be held for short times, spinlocks are useful.
3.12 SEMAPHORES
Semaphore S, is an integer variable for which only two standard atomic operations are
defined, the wait() and signal( ) operations, The wait() operation is termed as P and signal() is
called as V. The definition of wait( ) and signal( ) are as follows:
wait(S)
{
while (S <= 0)
; // busy wait
S--;
}
signal(S)
{
S++;
}
When one process modifies the semaphore value, no other process can simultaneously
modify that same semaphore value. The two basic types of semaphores are,
Counting semaphores: The value of a counting semaphore can range over an unrestricted
domain. Counting semaphores can be used to control access to a given resource
consisting of a finite number of instances.
Binary semaphores. The value of a binary semaphore can range only between 0 and 1.
Binary semaphores acts like mutex locks.
The semaphore is initialized to the number of resources available. Each process that wishes to
use a resource performs a wait( ) operation on the semaphore which decrements the count.
When a process releases a resource, it performs a signal( ) operation which increments the
count. When the count for the semaphore goes to 0, all resources are being used. After that,
processes that wish to use a resource will block until the count is greater than 0. Semaphores
can be used to solve various synchronization problems.
In general, the important properties of semaphores are,
- Works with many processes
- Can have many different critical sections with different semaphores
- Can permit multiple processes into the critical section at once, if needed
The major drawback of semaphore is, if not properly used it might lead towards a situation
called Deadlock.
Problem statement: There are five philosophers sitting around a table, in which there are five
chopsticks kept beside them and a bowl of rice in the centre, When a philosopher wants to
eat, he uses two chopsticks - one from their left and one from their right. When a philosopher
wants to think, he keeps down both chopsticks at their original place.
A philosopher can only eat when he has both left and right chopsticks. Each chopstick can be
held by only one philosopher and so a philosopher can use the fork only if it is not being used
by another philosopher. After he finishes eating, he needs to put down both chopsticks so
they become available to others. A philosopher can take the fork on his right or the one on his
left as they become available, but cannot start eating before getting both of them.
The problem: Design a discipline of behaviour (an algorithm) such that no philosopher will
starve; i.e., each can forever continue to alternate between eating and thinking, assuming that
no philosopher can know when others may want to eat or think.
semaphore chopstick[5];
do
{
wait(chopstick[i]);
wait(chopstick[(i+1) % 5]);
...
/* eat for awhile */
...
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
...
/* think for awhile */
...
} while (true);
This solution guarantees that no two neighbors are eating but it could create a deadlock.
Suppose that all five philosophers become hungry at the same time and each grabs the left
chopstick. All the elements of chopstick will now be equal to 0. When each philosopher tries
to grab the right chopstick, it delays the process forever.
4.1 DEADLOCKS
It is a situation in which several processes compete for the available of resources and the
process could not complete its job. Consider a situation where, a process - P1, request for a
resource. If the requested resource is not available at that time, the process enters a waiting
state. If the requested resource is held by other waiting process- P2, then P1 which is waiting
can never change from its waiting state. This situation is termed as deadlock.
In a deadlock, processes never finish executing, and system resources are tied up, preventing
other jobs from starting.
System Model
A system consists of a finite number of resources to be distributed among a number of
competing processes. The resources may be partitioned into several types (or classes), each
consisting of some number of identical instances. CPU cycles, files, and I/O devices (such as
printers and DVD drives) are examples of resource types. If a system has two CPUs, then the
resource type CPU has two instances. Similarly, the resource type printer may have five
instances.
If a process requests an instance of a resource type, the allocation of any instance of the type
should satisfy the request. If it does not, then the instances are not identical, and the resource
type classes have not been defined properly.
For example, a system may have two printers. These two printers may be defined to be in the
same resource class if no one cares which printer prints which output. However, if one printer
is on the ninth floor and the other is in the basement, then people on the ninth floor may not
see both printers as equivalent, and separate resource classes may need to be defined for each
printer.
A lock is typically associated with protecting a specific data structure—that is, one lock may
be used to protect access to a queue, another to protect access to a linked list, and so forth.
For that reason, each lock is typically assigned its own resource class, and definition is not a
problem.
A process must request a resource before using it and must release the resource after using it.
A process may request as many resources as it requires to carry out its designated task.
Obviously, the number of resources requested may not exceed the total number of resources
available in the system. In other words, a process cannot request three printers if the system
has only two.
Under the normal mode of operation, a process may utilize a resource in only the following
sequence:
1. Request. The process requests the resource. If the request cannot be granted
immediately (for example, if the resource is being used by another process), then the
requesting process must wait until it can acquire the resource.
2. Use. The process can operate on the resource (for example, if the resource is a
printer, the process can print on the printer).
3. Release. The process releases the resource.
The request() & release() device, open() & close() file, allocate() & free() memory are
system calls.
For each use of a kernel-managed resource by a process or thread, the operating system
checks to make sure that the process has requested and has been allocated the resource. A
system table records whether each resource is free or allocated. For each resource that is
allocated, the table also records the process to which it is allocated. If a process requests a
resource that is currently allocated to another process, it can be added to a queue of processes
waiting for this resource. A set of processes is in a deadlocked state when every process in
the set is waiting for an event that can be caused only by another process in the set.
All four conditions must hold for a deadlock to occur and these four conditions are not
completely independent.
To ensure that deadlocks never occur, the system can use either a deadlock prevention or a
deadlock-avoidance scheme. Deadlock prevention provides a set of methods to ensure that
at least one of the necessary conditions cannot hold. These methods prevent deadlocks by
constraining how requests for resources can be made.
Deadlock avoidance requires that the operating system be given additional information in
advance concerning which resources a process will request and use during its lifetime. With
this additional knowledge, the operating system can decide for each request whether or not
the process should wait. To decide whether the current request can be satisfied or must be
delayed, the system must consider the resources currently available, the resources currently
allocated to each process, and the future requests and releases of each process.
No Preemption
A necessary condition for deadlocks is that there be no preemption of resources that have
already been allocated. To ensure that this condition does not hold, the following methods
can be adopted.
Method 1:
If a process is holding some resources and requests another resource that cannot be
immediately allocated to it, then all resources the process is currently holding are
implicitly released. These preempted resources are added to the list of resources for
which the process is waiting. The process will be restarted only if it can regain its old
resources, as well as the new ones that it has requested.
Method 2:
If a process requests some resources, check for its availability and allocate it if
available. If not, check if it is allocated to some other process that is waiting for
additional resources. If so, preempt the desired resources from the waiting process and
allocate them to the requesting process.
If the resources are neither available nor held by a waiting process, the requesting
process must wait. A process can be restarted only when it is allocated the requested
new resources and recovers any resources that were preempted while it was waiting.
Circular Wait
To ensure that circular wait condition never holds, enforce a total ordering of all resource
types and require that each process requests resources in an increasing order of enumeration.
Ensuring that resources are acquired in the proper order is the responsibility of application
developers. Software can also be used to verify that locks are acquired in the proper order
and to give appropriate warnings when locks are acquired out of order.
Low device utilization and reduced system throughput are the major drawback of deadlock
prevention methods.
Any instructions or data for execution must be in main memory or register. If the data are not
in memory, it must be moved before CPU execution. Main memory is accessed through a
transaction on the memory bus. Registers that are built into the CPU are generally accessible
within one cycle of the CPU clock.
Cache is a fast memory that lies between the CPU and main memory, which is present on the
CPU chip for fast access.
Each process must have a separate memory space to protect the processes from each
other.
The memory spaces have distinct legal addresses that the process may access.
The base register and the limit register are the two registers which protects the address
space.
The base and limit registers can be loaded only by the operating system,
The base register holds the smallest legal physical memory address
The limit register specifies the size of the range. For example, if the base register
holds 3020 and the limit register is 1200, then the program can legally access all
addresses from 3020 through 4219.
Any attempt by a program executing in user mode to access operating-system
memory results in a fatal error thus preventing a user program from modifying the
content of the operating system.
The operating system can change the value of the registers but prevents user
programs from changing its contents.
An unrestricted access if given to the operating system, executing in kernel mode,
to OS memory and users‘ memory.
Virtual address is also known as Logical address and is generated by the CPU.
Physical address is the address that actually exists on memory.
Dynamic Loading: All the programs are loaded in the main memory for execution.
Sometimes complete program is loaded into the memory. If a certain part or routine of the
program alone is loaded into the main memory when it is called by the program, it is called as
Dynamic Loading.
Dynamic Linking: If one program is dependent on other program, instead of loading all the
dependent programs, CPU can link the dependent programs dynamically to the main
executing program when it is required. This mechanism is called as Dynamic Linking.
Swapping: If there is not enough main memory space to hold all the currently active
processes in a timesharing system, excess process are kept on disk and brought in to run
dynamically. Swapping is the process of bringing in each process in main memory, running it
for a while and then putting it back to the disk.
Address Binding: A program on a disk is in the form of a binary executable file. For
execution, the program must be brought into memory and placed within a process. An input
queue is formed if the processes on the disk are waiting to be brought into memory for
execution.
Single task Procedure: It selects one of the processes from the input queue and loads that
process into memory. During execution of process, it accesses instructions and data from
memory. When the process terminates, its memory space is declared available.
Normally, a user process may reside in any part of the physical memory starting from the first
address in address space of the computer say 00000.
Memory protection, Memory allocation, Fragmentation, Segmentation and Paging are some
of the important concepts considered in Contiguous memory allocation.
Memory Protection
Memory protection controls memory access rights on a computer. It prevents a
process from accessing memory that has not been allocated to it. It also prevents a bug
within a process from affecting other processes, or the operating system itself. The
relocation and limit registers protect both the operating system and the other users
programs and data from being modified by this running process.
The CPU scheduler selects a process for execution
The dispatcher loads the relocation and limit registers with the correct values as part
of the context switch.
Every address generated by a CPU is checked against the relocation and limit
registers
The advantage of using relocation-register scheme is, it provides an effective way to
allow the operating system‘s size to change dynamically.
Transient operating-system code: The operating system contains code and buffer space for
device drivers which is not always kept in memory. As needed, it enters and leaves the
memory changing the size of the operating system during program execution.
Memory Allocation
Memory allocation is a process by which computer programs are assigned memory or space.
The free blocks of memory are known as holes. The set of holes is searched to determine
which hole is best to allocate. The three types of memory allocation are:
a. First Fit: The first hole that is big enough is allocated to program. Searching can
start from the beginning, or at the location where the previous first-fit search ended.
Searching is stopped as soon as a free hole that is large enough is found
b. Best Fit: The smallest hole that is big enough is allocated to program. smallest hole
that is big enough. If the entire list, is ordered by size searching is easier. This method
produces the many smallest leftover holes after all allocations.
c. Worst Fit: The largest hole that is big enough is allocated to program. The entire
list, must be searched, it is not sorted by size. This method produces the largest
leftover hole, which may be better than a best-fit method.
Fragmentation
Fragmentation occurs in a dynamic memory allocation system when most of the free blocks
are too small to satisfy any request. It is a situation where these small blocks of available
memory cannot be used for any further memory allocation.
External fragmentation: When processes are loaded and removed from the memory. There
might exist small fragments. The existing free holes are non contiguous i.e. the memory is
fragmented into large number of small holes also known as External Fragmentation.
Internal fragmentation: If the physical memory is broken into fixed size blocks and
memory is allocated in unit of block sizes. The memory allocated to a space may be slightly
larger than the requested memory. The difference between allocated and required memory is
known as Internal fragmentation i.e. the memory that is internal to a partition but is of no use.
Compaction: It is a solution to the problem of external fragmentation. Shuffling the memory
contents to place all free memory together in one large block is the technique applied in
compaction. The simplest compaction algorithm moves all processes toward one end of
memory; all holes move in the other direction, producing one large hole of available memory
which involves lot of overhead.
After Compaction
Segmentation
Segmentation is a memory management scheme that supports the user-view of memory.
Segmentation allows breaking of the virtual address space of a single process into segments
that may be placed in non-contiguous areas of physical memory.
Paging
Paging is a solution to fragmentation problem that allows the physical address space of a
process to be non-contagious. The physical memory is divided into blocks of equal size
called Pages. The pages belonging to a certain process are loaded into available memory
frames.
Page Table
A Page Table is the data structure that stores the mapping between virtual
address and physical addresses. It is used by a virtual memory system in a computer
operating system.
CPU Scheduling
CPU scheduling process allows one process to use the CPU while the execution of another
process is on hold state / waiting state due to unavailability of resource. It makes use of CPU
fully. The aim of CPU scheduling is to make the system fast and efficient.
Scheduling Criteria
Some of the CPU scheduling criteria are listed below:
CPU utilization : To make out the best use of CPU and not to waste any CPU cycle,
CPU would be working most of the time. Considering a real system, CPU usage
should range from 40% to 90% for a lightly loaded to heavily loaded system
respectively.
Throughput: It is the total number of processes completed per unit time i.e., the total
amount of work done per unit time which may range widely depending on the specific
processes.
Turnaround time: It is the amount of time taken to execute a particular process, i.e.
The duration from time of submission of the process to the time of completion of the
process.
Waiting time: The amount of time a process has been waiting in the ready queue to
get control on the CPU.
Load average: The average number of processes residing in the ready queue waiting
for their turn to get into the CPU.
Response time: The duration between the submission of a request and the first
response produced.
Normally, in an efficient system, CPU utilization and Throughput must be maximized and
other factors must be reduced for proper optimization.
4.7 SEGMENTATION
Segmentation is a memory management technique in which each job is divided into several
segments of different sizes, one for each module that contains pieces that perform related
functions. Each segment is actually a different logical address space of the program.
When a process is to be executed, its corresponding segmentation are loaded into non-
contiguous memory though every segment is loaded into a contiguous block of
available memory.
Segments are of variable-length where as in paging pages are of fixed size.
A program segment contains the program's main function, utility functions, data
structures etc.,
Segments are numbered and are referred to by a segment number, rather than by a
segment name. The logical address consists of segment-number and the offset. The
segment number is used as an index to the segment table. The offset d of the logical
address must be between 0 and the segment limit.
4.8 PAGING
Pages: Paging is a memory management technique in which process address space is broken
into blocks of the same size called pages. The size of the pages is in power of 2, between 512
bytes and 8192 bytes. The size of the process is measured in the number of pages.
Frames: Main memory is divided into small fixed-sized blocks of physical memory
called frames and the size of a frame is kept the same as that of a page to have optimum
utilization of the main memory and to avoid external fragmentation.
Implementation: The ides behind is - breaking physical memory into fixed-sized blocks
called frames and breaking logical memory into blocks of the same size called pages. When a
process is to be executed, its pages are loaded into any available memory frames from their
source. When the system allocates a frame to any page, it translates this logical address into a
physical address and create entry into the page table to be used throughout execution of the
program. The backing store is divided into fixed-sized blocks that are the same size as the
memory frames.
Page address is called logical address and represented by page number and the offset.
Logical Address = Page number + page offset
Frame address is called physical address and represented by a frame number and the offset.
Physical Address = Frame number + page offset
Advantages:
The logical address space is now totally separate from the physical address space, so a
process can have a logical 64-bit address space even though the system has less than
264 bytes of physical memory.
Paging reduces external fragmentation,
Paging is simple to implement
Swapping is easy since the page size and frame size are equal.
Disadvantages:
Page table requires extra memory space
Has internal fragmentation.
***
OS UNIT V
Logical Physical
Page Table
memory memory Storage device
address
Pure demand paging: This scheme never bring a page into memory until it is required.
The hardware to support demand paging are,
• Page table – marks an entry invalid through a valid–invalid bit or a special value of
protection bits.
• Secondary memory - holds the pages that are not present in main memory. It is
known as the swap device, and the section of disk used for this purpose is known as
swap space.
Efficiency: Demand paging can significantly affect the performance of a computer system.
If there are no page faults, then
the effective access time = the memory access time.
If, a page fault occurs, we must first read the relevant page from disk and then access the
desired word. The effective access time can be calculated as,
effective access time = (1 − p) × ma + p × page fault time
where p – the probability of page fault and,
ma – the memory access time
Advantages
Efficient use of memory.
Multiprogramming capability.
Disadvantage : Processor overhead for handling page interrupts are greater
5.3 PAGE REPLACEMENT ALGORITHMS
In demand paging, when a process requests for more pages and free memory is not available
to bring them in, the pages that are not being used currently are identified and moved to the
disk to get free frames. This technique is called Page Replacement.
Page replacement takes the following approach.
If no frame is free, find one that is not currently being used and free it.
Before freeing a frame do the following
- writing its contents to swap space
- changing the page table and other tables to indicate that the page is no longer
in memory
Use the freed frame to hold the page for which the process faulted.
Modify the page-fault service routine to include page replacement:
Modify bit / dirty bit: Each page or frame has a modify bit associated with it in the
hardware. The modify bit for a page is set by the hardware whenever any byte in the page is
written into, indicating that the page has been modified.
Before selecting a page for replacement, examine its modify bit.
Bit is set - If the bit is set, it indicates that the page has been modified. If so, write the page to
the disk.
Bit not set- If the modify bit is not set, the page has not been modified since it was read into
memory and need not write the memory page to the disk. This scheme can significantly
reduce the time required to service a page fault, since it reduces I/O time by one-half if the
page has not been modified.
A frame-allocation algorithm and a page-replacement algorithm are needed to implement
demand paging. Frame allocation decides, the number of frames to be allocated to each
process. Page replacement decides, the frame to be selected for replacement.
9 9 9 6 6 6 4 4 4 0 0 0 9 9 9
0 0 0 3 3 3 6 6 6 1 1 1 0 0
1 1 1 0 0 0 3 3 3 6 6 6 1
Fig 5.3 FIFO Page replacement algorithm
9 9 9 6 6 6 6 6 9
0 0 0 0 4 0 0 0
1 1 3 3 3 1 1
Fig 5.4 FIFO Page replacement algorithm
9 9 9 6 6 4 4 4 0 1 1 1
0 0 0 0 0 0 3 3 3 0 0
1 1 3 3 6 6 6 6 6 9
Fig 5.5 LRU Page replacement algorithm
Application programs
I/O Control
Devices
Indexed sequential access: An index is created for each file which contains pointers to
various blocks. The Index is searched sequentially and its pointer is used to access the file
directly. It constructs an index for the file. To find a record in the file, initially the index is
searched and then the pointer is used to access the file directly and to find the desired record.
For large files, the index file itself may become too large to be kept in memory. Under such
situation, an index for the index file can be created. The primary index file contains pointers
to secondary index files, which point to the actual data items.
Two-Level Directory
The structure of two level directory is shown in Figure 5.10.
Two level directory has
o Master file directory
o User file directory.
Each user gets their own directory space.
D1 D2 D1 D2 D3 D4 D1
Directory Implementation
Directories need to be fast to search, insert, and delete, with a minimum of wasted disk space.
The two important types of directory structures are Linear List and Hash Table.
Linear List
A linear list is the simplest and easiest directory structure to set up.
Finding a file in a directory requires only a linear search.
Deleting a file can be done by moving all entries, by marking an entry as deleted with
a flag bit. Moving the last entry into the newly vacant position is another method
followed during deletion.
Complex insertions and deletions are performed for Sorting the list quickly.
A linked list makes insertions and deletions into a sorted list easier, with overhead for
the links.
Hash Table
A hash table can also be used to speed up searches.
Hash tables are generally implemented in addition to a linear or other structure
The hash table takes a value computed from the file name and returns a pointer to the
file name in the linear list.
It can greatly decrease the directory search time.
Insertion and deletion are performed directly
The Hash table has a fixed size and the hash function depends on that size.
Contiguous Allocation
Contiguous Allocation - all blocks of a file be kept together continuously.
Reading successive blocks of the same file generally requires no movement of the
disk heads, or at most one small step to the next adjacent cylinder.Hence, the
performance for reading operation is very fast
Storage allocation involves the allocation of contiguous blocks of memory like first
fit, best fit, fragmentation problems, etc.
Problems can arise when files grow, or if the exact size of a file is unknown at
creation time:
o Over-estimation of the file's final size increases external fragmentation and
wastes disk space.
o Under-estimation may require that a file be moved or a process aborted if the
file grows beyond its originally allocated space.
o If a file grows slowly over a long time period and the total final space must be
allocated initially, then a lot of space becomes unusable before the file fills the
space.
Extents: allocating file space in large contiguous chunks is called as extents. When a
file outgrows its original extent, then an additional one is allocated. An extent may be
the size of a complete track or even cylinder, aligned on an appropriate track or
cylinder boundary.
Linked Allocation
Disk files can be stored as linked lists, where the storage space is connected by each
link.
Linked allocation involves no external fragmentation
It does not require the file sizes to be known well in advance
It allows files to grow dynamically at any time.
Linked allocation is only efficient for sequential access files.
Allocating clusters of blocks reduces the space wasted by pointers, but it leads to
internal fragmentation.
If a pointer is lost or damaged this methods becomes unreliable.
Doubly linked list is more reliable, but has additional overhead and wasted space.
The File Allocation Table, FAT, used by DOS is a variation of linked allocation, where all
the links are stored in a separate table at the beginning of the disk. The benefit of this
approach is that the FAT table can be cached in memory, greatly improving random access
speeds.
Indexed Allocation
Indexed Allocation combines all of the indexes for accessing each file into a common
block for that file. Some disk space is wasted because an entire index block must be
allocated for each file, regardless of how many data blocks the file contains.
Several approaches for determining the block size are,
o Linked Scheme - An index block is one disk block, which can be read and
written in a single disk operation. The first index block contains some
header information, the first N block addresses, and if necessary a pointer
to additional linked index blocks.
Multi-Level Index - The first index block contains a set of pointers to
secondary index blocks, which in turn contain pointers to the actual data
blocks.
Combined Scheme - This is the scheme used in UNIX inodes, in which the
first twelve data block pointers are stored directly in the inode, and then
singly, doubly, and triply indirect pointers provide access to more data blocks
as needed. For both small files and huge files, the data blocks are readily
accessible using a relatively small number of disk accesses.
The advantage of magnetic bubble memory is that the large amount of data can be stored in a
less area. They are considerably faster than semiconductor memories. Charge coupled
devices (CCD) are volatile memory. In CCD‘s presence or absence of electric charge
represents a bit.
There exist different types of secondary storage devices, each of them suitable for a specific
purpose. They mainly differ in the following aspects: Technology used to store data,
Portability of storage device and Access time.
Magnetic Disks
One or more platters in the form of dsks covered with magnetic media. Hard
disk platters are made of rigid metal. Each platter has two working surfaces. The
magnetic medium of the disk (hard disk or floppy) is pre-written with empty tracks,
which are further divided into sector. The collection of all tracks that are the same
distance from the edge of the platter, i.e., all tracks immediately above one another is
called a cylinder. One of many concentric rings, that are encoded on the disk during
formatting are called tracks. A segment of one of the concentric tracks, encoded on a
disk during formatting are called sectors. (Figure 5.12)
In addition, an empty index is also placed on the disk. The data is stored in sectors and tracks
and the PC locates specific data with the help of the index. The placing of tracks, dividing
them into sectors, and placing of an empty index is called formatting of the disk. Without
formatting, the disk will not accept any data.
The basic unit of data storage in magnetic disk is called cluster. A cluster includes two or
more sectors. The smallest space that a file can occupy is a cluster. Depending on the size of
the file, it can take, track of the clusters, that hold the contents of each file.
Hard disks are designed to store very high volume of data. Hard disks can store gigabytes to
terabyte of data and they form an integral part of the computer. Most operating systems are
stored in hard disks and all contents (application software, documents, images, music files
etc) in the computer are also stored in the hard disk.
The data is stored on a metal platter in the form of magnetic spots. The metal platter is sealed
inside a disk drive that protects the platter and as well enables reading and writing to the disk.
Hard disk may contain several platters forming hard disk packs that increase the storage
capacity. These provide fast access for both reading and writing. Four common hard disk
storage technologies are:
ATA (Advanced Technology Attachment)
FireWire
SCSI(Small Computer System Interface)
RAID (Redundant Array of Inexpensive Disks) and Fiber Channel
Magnetic Tape
Magnetic tapes are very similar to the tapes that are used in radio cassette player. It provides
a very effective means for backing up of large amount of data. Tape drives are well suited for
backing up a systems entire hard disk. Capacities of tapes can be as high as 100 GB and
more, tape offers an inexpensive way to store a lot of data on a single cassette.
Special type of tape drive uses digital audiotape (DAT) to achieve high storage capacities.
DAT drives typically have multiple read and write heads built into a small wheel or cylinder
that spins near the tape at high speed. The main limitation is that data stored is in
a sequential mode, and hence needs long access time.
Magnetic tapes were once used for common secondary storage before the days of hard
disk drives, but today are used primarily for backups. Accessing a particular spot on a
magnetic tape can be slow, but once reading or writing commences, access speeds are
comparable to disk drives. Capacities of tape drives can range from 20 to 200 GB, and
compression can double that capacity.
Disk Structure
The traditional head-sector-cylinder, HSC numbers are mapped to linear block
addresses by numbering the first sector on the first head on the outermost track as
sector 0. Numbering proceeds with the rest of the sectors on that same track, and then
the rest of the tracks on the same cylinder before proceeding through the rest of the
cylinders to the center of the disk. In modern practice these linear block addresses are
used in place of the HSC numbers for a variety of reasons:
The linear length of tracks near the outer edge of the disk is much longer than for
those tracks located near the center, and therefore it is possible to squeeze many more
sectors onto outer tracks than onto inner ones.
All disks have some bad sectors. A few spare sectors can be used in place of the bad
ones. The mapping of spare sectors to bad sectors in managed internally to the disk
controller.
Modern Hard drives have thousands of cylinders, and hundreds of sectors per track on
their outermost tracks. These numbers exceed the range of HSC numbers on
traditional ones.
The limit on how closely packed individual bits can be placed on a physical media, is
growing increasingly due to technological advances.
Modern disks pack many more sectors into outer cylinders than inner ones, using one of two
approaches:
Constant Linear Velocity (CLV): the density of bits is uniform from cylinder to
cylinder. Because there are more sectors in outer cylinders, the disk spins slower
when reading those cylinders. This is the approach used by modern CDs and DVDs.
Constant Angular Velocity (CAV): the disk rotates at a constant angular speed, with
the bit density decreasing on outer cylinders. Hard disks have a constant number of
sectors per track on all cylinders.
Disk Attachment
Disk drives can be attached either directly to a particular host ( a local disk ) or to a network.
The two attachment methods are,
Host attached storage
Network attached storage
Host-Attached Storage: The most common interfaces are IDE or ATA, each of which allow
up to two drives per host controller. High end workstations or other systems that needs larger
number of disks use SCSI disks:
o The SCSI standard supports up to 16 targets on each SCSI bus, one of which
is generally the host adapter and the other 15 of which can be disk or tape
drives.
o A SCSI supports up to 8 units within each target. These would generally be
used for accessing individual disks within a RAID array.
o The SCSI standard also supports multiple host adapters in a single computer,
i.e. multiple SCSI busses.
o SCSI cables may be either 50 or 68 conductors. SCSI devices may be external
as well as internal.
Fibre channel (FC) is a high-speed serial architecture that can operate over optical
fiber or four-conductor copper wires, and has two variants:
A large switched fabric having a 24-bit address space that allows for multiple devices
and multiple hosts to interconnect, forming the basis for the storage-area networks,
SANs,
The arbitrated loop, FC-AL, that can address up to 126 devices like drives and
controllers.
Network-Attached Storage
Network attached storage connects storage devices to computers using a remote
procedure call (RPC) interface, with NFS file system mounts. (Figure 5.13)
Allows several computers in a group for shared storage.
Can be implemented using ISCSI cabling, allowing long-distance remote access to
shared files.
Allows computers to easily share data storage, but it is less efficient than standard
host-attached storage.
Client
NAS
LAN / WAN Client
NAS
NAS
Client
SSTF Scheduling
Shortest Seek Time First scheduling is more efficient. It tries to service all the
requests close to the current head position before moving the head far away to service
other requests.
SSTF selects the request with the least seek time from the current head position.
SSTF reduces the total head movement.
May lead to starvation if a constant stream of requests arrives for the same general
area of the disk. Example:
queue : 98, 183, 37, 122, 14, 124, 65, 67
head starts at 53
Movement 53 65 67 37 14 98 122 124 183
This scheduling method results in a total head movement of only 236 cylinders
SCAN Scheduling
The SCAN algorithm, also called as the elevator algorithm moves back and forth from
one end of the disk to the other, similar to an elevator processing requests in a
multistorey building.
If a request arrives just ahead of the moving head then it will be processed right away,
but if it arrives just after the head has passed, then it will have to wait for the head to
pass going the other way on the return trip. This leads to a wide variation in access
times.
Example:
queue : 98, 183, 37, 122, 14, 124, 65, 67
head starts at 53
Movement 53 37 14( 0 ) 65 67 98 122 124 183
C-SCAN Scheduling
The Circular-SCAN algorithm improves upon SCAN by treating all requests in a circular
queue fashion - Once the head reaches the end of the disk, it returns to the other end without
processing any requests, and then starts again from the beginning of the disk:
Example:
queue : 98, 183, 37, 122, 14, 124, 65, 67
head starts at 53
Movement 53 65 67 98 122 124 183 (199) (0) 14 37
LOOK Scheduling
LOOK scheduling improves upon SCAN by looking ahead at the queue of pending requests,
and not moving the heads any farther towards the end of the disk than is necessary.
Example:
queue : 98, 183, 37, 122, 14, 124, 65, 67
head starts at 53
Movement 53 65 67 98 122 124 183 14 37
The Selection of a Disk-Scheduling Algorithm can be based on the load. For slightly larger
loads, SSTF offers better performance than FCFS, but may lead to starvation when loads
become heavy enough. For busier systems, SCAN and LOOK algorithms eliminate starvation
problems.
***
Reference:
1. Abraham Silberschatz, Greg Gagne, and Peter Baer Galvin, "Operating System
Concepts, Eighth Edition ", Chapter 11