You are on page 1of 8

Ex: No: 5a CPU SCHEDULING ALGORITHM (Round Robin)

Date:

AIM:
To Simulate CPU Scheduling Algorithm Round Robin and calculate Average Turnaround time and
waiting time.

ALGORITHM
1. Quantum is set as q=1
2. Enter number of jobs
3. Initialize all arrays
4. Calculate waiting time for each process
5. For Each job enter
6. Enter process name
7. Enter burst time
8. Calculate waiting time for each process
9. Calculate lapsedtime,remaining time and finish time for each process
10. Calculate Average Turnaround time and Average waiting time.

PROGRAM:
#include<stdio.h>
main()
{
int i,bt[10],lt[10],wt[10],tbt=0,c=0,rt[10],complete[10],ft[10],ts,n;
float ATAT=0.0,awt=0.0;
printf("Enter number of jobs:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
wt[i]=i;
}
for(i=0;i<n;i++)
{
complete[i]=bt[i]=lt[i]=rt[i]=ft[i]=0;
awt+=wt[i];
}
awt=awt/n;
for(i=0;i<n;i++)
{
printf("\nEnter runtime for process %d",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
tbt+=bt[i];
}
while(tbt>0)
{
for(i=0;i<n;i++)
{
if(rt[i]>0&&complete[i]==0)
{
lt[i]+=1;
rt[i]-=1;
if(rt[i]<0)
rt[i]=0;
c++;
tbt--;
if(rt[i]==0&complete[i]==0)
{
ft[i]=c;
complete[i]=1;
ATAT+=ft[i];

}
printf("\nProcess:%s",process[i]);
printf("\tBT=%d\tLT=%d\tRT=%d\tClock=%d\t\tTBT=%d",bt[i],
l t[i],rt[i],c,tbt);
}
}
}
printf("\n\n\nProcess\tBursttime\tWaiting time \tFinishtime");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t\t%d\t\t%d",process[i],bt[i],wt[i],ft[i]);
}
ATAT=ATAT/n;
printf("\nAverage waiting time=%f",awt);
printf("\nAverage TurnAround time=%f",ATAT);
}

OUTPUT:

enter the number of process :3


enter time quantum:1
enter the runtime of each process :
1
2
3
process no. runtime remaining time
1 1 0
2 1 1
3 1 2
2 1 0
3 1 1
3 1 0
pro no runtime wait time tat
1 1 0 1
2 2 2 4
3 3 3 6

avg turnaround time=3.67

avg waiting time=1.67

RESULT:
The above program has been executed successfully and verified.

Ex: No: 5b CPU SCHEDULING ALGORITHMS (Shortest job first)


Date:

AIM:
To Simulate CPU Scheduling Algorithm Shortest Job First (SJF) and calculate Average Turnaround time
and waiting time.

ALGORITHM:
1. Enter The program
2. Enter no. of processes
3. Enter Runtime of each process
4. Sort processes according to runtime.
5. Calculate Turnaround time, Waiting time for each process.
6. Calculate Average Turnaround time, Average waiting time.
7. Stop the program.

