0% found this document useful (0 votes)
70 views30 pages

OS Lab Manual

The document describes a C++ program to simulate the First-Come-First-Serve (FCFS) CPU scheduling algorithm. It includes classes to represent job entries, wait times, and average wait time calculation. The program accepts job burst times as input, calculates waiting times for each job, and outputs a Gantt chart and average waiting time. The document also briefly describes Round Robin, Shortest Job First, and Priority scheduling algorithms and includes their program structures.

Uploaded by

Fahad Abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views30 pages

OS Lab Manual

The document describes a C++ program to simulate the First-Come-First-Serve (FCFS) CPU scheduling algorithm. It includes classes to represent job entries, wait times, and average wait time calculation. The program accepts job burst times as input, calculates waiting times for each job, and outputs a Gantt chart and average waiting time. The document also briefly describes Round Robin, Shortest Job First, and Priority scheduling algorithms and includes their program structures.

Uploaded by

Fahad Abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

OPERATING SYSTEM

Museb Khalid
[email protected]

LAB MANUAL
CPU Scheduling Algorithm - First-Come-First-Serve

Aim: To develop a C++ program for the simulation of First-Come-FirstServe

Description: FIFO is an acronym for First In, First Out, an abstraction in ways of organizing
and manipulation of data relative to time and prioritization. This expression describes the
principle of a queue processing technique or servicing conflicting demands by ordering process
by firstcome, first-served (FCFS) behavior: what comes in first is handled first, what comes in
next waits until the first is finished, etc.

Program:

#include<iostream.h>
#include<conio.h>

class FCFS
{
int b_time[10]; int w_time[10]; int n_jobs; float avg;
public:
FCFS()
{
n_jobs = 0; avg = 0;
}
void Job_Entry(int n)
{
n_jobs = n;
cout<<"\nEnter the Burst Time of each Job :";
cout<<"\n";
for(int i = 0;i < n_jobs;i++)
{ cout<<"\nJob "<<i+1<<": "; cin>>b_time[i];
}
}

void Wait_Time(void)
{
for(int i = 0;i < n_jobs; i++)
{ w_time[i] = 0;
for(int j = 0; j < i;j++)
{
w_time[i] = w_time[i] + b_time[j];
}
avg = avg + w_time[i];
} avg = avg/n_jobs;

/* Printing in Output Console */


cout<<"\n\t\t\t\tGantt chart\n";
cout<<"\nBurst Time \t\t|"; for(i = 0;i < n_jobs;i++)
cout<<" "<<b_time[i]<<" |"; cout<<"\nWaiting Time\t\t";
for(i = 0;i < n_jobs;i++)
cout<<w_time[i]<<" ";
cout<<"\n\nThe Average Waiting Time is : "<<avg<<" ms";
}
};

int main()
{
FCFS F;

int n; clrscr();
cout<<"\n\n\t\t\tFirst Come First Served (FCFS)"; cout<<"\n\nHow many Jobs going to be
Entered : "; cin>>n; F.Job_Entry(n); F.Wait_Time(); getch();
}

Result:
Thus the program for the simulation of First-Come-First-Serve has been developed and
executed successfully.

Output:

First Come First Serve (FCFS)

How many Jobs going to be Entered : 3


Enter the Burst Time of each Job :

Job 1: 12

Job 2: 10

Job 3: 4

Gantt chart

Burst Time | 12 | 10 | 4 |
Waiting Time 0 12 22

The Average Waiting Time is: 11.333333 ms

CPU Scheduling Algorithm - Round Robin

Aim: To develop a C++ Program for the simulation of Round Robin Scheduling
Round-robin (RR) is one of the simplest scheduling algorithms for processes in an operating
system, which assigns time slices to each process in equal portions and in order, handling all
processes without priority. Round-robin scheduling is both simple and easy to implement, and
starvation-free. Round robin scheduling can also be applied to other scheduling problems, such
as data packet scheduling in computer networks.
DESCRIPTION:
To aim is to calculate the average waiting time. There will be a time slice, each process should
be executed within that time-slice and if not it will go to the waiting state so first check
whether the burst time is less than the time-slice. If it is less than it assign the waiting time to
the sum of the total times. If it is greater than the burst-time then subtract the time slot from the
actual burst time and increment it by time-slot and the loop continues until all the processes are
completed.
ALGORITHM:
 Step 1: Start the process
 Step 2: Accept the number of processes in the ready Queue and time quantum (or) time
