Fcfs Algorithm
Fcfs Algorithm
P1 0 5 5 5
0
P2 1 3 8 7
4
P3 2 8 16 14
6
P4 3 6 22 19
13
----------
22
----------
First we can draw the Gantt chart for the Processers
P1 P2 P4
P3
0 5 8 16 22
Total TAT= 45
Average TAT= 45/4 = 11.2
Total Waiting Time= 23
Average Waiting = 23/4= 5.7
P1 0 8 8 8
0
P2 1 4 12 11
7
P3 2 9 32 30
21
P4 3 5 17 14
9
P5 4 6 23 19
13
----------
32
----------
First we can draw the Gantt chart for the Processers
P1 P2 P5 P3
P4
0 8 12 17 23
32
Total TAT= 82
Average TAT= 82/5 = 16.4
Total Waiting Time= 50
Average Waiting = 50/5= 10
5 P1 0 9 9 9
0
3 P2 1 4 25 24
20
1 P3 2 5 14 12
7
2 P4 3 7 21 18
11
4 P5 4 3 28 24
21
----------
28
----------
First we can draw the Gantt chart for the Processers
P1 P3 P2 P5
P4
0 9 14 21 25
28
Total TAT= 87
Average TAT= 87/5 = 17.4
Total Waiting Time= 59
Average Waiting = 59/5= 11.8
P1 0 5>3>1 12 12
7
P2 1 4>2>0 11 10
6
P3 2 2 >0 6 4
2
P4 4 1>0 9 5
4
Ready Queue
P1 P2 P3 P1 P4 P2 P1
Gantt chart
P1 P2 P3 P1 P4 P2 P1
0 2 4 6 8 9 11
12
Total TAT= 31
Average TAT= 31/4 = 7.75
Total Waiting Time= 19
Average Waiting = 19/4= 4.75
1. Ready Q0
2. Ready Q1
3. Ready Q2
2 Process P0, P1
int turn
boolean falg[2];
Structure of Process pi
while(true)
{
flag[i] = true;
turn =j;
while (flag[j]= = true && turn= =j);
Critical Section;
flag[i]= false;
}
P0:
flag[0]=true;
turn=1;
while (flag[1]= = true && turn= =1);
Critical Section;
flag[0]= false;
P1:
flag[1]=true;
turn=0;
while (flag[0]= = true && turn= =0);
Critical Section;
flag[1]= false;
Semaphores
2. signal():
1. After completing the process in CS the semaphore
value is incremented.
signal(s)
{
s++;
}
Types of Semaphores:
1. Binary Semaphore
2. Counting Semaphore
//p1,p2 processers
int mutex=1;
mutex=s; //s=semaphore
do
{
wait(mutex) //mutex=1
critical section
signal (mutex)
reminder section
}while(1);
CASE:1
while(s)
{
while (s<=0); //nothing
s--;
}
CASE:2
signal(s)
{
s++; //S=0
}
Classical Problems of Synchronization with
Semaphore Solution
Consumer
Structure of Producer:
while(true)
{
//produce an item in nextp
wait(empty); //n=5
wait(mutex);
Critical Section;
// add nextp to buffer
signal(mutex);
signal(full);
P0
C0 C1
P4 P1 P1
Rice
C2
C4
P3
C3 P2
5 Philosophers P0, P1, P2, P3, P4
A Boul of Rice and they are having 5 Plates
They are having 5 Chopsticks C0, C1, C2, C3, C4
Philosopher will do 2 Activates is:
1. Eat
2. Think
If Philosophers is not eating it means he can
thinking.
If Philosophers is not thinking it means he can
eating.
C[0] C[1] C[2] C[3] C[4]
1 1 1 1 1
0 – P0 0 –P1
1 1
0-P3 0-P3
1 1
1- Chopstick is free
0- Chopstick is already allotted to some one
do
{
wait(chopstick[i]) //semaphore= chopstick
wait(chopstick[(i+1)%5]);
----------
----------
Eat
----------
----------
signal(chopstick[i])
signal(chopstick[(i+1)%5]);
-----------
-----------
think
}while(1);
3. Readers and Writers Problem
Shared data
Shared file
integer readcount=0;
semaphore mutex=1;
semaphore wrt=1;
wait(wrt);
signal(mutex);
//Reading Performs // CRITICAL SECTION
wait(mutex);
readcount--;
if (readcount==0) EXIT SECTION
signal(wrt);
signal(mutex);
}
Problem Statement:
Conditions:
1 waiting room with N chairs
1 barber room with 1barber chair
Barber & Customer:
1. If there are no customer, the barber is goes to sleep
2. When a customer arrives, he has to wake up the barber.
3. If there are many customers and the barber is cutting a
customer’s hair, then the remaining customers either wait if there are
empty chairs in the waiting room or they leave if no chairs are empty.
Sleeping barber solution:
Entry queue
Shared data x,y
OPERATERS
initialization code
x.wait()
x.signal()
Deadlock Avoidance
Bankers Algorithm
1. Safety Algorithm
A B C (Resources)
10 5 7
1. WORK=AVAILABLE
FINISH[I] =FALSE FOR I=0…N-1
2. FIND AN I SUCH THAT
A) FINISH[I] =FALSE
B) NEED <= WORK
3. WORK=WORK + ALLOCATION
FINISH[I] =TRUE
GOTO STEP 2
4. IF FINISH[I] =TRUE FOR ALL I, THEN SYSTEM IS IN SAFE
STATE
WORK=AVAILABLE SO WORK = 3 3 2
FINISH
0 1 2 3 4
FALSE FALSE FALSE FALSE FALSE
TRUE TRUE TRUE
TRUE TRUE
P0:
NEED<=WORK
743<=332, FALSE
SO P0 MUST WAIT
P1:
NEED<=WORK
122<=332, TRUE
WORK=WORK+ALLOCATION
=332+200
=532
P2:
NEED<=WORK
600<=532, FALSE
P2 MUST WAIT
P3:
NEED<=WORK
011<=532, TRUE
WORK=WORK+ALLOCATION
=532+211
=743
P4:
NEED<=WORK
431<=743, TRUE
WORK=WORK+ALLOCATION
=743+002
=745
P0:
NEED<=WORK
743<=745, TRUE
WORK=WORK+ALLOCATION
=745+010
= 755
P2:
NEED<=WORK
600<=755, TRUE
WORK=WORK+ALLOCATION
=755+302
=10 5 7
A B C (Resources)
10 5 7
P0:
NEED<=WORK
743<=745, TRUE
WORK=WORK+ALLOCATION
=745+010
=755
P2:
NEED<=WORK
600<=755, TRUE
WORK=WORK+ALLOCATION
=755+302
=10 5 7
DEADLOCK DETECTION
1. Single Instance of Each Recourse Type
Recourse Allocation Graph:
P5
R1 R3 R4
P1 P2 P3
R2 P4 R5
P1 P2 P3
P4
1. WORK=AVAILABLE,
FINISH[i]=FALSE IS ALLOCATION #0,
FINISH[i]=TRUE IS ALLOCATION =0,
2. FIND AN i SUCH THAT
A) FINISH[i] =FALSE
B) REQ <= WORK
3. WORK=WORK + ALLOCATION
FINISH[i] =TRUE
GOTO STEP 2
4. IF FINISH[i] =FALSE FOR ALL i=0,1,…n-1, THEN SYSTEM IS IN
DEADLOCK(UNSAFE) OTHERWISE SAFE STATE
EXAMPLE:
Work=000
Finish
0 1 2 3 4
False False False False False
TRUE TRUE TRUE TRUE
P0:
REQ<=WORK
000<=000, TRUE
WORK=WORK+ALLOCATION
= 000+010
=010
FINISH[0]=TRUE
P1:
REQ<=WORK
202<=010, FALSE
SO, P1 MUST WAIT
P2:
REQ<=WORK
000<=010, TRUE
WORK=WORK+ALLOCATION
= 010+303
=313
FINISH [2]=TRUE
P3:
REQ<=WORK
100<=313, TRUE
WORK=WORK+ALLOCATION
= 313+211
=524
FINISH [3]=TRUE
P4:
REQ<=WORK
002<=524, TRUE
WORK=WORK+ALLOCATION
= 524+002
=526
FINISH [4]=TRUE
P1:
REQ<=WORK
202<=526, TRUE
WORK=WORK+ALLOCATION
= 526+200
=726
NO DEADLOCK DETECTED
726
DEADLOCK RECOVERY
1. Process Termination
a. Abort all Deadlock Processers
b. Abort 1 Process at a time until Deadlock cycle is
eliminated.
2. Recourse Preemption
a. Selecting a victim
b. Rollback
c. Starvation
O/S
P1 5MB
2 MB
P2 10MB
8MB
P3 3 MB
2MB
P4 25 MB
5MB
P5 20MB
5MB
P6 7MB
1MB
Main Memory
O/S
P1 5MB
P2 5MB
P3 5MB
P4 5MB
Main Memory
Let us assume,
P1=2 MB, P2=2MB, P3=1MB, P4=20MB, P5=15MB,
P6=6MB
Free space= 2+8+2+5+5+1=23 MB
Let us assume P7 wants to enter 20 MB, the OS Not
Possible allocate free space of 23 why because this is
contiguous allocation.
Advantages:
1. Easy to implement
2. No external fragmentation
Disadvantages:
1. Internal fragmentation
2. External fragmentation
3. Limit Process Size
4. Limitation on Degree of Multiprogramming
Three methods we can use
1. First Fit
2. Best Fit
3. Worst Fit
O/S
P1 5MB
P2 10MB
P3 7MB
P4 20MB
Main Memory
Advantages:
1. No Internal Fragmentation
2. No restriction on the Degree of Multiprogramming
3. No Limitation on the Size of the Process
Disadvantages:
1. Difficult Implementation
2. External Fragmentation
Compaction:
O/S
5 MB
P2 10MB
7 MB
P4 20MB
O/S
P2 10MB
P4 20MB
P5 12MB
Page 0
Page 1
Page 2
Page 3
Process (Logical Memory)
0 1
1 4
2 3
3 7
Page Table
0
1 Page 0
2
3 Page 2
4 Page 1
5
6
7 Page 3
Main Memory (Physical Memory)
Paging Hardware
2. Segmentation
Segmentation example:
Process:
1. Deposit –seg0
2. Withdraw- seg1
3. Cancel- seg2
0
100 O/S
Seg0
200
Seg1
350
750 Seg2
Case 1: 50 is offset, seg1 limit is 150
50<150
Case 2: 200 is offset , seg1 limit is 150
200<150
Then trap
0 A
1 B
2 C
3 D
Process or Logical Memory
02 V
1 I
25 V
3 I
Page Table
0
1
2 A
3
4
5 C
6
Main memory or Physical memory
A
B
C
D
Secondary Memory
Page Replacement
Page Replacement Algorithms
There are three types of Page Replacement Algorithms. They
are:
Allocation of Frames
The OS will provide allocation of frames in 5 types
1. Equal allocation
P1 require 10 Frames
P2 require 30 Frames
Main memory = 40 Frames
But OS is allocates equally P1-> 20
OS is allocates equally P2-> 20
Disadvantage: Priority wise not followed
2. Proportional allocation
3. Priority Allocation
4. Global Replacement Allocation
5. Local Replacement Allocation
Thrashing
Definition:
A Process is spending on more time on pages rather than
executing this is called Thrashing.
Causes of thrashing:
High degree of multiprogramming.
Lack of frames.
Page replacement policy.
1. When the CPU’s usage is low, the process scheduling
mechanism tries to load multiple processes into memory at the
same time, increasing the degree of Multi programming.
2. In this case, the number of processes in the memory
exceeds the number of frames available in the memory
3. If a high-priority process arrives in memory and the frame is
not vacant at the moment, the other process occupying the
frame will be moved to secondary storage, and the free frame
will be allotted to a higher-priority process.
4. At that time the memory is full, the procedure begins to take
a long time to swap in the required pages. Because most of the
processes are waiting for pages, the CPU utilization drops
again. Now thrashing will occurs.
Techniques to handle:
1. Working Set Model
Principal of Locality of Preference
Min-
MAX-
INFINITY-
2. Page Fault Frequency
UNIT-6
1. Single-level directory:
2. Two-level directory:
3. Tree Structure/ Hierarchical Structure:
Disadvantage:
The major drawback of using the hash table is that generally, it
has a fixed size and its dependency on size.
The file ‘mail’ in the following figure starts from the block 19
with length = 6 blocks. Therefore, it occupies 19, 20, 21, 22, 23,
24 blocks.
Advantages:
1. Both the Sequential and Direct Accesses are supported by
this. For direct access, the address of the kth block of the file
which starts at block b can easily be obtained as (b+k).
2. This is extremely fast since the number of seeks are minimal
because of contiguous allocation of file blocks.
Disadvantages:
1. This method suffers from both internal and external
fragmentation. This makes it inefficient in terms of memory
utilization.
2. Increasing file size is difficult because it depends on the
availability of contiguous memory at a particular instance.
2. Linked Allocation:
In this scheme, each file is a linked list of disk blocks which
need not be contiguous. The disk blocks can be scattered
anywhere on the disk.
The directory entry contains a pointer to the starting and the
ending file block. Each block contains a pointer to the next
block occupied by the file.
The file ‘jeep’ in following image shows how the blocks are
randomly distributed. The last block (25) contains -1 indicating
a null pointer and does not point to any other block.
Advantages:
1. This is very flexible in terms of file size. File size can be
increased easily since the system does not have to look for a
contiguous chunk of memory.
2. This method does not suffer from external fragmentation.
This makes it relatively better in terms of memory utilization.
Disadvantages:
1. Because the file blocks are distributed randomly on the disk,
a large number of seeks are needed to access every block
individually. This makes linked allocation slower.
3. Indexed Allocation:
In this scheme, a special block known as the Index block
contains the pointers to all the blocks occupied by a file. Each
file has its own index block. The ith entry in the index block
contains the disk address of the ith file block. The directory
entry contains the address of the index block as shown in the
image:
Advantages:
1. This supports direct access to the blocks occupied by the file
and therefore provides fast access to the file blocks.
2. It overcomes the problem of external fragmentation.
Disadvantages:
1. The pointer overhead for indexed allocation is greater than
linked allocation.
DISK STRUCTURE
What is Hard Disk?
Hard Disk is a secondary storage device. It is a type of electro
mechanical device. A hard disk stores and retrieves digital data
using magnetic storage. Disk is rigid rapidly rotating platters
coated with
Magnetic material.
Hard Disk Pack consist of more than one disk.
How Data is Stored in a Hard Disk?
● Data is stored in Disk in form of tracks and sectors.
● Disk surface is divided in to tracks.
● Track is further dived in to sectors.
● Sector is the addressable unit in disk.
Basic picture for disk storage concept is given below
Disk Structure in OS
Disk structure is as shown in following figure
Step wise description of Disk Structure is given below
● Disk surface divided into tracks
● A rеad/writе hеad positionеd just abovе thе disk surfacе
● Information storеd by magnеtic rеcording on thе track undеr
rеad/writе hеad
● Fixеd hеad disk
● Moving hеad disk
● Dеsignеd for largе amount of storagе
● Primary dеsign considеration cost, sizе, and spееd
Hardwarе for disk systеm
● Disk drivе, Dеvicе motor, Rеad/writе hеad, Associatеd logic
Disk controllеr
● Dеtеrminеs thе logical intеraction with thе computеr
● Can sеrvicе morе than onе drivе (ovеrlappеd sееks)
Cylindеr
● Thе samе numbеrеd tracks on all thе disk surfacеs
● Еach track contains bеtwееn 8 to 32 sеctors
Sеctor
● Smallеst unit of information that can bе rеad from/writtеn
into disk
● Rangе from 32 bytеs to 4096 bytеs
DISK MANAGEMENT
Disk management of the operating system includes:
1. Disk Format
- Low level (or) Physical level
- High level (or) Logical level
2. Booting from disk
- Bootstrap Loaded Program – ROM
- Tiny Bootstrap Program
3. Bad block recovery
The low-level format or physical format:
Divides the disk into sectors before storing data so that the
disk controller can read and write Each sector can be:
For example, the sizes can be 256,512, and 1,024 bytes. If disk
is formatted with larger sector size, fewer sectors can fit on
each track.
Boot block:
When the computer is turned on or restarted, the program
stored in the initial bootstrap ROM finds the location of the OS
kernel from the disk, loads the kernel into memory, and runs the
OS. start.
To change the bootstrap code, you need to change the ROM
and hardware chip. Only a small bootstrap loader program is
stored in ROM instead.
The full bootstrap code is stored in the “boot block” of the disk.
A disk with a boot partition is called a boot disk or system disk.
The bootstrap program is required for a computer to initiate the
booting after it is powered up or rebooted.
It initializes all components of the system, from CPU registers
to device controllers and the contents of main memory, and
then starts the operating system.
The bootstrap program then locates the OS kernel on disk,
loads that kemel into memory, and jumps to an initial address
to start the operating-system execution.
The Read Only Memory (ROM) does not require initialization
and is at a fixed location that the processor can begin
executing when powered up or reset. Therefore bootstrap is
stored in ROM.
Because of read only feature of ROM; it cannot be infected by a
computer virus. The difficulty is that modification of this
bootstrap code requires changing the ROM hardware chips.
Therefore, most systems store a small bootstrap loader
program in the boot ROM which invokes and bring full
bootstrap program from disk into main memory.
The modified version of full bootstrap program can be simply
written onto the disk.
The fixed storage location of full bootstrap program is in the
“boot blocks”.
A disk that has a boot partition is called a boot disk or system
disk.
Bad Blocks:
Disks are error-prone because moving parts have small
tolerances.
Most disks are even stuffed from the factory with bad blocks
and are handled in a variety of ways.
The controller maintains a list of bad blocks.
The controller can instruct each bad sector to be logically
replaced with one of the spare sectors. This scheme is known
as sector sparing or transfer.
A soft error triggers the data recovery process.
However, unrecoverable hard errors may result in data loss and
require manual intervention.
Failure of the disk can be:
1. Complete, means there is no way other than replacing the
disk. Back up of content must be taken on new disk.
Now for the further page reference string —> 0 Page fault
because they are already available in the memory.
Optimal page replacement is perfect, but not possible in
practice as the operating system cannot know future requests.
The use of Optimal Page replacement is to set up a benchmark
so that other replacement algorithms can be analyzed against it.