Os Edited Manual
Os Edited Manual
Experiment-6
Algorithm – monitor
ForkMonitor: integer
array[0..4]
fork ← [2,2,2,2,2]
condition array[0..4]OKtoEat
operation takeForks(integer i)
if(fork[i]!=2)
waitC(OKtoEat[i])
operation releaseForks(integer i)
fork[i+1] <- fork[i+1]+1 fork[i-
1] <- fork[i-1]
if(fork[i+1]==2) signalC(OKtoEat[i+1])
if(fork[i-1]==2) signalC(OKtoEat[i-1])
For each Philosopher – loop
forever :
p1 : think
p2 : takeForks(i)
p3 : eat
p4 : releaseForks(i)
Here Monitor will ensure all such needs mentioned above.
Implementation in C++ :
Name RA2311003030118
#define N 10
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N
class monitor {
public:
Test for the desired condition
i.e. Left and Right philosopher are not reading
void test(int phnum)
{
if (state[(phnum + 1) % 5] != EATING
and state[(phnum + 4) % 5] != EATING
and state[phnum] == HUNGRY) {
state[phnum] = EATING;
pthread_cond_signal(&phcond[phnum]);
}
}
pthread_mutex_lock(&condLock);
Name RA2311003030118
Indicates it is hungry
state[phnum] = HUNGRY;
pthread_mutex_unlock(&condLock);
}
pthread_mutex_lock(&condLock);
test(RIGHT);
test(LEFT);
pthread_mutex_unlock(&condLock);
}
constructor monitor()
{
pthread_mutex_init(&condLock, NULL);
}
destructor
~monitor()
{
pthread_mutex_destroy(&condLock);
}
}
int main()
{
Declaration... pthread_t
thread_id[N];
pthread_attr_t attr;
Initialization...
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_JOINABLE);
Creating...
for (int i = 0; i < N; i++) {
pthread_create(&thread_id[i], &attr, philosopher,
&phil[i]);
cout << "Philosopher " << i + 1 << " is thinking..."
<< endl;
}
Joining....
for (int i = 0; i < N; i++) {
pthread_join(thread_id[i], NULL);
}
Destroying
pthread_attr_destroy(&attr);
Name RA2311003030118
pthread_exit(NULL);
return 0;
}
Output :
Experiment-7
Name RA2311003030118
AIM : To implement Bankers Algorithm for Deadlock avoidance
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 's-state' check to test for
possible activities, before deciding whether allocation should be allowed to continue.
The Banker's algorithm got its name because it could be used in a banking system to ensure that the bank never
allocated its available cash in such a way that it could no longer satisfy the needs of all its customers.
Properties -
Multiple in
stances
Each process must a priori claim maximum use
When a process requests a resource it may have to wait
When a process gets all its resources it must return them in a finite amount of time
PROGRAM-
#include<bits/stdc++.h>
using namespace std;
int allocation[10][3],need[10][3],Max[10]
[3],available[10][3]; int p,current[3]; bool
executed[10],come; void IMP(){
come=false;
for (int i = 0; i < 10; ++i)
{
executed[i]=false;
}
} void
Calculate(){
IMP();
int i,j;
for (i = 0; i < p; ++i)
{
for (j = 0; j < p; ++j)
{
while(executed[j] && j<p-1){
j++;
}
if (need[j][0]<=current[0]&&need[j][1]<=current[1]&&need[j][2]<=current[2])
{
if (!executed[j])
{
executed[j]=true;
current[0]+=allocation[j][0];current[1]+=allocation[j][1];current[2]+=allocation[j][2];
cout<<"\nProcess P"<<j+1;
cout<<"\nCurrent: "<<current[0]<<" "<<current[1]<<"
"<<current[2]<<"\n";
cout<<"\nProcess executed without deadlock";
Name RA2311003030118
come=true;
break;
}
}
}
if (!come)
{
cout<<"\n Dead lock\n\n";
break;
}else{
come=false;
}
}
}
}
else{
break;
}
Calculate();
}
return 0;
}
Output:
Experiment-8
Name RA2311003030118
ALGO FCFS
1- Input the processes along with their burst time (bt).
2- Find waiting time (wt) for all processes. 3- As first process that
comes need not to wait so waiting time for process 1 will be 0 i.e.
wt[0] = 0.
4- Find waiting time for all other processes i.e. for
process i -> wt[i] = bt[i-1] + wt[i-1] .
5- Find turnaround time = waiting_time + burst_time
for all processes.
6- Find average waiting time =
total_waiting_time / no_of_processes.
7- Similarly, find average turnaround time =
total_turn_around_time / no_of_processes.
PROGRAM:
#include<iostream>
using namespace std;
findavgTime(processes, n, burst_time);
return 0;
}
OUTPUT
Name RA2311003030118
ALGO SJF:
● Then, we will choose the process with the shortest burst time and will execute the first process.
● We will check that if both processes have the same burst time length then the FCFS scheduling algorithm is used.
● We will make the average waiting time for the next process.
● We will begin with the first process, make the above selection, and place the other processes in a queue.
● Likewise, we will run all the steps until all the processes are executed.
● PROGRAM:-
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
{ pos
=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
Experiment-9
AIM: Write a program of Priority and Round robin scheduling
Program:
#include<iostream>
using namespace std; int
main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total Number of Process:"; cin>>n;
cout<<"\nEnter Burst Time and Priority\n";
for(i=0;i<n;i++)
{
cout<<"\nP["<<i+1<<"]\n";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1; contains process number
}
sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{ pos
=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
calculate waiting time
for(i=1;i<n;i++)
{ wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
cout<<"\nProcess\t Burst Time \tWaiting Time\tTurnaround Time"; for(i=0;i<n;i+
+)
{ tat[i]=bt[i]+wt[i]; //calculate turnaround
time total+=tat[i];
cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
Name RA2311003030118
}
avg_tat=total/n; //average turnaround time cout<<"\n\nAverage
Waiting Time="<<avg_wt;
cout<<"\nAverage Turnaround Time="<<avg_tat;
return 0;
PROGRAM
C program for FIFO page replacement algorithm
#include<stdio.h>
int main() {
int incomingStream[] = {4, 1, 2, 4, 5};
int pageFaults = 0; int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
Experiment-11
Name RA2311003030118
AIM: LRU and LFU Page Replacement Algorithm
Program:
#include<stdio.h>
#include<limits.h>
return 0;
}
int main()
{
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3; int queue[n]; int distance[n]; int
occupied = 0;
int pagefault = 0;
printFrame(queue, occupied);
}
else{
Name RA2311003030118
if(queue[j] == incomingStream[k])
break;
}
printf("\n");
}
return 0;
}
OUTPUT
Page Frame1 Frame2 Frame3
1: 1
2: 1 2
3: 1 2 3
2: 1 2 3
1: 1 2 3
5: 1 2 5
2: 1 2 5
1: 1 2 5
6: 1 2 6
2: 1 2 6
5: 5 2 6
6: 5 2 6
3: 5 3 6
1: 1 3 6
3: 1 3 6
Page Fault: 8
Experiment -12
AIM : Write a program of Best fit and Worst fit memory management policies
return 0 ;
OUTPUT:
Process No. Process Size Block no.
1 10 2
2 30 1
3 60 4
4 30 4
return
0; }
Output
Process No. Process Size Block no.
1 40 4
2 10 1
3 30 2
4 60 Not Allocated
Experiment-13
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
OUTPUT
It is also called as Elevator Algorithm. In this algorithm, the disk arm moves into a particular direction till the end,
satisfying all the requests coming in its path, and then it turns backend moves in the reverse direction satisfying
requests coming in its path.
It works in the way an elevator works, elevator moves in a direction completely till the last floor of that direction and
then turns back.
Example:- Given the following queue -- 95, 180, 34, 119, 11, 123, 62, 64 with the Read-write head initially at the
track 50 and the tail track being at 199. head movement is towards low value.
#include<stdio.h>
#include<stdlib.h>
int main() {
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\
n"); for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\
n"); scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
}
}
int index;
for(i=0;i<n;i++)
{ if(initial<RQ
[i])
{ index
=i;
break;
}
}
}
}
if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
last movement for min size
Name RA2311003030118
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-
0); initial =0; for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
Output:-
Enter the number of Request
8
Enter the Requests Sequence 95 180 34 119 11 123 62 64
Enter initial head position
50
Enter total disk size
200
Enter the head movement direction for high 1 and for low 0
1
Total head movement is 337
Experiment-14
Program Output:
Name RA2311003030118