slice
 Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
 Step 4: Calculate the no. of time slices for each process where No. of time slice for
process (n) = burst time process (n)/time slice
 Step 5: If the burst time is less than the time slice then the no. of time slices =1.
 Step 6: Consider the ready queue is a circular Q, calculate a) Waiting time for process
(n) = waiting time of process(n-1)+ burst time of process(n-1 ) + the time difference in
getting the CPU from process(n-1) b) Turnaround time for process(n) = waiting time of
process(n) + burst time of process(n)+ the time difference in getting CPU from
process(n).
 Step 7: Calculate c) Average waiting time = Total waiting Time / Number of process d)
Average Turnaround time = Total Turnaround Time / Number of process Step
 8: Stop the process

Program:

#include <iostream>
using namespace std;
int main()
{
int i, j, n, bu[10], wa[10], tat[10], t, ct[10], max;
float awt = 0, att = 0, temp = 0;
cout << " Enter the no of processes";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Enter Burst Time for process";
cin >> bu[i];
ct[i] = bu[i];
}
cout << " Enter the size of time slice ";
cin >> t;
max = bu[0];
for (i = 1; i < n; i++)
if (max < bu[i])
max = bu[i];
for (j = 0; j < (max / t) + 1; j++)
for (i = 0; i < n; i++)
if (bu[i] != 0)
if (bu[i] <= t)
{
tat[i] = temp + bu[i];
temp = temp + bu[i];
bu[i] = 0;
}
else
{
bu[i] = bu[i] - t;
temp = temp + t;
}
for (i = 0; i < n; i++)
{
wa[i] = tat[i] - ct[i];
att += tat[i];
awt += wa[i];
}

cout << "Average Turnaround time is" << att << "\n";

cout << "\nThe Average Waiting time is " << awt << "\n";
cout << "\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n";
for (i = 0; i < n; i++)
{
cout << "\n"
<< "\t" << i + 1 << "\t" << ct[i] << "\t" << wa[i] << "\t" << tat[i];
}
}

CPU Scheduling – Shortest Job First (Non-Preemptive)

Aim: To develop a C++ Program for the simulation of Shortest Job First (SJF) - Non
Preemptive
Description: Scheduling is a key concept in computer multitasking and multiprocessing
operating system design, and in real-time operating system design. It refers to the way processes
are assigned priorities in a priority queue. This assignment is carried out by software known as a
scheduler.
Shortest job first (SJF) (also known as Shortest Job Next (SJN)) is a scheduling policy that
selects the waiting process with the smallest execution time to execute next. Shortest job next is
advantageous because of its simplicity and because it maximizes process throughput (in terms of
the number of processes run to completion in a given amount of time). However, it has the
potential for process starvation for processes which will require a long time to complete if short
processes are continually added. Highest response ratio next is similar but provides a solution to
this problem.

Program:
#include<iostream.h>
#include<conio.h>

class SJF
{
int b_time[10]; int w_time[10]; int n_jobs; float avg;
public:
SJF()
{
n_jobs = 0; avg = 0;
}
void Job_Entry(int n)
{
n_jobs = n;
cout<<"\nEnter the Burst Time of each Job :"; cout<<"\n";
for(int i = 0;i < n_jobs;i++)
{ cout<<"\nJob "<<i+1<<": "; cin>>b_time[i];
}
Sorting(b_time,n);
}

void Sorting(int b_time[],int n)


{
for(int i = 0;i < n; i++)

{
for(int j = i+1; j < n; j++)
{
if(b_time[j] < b_time[i])
{
int temp = b_time[i];
b_time[i] = b_time[j];
b_time[j] = temp;
}
}
}
}
void Wait_Time(void)
{
/* Calculation */
for(int i = 0;i < n_jobs; i++)
{ w_time[i] = 0;
for(int j = 0; j < i;j++)
{
w_time[i] = w_time[i] + b_time[j];
}
avg = avg + w_time[i];
} avg = avg/n_jobs;

/* Printing in Output Console */ cout<<"\n\t\t\t\tGantt chart\n";


cout<<"\nBurst Time \t\t|"; for(i = 0;i < n_jobs;i++)
cout<<" "<<b_time[i]<<" |";
cout<<"\nWaiting Time\t\t"; for(i = 0;i < n_jobs;i++)
cout<<w_time[i]<<" ";
cout<<"\n\nThe Average Waiting Time is : "<<avg<<" ms";
}
};

