Uploaded by Sahil Kadbhane

OS ASS3 Kuldeep Bhangale 5

advertisement
Assinment No.:03
Title:To implement C program for CPU Scheduling algorithm .
Roll No.: 5
Round Robin:
#include <stdio.h>
int n,
process_name[30],arrival_time[30],burst_time[30],remain_burst_time[30],pending,finish[30],waiti
ng_time[30],count,time,turn_arround_time[30];
void input()
{
int i;
printf("enter no.of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
process_name[i]=i;
printf("\n enter arrival time of process %d",process_name[i]);
scanf("%d",&arrival_time[i]);
printf("\n enter burst time of process %d",process_name[i]);
scanf("%d",&burst_time[i]);
remain_burst_time[i]=burst_time[i];
}
}
void output()
{
int i;
printf("\n \n process_no \t arrival_time \t burst_time");
for(i=0;i<n;i++)
{
printf("\n %d \t \t \t%d \t \t%d \n",process_name[i],arrival_time[i],burst_time[i]);
}
}
void round_robin()
{
int time_quantum,i;
pending=n;
printf("\n enter time quantum");
scanf("%d",&time_quantum);
for(i=0;i<n;i++)
{
finish[i]=0;
waiting_time[i]=0;
}
//start execution
for(time=0, count=0; pending>0;)
{
//case1: for remain burst time less than or equal to time quantum
if(finish[count]==0 && arrival_time[count]<=time &&
remain_burst_time[count]<=time_quantum)
{
time=time+remain_burst_time[count];
pending--; //process is completed and declared as finish
finish[count]=1;
waiting_time[count]-=arrival_time[count];
turn_arround_time[count]=waiting_time[count]+burst_time[count];
//for priting of Gantt chart
for(i=0;i<remain_burst_time[count];i++)
{
printf("P[%d] \t",process_name[count]);
}
for(i=0;i<n;i++)
{
if(i==count || finish[i]==1)
continue;
waiting_time[i]=waiting_time[i]+remain_burst_time[count];
}
remain_burst_time[count]=0;
}
//case2: for remain burst time greater than time quantum
else if (finish[count]==0 && arrival_time[count]<=time &&
remain_burst_time[count]>time_quantum)
{
remain_burst_time[count]=remain_burst_time[count]-time_quantum;
time+=time_quantum;
for(i=0;i<n;i++)
{
if(i==count || finish[i]==1)
continue;
waiting_time[i]=waiting_time[i]+time_quantum;
}
for(i=0;i<time_quantum;i++)
{
printf("P[%d] \t",process_name[count]);
}
}
count= (count+1)%3;
}
}
void main()
{
input();
output();
round_robin();
}
OUTPUT:
os-lab@oslab-ThinkCentre-M71e:~/Documents$ gcc -o roundrobbin roundrobbin.c
os-lab@oslab-ThinkCentre-M71e:~/Documents$ ./roundrobbin
enter no.of process:3
enter arrival time of process 00
enter burst time of process 05
enter arrival time of process 11
enter burst time of process 16
enter arrival time of process 22
enter burst time of process 23
process_no
0
arrival_time
0
burst_time
5
1
1
6
2
2
3
enter time quantum4
P[0] P[0] P[0] P[0]
P[1]
P[1]
P[1]
P[1]
P[2]
P[2]
P[2]
P[0]
P[1]
P[1]
Download