Round robin process scheduling simulation
anomit | September 11, 2008No big deal. Everyone has to do it once for their UNIX concepts practical work I guess. What irked me was that the teacher and some of the 'good students' were hell bent on arguing that non-preemptive process scheduling simulations do not require any kind of data regarding the arrival time of processes in the ready queue. I still don't understand how that simulates a real life scenario, that is, after the completion of a process, to which other process would the scheduler perform a context switch. Could someone point me out if I am missing something here?
Anyways, this is the code I wrote today during the practical class:
struct processinfo
{
int id;
int burst;
int arr;
};
typedef struct processinfo process;
/*checks if all the processes have finished execution*/
int check(process* p,int n)
{
int i=0,flag=0;
for(i=0;i<n;i++)
{
if(p[i].burst!=0)
{
flag=1;
break;
}
}
return flag;
}
int main()
{
int n;
printf("No. of processes:");
scanf("%d",&n);
process p[n];
int i=0,j=0;
int wt[n]/*the waiting time array*/,last[n]/*last[i] stores the last executed instant of the process i*/;
for(i=0;i<n;i++)
{
printf("Proccess id:");
scanf("%d",&p[i].id);
printf("Arrival time:");
scanf("%d",&p[i].arr);
printf("Burst time:");
scanf("%d",&p[i].burst);
wt[i]=0;
last[i]=0;
}
/*gah, bubble sort*/
/*anyways we won't be doing any sorting in a real scheduler, I guess*/
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[j-1].arr>p[j].arr)
{
process temp=p[j-1];
p[j-1]=p[j];
p[j]=temp;
}
}
}
int timequantum=2/*the time quantum*/,time=0/*running time*/;
while(1)
{
if(!check(p,n))
break;
int k=0;
for(k=0;k<n;k++)
{
if(p[k].burst!=0)
{
if(p[k].burst<timequantum)
{
wt[k]+=time-last[k];
time+=p[k].burst;
p[k].burst=0;
}
else
{
p[k].burst-=timequantum;
wt[k]+=time-last[k];
time+=timequantum;
}
last[k]=time;
}
}
}//end of while
for(i=0;i<n;i++)
printf("\nWaiting time for Process %d:%d",p[i].id,wt[i]);
return 0;
}