int main()
{
SJF S;
int n; clrscr();
cout<<"\n\n\t\tShortest Job First (SJF) - Non Preemptive"; cout<<"\n\nHow many Jobs
going to be Entered : "; cin>>n; S.Job_Entry(n);
S.Wait_Time();
getch();
}
Result:
Thus the program for Simulation of SJF-non-preemptive has been developed successfully and
executed

Output:

Shortest Job First (SJF) - Non Preemptive

How many Jobs going to be Entered : 3

Enter the Burst Time of each Job :

Job 1: 10

Job 2: 5

Job 3: 15

Gantt chart

Burst Time | 5 | 10 | 15 |
Waiting Time 0 5 15

The Average Waiting Time is : 6.666667 ms

CPU Scheduling Algorithm – Priority

Aim: To develop a C++ program for the simulation of Priority Scheduling

Description: Scheduling is a key concept in computer multitasking and multiprocessing


operating system design, and in real-time operating system design. It refers to the way processes
are assigned priorities in a priority queue. This assignment is carried out by software known as a
scheduler.
In case of priority scheduling, each task/ process will be given some priority and based on this
priority, the scheduling mechanism will be done. For example, consider two processes – process-
1 and process-2. Process-1 will perform the scanning of system and Process-2 will perform the
printing of a file. Among these two processes, the Process-1 will be given priority and will be
scheduled first because of the prime importance for process-1 compared to process-2

Program:

#include<iostream.h>
#include<conio.h>
class Priority
{
public:
int p[25],ser[25],pri[25],pri1[25],wait[25],i,j,n;
double awt,tat,s; void Get()
{
cout<<"\nEnter the Number of Jobs : "; cin>>n;
for(i = 0;i < n;i++)
{
cout<<"\nEnter the Burst time for the Process
P["<<i+1<<"] : ";
cin>>ser[i];
cout<<"\nEnter the Priority for Process P["<<i+1<<"] : ";
cin>>pri[i];

pri1[i]=pri[i];
p[i]=i;
}
}
void Calculate()
{
int temp;
for(i = 0;i < n;i++)
{
for(j = 0;j <= (n-i-1);j++)
{
if(pri[j] >= pri[j+1])
{
temp = pri[j]; pri[j] =
pri[j+1]; pri[j+1] = temp;
}
}
}
for(i = 0;i < n;i++) for(j = 0;j < n;j++)
{
if(pri[i] == pri1[j])
{

if(i == 0)
{
wait[i] = 0; awt=0;
tat = ser[i]; temp=ser[j];
}
else
{
wait[i]=wait[i-1]+temp;
temp=ser[j];
awt +=wait[i];
tat +=ser[i]+wait[i];
}
}
}
awt=awt/n; tat=tat/n;
}

void Put()
{
wait[0]=0;
cout<<"\n\nScheduling Order";
cout<<"\nPID"<<"\tPriority"<<"\tService Time"<<"\tWait time";

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(pri[i] == pri1[j])
cout<<"\n"<<p[j]+1<<"\t"<<pri[i]<<"\t\t"<<ser[j]
<<"\t\t"<<wait[i];
}
}

cout<<"\n\nAverage Waiting Time : "<<awt<<" ms";


cout<<"\nAverage Turnaround Time : "<<tat<<" ms";
}
};

void main()
{
Priority P; clrscr(); P.Get();
P.Calculate(); P.Put(); getch();
}

