/* Implementation of RoundRobin method for CPU scheduling using Circular LL */
#include<stdlib.h>
#define newnode (struct node *)malloc(sizeof(struct node))
struct node
{ int jobno,time;
struct node *next;
}*JOBQ=NULL;
void erase()
{ struct node *S,*T;
if(JOBQ!=NULL)
{ for(T=JOBQ;T->next!=JOBQ; T=T->next) ; // go to last node
T->next=NULL;
while(JOBQ!=NULL) { S=JOBQ; JOBQ=JOBQ->next; free(S); }
}
}
void CreateJOBQ()
{ struct node *S; int i,n;
printf("\nHow many jobs?"); scanf("%d",&n);
if(JOBQ!=NULL) erase(JOBQ);
JOBQ=newnode;
printf("\nEnter JOB No. & execution time in sec. "); scanf("%d %d",&JOBQ->jobno,&JOBQ->time);
S=JOBQ;
for(i=2;i<=n;i++)
{ S->next=newnode;
S=S->next;
printf("\nEnter JOB No. & execution time in sec. "); scanf("%d %d",&S->jobno,&S->time);
}
S->next=JOBQ;
}
void DisplayJOBQ()
{struct node *T=JOBQ;
if(JOBQ==NULL) printf("\nJOB Q is Empty!");
else
{ printf("\nJOB Rem. Time");
do{ printf("\n J%d : %d ",T->jobno,T->time);
T=T->next;
}while(T!=JOBQ);
}
}
void ExecuteJOBQ()
{ struct node *T=JOBQ,*S=NULL,*t1; int flag,timeslot=1,time=0; char choice;
printf("\nEnter Time Slot (in sec.) "); scanf("%d",×lot);
printf("\nclock CPU JOBQ ");
while(JOBQ!=NULL)
{
do{ flag=0;
if(T->time<=timeslot) // job remaining time < time slot
{ printf("\n%3d J%d(%d) |--",time,T->jobno,T->time); // cpu allocated
for(t1=T->next; t1!=T; t1=t1->next) printf(" J%d(%d) ",t1->jobno,t1->time); // print JOBQ
time+=T->time; // increment clock
T->time=0; // job finished, delete from JOBQ
if(T==JOBQ) // 1st job
{ if(JOBQ->next==JOBQ) // only 1 job in Q
{ JOBQ=NULL; free(T); T=NULL; } // JOBQ became empty
else // more jobs in Q
{ for(t1=JOBQ;t1->next!=JOBQ; t1=t1->next) ; // go to last node
JOBQ=JOBQ->next; t1->next=JOBQ;
free(T); T=JOBQ; flag=1;
}
}
else // job other than 1st position
{ S->next=T->next; free(T); T=S->next;
}
}
else // job is executed for given time slot
{ printf("\n%3d J%d(%d) |--",time,T->jobno,T->time); // cpu allocated
for(t1=T->next; t1!=T; t1=t1->next) printf(" J%d(%d) ",t1->jobno,t1->time); // print JOBQ
T->time-=timeslot;
time+=timeslot;
S=T; T=T->next; // go to next job
}
printf(" --| Add New Job(y/n)? "); choice=getche();
if(choice=='y' || choice=='Y')
{ struct node *S1;
if(JOBQ==NULL) // JOBQ empty
{ JOBQ=newnode; JOBQ->next=JOBQ; // 1st job in Q
printf("\nEnter JOB No. & execution time in sec. "); scanf("%d %d",&JOBQ->jobno,&JOBQ->time);
T=JOBQ; flag=1;
}
else
{ for(S1=S;S1->next!=S; S1=S1->next) ; // go to last node of JOBQ
S1->next=newnode; S1=S1->next; S1->next=S; // add new job at the end of JOBQ
printf("\nEnter JOB No. & execution time in sec. "); scanf("%d %d",&S1->jobno,&S1->time);
}
}
}while(T!=JOBQ || flag);
}
printf("\n%3d -idle-",time);
}
char menu()
{ char choice='a';
clrscr();
printf("----- Round Robin ------");
printf("\nC: Create New JOB Queue");
printf("\nD: Display JOB Queue");
printf("\nE: Execute JOB Queue");
printf("\nX: exit");
printf("\nEnter your choice : ");
choice=getche();
return toupper(choice);
}
void main()
{
while(1)
{ switch(menu())
{ case 'X' : erase(); exit(0); // release memory allocated to JOBQ
case 'C' : CreateJOBQ(); break;
case 'D' : DisplayJOBQ(); break;
case 'E' : ExecuteJOBQ(); break;
default : printf("\7 Invalid Choice!");
}
printf("\npress any key to continue...");
getch();
}
}