0% found this document useful (0 votes)
16 views

OS

The document contains code snippets demonstrating different CPU scheduling algorithms: 1) FCFS - First Come First Serve scheduling is implemented, calculating waiting times and turnaround times. 2) SJF - Shortest Job First scheduling sorts processes by burst time and calculates metrics. 3) Round Robin scheduling implements a time quantum to rotate between processes, calculating average waiting and turnaround times. 4) Priority scheduling sorts processes by priority then calculates scheduling metrics. Deadlock avoidance/prevention and page replacement algorithms are also demonstrated.

Uploaded by

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

OS

The document contains code snippets demonstrating different CPU scheduling algorithms: 1) FCFS - First Come First Serve scheduling is implemented, calculating waiting times and turnaround times. 2) SJF - Shortest Job First scheduling sorts processes by burst time and calculates metrics. 3) Round Robin scheduling implements a time quantum to rotate between processes, calculating average waiting and turnaround times. 4) Priority scheduling sorts processes by priority then calculates scheduling metrics. Deadlock avoidance/prevention and page replacement algorithms are also demonstrated.

Uploaded by

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

FCFS

#include<stdio.h>

#include<conio.h>

main()

int bt[20], wt[20], tat[20], i, n;

float wtavg, tatavg;

clrscr();

printf("\nEnter the number of processes -- ");

scanf("%d", &n);

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

printf("\nEnter Burst Time for Process %d -- ", i);

scanf("%d", &bt[i]);

wt[0] = wtavg = 0;

tat[0] = tatavg = bt[0];

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

wt[i] = wt[i-1] +bt[i-1];

tat[i] = tat[i-1] +bt[i];

wtavg = wtavg + wt[i];

tatavg = tatavg + tat[i];

printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");

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

printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);

printf("\nAverage Waiting Time -- %f", wtavg/n);

printf("\nAverage Turnaround Time -- %f”, tatavg/n);

INPUT
Enter the number of processes – 3 Enter Burst Time for Process 0 – 24 Enter Burst Time for Process 1
– 3 Enter Burst Time for Process 2 -- 3

OUTPUT

PROCESS BURST TIME WAITING TIME TURNAROUND

TIME

P0 24 0 24

P1 3 24 27

P2 3 27 30

Average Waiting Time-- 17.000000

Average Turnaround Time -- 27.000000


SJF

#include<stdio.h>

#include<conio.h>

main()

int p[20], bt[20], wt[20], tat[20], i, k, n, temp; float wtavg,

tatavg;

clrscr();

printf("\nEnter the number of processes -- ");

scanf("%d", &n);

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

p[i]=i;

printf("Enter Burst Time for Process %d -- ", i);

scanf("%d", &bt[i]);

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

for(k=i+1;k<n;k++)

if(bt[i]>bt[k])

temp=bt[i];

bt[i]=bt[k];
bt[k]=temp;

temp=p[i];

p[i]=p[k];

p[k]=temp;

wt[0] = wtavg = 0;

tat[0] = tatavg = bt[0]; for(i=1;i<n;i++)

wt[i] = wt[i-1] +bt[i-1];

tat[i] = tat[i-1] +bt[i];

wtavg = wtavg + wt[i];

tatavg = tatavg + tat[i];

printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n”);

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

printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);

printf("\nAverage Waiting Time -- %f", wtavg/n);

printf("\nAverage Turnaround Time -- %f", tatavg/n); getch();}

INPUT

Enter the number of processes -- 4

Enter Burst Time for Process 0 -- 6

Enter Burst Time for Process 1 -- 8

Enter Burst Time for Process 2 -- 7

Enter Burst Time for Process 3 -- 3

OUTPUT

PROCESS BURST

TIME

WAITING

TIME

TURNARO
UND TIME

P3 3 0 3

P0 6 3 9

P2 7 9 16

P1 8 16 24

Average Waiting Time -- 7.000000

Average Turnaround Time -- 13.000000

ROUND ROBIN