Result:
Thus the program for simulation of Priority Scheduling has been developed successfully and
executed
Output:

Enter the Number of Jobs : 4

Enter the Burst time for the Process P[1] : 23

Enter the Priority for Process P[1] : 4

Enter the Burst time for the Process P[2] : 20

Enter the Priority for Process P[2] : 2

Enter the Burst time for the Process P[3] : 10

Enter the Priority for Process P[3] : 1

Enter the Burst time for the Process P[4] : 12


Enter the Priority for Process P[4] : 3

Scheduling Order
PID Priority Service Time Wait time
3 1 10 11896
2 2 20 11906
4 3 12 11926

Average Waiting Time : 8932 ms


Average Turnaround Time : 8942.5 ms

Banker’s Algorithm for Deadlock Avoidance

Aim: To develop a C++ program for the simulation of Banker’s Algorithm


Description: The Banker's algorithm is a resource allocation & deadlock avoidance algorithm
developed by Edsger Dijkstra that tests for safety by simulating the allocation of pre-determined
maximum possible amounts of all resources, and then makes a "safe-state" check to test for
possible deadlock conditions for all other pending activities, before deciding whether allocation
should be allowed to continue.

Program:
#include<iostream.h>
#include<conio.h>

class Banker
{
private:
int allocated[50][50],max[50][50],available[50]; int need[50][50];
int flag[50]; int m,n; int i,j;
public:

Banker()
{
cout<<"\nEnter the Row for Allocation and Maximum Matrix :
";
cin>>m;
cout<<"\nEnter the Column for Allocation and Maximum
Matrix : ";
cin>>n;
cout<<"\nAllocation Matrix ["<<m<<" X "<<n<<"]\n"; for(i = 0;i < m;i++) for(j
= 0;j < n;j++) cin>>allocated[i][j];

cout<<"\nMaximum Matrix ["<<m<<" X "<<n<<"]\n"; for(i = 0;i <


m;i++) for(j = 0;j < n;j++)
cin>>max[i][j];

cout<<"\nAvailable\n"; for(i = 0;i < n;i++)


{
cin>>available[i];
}

void MakeNull()
{
for(i = 0;i < 50;i++) flag[i] = 0;
}

void Need()
{
cout<<"\nNeed Matrix ["<<m<<" X "<<n<<"]\n"; for(i = 0;i < m;i++)
{
for(j = 0;j < n;j++)
{
need[i][j] = max[i][j] - allocated[i][j];
cout<<need[i][j]<<"\t";
}
cout<<"\n";
}
}
int Check(int column)
{
for(i = 0;i < n;i++)
if(need[column][i] > available[i])
return 0; return 1;
}

void AddResource(int column)


{
for(int i = 0;i < n;i++) available[i] = available[i] +
allocated[column][i];
}

void Process()
{
int t,k,l; int temp = 0;
MakeNull();

cout<<"\nSafety Sequence :"; for(k = 0;k < 3;k++)


{
temp = 0;
for(l = 0;l < m;l++)
{
if(flag[l] == 0)
{
temp = 1;
if(Check(l) == 1)
{
AddResource(l);
flag[l] = 1; cout<<" P"<<l;
}
}
}
}
if(temp == 0) cout<<"\n\nSystem in Safe State";
else
cout<<"\n\nSystem in UnSafe State";
}
};

int main()
{
clrscr(); Banker B;
B.Need(); B.Process(); getch();

Result:

Thus the Program for Simulation of Banker’s algorithm is developed and executed
successfully.
Output:

Enter the Row for Allocation and Maximum Matrix : 3

Enter the Column for Allocation and Maximum Matrix : 3

Allocation Matrix [3 X 3]
100
010
0 01

Maximum Matrix [3 X 3]
1 11
111
111

Available

123

Need Matrix [3 X 3]
0 1 1
1 0 1
1 1 0

Safety Sequence : P0 P1 P2
System in Safe State

Page Replacement Algorithm – FIFO

Aim: To develop a C++ program for the simulation of FIFO Page Replacement Algorithm
Description: The first-in, first-out (FIFO) page replacement algorithm is a low-overhead
algorithm that requires little bookkeeping on the part of the operating system. The idea is
obvious from the name - the operating system keeps track of all the pages in memory in a
queue, with the most recent arrival at the back, and the earliest arrival in front. When a page
needs to be replaced, the page at the front of the queue (the oldest page) is selected. While FIFO
is cheap and intuitive, it performs poorly in practical application.
Program:

#include<iostream.h>
#include<conio.h>

class FIFO
{ private:
int n,m; int pf; int count;
int a[100],b[100][100];
public:
FIFO()
{
pf = 0; int i,j; i = j = 0; cout<<"\nEnter the No. Of Frame :";
cin>>m;
cout<<"\nEnter the Length of the Reference String :";
cin>>n;
cout<<endl;

for(i = 0;i < n;i++) for(j = 0;j < n;j++) b[i][j] = -1;

for(i = 0;i < n;i++)


{ cout<<"Enter the Page Frame ["<<i+1<<"] :"; cin>>a[i];
}
}

void CopyPrevious(int r)
{
for(int i = 0;i < m;i++) b[r][i] = b[r-1][i];
}

int Check(int r,int t)


{
for(int i = 0;i < m;i++) if(b[r][i] == t) return i;
return -1;

void Print(int r)
{
cout<<"\nReference String "; cout<<"<"<<a[r]<<">"<<"\t"; for(int i =
0;i < m;i++) if(b[r][i] == -1) cout<<"-\t";
else
cout<<b[r][i]<<"\t";
}

void Replace()
{
count = -1; int set = 0;

for(int i = 0;i < n;i++)


{
set = 0;
if(count == m-1)
count = -1;
if(i == 0)
{
pf++;
b[i][++count] = a[i]; set = 1;
}
else
{
CopyPrevious(i); if(Check(i,a[i]) == -1)
{
b[i][++count] = a[i]; pf++;
set = 1; }
}
Print(i);
if(set == 1) cout<<"Page Fault";
} cout<<"\n\nNumber of Page Fault :"<<pf;
}
};
int main()
{ clrscr();
cout<<"\t\t\t\tFIFO Page Replacement Algorithm\n";
FIFO F; F.Replace(); getch();
}

Result:
Thus the program for the simulation of FIFO Page Replacement has been developed and
executed successfully.
Output:

FIFO Page Replacement Algorithm

Enter the No. Of Frame :3

Enter the Length of the Reference String: 3

Enter the Page Frame [1] :12


Enter the Page Frame [2] :14
Enter the Page Frame [3] :3

Reference String <12> 12 - - Page Fault


Reference String <14> 12 14 - Page Fault
Reference String <3> 12 14 3 Page Fault
Number of Page Fault :3

Page Replacement Algorithm – LRU

Aim: To develop a C++ Program for the simulation of LRU page replacement

Description: In LRU page replacement, the page which has not been allocated very recently
will be allocated now for the process.

Program:

#include<iostream.h>
#include<conio.h>

void display(int ar[], int n)


{
for(int i=0;i<n;i++)
cout<<"\t"<<ar[i]; cout<<"\n";
}

void main()
{
int pg[50],fr[10],rnk[5],set,tmp,n,nf,i,j,k,flag,flag1; clrscr(); cout<<"\nEnter the
Length of the Reference String: "; cin>>n;

for(i=0;i<n;i++)
{
cout<<"\nEnter the Page Frame["<<i+1<<"] : "; cin>>pg[i];
}
cout<<"\nEnter the No. of Frames: "; cin>>nf;
for(i=0;i<nf;i++)
{
rnk[i]=0; fr[i]=-1;
}

cout<<"\n\n"; j=0; set=0; int r=0; for(i=0;i<n;i++)


{
flag=0;flag1=0; for(k=0;k<nf;k++)
{

if(fr[k]==pg[i])
flag=1;
}
if(flag==0)
{
if(set>0)
{
tmp=rnk[0]; for(k=1;k<nf;k++)
{
if(rnk[k]<tmp)
tmp=rnk[k];
}
for(k=0;k<nf;k++)
{
if(rnk[k]==tmp)
flag1=k;
}
fr[flag1]=pg[i];rnk[j]=r; r++;
}
else
{
fr[j]=pg[i];rnk[j]=r;
r++;
}
display(fr,nf);
}
else
{
cout<<"\n"; rnk[j]=r;r++;
}
j++;
if(j>=nf)
{
set=1; j=0;
}
}
getch();
}

Result:
Thus the program for the simulation of Page Replacement - LRU has been developed and
executed successfully.
Output:

Enter the Length of the Reference String: 3

Enter the Page Frame[1] : 128

Enter the Page Frame[2] : 100

Enter the Page Frame[3] : 200

Enter the No. of Frames: 3

128 -1 -1
128 100 -1
128 100 200

First Fit, Best Fit and Worst Fit Algorithm

MEMORY ALLOCATION TECHNIQUES


AIM: To Write a C program to simulate the following contiguous memory allocation
techniques a) Worst-fit b) Best-fit c) First-fit
DESCRIPTION: One of the simplest methods for memory allocation is to divide memory into
several fixed-sized partitions. Each partition may contain exactly one process. In this multiple-
partition method, when a partition is free, a process is selected from the input queue and is loaded
into the free partition. When the process terminates, the partition becomes available for another
process. The operating system keeps a table indicating which parts of memory are available and
which are occupied. Finally, when a process arrives and needs memory, a memory section large
enough for this process is provided. When it is time to load or swap a process into main memory,
and if there is more than one free block of memory of sufficient size, then the operating system
must decide which free block to allocate. Best-fit strategy chooses the block that is closest in size
to the request. First-fit chooses the first available block that is large enough. Worst-fit chooses
the largest available block.

