#include<stdio.h>
#include<conio.h>
float avg_wt,avg_tt;
int i = 0,ttl_wt=0,ttl_tt=0,qt1=4,qt2=8;
struct process
{
int prn;
char type;
int bt;
int wt;
int tt;
struct process *nxt;
}*stfor1,*stfor2,*stbck,*np,*endfor1,*endfor2,*endbck,*temp;
void ins_node(struct process *np,int num)
{
if(num == 1)
{
if(stfor1 == NULL)
stfor1 = endfor1 = np;
else
{
endfor1->nxt = np;
endfor1 = np;
}
}
else if(num == 2)
{
if(stfor2 == NULL)
stfor2 = endfor2 = np;
else
{
endfor2->nxt = np;
endfor2 = np;
}
}
else
{
if(stbck == NULL)
stbck = endbck = np;
else
{
endbck->nxt = np;
endbck = np;
}
}
}
void del_node(struct process *np,int num,int choice)
{
if(num == 1)
{
if(stfor1 == endfor1)
stfor1 = NULL;
else
stfor1 = stfor1->nxt;
if(choice == 1)
delete np;
}
else if(num == 2)
{
if(stfor2 == endfor2)
stfor2 = NULL;
else
stfor2 = stfor2->nxt;
if(choice == 1)
delete np;
}
else
{
if(stbck == endbck)
stbck = NULL;
else
stbck = stbck->nxt;
delete np;
}
}
void ins_dat()
{
char ch;
int j=0,n;
stfor1 = endfor1 = stfor2 = endfor2 = stbck = endbck = NULL;
do
{
np = new process;
np->nxt = NULL;
np->prn = ++j;
printf("\n Enter the Burst time of Process #%d : ",np->prn);
scanf("%d",&np->bt);
printf("\n Enter the type of Process #%d (f-foreground or b-background): ",np->prn);
np->type = getche();
if(np->type == 'f')
np->nxt = stfor1;
else
np->nxt = NULL;
np->wt = np->tt = 0;
if(np->type == 'f')
ins_node(np,1);
else
ins_node(np,3);
printf("\n\n Continue ?? : ");
ch = getche();
}while(ch == 'y' || stfor1 == NULL);
}
void Round_Robin()
{
getch();
printf("\n\n Foreground RR Process Scheduling (Quantum Size = %d)\n\n",qt1);
printf("\n\n Process Burst Time Waiting Time Turnaround Time \n");
printf(" ------------------------------------------------------- \n");
temp = stfor1;
do
{
printf(" %d \t\t %d \t\t",stfor1->prn,stfor1->bt);
stfor1->wt += (temp->tt - stfor1->tt);
if(qt1 >= stfor1->bt)
{
stfor1->tt = temp->tt + stfor1->bt;
stfor1->bt = 0;
printf(" %d \t\t %d\n\n",stfor1->wt,stfor1->tt);
ttl_wt += stfor1->wt;
ttl_tt += stfor1->tt;
temp = np = stfor1;
del_node(np,1,1);
++i;
}
else
{
stfor1->bt -= qt1;
stfor1->tt = temp->tt + qt1;
printf(" %d \t\t %d\n\n",stfor1->wt,stfor1->tt);
temp = np = stfor1;
del_node(np,1,2);
if(temp->bt > qt1)
ins_node(temp,2);
else
ins_node(temp,1);
}
}while(stfor1 != NULL);
if(stfor1 == NULL && stfor2 != NULL)
{
getch();
printf("\n Foreground RR Process Scheduling (Quantum Size = %d)\n",qt2);
printf("\n\n Process Burst Time Waiting Time Turnaround Time \n");
printf(" ------------------------------------------------------- \n");
do
{
printf(" %d \t\t %d \t\t",stfor2->prn,stfor2->bt);
stfor2->wt += (temp->tt - stfor2->tt);
if(qt2 >= stfor2->bt)
{
stfor2->tt = temp->tt + stfor2->bt;
stfor2->bt = 0;
printf(" %d \t\t %d\n\n",stfor2->wt,stfor2->tt);
ttl_wt += stfor2->wt;
ttl_tt += stfor2->tt;
temp = np = stfor2;
del_node(np,2,1);
++i;
}
else
{
stfor2->bt -= qt2;
stfor2->tt = temp->tt + qt2;
printf(" %d \t\t %d\n\n",stfor2->wt,stfor2->tt);
np = temp = stfor2;
del_node(np,2,2);
if(temp->bt > qt2)
ins_node(temp,3);
else
ins_node(temp,2);
}
}while(stfor2 != NULL);
}
}
void FCFS()
{
getch();
printf("\n\n Background FCFS Process Scheduling \n\n");
printf("\n\n Process Burst Time Waiting Time Turnaround Time \n");
printf(" ------------------------------------------------------- \n");
do
{
printf(" %d \t\t %d \t\t",stbck->prn,stbck->bt);
stbck->wt += (temp->tt-stbck->tt);
stbck->tt = temp->tt + stbck->bt;
printf(" %d \t\t %d\n\n",stbck->wt,stbck->tt);
ttl_wt += stbck->wt;
ttl_tt += stbck->tt;
temp = np = stbck;
del_node(np,3,1);
++i;
}while(stbck != NULL);
}
void main()
{
clrscr();
ins_dat();
if(stfor1 != NULL)
Round_Robin();
if(stfor1 == NULL && stfor2 == NULL && stbck != NULL)
FCFS();
avg_wt = (float) ttl_wt/i;
avg_tt = (float) ttl_tt/i;
printf("\n\n Average Waiting Time : %f",avg_wt);
printf("\n Average Turnaround Time : %f",avg_tt);
getch();
}
No comments:
Post a Comment