PROGRAM:
#include<stdio.h>
main()
{
int i,j,n,temp,wtp,twt,tatp,ttat,burst[10];
float avgtat,avgwt;
printf("Enter the no of ps:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the run time of ps[%d]",i+1);
scanf("%d",&burst[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(burst[i]>burst[j])
{
temp=burst[i];
burst[i]=burst[j];
burst[j]=temp;
}
}
}
i=ttat=twt=tatp=wtp=0;
printf("\n ps no \t run time\t\tres time\tTA”);
while(i<n)
{
tatp=tatp+burst[i];
ttat=ttat+tatp;
twt=twt+wtp;
printf("\n %d\t\t%d\t\t%d\t\t%d",i+1,burst[i],wtp,ta
wtp=burst[i]+wtp;
i=i+1;
}
avgwt=twt*1.0/n;
avgtat=ttat*1.0/n;
printf("\n avg turn aru time=%0.2f",avgtat);
printf("\n avgwait time =%0.2f",avgwt);
return(0);
}

OUTPUT:
[cse2k702@jcs ~]$ cc sjf.c
[cse2k702@jcs ~]$ ./a.out
Enter the no of ps:5
Enter the run time of ps[1]56
Enter the run time of ps[2]87
Enter the run time of ps[3]96
Enter the run time of ps[4]12
Enter the run time of ps[5]36
ps no run time res time TAT
1 12 0 12
2 36 12 48
3 56 48 104
4 87 104 191
5 96 191 287

avg turn aru time=128.40


avgwait time =71.00

RESULT:
The above program has been executed successfully and verified.
Ex: No: 5c CPU SCHEDULING ALGORITHMS (FCFS)
Date:

AIM :
To Simulate CPU Scheduling Algorithm First Come First Serve(FCFS) and calculate Average
Turnaround time and waiting time.

ALGORITHM:
1. Enter The program
2. Enter no. of processes
3. Enter Runtime of each process
4. Calculate Turnaround time, Waiting time for each process.
5. Calculate Average Turnaround time, Average waiting time.

PROGRAM:
#include<stdio.h>
main()
{
int i,n ,wtp,twt,tatp,ttat,burst[10];
float avgtat,avgwt;
printf("Enter the number of process");
scanf("%d",&n);
printf("\nEnter the runtime of each process");
for(i=0;i<n;i++)
scanf("%d",&burst[i]);
i=ttat=twt=tatp=wtp=0;
printf("Process no.\tBurst time\tResponse time\tTAT");
while(i<n)
{
tatp=tatp+burst[i];
ttat=ttat+tatp;
twt=twt+wtp;
printf("\n%d\t\t%d\t\t%d\t\t%d",i+1,burst[i],wtp,tatp);
wtp=burst[i]+wtp;
i=i+1;
}
avgwt=twt*1.0/n;
avgtat=ttat*1.0/n;
printf("\nAvg turn around time :%0.2f",avgtat);
return 0;
}

OUPUT:
[cse2k721@jcs ~]$ cc os.c
[cse2k721@jcs ~]$ ./a.out
Enter the number of process5
Enter the runtime of each process34 45 67 89 10
Process no. Burst time Response time TAT
1 34 0 34
2 45 34 79
3 67 79 146
4 89 146 235
5 10 235 245
Avg turn around time :147.80
Avg waiting time :98.80[cse2k721@jcs ~]$

RESULT:
The above program has been executed successfully and verified.

Ex: No: 5d CPU SCHEDULING ALGORITHMS(Priority)


Date:

AIM:
To Simulate CPU Scheduling Algorithm Priority and calculate Average Turnaround time and waiting
time.

ALGORITHM:
1. Enter the program.
2. Enter no. of processes
3. Enter Runtime of each process
4. Enter priority of each process.
5. Sort processes according to priority.
6. Calculate Turnaround time, Waiting time for each process.
7. Calculate Average Turnaround time, Average waiting time.
8. Stop the program.

PROGRAM:
#include<stdio.h>
main()
{
int i,j,n,temp,wtp,twt,tatp,ttat,burst[10],pri[10];
float avgtat,avgwt;
printf("Enter the no of ps:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the run time of ps[%d]",i+1);
scanf("%d",&burst[i]);
scanf("%d",&pri[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(pri[i]<pri[j])
{
temp=pri[i];
pri[i]=pri[j];
pri[j]=temp;
temp=burst[i];
burst[i]=burst[j];
burst[j]=temp;
}
}
}
i=ttat=twt=tatp=wtp=0;
printf("\n ps no \t run time\t\tpriority\t\tres time\tTAT");
while(i<n)
{
tatp=tatp+burst[i];
ttat=ttat+tatp;
twt=twt+wtp;
printf("\n %d\t\t%d\t\t%d\t\t%d\t\t%d",i+1,burst[i],pri[i],wtp,tatp);
wtp=burst[i]+wtp;
i=i+1;
}
avgwt=twt*1.0/n;
avgtat=ttat*1.0/n;
printf("\n avg turn aru time=%0.2f",avgtat);
printf("\n avgwait time =%0.2f",avgwt);
return(0);
}

OUTPUT:

[cse2k702@jcs ~]$ cc pri.c


[cse2k702@jcs ~]$ ./a.out
Enter the no of ps:5
Enter the run time of ps[1]5
9
Enter the run time of ps[2]8
2
Enter the run time of ps[3]6
7
Enter the run time of ps[4]1
3
Enter the run time of ps[5]10
8

ps no run time priority res time TAT


1 5 9 0 5
2 10 8 5 15
3 6 7 15 21
4 1 3 21 22
5 8 2 22 30

avg turn aru time=18.60

avgwait time =12.60

RESULT:
The above program has been executed successfully and verified.

You might also like