PROGRAM

WORST-FIT
#include <iostream>
using namespace std;
#define max 25
int main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max], ff[max];
cout<<"\n\tMemory Management Scheme - First Fit";
cout<<"\nEnter the number of blocks:";
cin>>nb;
cout<<"Enter the number of files:";
cin>>nf;
cout<<"\nEnter the size of the blocks:-\n";
for (i = 1; i <= nb; i++)
{
cout<<"Block :"<< i;
cin>>b[i];
}
cout<<"Enter the size of the files :-\n";
for (i = 1; i <= nf; i++)
{
cout<<"File :"<< i;
cin>>f[i];
}
for (i = 1; i <= nf; i++)
{
for (j = 1; j <= nb; j++)
{
if (bf[j] != 1)
{
temp = b[j] - f[i];
if (temp >= 0)
{
ff[i] = j;
break;
}
}
}
frag[i] = temp;
bf[ff[i]] = 1;
}
cout<<"\nFile No\tFile Size \tBlock No\tBlock Size\tFragment";
for (i = 1; i <= nf && ff[i] != 0; i++)
{
cout<<"\n" <<i<<"\t"<<f[i]<<"\t"<<ff[i]<<"\t"<<b[ff[i]]<<"\t"<<frag[i];
}

}
INPUT
Enter the number of blocks: 3 Enter the number of files: 2
Enter the size of the blocks:- Block 1: 5 Block 2: 2 Block 3: 7
Enter the size of the files:- File 1: 1 File 2: 4
OUTPUT File No File Size Block No Block Size Fragment
1115424373