#include<stdio.h>

main()

int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;

float awt=0,att=0,temp=0;

clrscr();

printf("Enter the no of processes -- ");

scanf("%d",&n);

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

printf("\nEnter Burst Time for process %d -- ", i+1);

scanf("%d",&bu[i]);

ct[i]=bu[i];

printf("\nEnter the size of time slice -- ");

scanf("%d",&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];}

printf("\nThe Average Turnaround time is -- %f",att/n);

printf("\nThe Average Waiting time is -- %f ",awt/n);

printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");

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

printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);

getch();}

INPUT:

Enter the no of processes – 3

Enter Burst Time for process 1 – 24

Enter Burst Time for process 2 -- 3

Enter Burst Time for process 3 – 3

Enter the size of time slice – 3

OUTPUT:

PROCESS BURST TIME WAITING TIME TURNAROUNDTIME

1 24 6 30

2347

3 3 7 10

The Average Turnaround time is – 15.666667 The


Average Waiting time is------------ 5.666667
PRIORITY

#include<stdio.h>

main()

int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp; float wtavg,

tatavg;

clrscr();

printf("Enter the number of processes --- ");

scanf("%d",&n);

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

p[i] = i;

printf("Enter the Burst Time & Priority of Process %d --- ",i); scanf("%d

%d",&bt[i], &pri[i]);

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

for(k=i+1;k<n;k++)

if(pri[i] > pri[k]){

temp=p[i];

p[i]=p[k];

p[k]=temp;

temp=bt[i];

bt[i]=bt[k];

bt[k]=temp;

temp=pri[i];

pri[i]=pri[k];

pri[k]=temp;

wtavg = wt[0] = 0;

tatavg = tat[0] = bt[0];

for(i=1;i<n;i++)
{

wt[i] = wt[i-1] + bt[i-1];

tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i];

tatavg = tatavg + tat[i];

printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND

TIME");

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

printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);

printf("\nAverage Waiting Time is --- %f",wtavg/n); printf("\nAverage Turnaround Time is--


%f”,tatavg/n);

getch();}

FIFO PAGE REPLACEMENT ALG

#include<stdio.h>

#include<conio.h> int fr[3];

void main() {

void display();

int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};

int

flag1=0,flag2=0,pf=0,frsize=3,top=0;

clrscr();

for(i=0;i<3;i++) {

fr[i]=

-1;

for(j=0;j<12;j++) {

flag1=0; flag2=0; for(i=0;i<12;i++) {

if(fr[i]==page[j]) {

flag1=1; flag2=1; break; }}

if(flag1==0) {
for(i=0;i<frsize;i++) {

if(fr[i]==

-1)

fr[i]=page[j]; flag2=1; break; }}}

if(flag2==0) {

fr[top]=page[j];

top++;

pf++;

if(top>=frsize)

top=0; }

display(); }

Page 31

printf("Number of page faults : %d ",pf+frsize);

getch();

void display()

int i; printf("\n");

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

printf("%d\t",fr[i]);}

OUTPUT:

2 -1 -1

2 3 -1

2 3 -1

231

531

521

524

524

324
324

354

352

Number of page faults: 9

LEAST RECENTLY USED

#include<stdio.h>

#include<conio.h>

int fr[3];

void main()

void display();

int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];

int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;

clrscr();

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

fr[i]=-1;

for(j=0;j<12;j++)

flag1=0,flag2=0;

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

if(fr[i]==p[j])

flag1=1;

flag2=1; break;

if(flag1==0)

Page 33
{

for(i=0;i<3;i++) {

if(fr[i]==

-1)

fr[i]=p[j]; flag2=1;

break; }}}

if(flag2==0) {

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

fs[i]=0;

for(k=j

-1,l=1;l<=frsize

-1;l++,k--

for(i=0;i<3;i++) {

if(fr[i]==p[k]) fs[i]=1;

}}

for(i=0;i<3;i++) {

if(fs[i]==0)

index=i; }

fr[index]=p[j];

pf++; }

