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

Operating System46761

The document describes three scheduling algorithms - First Come First Serve (FCFS), Shortest Job First (SJF), and Priority scheduling. It includes code implementations and sample outputs for each algorithm. The FCFS algorithm schedules processes in the order they arrive without preemption. SJF is non-preemptive and schedules the process with the shortest burst time first. Priority scheduling is also non-preemptive and schedules processes based on priority, with the highest priority process executed first.

Uploaded by

venkat bittu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Operating System46761

The document describes three scheduling algorithms - First Come First Serve (FCFS), Shortest Job First (SJF), and Priority scheduling. It includes code implementations and sample outputs for each algorithm. The FCFS algorithm schedules processes in the order they arrive without preemption. SJF is non-preemptive and schedules the process with the shortest burst time first. Priority scheduling is also non-preemptive and schedules processes based on priority, with the highest priority process executed first.

Uploaded by

venkat bittu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OS

FCFS
#include<stdio.h>

int main()

int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;

printf(" Enter total number of processes(maximum 20)");

scanf("%d",&n);

printf("\nEnter process burst time\n");

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

printf("p[%d]",i+1);

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

wt[0]=0;

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

wt[i]=0;

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

wt[i]+=bt[j];

printf("\nprocess\t\tburst time\twaiting time\tTurn around time");

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

tat[i]=bt[i]+wt[i];

avwt+=wt[i];

22701F00I5 Page 1
OS

avtat+=tat[i];

printf("\np[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);

avwt/=i;

avtat/=i;

printf("\n\n average waiting time : %d ",avwt);

printf("\n average Turn aroud time : %d ",avtat);

return 0 ;

22701F00I5 Page 2
OS

SHORTEST JOB FIRST


#include<stdio.h>

main()

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

float wtavg,tatavg;

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;

22701F00I5 Page 3
OS

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\tPROCESSS \tBURST TIME\tWAITING TIME\tTURN AROUND TIME\n");

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

printf("\n\tP%d\t\t%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 turn around time --%f",tatavg/n);

OUTPUT

Enter the number of processes--5

Enter Burst time for process 0--6

Enter Burst time for process 1--5

Enter Burst time for process 2--2

Enter Burst time for process 3--3

Enter Burst time for process 4--9

22701F00I5 Page 4
OS

PROCESSS BURST TIME WAITING TIME TURN AROUND TIME

P2 2 0 2 0

P3 3 2 5 0

P1 5 5 10 0

P0 6 10 16 0

P4 9 16 25 0

Average waiting time--6.600000

Average turn around time --11.600000

--------------------------------

Process exited after 20.19 seconds with return value 37

Press any key to continue . . .

22701F00I5 Page 5
OS

PRIORITY
#include<stdio.h>

main()

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

float wtavg,tatavg;

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

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];

22701F00I5 Page 6
OS

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\tTURN AROUND 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 --%f",wtavg/n);

printf("\nAverage Turn around time is --%f",tatavg/n);

OUTPUT
Enter the number of process--5

Enter the burst time & priority of process 0--10

Enter the burst time & priority of process 1--1

Enter the burst time & priority of process 2--2

22701F00I5 Page 7
OS

Enter the burst time & priority of process 3--1

Enter the burst time & priority of process 4--5

PROCESS PRIORITY BURST TIME WAITING TIME TURN AROUND TIME

0 3 10 0 10

1 1 1 10 11

2 4 2 11 13

3 5 1 13 14

4 2 5 14 19

Average waiting time --9.600000

Average Turn around time is --13.400000

--------------------------------

Process exited after 20.04 seconds with return value 40

Press any key to continue . . .

22701F00I5 Page 8
OS

FILE ALLOCATIONS

A)SEQUENTIAL
#include<stdio.h>

#include<stdlib.h>

main()

int f[50],i,st,j,len,c,k;

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

f[i]=0;

x:

printf("\n Enter the starting block and length of file \t:");

scanf("%d%d",&st,&len);

for(j=st;j<(st+len);j++)

if(f[j]==0)

f[j]=1;

printf("\n%d->%d",j,f[j]);

else

printf("Block already allocated ");

break;

if(j==(st+len))

printf("\n the file is allocated to disk ");

22701F00I5 Page 9
OS

printf("\n if u want to enter more files ?(y-1/n-0)\t :");

scanf("%d",&c);

if(c==1)

goto x;

OUTPUT:

Enter the starting block and length of file :2

10

2->1

3->1

4->1

5->1

6->1

7->1

8->1

9->1

10->1

11->1

the file is allocated to disk

if u want to enter more files ?(y-1/n-0)

22701F00I5 Page 10

You might also like