BEST-FIT
#include <iostream>
#define max 25
using namespace std;
int main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp, lowest = 10000;
static int bf[max], ff[max];
cout<<"\nEnter the number of blocks:";
cin>>nb;
cout<<"Enter the number of files:";
cin>>nf;
cout<<"\nEnter the size of the blocks:\n";
for (i = 1; i <= nb; i++)
{
cout<<"Block "<<":"<< i;
cin>>b[i];
}

cout<<"Enter the size of the files :-\n";


for (i = 1; i <= nf; i++)
{
cout<<"File "<<":"<< i;
cin>>f[i];
}
for (i = 1; i <= nf; i++)
{
for (j = 1; j <= nb; j++)
{
if (bf[j] != 1)
{
temp = b[j] - f[i];
if (temp >= 0)
if (lowest > temp)
{
ff[i] = j;
lowest = temp;
}
}
}
frag[i] = lowest;
bf[ff[i]] = 1;
lowest = 10000;
}
cout<<"\nFile No\tFile Size \tBlock No\tBlock Size\tFragment";
for (i = 1; i <= nf && ff[i] != 0; i++)
{
cout<<"\n" <<i<<"\t"<<f[i]<<"\t"<<ff[i]<<"\t"<<b[ff[i]]<<"\t"<<frag[i];
}
}