display()

printf("

\n no of page faults :%d",pf+frsize);

getch(); }

void display(); {

int i; printf("\n");

for(i=0;i<3;i++)
printf("\t%d",fr[i]);

OUTPUT:

2 -1 -1

2 3 -1

2 3 -1

231

251

251

254

254

354

352

352

352

No of page faults: 7

DEADLOCK AVOIDANCE

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int alloc[10][10],max[10][10];

int avail[10],work[10],total[10];

int i,j,k,n,need[10][10];

int m;

int count=0,c=0;

char finish[10];

clrscr();

printf("Enter the no. of processes and resources:");

scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)

finish[i]='n';

printf("Enter the claim matrix:\n");

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

for(j=0;j<m;j++)

scanf("%d",&max[i][j]);

printf("Enter the allocation matrix:\n");

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

for(j=0;j<m;j++)

scanf("%d",&alloc[i][j]);

printf("Resource vector:");

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

scanf("%d",&total[i]);

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

avail[i]=0; for(i=0;i<n;i++)

Page 57

for(j=0;j<m;j++)

avail[j]+=alloc[i][j];

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

work[i]=avail[i];

for(j=0;j<m;j++)

work[j]=total[j]-work[j];

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

for(j=0;j<m;j++)

need[i][j]=max[i][j]-alloc[i][j];

A:

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

c=0;

for(j=0;j<m;j++)

if((need[i][j]<=work[j])&&(finish[i]=='n'))
c++;

if(c==m)

printf("All the resources can be allocated to Process %d", i+1);

printf("\n\nAvailable resources are:");

for(k=0;k<m;k++)

work[k]+=alloc[i][k];

printf("%4d",work[k]);

printf("\n");

finish[i]='y';

printf("\nProcess %d executed?:%c \n",i+1,finish[i]);

count++;

if(count!=n)

goto A;

else

printf("\n System is in safe mode");

printf("\n The given state is safe state");

getch();

Page 58

OUTPUT

Enter the no. of processes and resources: 4 3

Enter the claim matrix:

322

613

314

422
Enter the allocation matrix:

100

612

211

002

Resource vector:9 3 6

All the resources can be allocated to Process:2

Available resources are: 6 2 3

Process 2 executed?:y

All the resources can be allocated to Process 3 Available resources

are: 8 3 4

Process 3 executed?:y

All the resources can be allocated to Process 4 Available resources

are: 8 3 6

Process 4 executed?:y

All the resources can be allocated to Process 1

Available resources are: 9 3 6

Process 1 executed?:y

System is in safe mode

The given state is safe state

DEADLOCK PREVENTION

#include<stdio.h>

#include<conio.h>

void main()

char job[10][10];

int time[10],avail,tem[10],temp[10]; int safe[10];

int ind=1,i,j,q,n,t;

clrscr();

printf("Enter no of jobs: ");

scanf("%d",&n);
for(i=0;i<n;i++)

printf("Enter name and time: ");

scanf("%s%d",&job[i],&time[i]);

printf("Enter the available resources:");

scanf("%d",&avail);

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

temp[i]=time[i];

tem[i]=i;

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

for(j=i+1;j<n;j++)

if(temp[i]>temp[j])

t=temp[i];

Page 61

temp[i]=temp[j];

temp[j]=t; t=tem[i];

tem[i]=tem[j];

tem[j]=t;

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

q=tem[i];

if(time[q]<=avail)

safe[ind]=tem[i];
avail=avail-tem[q];

printf("%s",job[safe[ind]]);

ind++;

else

printf("No safe sequence\n");

printf("Safe sequence is:");

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

printf("%s %d\n",job[safe[i]],time[safe[i]]);

getch();}

OUTPUT: Enter no of jobs:4

Enter name and time: A 1

Enter name and time: B 4

Enter name and time: C 2

Enter name and time: D 3

Enter the available resources: 20

Safe sequence is: A 1, C 2, D 3, B 4.

You might also like