19BCS1762 Experiment1,2,3,4,5,6
19BCS1762 Experiment1,2,3,4,5,6
1. FCFS CPU SCHEDULING ALGORITHM :- For FCFS scheduling algorithm, read the
number of processes/jobs in the system, their CPU burst times. The scheduling is
performed on the basis of arrival time of the processes irrespective of their other
parameters. Each process will be executed according to its arrival time. Calculate the
waiting time and turnaround time of each of the processes accordingly.
2. SJF CPU SCHEDULING ALGORITHM :- For SJF scheduling algorithm, read the number
of processes/jobs in the system, their CPU burst times. Arrange all the jobs in order
with respect to their burst times. There may be two jobs in queue with the same
execution time, and then FCFS approach is to be performed. Each process will be
executed according to the length of its burst time. Then calculate the waiting time
and turnaround time of each of the processes accordingly.
3. ROUND ROBIN CPU SCHEDULING ALGORITHM :- For round robin scheduling
algorithm, read the number of processes/jobs in the system, their CPU burst times,
and the size of the time slice. Time slices are assigned to each process in equal
portions and in circular order, handling all processes execution. This allows every
process to get an equal chance. Calculate the waiting time and turnaround time of
each of the processes accordingly.
4. PRIORITY CPU SCHEDULING ALGORITHM :- For priority scheduling algorithm, read
the number of processes/jobs in the system, their CPU burst times, and the priorities.
Arrange all the jobs in order with respect to their priorities. There may be two jobs in
queue with the same priority, and then FCFS approach is to be performed. Each
process will be executed according to its priority. Calculate the waiting time and
turnaround time of each of the processes accordingly.
PSEUDO CODE/ALGORITHMS/FLOWCHART/STEPS:
1. FCFS CPU SCHEDULING ALGORITHM:
Assigner (R(ti), RSP, MSP)
r listß RSP (R(ti))
for each r belongs to r list do
Mß MSP (r,M,e)
if M= NULL then
assign(r,M)
end if
end for
2. SJF CPU SCHEDULING ALGORITHM
Sort all the processes according to the arrival time. Then select that process that has
minimum arrival time and minimum Burst time.
After completion of the process make a pool of processes that arrives afterward till the
completion of the previous process and select that process among the pool which is having
minimum Burst time.
3. ROUND ROBIN CPU SCHEDULING ALGORITHM
Create an array rem_bt[] to keep track of remaining burst time of processes. This array is
initially a copy of bt[] (burst times array) Create another array wt[] to store waiting times of
processes. Initialize this array as 0.
Initialize time : t = 0 Keep traversing all the processes while they are not done. Do following
for i’th process if it is not done yet.
If rem_bt[i] > quantum
t = t + quantum
rem_bt[i] -= quantum;
Else // Last cycle for this process
t = t + rem_bt[i];
wt[i] = t – bt[i]
{
wt[0] = 0;
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}
void findTurnAroundTime( int processes[], int n,int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
}
cout << "Average waiting time = "<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "<< (float)total_tat / (float)n;
}
int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
//Burst time of all processes
int burst_time[] = {24,3,3};
findavgTime(processes, n, burst_time);
return 0;
}
2. SJF CPU SCHEDULING ALGORITHM
#include <stdio.h>
int main()
{
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process:
"); scanf("%d", &n);
printf("Enter Burst Time:\n");
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT
TAT\n"); for (i = 0; i < n;
i++) { A[i][3] = A[i][1] +
A[i][2];
total += A[i][3];
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
while (1)
{
else
{
}
}
}
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
if (done == true)
break;
}
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime(int processes[], int n, int bt[],int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "PN\t "<< " \tBT "<< " WT " << " \tTAT\n";
for (int i=0; i<n; i++)
{
}
cout << "Average waiting time = "<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "<< (float)total_tat / (float)n;
}
int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {24, 3, 3};
int quantum = 3;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
int at,bt,pr,pno;
};
process proc[50];
bool comp(process a,process b)
{if(a.at == b.at)
{
return a.pr<b.pr;
}
else
{
return a.at<b.at;
}
}
void get_wt_time(int wt[])
{
int service[50];
service[0] = proc[0].at;
wt[0]=0;
for(int i=1;i<totalprocess;i++)
{
service[i]=proc[i-1].bt+service[i-1];
wt[i]=service[i]-proc[i].at;
if(wt[i]<0)
{
wt[i]=0;
}
}}
void get_tat_time(int tat[],int wt[])
{for(int i=0;i<totalprocess;i++)
{ tat[i]=proc[i].bt+wt[i];
}}
void findgc()
{int wt[50],tat[50];
double wavg=0,tavg=0;
get_wt_time(wt);
get_tat_time(tat,wt);
int stime[50],ctime[50];
stime[0] = proc[0].at;
ctime[0]=stime[0]+tat[0];
for(int i=1;i<totalprocess;i++)
{ stime[i]=ctime[i-1];
ctime[i]=stime[i]+tat[i]-wt[i]
;
}
cout<<"Process_no\tStart_time\tComplete_time\tTurn_Around_Time\tWaiting_Time"<<e
n dl;
for(int i=0;i<totalprocess;i++)
{ wavg += wt[i];
tavg += tat[i];
cout<<proc[i].pno<<"\t\t"<< stime[i]<<"\t\t"<<ctime[i]<<"\t\t"<<
tat[i]<<"\t\t\t"<<wt[i]<<endl;
}
cout<<"Average waiting time is : ";
cout<<wavg/(float)totalprocess<<endl;
cout<<"average turnaround time : ";
cout<<tavg/(float)totalprocess<<endl;
}
int main()
{int arrivaltime[] = { 1, 2, 3, 4, 5 };
int bursttime[] = { 10, 1, 2, 1, 5 };
int priority[] = { 3, 4, 1, 7, 8 };
for(int i=0;i<totalprocess;i++)
{ proc[i].at=arrivaltime[i];
proc[i].bt=bursttime[i];
proc[i].pr=priority[i];
proc[i].pno=i+1;
}
sort(proc,proc+totalprocess,comp);
findgc();
return 0;}
OUTPUT:
1. FCFS CPU SCHEDULING ALGORITHM
2. SJF CPU SCHEDULING ALGORITHM
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces" "item %d",x);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes " "item %d", x);
x--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
for (i = 1; i > 0; i++) {
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)
else {
printf("Buffer is full!");
}
break;
case 2:
if ((mutex == 1)
&& (full != 0)) {
consumer();
}
}
break; case 3:
exit(0);
break;
}
}
}
OUTPUT:
Experiment:3
char name[20];
int sb, nob;
}
ft[30];
void main()
{
int i, j, n; char s[20];
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("Enter starting block of file %d :",i+1);
scanf("%d",&ft[i].sb);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
}
break;
if(i==n)
printf("\nFile Not Found");
else
{
}
getch();
}
OUTPUT:
char name[20];
int nob; struct block *sb;
}
ft[30];
struct block
{
int bno;
struct block *next;
};
void main()
{
int i, j, n;
char s[20];
struct block *temp;
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
{
printf("%d ",temp->bno);
temp = temp->next;
}
}
}
Output:
{
char name[20];
int nob, blocks[30];
}
ft[30];
void main()
{
int i, j, n;
char s[20];
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
}
getch();
}
OUTPUT:
Experiment:4
}
SOURCE CODE :
#include <iostream>
#include <stdlib.h>
//For exit() function
#define N 5
//No. of philosophers=5
using namespace std;
int semaphore[]={1, 1, 1, 1, 1};
}
//signal function to increment semaphore0 by 1
int signal_(int y){
return (y+1);
}
//Philosopher function for evaluating whether philosopher is eating or
thinking void philosopher(int i0){
wait(semaphore[i0]);
wait(semaphore[(i0+1)]%N);
cout<<"Philosopher p"<<i0<<" is eating"<<endl;
signal_(semaphore[i0]);
signal_(semaphore[(i0+1)%N]);
}
if(i1==i0+2 || i1==i0+3){
cout<<"Philosopher p"<<i0<<" and p"<<i1<<" are eating"<<endl;
}
else{
cout<<"Philosopher p"<<i0<<" is eating and p"<<i1<<" is thinking"<<endl;
}
}
//3 philosophers but only 2, 1 and 0 philososphers can eat at a time
void philosopher(int i0, int i1, int i2){
if(i1==i0+2 || i1==i0+3){
//Driver code
int main(void)
{
int x=0;
//Selection variable for menu driven program
int i0, i1, i2, i3, i4;
//philosopher func parameters
cout<<"Enter the no. of philosophers or condition you want to execute for dining
philosophers problem:";
cout<<"\n1. 1 philosopher";
cout<<"\n2. 2 philosophers";
cout<<"\n3. 3 philosophers";
cout<<"\n4. 4 philosophers";
cout<<"\n5. Deadlock
condition";
cout<<"\n6. Exit"<<endl;
// Banker's Algorithm
#include <iostream>
using namespace std;
int main()
{
// Allocation Matrix
{ 2, 0, 0 },
// P1
{ 3, 0, 2 },
// P2
{ 2, 1, 1 },
// P3
{ 0, 0, 2 } };
// P4
int max[5][3] = { { 7, 5, 3 },
// P0
// MAX Matrix
{ 3, 2, 2 },// P1
{ 9, 0, 2 }// P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4
int avail[3] = { 3, 3, 2 }; // Available Resources
int f[n], ans[n], ind = 0;
for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] >
avail[j]){
flag = 1;
break;
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;
// To check if sequence is safe or not
for(int i = 0;i<n;i++)
{
if(f[i]==0)
{
flag = 0;
cout << "The given sequence is not safe";
break;
}
if(flag==1)
{
cout << "Following is the SAFE Sequence" << endl;
for (i = 0; i < n - 1; i++)
cout << " P" << ans[i] << " ->";
cout << " P" << ans[n - 1] <<endl;
}
return (0);
}
Output:
Experiment:6
One of the responsibilities of the operating system is to use the hardware efficiently. For the
disk drives, meeting this responsibility entails having fast access time and large disk
bandwidth. Both the access time and the bandwidth can be improved by managing the
order in which disk I/O requests are serviced which is called as disk scheduling. The simplest
form of disk scheduling is, of course, the first-come, first-served (FCFS) algorithm. This
algorithm is intrinsically fair, but it generally does not provide the fastest service. In the
SCAN algorithm, the diskarm starts at one end, and moves towards the other end, servicing
requests as it reaches each cylinder, until it get sto the other end of the disk. At the other
end, the direction of head movement is reversed, and servicingcontinues.The head
continuously scans back and forth across the disk. C-SCAN is a variant of SCAN designed to
provide amore uniform wait time. Like SCAN, C-SCAN moves the head from one end of the
disk to the other, servicing requests along the way. When the head reaches the other end,
however, it immediately returns to the beginning of the disk without servicing any requests
on the return trip.
CODE;
#include<stdio.h>
#include<conio.h>
void main(){
int t[20], n, i, j, tohm[20], tot=0;
float avhm;clrscr();
printf("enter the no.of tracks");
scanf("%d",&n);
printf("enter the tracks to be traversed");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0) tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no ofresource
classes:"); scanf("%d",&r);
printf("Enter the total existed resource in each
class:"); for(k=1; k<=r; k++)
scanf("%d",&totext[k]);
if(i!=p)
{
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}
}
block[p]=0;
run[p]=0;
for(k=1; k<=r; k++)
j=0;
for(i=1; i<=n; i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1; i<=n; i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1; k<=r; k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
{
active[i]=0;
for(k=1; k<=r; k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1; k<=r; k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
else
{
for(k=1; k<=r; k++)
{
resalloc[p][k]=newreq[k]
; totalloc[k]=newreq[k];
}