Final OS Labfile CodeAligned
Final OS Labfile CodeAligned
EXPERIMENT – 4
Objective - Implement file storage allocation technique:
i. Contiguous(using array) ii.
Linked –list(using linked-list) iii.
Indirect allocation (indexing)
Theory –
1. Contiguous Allocation
About Contiguous allocation technique:
In this scheme, each file occupies a contiguous set of blocks on the disk. For example, if a file requires
n blocks and is given a block b as the starting location, then the blocks assigned to the file will be: b,
b+1, b+2,……b+n-1. This means that given the starting block address and the length of the file (in
terms of blocks required), we can determine the blocks occupied by the file.
The directory entry for a file with contiguous allocation contains Address
of starting block
Length of the allocated portion.
j=j+f[i].count;
break;
}
else {
j=j+flag;
flag=0;
}}
if(j==99 && flag == 0)
printf("\n disk is full \n");
}}
printf("\n here is the disk allocation details \n");
for(i=0;i<100;i++) printf("%d ", disk[i]); return
0;
}
Input: Program 4(i):
Disk Size = 100 (Block (all of equal size))
File ID Block_Count
File 0 5
File 1 3
File 2 2
Output:
here is the disk allocation details
0 0 0 0 0 1 1 1 2 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -
1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2. Linked List
About Linked List Allocation technique:
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.
File ID Block_Count
File 1 5
Output:
Here is the disk allocation for file :
file id: 1 block number: 0 block address 38287408
file id: 1 block number: 1 block address 38287440
file id: 1 block number: 2 block address 38287472
3. Indirect Allocation
About Indirect allocation (indexing) technique:
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:
temp2->id=i; temp2-
>block=j;
temp2->next_block=NULL; temp-
>next_block = temp2;
printf("%d\t", i);
}}}
temp=files[id]; while(temp!=NULL){
printf("file id: %d\t block number: %d\t block address %d\n", temp->id, temp-
>block,(int)temp);
temp=temp->next_block;
}
return 0;
}
Input: Program 4(iii):
File ID Block_Count
File 0 5
File 1 4
File 2 6
Output:
disk allocation using index as file id
file id: 0 block number: 0 block address 35997744
file id: 0 block number: 1 block address 35997776
file id: 0 block number: 2 block address 3599780
file id: 0 block number: 3 block address 35997840
file id: 0 block number: 4 block address 35997872
EXPERIMENT – 5
Objective - Implementation of contiguous allocation techniques:
i. Worst-Fit
ii. Best-Fit
iii. First-Fit
Theory –
1. Worst Fit
Worst Fit Allocate the process to the partition which is the largest sufficient among the freely
available partitions available in the main memory.
Algorithm
Step 1. Input the total number of blocks and their size.
Step 2. Input the total number of processes and their size.
Step 3. For each process in list
Find free block having max size and greater than process size Allocate
block to process
}
printf(" enter numebr of process: ");
scanf("%d",&np);
}
for(i=0;i<nb-1;i++){
for(j=i+1;j<nb;j++){
if(bls[j].size > bls[i].size){
temp.bid = bls[j].bid;
temp.size = bls[j].size;
temp.index = bls[j].index;
bls[j].bid = bls[i].bid;
bls[j].size = bls[i].size;
bls[j].index = bls[i].index;
bls[i].bid = temp.bid;
bls[i].size = temp.size;
bls[i].index = temp.index;
}}}
for(i=0;i<np;i++){
if(i >= nb || bls[i].size < ps[i].size)
printf("\n no block is available for the process \n");
else
ps[i].b=bls[i];
}
printf(" \n now process block allocation list is \n");
for(i=0;i<np;i++){
printf(" process id: %d process size %d block id %d free space %d\n", ps[i].pid, ps[i].size,
ps[i].b.bid, ps[i].b.size-ps[i].size);
} return
0;
}
Input: Program 5(i):
Block ID Block_size
Block 0 5
Block 1 4
Block 2 8
Block 3 3
Process ID Process_size
Process 0 3
Process 1 2
Process 2 4
Output:
now process block allocation list is
2. Best-Fit
Best Fit Allocate the process to the partition which is the first smallest sufficient partition among the
free available partition.
Algorithm
Step 1. Input the total number of blocks and their size.
Step 2. Input the total number of processes and their size.
Step 3. For each process in list
Find free block where, block_size - process _size = minimum Allocate
block to process
for(j=0;j<np;j++){
temp = bls[i].size - ps[j].size;
if(temp < 0){
continue;
}
else if(temp < temp2) {
if(ps[j].flag == -1){
pindex=j;
temp2=temp;
}}}
ps[pindex].b = bls[i];
ps[pindex].flag = 1;
temp2=20;
}
printf(" \n now process block allocation list is \n");
for(i=0;i<np;i++){
printf(" process id: %d process size %d block id %d free space %d\n", ps[i].pid, ps[i].size,
ps[i].b.bid, ps[i].b.size-ps[i].size);
} return
0;
}
Block ID Blocksize
Block 0 5
Block 1 4
Block 2 8
Block 3 3
Output:
now process block allocation list is
process id: 0 process size 3 block id 1 free space 1 process
id: 1 process size 2 block id 3 free space 1 process id: 2
process size 4 block id 0 free space 1
3. First Fit
First Fit: In the first fit, the partition is allocated which is first sufficient block from the top of Main
Memory.
Algorithm
Step 1. Input the total number of blocks and their size.
Step 2. Input the total number of processes and their size.
Block ID Block_size
Block 0 5
Block 1 4
Block 2 8
Block 3 3
Process ID Process_size
Process 0 3
Process 1 2
Process 2 4
Output:
now process block allocation list is process id: 0
process size 4 block id 0 free space 1 process id: 1
process size 2 block id 1 free space 2
process id: 2 process size 3 block id 2 free space 5
EXPERIMENT – 6
Objective – Calculation of Internal and External fragmentation
Theory –
Block ID Blocksize
Block 0 5
Block 1 4
Block 2 8
Block 3 3
Output:
Output:
now process block allocation list is process id: 0
process size 4 block id 0 free space 1 process id: 1
process size 2 block id 1 free space 2
process id: 2 process size 3 block id 2 free space 5
EXPERIMENT – 7
Objective – Implementation of compaction for the continually changing memory layout and
calculate total movement of data
Theory –
Memory compaction
The longstanding memory fragmentation problem has been covered many times in these pages. In
short: as the system runs, pages tend to be scattered between users, making it hard to find groups of
physically-contiguous pages when they are needed. Much work has gone into avoiding the need for
higher-order (multi-page) memory allocations whenever possible, with the result that most kernel
functionality is not hurt by page fragmentation. But there are still situations where higher-order
allocations are needed; code which needs such allocations can fail on a fragmented system.
Example:
Example program for Implementation of compaction for the continually changing memory
layout and calculate total movement of data
#include<stdio.h>
#include<stdlib.h
> int disk[20];
struct blocks{
int bid;
int size;
int index;
int status;
}block; struct
processs{
int pid;
int size;
int flag;
struct blocks
b;
int fragment;
}process;
int main(){ int nb,
np, i , j;
int disk_index=0;
printf(" Total disk size is 50 \n");
printf(" enter numebr of blocks: ");
scanf("%d", &nb);
struct blocks bls[nb], temp;
for(i=0;i<nb;i++) {
bls[i].bid=i;
printf("enter the size of block %d ", i);
scanf("%d", &bls[i].size);
bls[i].index=disk_index;
disk_index=disk_index+bls[i].size;
bls[i].status = -1;
}
printf(" enter numebr of process:
"); scanf("%d",&np); struct
processs ps[np];
for(i=0;i<np;i++){
ps[i].pid=i;
printf("enter the size of process %d ", i);
scanf("%d", &ps[i].size);
ps[i].flag=-1;
}
for(i=0;i<np;i++){
for(j=0;j<nb;j++) {
if(bls[j].size > ps[i].size && bls[j].status ==-1){ ps[i].b=bls[j];
bls[j].status = 1;
break;
}}}
printf(" \n now process block allocation list is \n"); for(i=0;i<np;i++){ printf(" process id:
%d process size %d block id %d free space %d\n", ps[i].pid, ps[i].size, ps[i].b.bid,
ps[i].b.size-ps[i].size);
}
printf(" \n now Free space list of blocks is \n");
int free=0; for(i=0;i<nb;i++){
printf(" Block id: %d Block size %d block index %d \n", bls[i].bid, bls[i].size,
bls[i].index);
}
int sid, count=0;
for(i=0;i<nb-1;i++){
count = 0;
for(j=1; j<nb;j++){
if(bls[i].status == -1 && bls[j].status !=-1){
sid = bls[j].index;
bls[j].index= bls[i].index;
bls[i].index = sid;
}
else{
count = count +1;
i=i+count;
}
}
i=i-count;
}
printf(" \n now After compaction list of blocks is \n");
free=0; for(i=0;i<nb;i++){
if(bls[i].status == -1){
printf(" Block id: %d Block size %d New block index %d \n", bls[i].bid, bls[i].size, bls[i].index);
free= free + bls[i].size;
}
}
printf(" \n Total external free space is: %d \n",
free); return 0; }
Input: Program 7:
Block ID Block_size
Block 0 3
Block 1 8
Block 2 5
Block 3 4
Process Process_size
ID
Process 4
0
Process 2
1
Output:
now process block allocation list is
EXPERIMENT – 8
Objective – Implementation of Resource Allocating Graph (RAG).
Input: Program 8:
Output:
P0 P1 P2 R0 R1 R2 P0
000001
P1 0 0 0 1 0 0
P2 0 0 0 0 1 0
R0 1 00 0 0 0
R1 0 1 0 0 0 0
R2 0 0 1 0 0 0
EXPERIMENT – 9
Objective – Implementation of Banker’s Algorithm
Theory – Banker’s Algorithm
The Banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by
simulating the allocation for predetermined maximum possible amounts of all resources, then makes an
“sstate” check to test for possible activities, before deciding whether allocation should be allowed to continue.
Following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available:
• It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
• Available[ j ] = k means there are ‘k’ instances of resource type Rj Max:
• It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.
• Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation:
• It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently
allocated to each process.
• Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj
Need :
• It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
• Need [ i, j ] = k means process Pi currently need ‘k’ instances of resource type Rj for its
execution.
• Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional
resources that process Pi may still request to complete its task.
Banker’s algorithm consists of Safety algorithm and Resource request algorithm.
Safety Algorithm
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both a) Finish[i] = false
b) Needi<= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i then the system is in a safe state
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k instances of
resource type Rj. When a request for resources is made by process Pi, the following actions are taken:
1) If Requesti<= Needi
Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum claim. 2)
If Requesti<= Available
Goto step (3); otherwise, Pi must wait, since the resources are not available.
3) Have the system pretend to have allocated the requested resources to process Pi by modifying the state as
follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti
Output:
Following is the SAFE Sequence
P1 -> P3 -> P4 -> P0 -> P2
EXPERIMENT – 10
Objective - Conversion of resource allocation graph (RAG) to wait for graph (WFG) for each type of
method used for storing graph.
Theory –
Wait-for-Graph (WFG)
A Wait-For Graph (WFG) is the same as the SRAG with the resource elements stripped out. Example: