We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 19
EXPERIMENT - 5
Objective - Implementation of contiguous allocation techniques:
i Worst-Fit
ii, Best-Fit
ili, 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 I. 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 sizeAllocate block to
process
Example program for implementation of contiguous allocation techniques: Worst-Fit
#include
Hinclude int
disk{20);
struct blocks {
int bid; int size; int
index;
}block;
struct processs{
int pid; int size; int
flag;
struct blocks
fragment;
}process; int
main(){
int nb, np, i, js 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-O:i bls[i].size){ temp.bid ~ bis{j].bid; temp size ~
blsfj].size; temp.index = bls{j].index;bls{j].bid = bls[i). bid;
bisfj].size = bis[i] size; bls[j].index = bls{i].index;bls[i].bid
temp.bid; bls[i].size = temp size; bls[i]-index = temp. index;
Ww
for(i=0;i= nb || bIs[iJ.size < ps[i] size)
printf("\n no block is available for the process \n");
else
psfil.b-bisfi);
}
printf(" \n now process block allocation list is \n");
for(i-Osi
‘include
int disk(20};
struct blocks {
int
int size;
int index;
block;
struct processs {
int pid;
int size:
int flag;
struct blocks b;
int fragment;
}process;
int main(){
int nb, np, i, js
int disk_index-0;
printf(" Total disk size is 20 \n");
printf(" enter numebr of blocks:
seanf("%d", Senb);
struct blocks bls{nb);
forti=0;i process _size
Allocate block to processExample program for implementation of contiguous allocation techniques: First-Fit
#include
#include
int disk([20),
struct blocks {
int bid;
int size;
struct processs{
int pid;
int size:
int flag;
struct blocks b;
int fragment;
int nb, np, i, j;
int disk_index=0:
printf(" Total disk size is 50 \n");
printf(" enter numebr of blocks:
scanfi"%d", &enb);
struct blocks bls[nb], temp;
for(i=Osi ps[ij.size && bis{j} status =-1)
psliJ:b=blsfj]:
bis[j}.status = 1;
break;
uy
printi(” \n now process block allocation list is \n");
for(i=0;i
ifinelude
int disk{20];
struct blocks {
int status;
yblock;
struct processs {
int pid;
int size;
int flag;
struct blocks b;
int fragment;
}process;
int main(){
int nb, np, i js
int disk_index=0;
printf" Total disk size is 50 \n");
printf(" enter numebr of blocks: ");
seanfi"%d", Sn);
struct blocks bls[nb], temp;
for(i=0;i psfi).size && bis{j].status
1){psfil-b=bisfj];
bis{j}.status = 1;
break;
yw
printf(" \n now Free space list of blocks i
int free=0;
si
ifinelude
int disk{20};
struct blocks {
int bid;
int size;
int index;
int status;
yblock;
struct process {
int pid;
nt 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 block
scanfi"%d", Snb);
struct blocks bls[nb], temp;
for(i=0;i psfiJ.size && bis{j]-status
1fpsfi]-b=bls[j];
bis{j}.status = 1;
break;
wy
printf(” \n now process block allocation list is \n");
si
include
int disk[20);
struct blocks {
int bid;
int size
int index;
int status;
block;
struct process {
int pid:
int size;
int flag
struct blocks b;
int fragment;
np, i, js
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=O;icnbsi++) {
bls[i].bid=i;
printf"enter the size of block %d ", i);
scanf("%d", &ebls[i]. size);
bls[i].index=disk_index;
disk_index=disk_index+blsfiJ.size;
bls[i).statu
$
printf(" enter numebr of pro
scanf("%d",&np);
struct process ps[np]};
forti=0;i psfi).size && bls{j]-status
psfi].b-bis{j];
bisfj).status = 1;
break;
uw
printf(" \n now process block allocation list is \n");
icnpsit+){
process id: %d process size Yad block id %d free space Yod\n", psfil.pid, psfil.size,
ps[il.b.bid, pslil-b.size-psi] size);
}
printf(" \n now Free space list of blocks is \n");
int free=(
fort
printf(" Block id: %d Block size %d_ block index %d \n", bls[i].bid, bls[
bls[i].index);
+
int sid, count=0;
for(i=O;i
int main {
int np, nr, temp, templ;
printf("enter number of resources: ");
scanf("Yod", Sent);
printf("enter number of proces
scanf("%d", &np);
int rag(nr+p][nrinp];
inti j
for(i=Q:isnpinrsi++) {
for(i=0; jInt
main() {
intn, m, i, j, kin = 5;
m=3;
intalloc[5][3
intmax[5][3]
intavail[3] = { 3, 3,2};
intffn}, ans[n}, ind = 0;
for(k = 0; k availfj)){
flag = Isbreak;
a
iffflag == 0) {
ansfind+] =
forty = 0; y4)
availly] += alloctillyl;
fliJ=15
3b od
printf("Following is the SAFE Sequence\n");for(i
=0;i", ansfil);
printf(" P%d", ans{n - 1);
return0;
}
Input: Program 9:
Process | Allocation Available
ABC
a3 2
Pe
Output:
Following is the SAFE
SequencePl => P3 =>
Po>P2EXPERIMENT - 10
Objective - Conversion of resource allocation graph (RAG) to wait for graph (WEG) for each type of
methodused for storing graph.
‘Theory -
Wait-for-Graph (WFG)
‘A Wait-For Graph (WEG) is the same as the SRAG with the resource elements stripped
out. Example:
Figure: Resource Allocation Graph
Figure: Equivalent Wait for graph of above resource allocation graph
10. Example program for Implementation of conversion of resource allocation geaph (RAG) to wait
forgraph (WFG)
itincludeint main(){
int np, nr, temp, temp;
printf("enter number of resources: ");
seanf{"%od", Snr);
printf("enter number of process: ")
scanf("%d", &np);
int rag(ne+np][nr+np):int i,
jj; forli-O;i