OSgate
OSgate
Operating System
Operating System
An operating system acts as an intermediary between the user of a
computer and the computer hardware. An Operating System (OS) is a
software that manages the computer hardware.
User 1 User 2 User 3 User 3
Batch Systems
Early computers were the large machines that run from a console. The
users of batch systems did not interact directly with the computer systems.
Rather, the user prepared a job which consists of programs, data and
control information and then submit it to the computer operator. The job
prepared would be in the form of punch cards. After sometime perhaps
minutes, hours or days, the output is prepared.
Key Points
✦ The main drawback of batch system is the lack of interaction between the
user and the job while it was executing.
✦ Multiprogramming creates logical parallelism.
Multiprogramming
It is the technique of running several programs at a time using time sharing.
It allows a computer to do several things at the same time.
The concept of multiprogramming is that the OS keeps several jobs in
memory simultaneously. The operating system selects a job from the job
pool and starts executing a job, when that job needs to wait for any
input/output operations, the CPU is switched to another job. So, the main
idea here is that the CPU is never idle.
Multitasking
Multitasking is the logical extension of multiprogramming. The concept of
multitasking is quite similar to multiprogramming but difference is that the
switching between jobs occurs so frequently that the users can interact
with each program while it is running.
This concept is also known as time sharing system. A time shared
operating system uses CPU scheduling and multiprogramming to provide
each user with a small portion of time shared system.
Distributed Systems
In distributed systems, the computation is distributed among several
processors. Each processor in distributed system has its own local
memory and do not share memory or a clock.
A distributed operating system governs the operation of a
distributed computer system and provides a virtual machine abstraction to
its users.
Key Points
✦ A pool of job on disk allows the OS to select which job to run next, to
increase CPU utilization
✦ If jobs come directly on cards (or on magnetic tape), they run sequentially on
FCFS basis but when several jobs are on direct access devices like disk, job
scheduling is possible.
✦ The most important aspect of job scheduling is the ability to
multiprogramming.
✦ If several jobs are ready to be brought into memory, and there is not enough
room for all of them, then the system must choose among them, making this
decision is job scheduling.
✦ When the OS selects a job from job pool, it loads that job into memory. This
cause the residence of several programs in memory and calls for memory
management scheme.
✦ If several jobs are ready to run at same time, the system must choose among
them. This decision is called CPU scheduling.
Threads
A thread is a basic unit of CPU utilisation. A thread comprises a
1- dimensional program counter , a register set and a stack. It shares with
other threads belonging to the same process its code section, data section
and other system resourses such as open files and signals . A traditional
procoss has a single thread of control. If a process has multiple thread of
control, it can perform more than one task at a time.
Multithreading
An application typically is implemented as a separate process with several
threads of control. In some situations, a single application may be required
to perform several similar tasks. e.g., a web server accepts client request
for web pages, images, sound and so on. A busy web server may have
several of clients concurrently accessing it. If the web server run as a
traditional single threaded process, it would be able to service only one
client at a time.
The amount of time that a client might have to wait for its request to be
serviced could be enormous. So, it is efficient to have one process that
contains multiple threads to serve the same purpose.
This approach would multithreaded the web server process, the server
would create a separate thread that would listen for client requests, when a
request was made rather than creating another process, it would create
another thread to service the request.
Multithreading Model
There are two types of threads
(i) User threads (ii) Kernel threads
Kernel Threads
Kernel threads are supported and managed directly by the operating system.
User Threads
They are above the kernel and they are managed without kernel support.
There are three common ways of establishing relationship between user
threads and kernel threads
(a) Many-to-many model (b) One-to-one model
(c) Many-to-one model
Handbook Computer Science & IT 187
Key Points
✦ User level threads are threads that are visible to the programmer and
unknown to the Kernel.
✦ User level threads are faster to create and manage than that of Kernel threads.
Process
A process is a program in execution. A process is more than the program
code i . e., text section. It also includes the current activity as represented by
the value of the program counter and the contents of the processor’s register.
Max
Stack ® Contains temporary data (such as function parameters, return
addresses and local variables).
¯
Heap ® Memory that is dynamically allocated during process run time.
Data ® Data section contains global variables.
Text ® Code section
A simple process block
Process in Memory
Each process is represented in the OS by a Process Control Block (PCB)
also called a task control block.
Ready Running
Scheduler dispatch
I/O or event I/O or event
completion wait
Waiting
Diagram of a process state
188 Operating System
Key Points
✦ The list of processes waiting for a particular I/O device is called a device
queue. Each device has it’s own device queue.
✦ I/O bound process is one that spends more of its time doing I/O rather than it
spends doing computations.
✦ A CPU bound process is one which uses more of its time doing computations
rather than it spends doing I/O activities.
Schedulers
A process migrates among various scheduling queues throughout its
lifetime. The OS must select for scheduling purposes, processes from
these queues in some fashion. The selection process is carried out by the
appropriate scheduler.
Dispatcher
It is the 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.
Scheduling Algorithm
All of the processes which are ready to execute and are placed in main
memory then selection of one of those processes is known as scheduling,
and after selection that process gets the control of CPU.
Scheduling Criteria
The criteria for comparing CPU scheduling algorithms include the following
CPU Utilization Means keeping the CPU as busy as possible.
Throughput It is nothing but the measure of work i . e., the number of
processes that are completed per time unit.
Turnaround Time The interval from the time of submission of a process to
the time of completion. It is the sum of the periods spent waiting to get into
memory, waiting in the ready queue, executing on the CPU and doing I/O.
Waiting Time The sum of the periods spent waiting in the ready queue.
Response Time The time from the submission of a request until the first
response is produced.
Key Points
✦ The amount of time, a process takes to start responding not the time it takes to
output the response is called response time.
✦ Response time is desirable to maximize CPU utilization and throughput and
to minimize turnaround time, waiting time and response time.
0 24 27 30 P1 24
Waiting time for P1 = 0 ms P2 3
Waiting time for P2 = 24 ms P3 3
Waiting time for P3 = 27 ms Turn around time for P1 = 24 - 0 = 24 ms
Average waiting time Turn around time for P2 = 27 - 24 = 3 ms
Turn around time for P3 = 30 - 27 = 3 ms
P1 6 0 3 9 16 24
P2 8 Average waiting time
P3 7 3 + 16 + 9 + 0
= = 7 ms
P4 3 4
waiting time for P1 = 3
waiting time for P2 = 16
waiting time for P3 = 9
waiting time for P4 = 0
A more appropriate term for this scheduling method would be the shortest
next CPU burst algorithm because scheduling depends on the length of
the next CPU burst of a process rather than its total length.
Priority Scheduling
A priority is associated with each process and the CPU is allocated to the
process with the highest priority. Equal priority processes are scheduled in
FCFS order.
We can be provided that low numbers represent high priority or low
numbers represent low priority. According to the question, we need to
assume any one of the above.
Key Points
✦ Priority scheduling can be either preemptive or non-preemptive.
✦ A preemptive prioirty scheduling algorithm will preempt the CPU, if the
priority of the newly arrived process is higher than the priority of the currently
running process.
✦ A non-preemptive priority scheduling algorithm will simply put the new
process at the head of the ready queue.
192 Operating System
Process Table
Burst Time
Process
(in milliseconds)
P1 24
P2 3
P3 3
Let’s take time quantum = 4 ms. Then the resulting RR schedule is as
follows
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 waits for the 6 ms (10 - 4), P2 waits for 4 ms and P3 waits for 7 ms.
Thus,
6 + 4+7
Average waiting time = = 5.66 ms
3
Synchronization
When 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 race
condition.
e.g., Suppose we have two variables A and B. The operation on A and B are
as follows
Operation 1 A = Result Operation 2 B = Result
A= A+1 B = B -1
Result = A Result = B
Now, initially if value of result = 4 and sequence is operation 1, then
operation 2. Then,
A=4
A = 4 + 1= 5
Result = A = 5
B = Result = 5
B = 5 - 1= 4
Result = B = 4
Now, if the sequence of operation gets changed as is operation 2, then
operation 1. Then,
B = Result = 4
B = B - 1= 4 - 1= 3
Result = B = 3
A = Result = 3
A = A + 1= 4
Result = A = 4
For the race condition above, we need to ensure that only one process at a
time can be manipulating the variable result. To make such a gaurantee,
we require that the processes be synchronized is some way.
Handbook Computer Science & IT 195
Kernel M Kernel
Semaphores
Semaphore is nothing but a synchronization tool. A semaphore S is an
integer variable that apart from initialization, is accessed only through two
standard atomic operations wait ( ) and signal ( ). The wait ( ) operation
does testing of the integer value of S (S £ 0) as well as its possible
modification (S - -).
wait (S)
{
while S < = 0
i ; / / no operation
S - -;
}
The signal ( ) operation does increment to the integer value of S (S + + ).
Signal (S)
{
S + +;
}
Handbook Computer Science & IT 197
All modifications to the integer value of semaphore in the wait ( ) and signal
( ) operations must be executed indivisibly.
Usage of Semaphores
● We can use binary semaphores to deal with the critical section problem
for multiple processes. The n processes share a semaphore, mutex,
initialized to 1.
● The value of binary semaphore can range only between 0 and 1.
● Binary semaphores are known as mutex locks as they are locks that
provide mutual exclusion.
Mutual exclusion implementation with semaphores
do
{
wait (mutex);
// Critical section
signal (mutex);
// Remainder section
} while (TRUE);
Key Points
✦ Counting semaphores can be used to control access to a given resource
consisting of a finite number of instances.
✦ The value of a counting semaphore can range over an unrestricted domain.
Producer-Consumer Problem
In this problem, there are two processes
(i) producer process (ii) consumer process.
A shared buffer (global) is defined which is accessible by producer and
consumer.
●
Producer process produces items and appends these in buffer.
●
Consumer process consumes items from buffer.
198 Operating System
Key Points
✦ Both of the processes (i.e., producer and consumer) cannot access buffer
simultaneously.
✦ Mutual exclusion should be here.
✦ Producer cannot append item in full buffer (finite buffer) and consumer can
not take item from empty buffer.
Mutual Exclusion
Resources must be allocated to processes at any time in an exclusive
manner and not on a shared basis for a deadlock to be possible. If another
process requests that resource, the requesting process must be delayed
until the resource has been released.
No Preemption Condition
Resources can’t be preempted. A resource can be released only voluntarily
by the process holding it, after that process has completed its task.
Process Process
P1 P2
Resource Instance
■ One instance of resource type R 1. ■ Two instances of resource type R 2.
■ One instance of resource type R 3. ■ Three instances of resource type R 4 .
R1 R3
P1 P2 P3
R2 R4
Example of resource allocation graph
Process States
●
Process P1 is holding an instance of resource type R2 and is waiting for an
instance of resource type R1.
●
Process P2 is holding an instance of R1 and R2 is waiting for an instance of
resource type R3.
●
Process P3 is holding an instance of R3.
●
Basic facts related to resource allocation graphs are given below
Deadlock Prevention
Deadlock prevention is a set of methods for ensuring that atleast one of the
necessary conditions can’t hold.
Deadlock Avoidance
A deadlock avoidance algorithm dynamically examines the resource
allocation state to ensure that a circular wait condition can never exist. The
resource allocation state is defined by the number of available and
allocated resources and the maximum demands of the processes.
Safe State
A state is safe, if the system can allocate Deadlock Unsafe
resources to each process and still avoid a
deadlock.
A system is in safe state, if there exists a safe
sequence of all processes. A deadlock state is Safe
an unsafe state. Not all unsafe states cause
deadlocks. Safe, unsafe and deadlock
Banker’s Algorithm
Data structures for Banker’s algorithm available. Vector of length m. If
available [ j ] = k, there are k instances of resource type R j available.
●
Max n ´ m matrix. If max [ i , j ] = k, then process Pi may request at most k
instances of resource type R j .
●
Allocation n ´ m matrix. If allocation[ i , j ] = k, then Pi is currently allocated
k instances of Ri .
●
Need n ´ m matrix. If need[ i , j ] = k, then Pi may need k more instances of
R j to complete its task.
Need [ i , j ] = max [ i , j ] - allocation [ i , j ]
●
Safety Algorithm
Step 1 Let work and finish be vectors of length m and n, respectively.
Initialize
Work = Available
Finish [ i ] = False (for i = 1, 2, ..., n)
202 Operating System
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Deadlock Detection
Allow system to enter deadlock state and then there is
(i) Detection algorithm (ii) Recovery scheme
Single instance of each resource type
(i) Maintain wait for graph.
(a) Nodes are processes. (b) Pi ® Pj , if Pi is waiting for Pj .
(ii) Periodically invoke an algorithm that searches for a cycle in the
graph.
(iii) An algorithm to detect a cycle in a graph requires an order of n2
operations, where n is the number of vertices in the graph.
P5
R4
R1 R3 P5
P1 P2 P3 P1 P2 P3
R2 P4 R5 P4
Resource allocation graph Corresponding wait for graph
Recovery Scheme
●
Resource preemption
●
Process termination
(i) Abort all deadlock processes
(ii) Abort one process at a time until the deadlock cycle eliminated.
Memory Management
Memory management techniques allow several processes to share
memory. When several processes are in memory they can share the CPU,
thus increasing CPU utilisation.
Overlays
This techniques allow to keep in memory only those instructions and data,
which are required at given time.
The other instruction and data is loaded into the memory space occupied
by the previous ones when they are needed.
Swapping
Consider an environment which supports multiprogramming using say
Round Robin (RR) CPU scheduling algorithm. Then, when one process has
finished executing for one time quantum, it is swapped out of memory to a
backing store.
Operating
system
1 Swap out
Process
P1
2 Swap in
Process
User P2
space
Logical Physical
address address
CPU +
248 14248 Memory
Relocation
register
14000
Paging
It is a memory management technique, which allows the memory to be
allocated to the process wherever it is available. Physical memory is
divided into fixed size blocks called frames. Logical memory is broken into
blocks of same size called pages. The backing store is also divided into
same size blocks.
208 Operating System
When a process is to be executed its pages are loaded into available page
frames. A frame is a collection of contiguous pages. Every logical address
generated by the CPU is divided into two parts. The page number ( P ) and
the page offset (d ). The page number is used as an index into a page
table.
Logical address
CPU P d f d
Physical
address
P f
(Main memory)
(Page table)
A paging block diagram
Each entry in the page table contains the base address of the page in
physical memory (f). The base address of the Pth entery is then combined
with the offset (d) to give the actual address in memory.
Key Points
✦ The size of a page is typically a power of 2. The selection of a power of 2 as a
page size makes the translation of a logical address into a page number and
page off set, particularly easy.
✦ If the size of logical address space is 2m and a page size is 2n addressing units
(bytes or words), then the high order (m-n) bits of a logical address designates
the page number and the n low- order bits designates the page offset.
]
Advantages of Paging
The following are the advantages of paging
It allows the memory of a process to be non-contiguous. This also solves
the problem of fitting or storing memory blocks of varying sizes in
secondary memory (such as backing store).
Paging also allows sharing of common code. Two or more processes are
allowed to execute the same code at the same time.
Virtual Memory
Separation of user logical memory from physical memory. It is a technique to
run process size more than main memory. Virtual memory is a memory
management scheme which allows the execution of a partially loaded process.
Handbook Computer Science & IT 209
Segmentation
● Logical address is divided into blocks called segment i.e., logical address
space is a collection of segments. Each segment has a name and length.
● Logical address consists of two things
< segment_number, offset>
● Segmentation is a memory-management scheme that supports the
following user view of memory. All the location within a segment are
placed in contiguous location in primary storage.
Demand Paging
Virtual memory can be implemented by using either of the below.
(i) Demand paging (ii) Demand segmentation
Demand paging is combination of swapping and paging. With demand
paged virtual memory, pages are only loaded when they are demanded
during program execution. As long as we have no page faults, the effective
access time is equal to the memory access time. If however, a page fault
occurs, we must first read the relevant page from disk and then access the
desired word.
Page Replacement
In a multiprogramming environment, the following scenario often results.
While execution of a process, page fault occurs and there are no free
frames on the free frame list. This is called over allocation of memory and
results due to increase in the degree of multiprogramming. Page
replacement is a technique to solve this problem.
Key Points
✦ Paging and segmentation implement non-contiguous allocation of memory.
✦ Logical memory is divided into blocks called pages physical memory is
divided into fixed sized blocks known as frames.
Concept
If no free frame is available, a frame is found which is not in use. The
contents of the frame are written onto a backing store and the page tables
are modified to indicate that the page is no longer in memory. Thus, the
frame can be used to hold the page for which the page fault was
generated.
One bit is associated with each frame. If the bit is 1, this implies that the
contents of the page was modified this is called the dirty bit. If the dirty bit is
0, the content of the frame need not be written to the backing store.
Initially our 3 frames (frame size = 3) are empty. The first 3 references
(7, 0, 1) cause page faults and are brought into these empty frames. The
next reference (2) replaces page 7, because page 7 was brought in first.
Since, 0 is the next reference and 0 is already in memory, we have no fault
for this reference. The first reference to 3 results in replacement of page 0,
since it is now first in line. Because of this replacement, the next reference
to 0, will fault. Page 1 is then replaced by page 0. This process continues
as shown in below figure.
Page faults occurs ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
Reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 4 4 4 0 0 0 7 7 7
Page frames 0 0 0 3 3 3 2 2 2 1 1 1 0 0
1 1 1 0 0 0 3 3 3 2 2 2 1
Total number of page faults in the above example = 15
Key Points
✦ Beladys’ Anomaly For some page replacement algorithms, the page fault rate
may increase as the number of allocated frames increases.
✦ This phenomenon known as Belady’s anomaly.
212 Operating System
Frame Allocation
The maximum number of frames that can be allocated to a process
depend on the size of the physical memory (i . e., total number of frames
available). The minimum number of frames which can be allocated to a
process depend on the instruction set architecture.
Thrashing
●
A process is said to be thrashing when it is spending more time in paging
( i . e. , it is generating a lot of page faults) than executing.
●
Thrashing causes low CPU utilisation. The
Thrashing
CPU utilisation
File Attributes
Each file is referred to by its name. The file is named for the convenience of
the users and when a file is named, it becomes independent of the user
and the process. Below are file attributes
●
Name ●
Type
●
Location ●
Size
●
Protection ●
Time and date
Disk Scheduling
One of the responsibilities of the OS is to use the hardware efficiently. For
the disk drives, meeting this responsibility entails having fast access time
and large disk bandwidth.
Access time has two major components
●
Seek time is the time for the disk arm to move the heads to the cylinder
containing the desired sector.
214 Operating System
● The rotational latency is the additional time for the disk to rotate the
desired sector to the disk head. It is not fixed, so we can take average
value.
One complete revolution time
Rotational latency =
2
Disk bandwidth is the total number of bytes transferred, divided by the
total time between the first for service and the completion of last transfer.
File Operations
The OS provides system call to create, read , write, delete and truncate files.
The common file operations are as follows
■ File Creation For a file to be created firstly, a space must be found for a file, in the
file system and secondarily a new entry must be made in the directory for the new
file.
■ Writing a File In order to write a file, a system call is made, which specify the file
name and the information to be written in file.
■ Reading a File To read from a file, we use a system call that specifies the name of
the file and where in the memory. The next block of the file should be put and
again for the associated directory entry.
■ Repositioning within a File Repositioning within a file desn’t need to invoke any
actual I/O. This is also known as file seek.
■ File Deletion The directory for the named file is searched in order to delete a file
and when the associated directory entry is found, all the file space is released and
the directory entry is removed.
■ Truncating a File Truncating a file is used when the user wants the attributes of the
file to remain the same but wants to erase the contents of the file. There is no use of
deleting a file and recreating it rather this function allow all attributes to remain
unchanged but for the file to reset the length zero.
FCFS Scheduling
This is also known as First In First Out (FIFO) simply queues processes in
the order that they arrive in the ready queue.
The following features which FIFO scheduling have.
●
First come first served scheduling.
●
Processes request sequentially.
●
Fair to all processes, but it generally does not provide the fastest service.
●
Consider a disk queue with requests for I/O to blocks on cylinder i . e.,
Queue = 98, 183, 37, 122, 14, 124, 65, 67
Handbook Computer Science & IT 215
Head starts at 53
0 14 37 53 65 67 98 122 124 183 199
SCAN Scheduling
In the SCAN algorithm, the disk arm starts at one end of the disk and
moves toward the other end, servicing requests as it reaches each cylinder
until it gets to the other end of the disk. At the other end, the direction of
head movement is reversed and servicing continues. The head
continuously scans back and forth across the disk. The SCAN algorithm is
sometimes called the elevator algorithm, since the disk arm behaves just
like an elevator in a building, first servicing all the request going up and
then reversing to service requests the other way.
Queue = 98, 183, 37, 122, 14, 124, 65, 67
Head starts at 53.
0 14 37 53 65 67 98 122 124 183 199
C-SCAN Scheduling
Circular SCAN is a variant of SCAN, which is designed to provide a more
uniform wait time. Like SCAN, C-SCAN moves the head from one end of
the disk to the other, servicing requests along the way. When the head
reaches the other end, however it immediately returns to the beginning of
the disk without servicing any requests on the return trip. The C-SCAN
scheduling algorithm essentially treats the cylinders as a circular list that
wraps around from the final cylinder to the first one.
Queue = 98, 183, 37, 122, 14, 124, 65, 67
Handbook Computer Science & IT 217
Key Points
✦ Bootstrap For a computer to start running for instances when it is powered up
or rebooted, it needs to have an initial program to run. This initial program is
known as bootstrap program.
✦ Bootstrap program is stored in ROM or EEPROM which is known as a
firmware.
✦ System Calls System calls provide an interface to the service made available
by operating system.
✦ Single Processor System In a single processor system, there is one main CPU
capable of executing a general purpose instruction set, including instructions
from user processes.
✦ Multi Processor System It is alos known as a parallel system or tightly
coupled system.
✦ Cluster System In Cluster system, computers share common storage and are
closely linked via a LAN.