Os PPT-22
Os PPT-22
DEFINITION
ØDefinition of OS An operating system is a program that manages a computer’s
hardware. It also provides a basis for application programs and
ØComponents of Computer System acts as an intermediary between the computer user and the
ØMainframe Systems computer hardware. An amazing aspect of operating systems
is how they vary in accomplishing these tasks. Mainframe
ØComputer System operation operating systems are designed primarily to optimize
ØSingle and Multi processor System utilization of hardware. Personal computer (PC) operating
systems support complex games, business applications, and
ØComponents of Operating System everything in between. Operating systems for mobile
Computers provide an environment in which a user can easily
interface with the computer to execute programs. Thus, some
operating systems are designed to be convenient, others to be
1 efficient, and others to be some combination of the two. 2
Lecture 1 BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
5 6
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS MAINFRAME SYSTEMS
Mainframe computers are the computers which are used to The jobs were entered in the punched cards.
solve Commercial and Scientific applications. Growth of the The operating systems were simple and their tasks were to
computer systems ranges from batch systems to time –shared transfer control automatically fro job to another. They resided
systems. in the memory as
1.Batch Systems
Early computers were bigger machine with the input devices , Operating
card readers, tape drives and the common output devices System
such as line printers and card punches. The users could not
interact wit the computer system directly. Instead , the user User Program
prepared a job [ consisted of program, data and control area
information] and submitted to the computer for the execution .
MEMORY LAY OUT
7 8
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
To speed up the processing, the similar jobs were batched executed and were kept in the job pool. The operating system
together and were executed. In this type of systems, the CPU is picks and executed one of the jobs in the memory . When a
often idle because the I/O devices are slower than memory job waits for some task such waiting for an input, the operating
speed. system switches to other job. If this job needs some wait, the
2.Multi programmed Systems CPU is switched to another job, and so on. As long as one job
The most important aspect of job scheduling is the ability of needs to be executed, the CPU is never idle.
multi programming. Multi programming increases the CPU If many jobs are waiting for the execution and are ready to be
utilization by organizing the jobs in a such a way that CPU brought into memory for the execution, the operating system
executed them efficiently. chooses one among them. This decision is called as Job
In multi programmed systems, the operating system keeps Scheduling.
several jobs in the memory simultaneously as follow:
The jobs that are kept in the memory are the jobs to be
9 10
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
15 16
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
phones with connectivity to a network such as the internet. A modern general-purpose computer system consists of one
They have between 512 KB to 8 MB memories. Some hand held or more CPUs and a number of device controllers connected
systems use wireless technology such as Blue tooth allowing through a common bus that provides access to shared memory
remote access to e-mail and web browsing. (See the Figure ). Each device controller is in charge of a
specific type of device (for example, disk drives, audio
devices, or video displays). The CPU and the device
controllers can execute in parallel, competing for memory
cycles. To ensure orderly access to the shared memory, a
memory controller synchronizes access to the memory.
on-line
19 20
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
21 22
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
To accomplish this goal, the bootstrap program must locate may trigger an interrupt at any time by sending a signal to the
the operating-system kernel and load it into memory.Once the CPU, usually by way of the system bus. Software may trigger an
kernel is loaded and executing, it can start providing services interrupt by executing a special operation called a system
to the system and its users. Some services are provided call (also called a monitor call).
outside of the kernel, by system programs that are loaded into Storage Structure
memory at boot time to become system processes, or The CPU can load instructions only from memory, so any
system daemons that run the entire time the kernel is programs to run must be stored there. General-purpose
running. On UNIX, the first system process is “init,” and it starts computers run most of their programs from rewritable
many other daemons. Once this phase is complete, the system memory, called main memory (also called random-access
is fully booted, and the system waits for some event to occur. memory, or RAM). Main memory commonly is implemented
The occurrence of an event is usually signaled by an interrupt in a semiconductor technology called dynamic random-
from either the hardware or the software. Hardware access memory (DRAM).
23 24
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
Computer System operation Computer System operation
Computers use other forms of memory as well. We have load or store instructions to specific memory addresses. The
already mentioned read-only memory, ROM) and electrically load instruction moves a byte or word from main memory to an
erasable programmable read-only memory, EEPROM). internal register within the CPU, whereas the store instruction
Because ROM cannot be changed, only static programs, such moves the content of a register to main memory. Aside from
as the bootstrap program described earlier, are stored there. explicit loads and stores, the CPU automatically loads
The immutability of ROM is of use in game cartridges. instructions from main memory for execution.
EEPROM can be changed but cannot be changed frequently Thus, most computer systems provide secondary storage as
and so contains mostly static programs. For example, an extension of main memory. The main requirement for
smartphones have EEPROM to store their factory-installed secondary storage is that it be able to hold large quantities of
programs. data permanently.
All forms of memory provide an array of bytes. Each byte has The most common secondary-storage device is a magnetic
its own address. Interaction is achieved through a sequence of disk, which provides storage for both programs and data. Most
25 26
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
29 30
Lecture 1 BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
Computer System operation Single and Multi processor
Systems
Solid-state disks have several variants but in general are A computer system can be organized in a number of different
faster than magnetic disks and are nonvolatile. Another form of ways, which we can categorize roughly according to the
solid-state disk is flash memory, which is popular in cameras number of general-purpose processors used.
and personal digital assistants (PDAs), in robots, and Single-Processor Systems
increasingly for storage on general-purpose computers. Flash Until recently, most computer systems used a single processor.
memory is slower than DRAM but needs no power to retain its On a single- processor system, there is one main CPU capable
contents. Another form of nonvolatile storage is NVRAM, of executing a general-purpose instruction set, including
which is DRAM with battery backup power. This memory can instructions from user processes. Almost all single- processor
be as fast as DRAM and (as long as the battery lasts) is systems have other special-purpose processors as well. They
nonvolatile. may come in the form of device-specific processors, such as
disk, keyboard, and graphics controllers; or, on mainframes,
they may come in the form of more general-purpose
31 32
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
43 44
Lecture I BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM
Hardware
45 46
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
47 48
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM OPERATING SYSTEM
SERVICES SERVICES
• File-system manipulation. The file system is of particular • Communications. There are many circumstances in which
interest. Obviously, programs need to read and write files and one process needs to exchange information with another
directories. They also need to create and delete them by name, process. Such communication may occur between processes
search for a given file, and list file information. Finally, some that are executing on the same computer or between
operating systems include permissions management to allow processes that are executing on different computer systems
or deny access to files or directories based on file ownership. tied together by a computer network
Many operating systems provide a variety of file systems, Error detection. The operating system needs to be detecting
sometimes to allow personal choice and sometimes to provide and correcting errors constantly. Errors may occur in the CPU
specific features or performance characteristics. and memory hardware (such as a memory error or a power
failure), in I/O devices (such as a parity error on disk, a
connection failure on a network, or lack of paper in the
printer), and in the user program (such as an arithmetic
49 50
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
A system call is a method for programs to interact with the - Wait and assigned event
operating system. It provides an interface between a process - Allocates and De allocates
and the operating system to allow user – level processes to 2. In file management,
request services of the operating system. The system call performs file manipulations
Services Provided by the System calls Functions:
1. In process control, - Create a file
The system call performs the task of process creating, process - Open and Close file
termination, etc., - Read, Write and Reposition
Functions: - Delete a file
- End and Abort - Get and Set file attributes
- Load and Execute 3. In device management,
- Create and terminate process The system call does the jobs of device manipulation
53 54
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
SYSTEM CALLS SYSTEM CALLS
Functions: Functions:
- Request and Release device Create and delete communication
- Get and Set device attribute Sending and receiving message
4.In Information Maintenance Attach and detach remote services
The system call handles information and its transfer between The following are the example for System calls in Windows
the user and the OS and Unix OS
Functions: Windows Unix
Get the time or setting the time Process CreateProcess() fork()
Get the process and device attributes Control ExitProcess() exit()
5. Communication File CreateFile() open()
Manipulation ReadFile() read()
The system call is used for inter-process communication
WriteFile() write()
55 56
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
57 58
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
77 78
Lecture - III BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
PROCESS CONCEPT PROCESS CONCEPT
Processes executing concurrently in the operating system may 2. Modularity – The complex problem can broken down into
be either independent processes or coordinating processes. several tasks. Each task is the process. The process of breaking
Independent Processes – They can not affect or be affected the system into multiple modules is called as Modularity.
by other processes executing in the system There are various advantages of modularity. Some of them are
Coordinating Processes – They can affect or can affected by - Debugging is easy
other processes executing in the system. - Work load can be shared
Any process that shares data with other processes is a - Reducing the cost
coordinating process 3. Computation Speed – Concurrent execution of multiple
There are several reasons for providing an environment that parts of a program reduces the time and computation speed is
allows process coordination maximized.
1. Information Sharing – Several processes want to share the 4. Convenience – If we allow the processes to cooperate with
information each other , it will be convenient to the user to multi tasking
79 80
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
Kernel M
83 84
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
THREAD THREAD
A thread is a basic unit of CPU utilization; it comprises a thread
ID, a program counter, a register set, and a stack. It shares with Code Data Files Code Data Files
other threads belonging to the same process its code section,
data section, and other operating-system resources, such as registers Stack registers registers registers
open files and signals. A traditional (or heavyweight) process
has a single thread of control. If a process has multiple threads Stack Stack
Stack
of control, it can perform more than one task at a time.
The following figure illustrates the difference between a Thread
traditional single- threaded process and a multithreaded Thread
process.
Most software applications that run on modern computers are
multithreaded. An application typically is implemented as a
separate process with several threads of control. 85 86
Lecture - IV BITS Pilani, Pilani Campus Single thread process Multiple
Lecture - IV thread process
BITS Pilani, Pilani Campus
THREAD THREAD
A web browser might have one thread display images or text its request to be serviced.
while another thread retrieves data from the network, for One solution is to have the server run as a single process that
example. A word processor may have a thread for displaying accepts requests. When the server receives a request, it
graphics, another thread for responding to keystrokes from the creates a separate process to service that request. In fact, this
user, and a third thread for performing spelling and grammar process-creation method was in common use before threads
checking in the background. In certain situations, a single became popular. Process creation is time consuming and
application may be required to perform several similar tasks. resource intensive, however. If the new process will perform
For example, a web server accepts client requests for web the same tasks as the existing process, why incur all that
pages, images, sound, and so forth. A busy web server may overhead? It is generally more efficient to use one process that
have several (perhaps thousands of) clients concurrently contains multiple threads. If the web-server process is
accessing it. If the web server ran as a traditional single- multithreaded, the server will create a separate thread that
threaded process, it would be able to service only one client at listens for client requests. When a request is made, rather than
a time, and a client might have to wait a very long time for 87 creating another process, the server creates a new thread to 88
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
THREAD THREAD
service the request and resume listening for additional Benefits
requests. It is illustrated in the following diagram: The benefits of multithreaded programming can be broken
down into four major categories:
(1) request (2) create a new thread Responsiveness. Multithreading an interactive application
to service the request
may allow a program to continue running even if part of it is
blocked or is performing a lengthy operation, thereby
Client Server thread
increasing responsiveness to the user. This quality is
especially useful in designing user interfaces. For instance,
consider what happens when a user clicks a button that results
(3) resume listening for
additional requests in the performance of a time-consuming operation. A single-
threaded application would be unresponsive to the user until
the operation had completed. In contrast, if the time-
Multithreaded server architecture 89 consuming operation is performed in a separate thread, the 90
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
THREAD THREAD
application remains responsive to the user. difference in overhead can be difficult, but in general it is
Resource sharing. Processes can only share resources significantly more time consuming to create and manage
through techniques such as shared memory and message processes than threads. In Solaris, for example, creating a
passing. Such techniques must be explicitly arranged by the process is about thirty times slower than is creating a thread,
programmer. However, threads share the memory and the and context switching is about five times slower.
resources of the process to which they belong by default. The Scalability. The benefits of multithreading can be even
benefit of sharing code and data is that it allows an application greater in a multiprocessor architecture, where threads may be
to have several different threads of activity within the same running in parallel on different processing cores. A single-
address space. threaded process can run on only one processor, regardless
Economy. Allocating memory and resources for process how many are available. We explore this issue further in the
creation is costly. Because threads share the resources of the following section.
process to which they belong, it is more economical to create
and context-switch threads. Empirically gauging the 91 92
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
THREAD THREAD
Multithreading Models Many – to – One Model
Support for threads may be provided either at the user level, The many-to-one model (See the figure ) maps many user-
for user threads, or by the kernel, for kernel threads. User level threads to one kernel thread. Thread management is
threads are supported above the kernel and are managed done by the thread library in user space, so it is efficient.
without kernel support, whereas kernel threads are supported However, the entire process will block if a thread makes a
and managed directly by the operating system. Virtually all blocking system call.
contemporary operating systems— including Windows, Linux, Also, because only one thread can access the kernel at a time,
Mac OS X, and Solaris— support kernel threads. multiple threads are unable to run in parallel on multicore
Ultimately, a relationship must exist between user threads and systems. Green threads — a thread library available for
kernel threads. In this section, we look at three common ways Solaris systems and adopted in early versions of Java— used
of establishing such a relationship: the many-to-one model, the the many-to-one model. However, very few systems continue
one-to-one model, and the many-to- many model. to use the model because of its inability to take advantage of
93 multiple processing cores. 94
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
K K K Kernel
Thread
99 100
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
Learning Objectives
Ø CPU Scheduling
- What is Process Scheduling?
Ø Types of Scheduling algorithms
Ø Various Methods of Process Scheduling
- First Come First Served
- Shortest Job Next
- Priority Scheduling
- Round Robin Scheduling
- Multiple Queue Scheduling
102
BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
THREAD LIBARARIES THREAD LIBARARIES
Thread libraries are the APIs to be used for creating , int sum=0;
executing and terminating the threads. void *run(void *p);
There are two types of threads. They are int main(int ac,char *argv[])
{
1. PTHREAD (POSIX)
pthread_t tid;
2. Java Threads
pthread_attr_t attr;
PTHREAD
pthread_attr_init(&attr);
It is an API to create and run a thread. The API is written in C pthread_create(&tid,&attr,run,argv[1]);
language. pthread_join(tid,NULL);
The following is the code written for finding the sum of first ‘n’
printf("\n Summation is %d",sum);
numbers.
return 0;
#include <stdio.h>
}
#include <pthread.h>
#include <stdlib.h>
103 104
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
105 106
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
107 108
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
PROCESS SHEDULING PROCESS SHEDULING
Arrival Time(AT): when a process enters in a ready state Maximizing:
Completion Time (CT): It is the finishing or completion time of the CPU utilization: CPU utilization is the main task in which the
process in the system operating system needs to make sure that CPU remains as busy as
Turnaround Time (TA): It is the time difference between possible. It can range from 0 to 100 percent. However, for the RTOS, it
Completion Time and Arrival Time can be range from 40 percent for low-level and 90 percent for the
TA = CT - AT high-level system.
Waiting Time (WT): It is the time difference between Turnaround Throughput: The number of processes that finish their execution per
Time and Burst Time unit time is known Throughput. So, when the CPU is busy executing
WT = TA - BT the process, at that time, work is being done, and the work
CPU Scheduling Criteria completed per unit time is called Throughput.
A CPU scheduling algorithm tries to maximize and minimize the Minimizing:
following: Waiting time: Waiting time is an amount that specific process needs
to wait in the ready queue.
109 110
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
113 114
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
PROCESS SHEDULING PROCESS SHEDULING
Process Arrival Burst Solution: Process Arrival Burst Completion Turnaround Waiting
Time(AT) Time(BT) The Gantt Chart for the Jobs is Time(AT) Time(BT) Time(CT) Time(TA) Time(WA)
P0 0 5 P0 P1 P3 P2 P0 0 5 5 5 0
P1 1 3 0 5 8 14 22 P1 1 3 8 7 4
P2 2 8 P2 2 8 22 20 12
P3 3 6 P3 3 6 14 11 5
Note:- Important Point is that though P1 has shortest time than P0, P0 arrives Average Waiting Time = ( 0 + 4 + 12 + 5 ) / 4 = 21 / 4 = 5.25
at time 0 (When the entire system begins) and P1 comes after 1 unit of time
115 116
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
P0 P3 P1 P2
0 5 11 14 22
117 118
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
119 120
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
PROCESS SHEDULING PROCESS SHEDULING
• Once a process is executed for a given time period, it is preempted
and other process executes for a given time period. Process Arrival Burst Completion Waiting
• Context switching is used to save states of preempted processes. Time(AT) Time(BT) Time(CT) Time
The problem is solved with the quantum time 3 as follows:
Process Arrival Burst P0 0 5 14 0+(12-3) = 9
Time(AT) Time(BT)
P1 1 3 6 (3 - 1) = 2
P0 0 5
P1 1 3
P2 2 8 22 (6 – 2) + ( 14 – 9 ) +
P2 2 8 Solution: The Gantt Chart is (20 – 17 ) = 12
P3 3 6 drawn as follows:
P3 3 6 20 (9 – 3 ) + (17 - 12) = 11
P0 P1 P2 P3 P0 P2 P3 P2
Average Waiting Time = ( 9 +2 + 12 + 11 ) / 4 = 34 / 4 = 8.5
0 3 6 9 12 14 17 20 22
121 122
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
123 124
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
MULTIPLE-LEVEL QUEUES
Learning Objectives
SCHEDULING
Ø Multiple Queue Scheduling Advantages of Multiple Level Queues Scheduling:
• Multilevel Queue Scheduling has some advantages like:
- Advantages and Disadvantages • Multilevel queue scheduling helps us apply different
- Worked Example scheduling algorithms for different processes.
Ø Multiple Processor Scheduling • It will have a low scheduling overhead.
Disadvantages:
Ø Algorithm evaluation • Some of the disadvantages of Multilevel queue scheduling
- Deterministic Modeling are as follows:
- Queuing Modeling • There are chances of starving for the lower priority
processes.
- Simulations • It is inflexible in nature.
Ø Thread Scheduling
125 126
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE-LEVEL QUEUES MULTIPLE-LEVEL QUEUES
SCHEDULING SCHEDULING
Example of Multilevel queue scheduling: Priority of queue 1 is greater than queue 2. queue 1 uses
Consider below table of four processes under Multilevel Round Robin (Time Quantum = 2) and queue 2 uses FCFS.
queue scheduling. Queue number denotes the queue of the Below is the Gantt chart of the problem :
process.
P1 P2 P1 P2 P3 P4 P5
Process Arrival Time CPU Burst Time Queue Number
P1 0 4 1 0 2 4 6 7 10 15 20
P2 0 3 1 At starting both queues have process so process in queue 1
P3 0 8 2 (P1, P2) runs first (because of higher priority) in the round
robin fashion and completes after 7 units then process in
P4 10 5 queue 2 (P3) starts running (as there is no process in queue 1)
1
but while it is running P4 comes in queue 1 and interrupts P3
127 128
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE PROCESSOR
ALGORITHM EVALUATION
SCHEDULING
user code. This is simple and reduces the need of data sharing. This Selecting an algorithm from many scheduling algorithms is a
entire scenario is called Asymmetric Multiprocessing. difficult process. Defining the criteria to select the algorithm is
A second approach uses Symmetric Multiprocessing where each the first problem. Normally, criteria are often defined in terms
processor is self scheduling. All processes may be in a common
of CPU utilization, response time and throughput. The criteria
ready queue or each processor may have its own private queue for
may include the several measures such as
ready processes. The scheduling proceeds further by having the
• Maximize CPU utilization under the constraint that the
scheduler for each processor examine the ready queue and select a
maximum response time is 1 second.
process to execute.
• Maximize throughput such that turnaround time is ( on
average ) linearly proportional to total execution time
Once the selection criteria is defined, we want to evaluate the
various algorithms under consideration.
There are three types of algorithm evaluation methods. They
are
131 132
Lecture - VI BITS Pilani, Pilani Campus : Lecture - VI BITS Pilani, Pilani Campus
ALGORITHM EVALUATION ALGORITHM EVALUATION
1. Deterministic Modeling Process Burst Time
2. Queuing Models P1 10
3. Simulations P2 29
Deterministic Modeling
P3 3
One major class of evaluation methods is called analytic evaluation.
P4 7
Analytic evaluation uses the given algorithm and the system
P5 12
workload to produce a formula or number that evaluates the
performance of the algorithm for that workload. Calculate the average waiting time for each method.
One type of analytic evaluation is deterministic modelling. For FCFS,
For example, assume that we have the workload for 5 processes is Average Waiting time = ( 0 + 10 + 39 + 42 + 49 ) / 5 = 28
given below milliseconds
For SJF,
Average Waiting time = ( 10 + 32 + 0 + 3 + 20 ) / 5 = 13
133
milliseconds 134
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus
Thread priorities are integers ranging between MIN_PRIORITY • It yields, or its run method exits.
And MAX_PRIORITY (Constants defined in the Thread Class). • On systems that support time-slicing, its time allotment has
The higher the integer, the higher the priority. At any given expired.
time, when multiple threads are ready to be executed, the Then the second thread is given a chance to run, and so on,
runtime system chooses for execution the Runnable thread that until the interpreter exits.
has the highest priority. Only when that thread stops, yields, or The Java runtime system's thread scheduling algorithm is also
becomes Not Runnable will a lower-priority thread start preemptive. If at any time a thread with a higher priority than
executing. If two threads of the same priority are waiting for all other Runnable threads becomes Runnable, the runtime
the CPU, the scheduler arbitrarily chooses one of them to run. system chooses the new higher-priority thread for execution.
The chosen thread runs until one of the following conditions is The new thread is said to preempt the other threads.
true: http://www.btechsmartclass.com/java/java-threads-
• A higher priority thread becomes runnable. priority.html
139 140
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus
to avoid extended priority inversion problems. Not more than Solutions To The Critical Section
one process can execute in its critical section at one time. In Process Synchronization, critical section plays the main role
(ii) Progress: This solution is used when no one is in the so that the problem must be solved.
Critical section, and someone wants in. Then those processes Here are some widely used methods to solve the critical
not in their reminder section should decide who should go in, section problem.
in a finite time. Solution 1:-
(iii) Bound Waiting: When a process makes a request for We declare an array : boolean flag[2];
getting into critical section, there is a specific limit about If Flag[i] is true then
number of processes can get into their critical section. So, Pi is ready to enter into the critical region.
when the limit is reached, the system must allow request to the The structure is :
process to get into its critical section. do
{
147 Flag[i]=true; 148
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus
P1 P2 P3 …… Pn • Assume there are N processes (P1, P2, ... PN) and every
Flag[1]=False Flag[2]=False Flag[3]=False … Flag[n]=False process at some point of time requires to enter the Critical
Do Section
{ • A FLAG[] array of size N is maintained which is by default
false. So, whenever a process requires to enter the critical
Flag[j]=true; section, it has to set its flag as true. For example, If Pi wants
turn=j; to enter it will set FLAG[i]=TRUE.
while (Flag[j] && turn == j) ; • Another variable called TURN indicates the process number
Critical Section which is currently wafting to enter into the CS.
Flag[i]=False; • The process which enters into the critical section while
Remainder Section exiting would change the TURN to another number from the
} While (1); list of ready processes.
Example: turn is 2 then P2 enters the Critical section and while
exiting turn=3 and therefore P3 breaks out of wait loop.
151 152
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus
way. It ensures that only one process is permitted to enter into Repeat
the critical section. To do this, semaphore uses two atomic Wait(mutex) Entry section
functions :wait ( ) and signal(). Critical Section
The code for the functions are: Signal(mutex) Exit section
Wait(S) : while S ≤ 0 do no – operation [ Not permitted inside Remainder section
the critical section] Until (True);
S=S-1; It specifies that any process is allowed to enter into critical
Signal(S) : S=S+1; section if mutex is 1. Before executing any process, assume
It uses two atomic operations, 1)wait, and 2) signal for the that mutex=1
process synchronization. When P1 wants to enter into the critical section but P0 is
Let mutex is the short form of mutual exclusion variable. Then already in the critical section, the value of mutex is 0. So it can
the critical section code can be written as not enter into the critical section. So it has to wait till mutex is
155 changed to 1. Mutex is changed to 1 only when the running 156
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION Learning Objectives
process P0 has exited the critical section and execute the Ø Classical problem 1- Bounded Buffer problem / Producer
Signal() function to make the value 0 mutex as 1 [Signal and Consumer problem
function increments the value of mutex by 1]. - Definition
CLASSICAL SYNCHRONIZATION PROBLEMS - How the problem is synchronized with Semaphore?
The following are the classical examples of for - Structures of Producer and Consumer
synchronization problems. Ø Classical problem 2 – Dining and Philosopher problem
1. Producer and Consumer problem
- Definition
2. Reader and Writer Problem
- Solving the problem with Semaphore
3. Diner – Philosopher Problem
Ø What are monitors?
Ø Solving Dining and Philosopher problem using monitors
157 158
Lecture - VII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION-
PROCESS SYNCHRONIZATION
Bounded Buffer Problem
- This problem is also called as producer – consumer problem Solution to the problem is ,
- It is problem based on synchronization - Create two counting semaphores “Full” and “Empty” to track
- There are two enteritis such as Producer and Consumer whether the buffer/ container is full or empty
- Producer produces a product and it should be placed on the - When the container is full, the producer can not place the product
container [buffer] in it
- Consumer consumes the product that is available in the container - When the container is empty, the consumer can not take any
- The container should be used by only one of them , either the product to consume
producer or the consumer The procedure for the problem is written as follows:
1 2 3 4 ……… N Structure of Producer
While (True)
Product Consumer {
wait(empty);
wait(mutex);
Add Item into the buffer
159 160
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
161 162
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION
while (true ) // Initially true Now if Consumer wants to consume the product, the code is
waif(empty) -> wait(N); Here N is not <=0, so permits the executed as follows:
operation, N = N - 1 while(true) – Condition is true
wait(full) -> wait(1) : Here 1 is not <=0, so permits the
wait(mutext) -> wait(1): Here 1 is not <=0, so permits the operation, full= full-1 =0
operation, mutex = mutex-1 =0 wait(mutex) -> wait(1) : Here 1 is not <=0, so permits
Add Item to the Buffer / Container the operation, mutex= mutex-1 =0
signal(mutex) -> Increments mutex by 1. Now mutex = 1 Removes the Product from the buffer
signal(full) -> Increments full by 1. Full = 1 Signal(mutex) -> mutext = mutex + 1 = 1
Now the Buffer has a single product as Signal(empty) -> empty = empty + 1 = N – 1 + 1= N
P Empty = n - 1
Full = 1
163 164
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION –
PROCESS SYNCHRONIZATION
Dining Philosopher Problem
What is Dining Philosopher Problem? Semaphore. The structure of the chopstick is
It states that there are 5 philosophers (may be more than 5 also) semaphore Chopstick [5];
sharing a circular table and they eat and think alternatively. Thre is a Initially the elements of the chopstick are initialized to as they are on
bowl of rice for each of the philosophers and 5 chopsticks. A the table and picked by any philosopher.
philosopher needs both their right and left chopstick to eat. The Structure of random philosopher i to eat is given as follows:
A hungry philosopher only eat if there are both the chopsticks While (True)
available. Otherwise the philosopher puts down their chopstick and {
begin thinking again. wait(Chopstick[i] );
It is a classical synchronization problem as it demonstrates a large wait(Chopstick[ (i+1) % 5 );
class of concurrency control problem. Eating Rice ;
Solution: Signal(Chopstick(i]);
The Solution for this problem is to use a semaphore to represent a Signal(Chopstick[( i + 1 ) % 5 );
chopstick. A chopstick can be picked up by executing a wait }
operation on the semaphore and released by executing a signal
165 166
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK
What is Deadlock? able to complete their execution. As the processes are blocked,
A deadlock is a common situation in operating systems where a the system may become unresponsive.
process waiting for a resource can be executed because that So deadlock is the situation in which multiple processes are
resource is currently held by another process and is being waiting infinitely in which there is no progress for waiting
utilised for its execution, therefore, the process does not get processes.
executed. Moreover, many other processes may also be waiting
for a resource to be released. A deadlock situation is created
Let us assume that there are two processes called P1 and P2.
Resources R1 and R2 are assigned to them respectively. So if
P1 needs R2 to complete its execution, but it is held by P2, P1
has to wait. Similarly, P2 may be waiting for R1 that is held by
P1. Thus, P2 has to wait until P1 has released the resource. As
a result, a deadlock situation arises and both processes are not 177 178
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK
Example of Deadlock
• A real-world example would be traffic, which is going only in
one direction.
• Here, a bridge is considered a resource.
• So, when Deadlock happens, it can be easily resolved if one
car backs up (Preempt resources and rollback).
• Several cars may have to be backed up if a deadlock situation
occurs.
• So starvation is possible.
179 180
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK
Necessary conditions for deadlock 4. Circular wait
1. Mutual exclusion If the processes are waiting in the circular fashion such as the
The processes are permitted to use the shared resource in following diagram, deadlock may occur.
mutual exclusive way. Only one process is permitted to use a
shared resource. When a resource is used by another process, a
new process is permitted to use it. If mutual exclusion is not
followed, there is a possibility of deadlock
2. Hold and Wait condition
The process which asks for a resource is holding a resource
already. If so, a deadlock may occur
3. Pre emption
When shared resource can be pre-empted, there is a possibility of
Deadlock. Pre emption allocates the resource held by a process to
another process.
181 182
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK
If all the above conditions are satisfied, deadlock may occur. Deadlock Prevention procedure removes hold and wait condition
Deadlock Handling Mechanism by not permitting any process to waits for any resource if it holds
Deadlock detection, deadlock prevention and deadlock another process already. It is done in any of the following
avoidance are the main methods for handling deadlocks. procedure:
– Deadlock Prevention • A process must acquire all the necessary resources before
– Deadlock avoidance entire system starts. It reduces the utilization of the resources
– Deadlock Detection & Recovery • A process that holds some resources requests for other
Deadlock Prevention resources is asked to release the acquired resource first.
Starvation is the problem of this method
In this method, we have to include the preventive mechanism to
• Apply wait time out procedure. In this procedure, a process
stop the deadlock to occur. In this procedure, any one of the that holds some resources is asked to release the acquired
necessary condition mentioned above should be made as False. resource for a particular period of time though the resources
Deadlock prevention procedure removes mutual exclusion by may be requested or not requested by another process.
making all the resources sharable 183 184
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK
to release Memory and then it can hold CPU. The deadlock Avoidance method is used by the operating
DEADLOCK AVOIDANCE system in order to check whether the system is in a safe state or
In deadlock avoidance, the resource for any process is granted if in an unsafe state and in order to avoid the deadlocks, the
the resulting state of the system does not cause any deadlock in process must need to tell the operating system about the
the system. The state of the system is continuously checked for maximum number of resources a process can request in order to
Safe and Unsafe states. complete its execution.
In order to avoid deadlock, the process must tell OS , the How does Deadlock Avoidance work?
maximum number of resources a process can request to In this method, the request for any resource will be granted only
complete its execution. The deadlock avoidance algorithm if the resulting state of the system doesn't cause any deadlock in
examines the resource allocations so that there can never be a the system. This method checks every step performed by the
circular wait condition operating system. Any process continues its execution until the
system is in a safe state. Once the system enters into an unsafe
187 state, the operating system has to take a step back. 188
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK
With the help of a deadlock-avoidance algorithm, you can deadlocked state and conversely a deadlocked state is an
dynamically assess the resource-allocation state so that there unsafe state.
can never be a circular-wait situation. In an Unsafe state, the operating system cannot prevent
According to the simplest and useful approach, any process processes from requesting resources in such a way that any
should declare the maximum number of resources of each type it deadlock occurs. It is not necessary that all unsafe states are
will need. The algorithms of deadlock avoidance mainly examine deadlocks; an unsafe state may lead to a deadlock
the resource allocations so that there can never be an
occurrence of circular wait conditions.
Safe State and Unsafe State
A state is safe if the system can allocate resources to each
process( up to its maximum requirement) in some order and still
avoid a deadlock. Formally, a system is in a safe state only, if The above Figure shows the Safe, unsafe, and deadlocked state
there exists a safe sequence. So a safe state is not a 189
spaces 190
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK
Deadlock Avoidance Example So at time t0, the system is in a safe state. The sequence is
Let us consider a system having 12 magnetic tapes and three <P2,P1,P3> satisfies the safety condition. Process P2 can
processes P1, P2, P3. Process P1 requires 10 magnetic tapes, immediately be allocated all its tape drives and then return them.
process P2 may need as many as 4 tapes, process P3 may After the return the system will have 5 available tapes, then
need up to 9 tapes. Suppose at a time to, process P1 is holding process P1 can get all its tapes and return them ( the system will
5 tapes, process P2 is holding 2 tapes and process P3 is then have 10 tapes); finally, process P3 can get all its tapes and
holding 2 tapes. (There are 3 free magnetic tapes) return them (The system will then have 12 available tapes).
Processes Maximum Needs Current Needs
A system can go from a safe state to an unsafe state. Suppose
at time t1, process P3 requests and is allocated one more tape.
P1 10 5
The system is no longer in a safe state. At this point, only
P2 4 2 process P2 can be allocated all its tapes. When it returns them
P3 9 2 the system will then have only 4 available tapes. Since P1 is
191 allocated five tapes but has a maximum of ten so it may request 192
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK
5 more tapes. If it does so, it will have to wait because they are Deadlock Avoidance Solution
unavailable. Similarly, process P3 may request its additional 6 Deadlock Avoidance can be solved by two different algorithms:
tapes and have to wait which then results in a deadlock. 1. Resource allocation Graph
The mistake was granting the request from P3 for one more 2. Banker's Algorithm
tape. If we made P3 wait until either of the other processes had
finished and released its resources, then we could have avoided
the deadlock
Note: In a case, if the system is unable to fulfill the request of all
processes then the state of the system is called unsafe.
The main key of the deadlock avoidance method is whenever
the request is made for resources then the request must only be
approved only in the case if the resulting state is a safe state.
193 194
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
Ø Dead lock avoidance In deadlock avoidance, the resource for any process is granted if
the resulting state of the system does not cause any deadlock in
- Safe and Unsafe states
the system. The state of the system is continuously checked for
- Resource allocation graph
Safe and Unsafe states.
- Banker’s Algorithm
In order to avoid deadlock, the process must tell OS , the
ØDeadlock Detection and Recovery maximum number of resources a process can request to
- Detection complete its execution. The deadlock avoidance algorithm
(i) Wait for Graph examines the resource allocations so that there can never be a
(ii) Banker’s Algorithm Circular wait condition
- Recovery How does Deadlock avoidance work?
(i) Process Termination Let's understand the working of Deadlock Avoidance with the
(ii) Resource Termination/ Pre-emption help of an intuitive example.
195 196
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
Process Maximum Required current Available • P2 process needs a maximum of 5 resources and is currently
allocated with 2 resources. So it needs 3 more resources to
P1 9 5 complete its execution.
• P3 process needs a maximum of 3 resources and is currently
P2 5 2
allocated with 1 resource. So it needs 2 more resources to
P3 3 1
complete its execution.
Operating System knows that only 2 resources out of the total
Let us assume that there are three processes P1, P2, P3. Some available resources are currently free.
more information on which the processes tells the Operating But only 2 resources are free now. Can P1, P2, and P3 satisfy their
System are : requirements? Let's try to find out.
• P1 process needs a maximum of 9 resources (Resources like As only 2 resources are free for now, then only P3 can satisfy its
tape drive or printer etc..) to complete its execution. P1 is need for 2 resources. If P3 takes 2 resources and completes its
currently allocated with 5 Resources and needs 4 more to execution, then P3 can release its 3 (1+2) resources. Now the
complete its execution.
197 three free resources which P3 released can satisfy the need 198
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE DEADLOCK AVOIDANCE
of P2. Now, P2 after taking the three free resources, can Safe State and Unsafe State
complete its execution and then release 5 (2+3) resources. Now Safe State - In the above example, we saw that Operating
five resources are free. P1 can now take 4 out of the 5 free System was able to satisfy the need of all three
resources and complete its execution. So, with 2 free resources processes, P1, P2, and P3, with their resource requirements. So
available initially, all the processes were able to complete their all the processes were able to complete their execution in a
execution leading to Safe State. The order of execution of the certain order like P3->P2->P1.
processes was <P3, P2, P1>. So, If Operating System is able to allocate or satisfy the
What if initially there was only 1 free resource available? None of maximum resource requirements of all the processes in any
the processes would be able to complete its execution. Thus order then the system is said to be in Safe State.
leading to an unsafe state. So safe state does not lead to Deadlock.
We use two words, safe and unsafe states. What are those Unsafe State - If Operating System is not able to prevent
states? Let's understand these concepts. Processes from requesting resources which can also lead to
199 Deadlock, then the System is said to be in an Unsafe State. 200
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
Unsafe State does not necessarily cause deadlock it may or in the System just by observing the Graph, which can not be
may not causes deadlock. done easily by using tables that we use in Banker's algorithm.
Deadlock Avoidance Solution Resource Allocation Graph has a process vertex represented by
Deadlock Avoidance can be solved by two different algorithms: a circle and a resource vertex represented by a box. The
• Resource allocation Graph instance of the resources is represented by a dot inside the box.
• Banker's Algorithm The instance can be single or multiple instances of the resource.
We will discuss both algorithms in detail in their separate article. An example of RAG is shown below.
Resource Allocation Graph Banker's Algorithm
Resource Allocation Graph (RAG) is used to represent the state Banker's algorithm does the same as we explained the Deadlock
of the System in the form of a Graph. The Graph contains all avoidance with the help of an example. The algorithm
processes and resources which are allocated to them and also predetermines whether the System will be in a safe state or not
the requesting resources of every Process. Sometimes if the by simulating the allocation of the resources to the processes
number of processes is less, We can easily identify a deadlock 201 according to the maximum available resources. 202
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
It makes an "s-state" check before actually allocating the A process must specify in the beginning the maximum number of
resources to the Processes. instances of each resource type it may require. It is obvious that
When there are more number of Processes and many this number should not be more than the available. When the
Resources, then Banker's Algorithm is useful. process requests resources, the system decides whether
It is used to avoid deadlocks when multiple instances of each allocation will result in deadlock or not. If not, resources are
resource type are present. This is not possible, using the allocated otherwise process has to wait.
methods like safe state and resource allocation graphs. It is The following are the various data structures that have to be
similar to a banking system where a bank never allocates cash created to implement Banker's algorithm. If 'n' is the number of
in such a way that it could not satisfy the needs of all its processes and 'm' is the number of resources.
customers and also it cannot allocate more than what is Max : A 'n × m' matrix indicating the maximum resources
available. Here, customers are analogous to processes, cash to required by each process.
resources, and bank to the operating system. Allocation : A 'n × m' matrix indicating the number of resources
already allocated to each process.
203 204
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE DEADLOCK AVOIDANCE
Need : A 'n × m' matrix indicates the number of resources Step 2 - If Requesti ≤ Available, then proceed to step three,
required by each process. otherwise block Pi because resources are not available.
Available : It is a vector of size 'm' which indicates the resources Step 3 - Allocate resources to Pi as follows,
that are still available (not allocated to any process). Available = Available - Requesti
Request : It is a vector of size 'm' which indicates that process Allocationi = Allocationi + Requesti
Pi has requested some resources. Needi = Needi - Requesti
Each row of matrices "allocation" and "need" can be referred to Safety Algorithm :
as vectors. Then "allocation" indicates the resources currently The job of the banker's algorithm is to perform allocation, it will
allocated to process Pi and "need" refers to resources required not see whether this allocation has resulted in a safe or unsafe
by Pi. The following algorithm is used to determine whether the state. It is the safety algorithm that is called immediately after the
request can be safely granted or not. banker's algorithm to check for the system state after allocation.
Step 1 - If Requesti ≤ Needi, then proceed to step two, otherwise The following is the safety algorithm that requires m x
raise an exception saying the process has exceeded its
maximum claim. 205
n2 operations to find the system state. 206
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
Step 1 - Assume work and finish as vectors of length 'm' and 'n' Example of Banker’s Algorithm
respectively. Consider the following problem:
Work = Available
Allocation Max Available
Finish[i] = 'false' Processes
A B C A B C A B C
Needi ≤ Work
P1 2 1 2 3 2 2
If no such 'i' is found jump to step four.
Step 3 - Work = Work + Allocation P2 4 0 1 9 0 2
Finish[i] = 'true'
Jump to step two. P3 0 2 0 7 5 3
221 222
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
Learning Objectives MEMORY MANAGEMENT
Ø Swapping SWAPPING
We know that all the processes are executed in the memory. A
Ø Contiguous Memory allocation
process can be swapped temporarily out of memory to a backing
- Fixed Size partition
- Variable size partition
store and then brought back into memory for continued
- Block allocation list execution.
- Bit Map For example, assume that a multiprogramming environment with
a round-robin CPU algorithm. When a quantum expires, the
Ø Non Contiguous Memory allocation
- Segmentation
memory manager will swap out the process that has just
- Paging reached the quantum time and to swap in another process to the
- Segmentation with Paging memory. Like this, when each process finishes its quantum time,
it will be swapped with another process. The quantum time must
be sufficiently large that reasonable amounts of computing are
223 done between swaps. 224
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
It is depicted in the following diagram: Backing Store When we use priority-based scheduling algorithm, another
swapping policy is used. If a higher priority process arrives and
wants to execute, the memory manager swaps out the lower
Operating priority process so that higher priority process is executed. Once
System Process the higher priority process finishes its execution, the lower
1. Swapped out P1 priority process is swapped back in and continued. This variant
swapping is sometimes is called as Roll out, Roll in.
2. Swapped In Process Normally a process that is swapped out will be swapped back
P2 into the same memory space that was occupied previously. This
restricted is dictated by the method of address binding.
User Space Swapping requires backing store. It is commonly a fast disk. The
disk must have enough space to accommodate swapped out
225 process images for all the users. There are two alternatives to 226
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
keep the swapped out process images on disk. CONTIGUOUS MEMORY ALLOCATION
The first alternative is to create a separate swap file for each The main memory has to accommodate both the operating
swapped out process. But this method increases the number of system and user space. Now, here the user space has to
files and directory entries. It creates increased overhead that accommodate various user processes. We also want these
requires high search time. several user processes must reside in the main memory at the
The second alternative is to create a common swap file that is same time.
kept in the disk and the location of each swapped out process Contiguous memory allocation is a memory allocation method
image has to be noted in the common swap disk. that allocates a single contiguous section of memory to a
The major part of the swap time is transfer time. The total process or a file.
transfer time is directly proportional to the amount of memory In Contiguous memory allocation, when the process arrives from
swapped. the ready queue to the main memory for execution, the
contiguous memory blocks are allocated to the process
227 according to its requirement. Now, to allocate the contiguous 228
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT MEMORY MANAGEMENT
space to user processes, the memory can be divided either in Fixed-Sized Partition: In the fixed-sized partition, the memory is
the fixed-sized partition or in the variable-sized partition. divided into fixed-sized blocks and each block contains exactly
one process. But, the fixed-sized partition will limit the degree of
multiprogramming as the number of the partition will decide the
number of processes.
Variable-Size Partition: In the variable size partition method,
the operating system maintains a table that contains the
information about all memory parts that are occupied by the
processes and all memory parts that are still available for the
processes.
Initially, the whole memory space is available for the user
processes as a large block, a hole. Eventually, when the
229 processes arrive in the memory, executes, terminates and 230
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
leaves the memory we will see the set of holes of variable sizes. does not have a memory block (hole) that is large enough to
In the figure above, we can see that when file A and file C hold that process.
release the memory allocated to them, creates the holes in the If the memory block (hole) is too large for the process it
memory of variable size. gets spilt into two parts. One part of the memory block is
In the variable size partition method, the operating system allocated to the arrived process and the other part is returned to
analyses the memory requirement of the process and see the set of holes. When a process terminates and releases the
whether it has a memory block of the required size. memory allocated to it, the released memory is then placed back
If it finds the match, then it allocates that memory block to the to the set of holes. The two holes that are adjacent to each
process. If not, then it searches the ready queue for the process other, in the set of holes, are merged to form one large hole.
that has a smaller memory requirement. The operating system uses either the block allocation list or the
The operating system allocates the memory to the process until bit map to select the hole from the set of holes.
it cannot satisfy the memory requirement of the next process in
the ready queue. It stops allocating memory to the process if it 231 232
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
2. Bit Map It does not have entries of the files or processes to which the
The bit map method only keeps track of the free or allocated specific blocks are allocated. Normally, implementing the first fit
block. One block is represented by one bit, bit 0 resembles the will search the number of consecutive zeros/free blocks required
free block and bit 1 resembles that the block is allocated to a file by a file of process. Having found that much of consecutive
or a process. zeros it allocates a file or process to those blocks.
But implementing best-fit or worse-fit will be expensive, as the
table of free blocks sorted according to the hole size has to be
maintained. But the bit map method is easy to implement.
Non-contiguous memory allocation
In the non-contiguous memory allocation the available free
memory space are scattered here and there and all the free
memory space is not at one place. So this is time-consuming. In
235 the non-contiguous memory allocation, a process will 236
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
237 238
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
Non-contiguous memory allocation is of different types, we have an invalid pointer which means the page is not in main
1. Paging memory or we will get the corresponding frame number. When
2. Segmentation the frame number is combined with instruction of set D than we
3. Segmentation with paging will get the corresponding physical address. Size of a page table
i) Paging is generally very large so cannot be accommodated inside the
A non-contiguous policy with a fixed size partition is called PCB, therefore, PCB contains a register value PTBR( page table
paging. A computer can address more memory than the amount base register) which leads to the page table.
of physically installed on the system. This extra memory is Advantages: It is independent of external fragmentation.
actually called virtual memory. Paging technique is very Disadvantages:
important in implementing virtual memory. Secondary memory is • It makes the translation very slow as main memory access
divided into equal size partition (fixed) called pages. Every two times.
process will have a separate page table. The entries in the page • A page table is a burden over the system which occupies
table are the number of pages a process. At each entry either 239 considerable space.
240
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT MEMORY MANAGEMENT
243 244
Lecture - XI BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
245 246
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT – MEMORY MANAGEMENT –
segmentation segmentation
What is the need of Segmentation? functions such as the main function can be included in one
Till now, we were using Paging as our main memory segment and the library functions can be included in the other
management technique. Paging is more close to the Operating segment.
system rather than the User. It divides all the processes into the MAIN SUB1 SUB2
form of pages regardless of the fact that a process can have Statement 1 Statement 1 Statement 1
some relative parts of functions which need to be loaded in the Statement 2 Statement 2 Statement 2
same page. ------------- --------------- --------------
Operating system doesn't care about the User's view of the ------------- --------------- Statement K
process. It may divide the same function into different pages and -------------- Statement L
those pages may or may not be loaded at the same time into the --------------
memory. It decreases the efficiency of the system. Statement M
It is better to have segmentation which divides the process into
the segments. Each segment contains the same type of 247 248
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
253 254
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
257 258
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE STRUCTURE OF PAGE TABLE
Each element mainly consists of : The Virtual Page numbers are compared in this chain searching
1. The virtual page number for a match; if the match is found then the corresponding
2. The value of the mapped page frame. physical frame is extracted.
3.A pointer to the next element in the linked list. In this scheme, a variation for 64-bit address space commonly
The following figure shows the address translation scheme of the uses clustered page tables.
Hashed Page Table: Clustered Page Tables
These are similar to hashed tables but here each entry refers to
several pages (that is 16) rather than 1.
Mainly used for sparse address spaces where memory
references are non-contiguous and scattered
Inverted Page Tables
The Inverted Page table basically combines A page table and A
263 frame table into a single data structure. 264
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE STRUCTURE OF PAGE TABLE
• There is one entry for each virtual page number and a real
page of memory
• And the entry mainly consists of the virtual address of the
page stored in that real memory location along with the
information about the process that owns the page.
• Though this technique decreases the memory that is needed
to store each page table; but it also increases the time that is
needed to search the table whenever a page reference
occurs.
The following figure shows the address translation scheme of the
Inverted Page Table:
265 266
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
281 282
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT – FILE MANAGEMENT –
BASICS BASICS
Indexed Allocation
• Provides solutions to problems for contiguous and linked
allocation.
• An index block is created having all pointers to files.
• Each file has its own index block which stores the addresses
of disk space occupied by the file.
• Directory contains the addresses of index blocks of files.
283 284
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
285 286
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
293 294
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT – FILE MANAGEMENT –
Directory Directory
Here, the files are accessed by their location using the path. 4. Acyclic-Graph Directory Structure
There are two types of paths to locate the file in this directory This problem can be solved by the acyclic-graph directory
structure structure. As this directory structure allows a directory or a file to
Absolute Path have many parent directories. So, a shared file in a directory can
Here, the path for the desired file is described by considering the be pointed by the other user directories who have access to that
root directory as the base directory. shared file using the links.
Relative Path In the diagram below we can see that the directory having file
Here, either the user’s directory is considered as the base F7 and F8 have two parent directories.
directory or the desired file directory is considered as the base
directory.
295 296
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
329 330
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK MANAGEMENT –
MAGNETIC DISK STRUCTURE MAGNETIC DISK STRUCTURE
Magnetic Disk Structure
In modern computers, most of the secondary storage is in the
form of magnetic disks. Hence, knowing the structure of a
magnetic disk is necessary to understand how the data in the
disk is accessed by the computer.
Structure of a magnetic disk
A magnetic disk contains several platters. Each platter is divided
into circular shaped tracks. The length of the tracks near the
centre is less than the length of the tracks farther from the
centre. Each track is further divided into sectors.
Tracks of the same distance from centre form a cylinder. A read-
write head is used to read data from a sector of the magnetic
Disk. 331 332
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
335 336
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS SCHEDULING ALGORITHMS
Disk Scheduling Algorithms First Come First Serve
On a typical multiprogramming system, there will usually be This algorithm performs requests in the same order asked by the
multiple disk access requests at any point of time. So those system. Let's take an example where the queue has the
requests must be scheduled to achieve good efficiency. Disk following requests with cylinder numbers as follows:
scheduling is similar to process scheduling. Some of the disk 98, 183, 37, 122, 14, 124, 65, 67
scheduling algorithms are Assume the head is initially at cylinder 56. The head moves in
1. First Come First Serve the given order in the queue i.e., 56→98→183→...→67.
2. Shortest Seek time first
3. SCAN algorithm
4. LOOK
5. C-SCAN
6. C-LOOK
337 338
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
339 340
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
343 344
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
347 348
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS Learning Objectives
Most of the time, some amount of physical memory in a Virtual memory is a combination of RAM and disk space that
computer is swapped on disk. Swap space is a space on a hard running processes can use. Swap space is the portion of
disk that is a substitute for physical memory. It is used as virtual virtual memory on the hard disk, used when RAM is full. Swap
memory, which contains images of the running process in the space can be useful to computers in the following various ways,
memory. Whenever our computer runs short of physical memory, such as:
it uses its virtual memory and stores information in memory on a • It can be used as a single contiguous memory which reduces
disk. I/O operations to read or write a file.
The process of swapping or interchanging of data between • Applications that are not used or less frequently used can be
virtual memory and real memory is called swapping and the kept in a swap file.
space on disk is known as swap space. Swap space helps the • Having sufficient swap files helps the system keep some
physical memory free all the time.
computer's operating system pretend that it has more RAM than
• The space in physical memory that has been freed due to
it actually has. It is also called a swap file. swap space can be used by OS for other important tasks.
351 352
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
Operating systems such as Windows, Linux, etc. systems system performance. The main goal for the design and
provide a certain amount of swap space by default which users implementation of swap space is to provide the best throughput
can change according to their needs. If we don't want to use for the virtual memory system.
virtual memory, we can easily disable it together. Still, if we run Swap space is used in various ways by different operating
out of memory, then the kernel will kill some of the processes to systems, depending on the memory-management algorithms in
create a sufficient amount of space in physical memory so that it use. For example, systems that implement swapping may use
totally depends upon the user whether he wants to use swap swap space to hold an entire process image, including the code
space or not. and data segments. Paging systems may simply store pages
What is Swap-Space Management? that have been pushed out of the main memory. The amount of
Managing the Swap-space is another low-level task of the swap space needed on a system can vary depending on the
operating system. Virtual memory uses disk space as an amount of physical memory, the amount of virtual memory it is
extension of main memory. Since disk access is much slower backing, and how it is used. It can range from a
than memory access, using swap space significantly decreases 353 few megabytes of disk space to gigabytes. 354
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT SWAP SPACE MANAGEMENT
Note that it may be safer to overestimate than to underestimate Some operating systems, including Linux, allow multiple swap
the amount of swap space required because if a system runs spaces. These swap spaces are usually put on separate disks
out of swap space, it may be forced to abort processes or may so the load placed on the I/O system by paging and swapping
crash entirely. Overestimation wastes disk space that could can be spread over the system's I/O devices.
otherwise be used for files, but it does no other harm. Some Uses of Swap Space
systems recommend the amount to be set aside for swap space. The different operating system uses Swap-space in various
Solaris, for example, suggests setting swap space equal to the ways. The systems that are implementing swapping may use
amount by which virtual memory exceeds page-able physical swap space to hold the entire process, including image, code,
memory. Previously, Linux suggested setting swap space to and data segments.
double the amount of physical memory, although most Linux Swapping is a memory management technique used in multi-
systems now use considerably less swap space. There is programming to increase the number of processes sharing the
currently much debate in the Linux community about whether to CPU. It is a technique of removing a process from the main
set aside swap space at all. 355 memory, storing it into secondary memory, and then bringing it 356
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
back into the main memory for continued execution. This action files, but it does not harm others. The following table shows
of moving a process out from main memory to secondary different systems using the amount of swap space:
memory is called Swap Out. The action of moving a process out S.No. System Swap Space
from secondary memory to main memory is called Swap In.
1. Solaris Swap space is equal to the amount of
Paging systems may simply store pages that have been pushed physical memory.
out of the main memory. The need for swap space on a system 2. Linux Swap space is double the amount of physical
memory.
can vary from megabytes to gigabytes. Still, it also depends on
the amount of physical memory, the virtual memory it is backing, Solaris setting swap space equal to the amount by which virtual
and how it uses the virtual memory. memory exceeds page-able physical memory.
It is safer to overestimate than to underestimate the amount of Previously, Linux has suggested setting swap space to double
swap space required because if a system runs out of swap the amount of physical memory. Today, this limitation is gone,
space, it may be forced to abort the processes or may crash and most Linux systems use considerably less swap space.
entirely. Overestimation wastes disk space that could be used for357 358
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
Including Linux, some operating systems allow the use of RAID stands for ‘Redundant Arrays of Independent Disks’ or
multiple swap spaces, including both files and dedicated swap ‘Redundant Arrays of Inexpensive Disks. This technology
partitions. The swap spaces are placed on the disk, so the load plays a vital role in storing large amounts of data while
on the I/O by the paging and swapping will spread over the preserving Data Integrity. It helps in real-time data recovery
system's bandwidth. when a hard drive fails. In other words, this technology either
divides or duplicates the task of one hard disk between multiple
disks.
This is done to create data redundancy in case of a drive failure.
RAID Mode also called Raid level is set for different application
requirements. E.g. when mode “RAID 0” is set, then the system
splits the data evenly between two or more disks.
359 360
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID
RAID Configuration Levels configuration offers best ‘Write’ performance and it does not
Different levels represent specific configuration of Disk Arrays. employ redundancy at all. ‘Read’ performance is low in this
Only few configurations are practical for most of the processing configuration.
systems. Hence RAID – 0 ,1,3,5 and 6 are discussed below. If one physical disk in the array fails, the data is lost. This type of
RAID – 0 (Non-Redundant Configuration) configuration is preferred in super-computing environments
RAID – 1 (Mirrored Configuration) where performance and capacity, rather than reliability are the
RAID – 3 (Bit-Interleaved Parity) primary concerns. The following figure shows Non-Redundant
RAID – 5 (Block-Interleaved Distributed-Parity) Configuration where the Data is distributed evenly and striped
RAID – 6 (P+Q Redundancy) across three disks.
RAID – 0 (Non-Redundant Configuration)
This is the fastest RAID mode which stripes the data onto each
disk evenly. Data Striping refers to the distribution of data over
multiple disks to make them appear as a single, large disk. This 361 362
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID
363 364
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID
RAID – 3 Configuration
367 368
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID
369 370
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID
Advantages of RAID
The advantages are:
• Transfer of large sequential files and graphic images is easier.
• Hardware based implementation is more robust.
• Software based implementation is cost-effective.
• Highest performance and Data protection can be achieved.
• Fault tolerance capacity is high.
• They require less power.
Disadvantages of RAID
The disadvantages include:
RAID 6 Configuration • In spite of using this technology, backup software is a must.
• Mapping Logic blocks onto physical locations is complex.
371 • Data chunk size affects the performance of disk array. 372
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID - Problems RAID
Problem 1:What is the average access time for transferring 512 bytes Solution for Problem 1:
of data with the following specifications- Average seek time = 5 msec
Average seek time = 5 msec Disk rotation = 6000 RPM
Disk rotation = 6000 RPM
Data rate = 40 KB/sec
Data rate = 40 KB/sec
Controller overhead = 0.1 msec
Controller overhead = 0.1 msec
Time taken for one full rotation
Problem 2:A certain moving arm disk storage with one head has the
following specifications- = (60 / 6000) sec
Number of tracks per surface = 200 = (1 / 100) sec
Disk rotation speed = 2400 RPM = 0.01 sec
Track storage capacity = 62500 bits = 10 msec
Average latency = P msec Average rotational delay
Data transfer rate = Q bits/sec = 1/2 x Time taken for one full rotation
What is the value of P and Q? = 1/2 x 10 msec = 5 msec
373 374
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID
RAID
377
Lecture - XVI BITS Pilani, Pilani Campus