INPUT
Enter the number of blocks: 3 Enter the number of files: 2
Enter the size of the blocks:- Block 1: 5 Block 2: 2 Block 3: 7
Enter the size of the files:- File 1: 1 File 2: 4
OUTPUT File No File Size
Block No
Block Size
Fragment 1 1 2 2 1 2 4 1 5 1

FIRST-FIT
#include <iostream>
using namespace std;
#define max 25
int main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp, highest = 0;
static int bf[max], ff[max];

cout<<"\n\tMemory Management Scheme - Worst Fit";


cout<<"\nEnter the number of blocks:";
cin>>nb;
cout<<"Enter the number of files:";
cin>>nf;
cout<<"\nEnter the size of the blocks:-\n";
for (i = 1; i <= nb; i++)
{
cout<<"Block :"<< i;
cin>>b[i];
}
cout<<"Enter the size of the files :-\n";
for (i = 1; i <= nf; i++)
{
cout<<"File :"<< i;
cin>>f[i];
}

for (i = 1; i <= nf; i++)


{
for (j = 1; j <= nb; j++)
{
if (bf[j] != 1) //if bf[j] is not allocated
{
temp = b[j] - f[i];
if (temp >= 0)
if (highest < temp)
{
}
}
frag[i] = highest;
bf[ff[i]] = 1;
highest = 0;
}
ff[i] = j;
highest = temp;
}
cout<<"\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragement";
for (i = 1; i <= nf; i++)
{
cout<<"\n"<<"\t"<< i<<"\t"<< f[i]<<"\t"<< ff[i]<<"\t"<< b[ff[i]]<<"\t"<< frag[i];
}
}

INPUT
Enter the number of blocks: 3 Enter the number of files: 2
Enter the size of the blocks:- Block 1: 5 Block 2: 2 Block 3: 7
Enter the size of the files:- File 1: 1 File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment 1 1 3 7 6 2 4 1 5 1

Producer- Consumer Problem


AIM:

To implement producer/consumer problem

ALGORITHM:

1. Declare variable for producer & consumer.

2. Declare array to keep track of buffer.

3. Read number the items to be produced and consumed.

5. Define producer function.

6. Define consumer function.

7. Call producer and consumer.

8. Stop the execution.

PROGRAM: (PRODUCER-CONSUMER PROBLEM)

#include<iostream>
using namespace std;

int main()

int buffer[10], bufsize, in, out, produce, consume, choice=0;


in = 0; out = 0; bufsize = 10;
while(choice !=3)

cout<<"\n1. Produce \t 2. Consume \t3. Exit";

cout<<"\nEnter your choice: =";

cin>>choice;

switch(choice)

case 1: if((in+1)%bufsize==out)

cout<<"\nBuffer is Full";

else

{
cout<<"\nEnter the value: ";
cin>>produce;
buffer[in] = produce;
in = (in+1)%bufsize;

break;

case 2: if(in == out)

cout<<"\nBuffer is Empty";

else
{

consume = buffer[out];

cout<<"\nThe consumed value is"<< consume;

out = (out+1)%bufsize;

break;

} } }

OUTPUT:

1. Produce 2. Consume 3. Exit

Enter your choice: 2

Buffer is Empty

1. Produce 2. Consume 3. Exit

Enter your choice: 1

Enter the value: 100

1. Produce 2. Consume 3. Exit

Enter your choice: 2

The consumed value is 100

1. Produce 2. Consume 3. Exit

Enter your choice:

You might also like