Batch - B Practical Test Question

Batch - B Practical Test Question
Batch - B Practical Test Question

Round Robin Algorithm Using LinkedList and Queue

#include<stdio.h>
#include<memory.h>
#include<stdlib.h>

struct Process
{
int pName;
int arrTime;
int proTime;
struct Process *next;
};

struct Process *Q=NULL,*FIRST=NULL,*head=NULL, *temp=NULL,*inProcess=NULL,*FRONT=NULL,*REAR=NULL;

int qunatomTime;

void createLL();
struct Process * sortLL(struct Process *);
void printLL();

void myEnqueue(struct Process *);
struct Process * myDequeue();

void RoundRobin(int );
int getTotalTime();
void printQueue();

int main()
{
int n;
printf("\n Enter How Many Process You Have : ");
scanf("%d",&n);
while(n>0)
{
createLL();
n--;
}
head=FIRST;
FIRST=sortLL(head);

printf("\n Enter Quantom Time : ");
scanf("%d",&qunatomTime);

RoundRobin(getTotalTime());

return 0;
}

void RoundRobin(int totalTime)
{
int clockTime=0;
printf("\n Process Execution Sequence \n");
do
{
if(FIRST!=NULL)
{
while(FIRST->arrTime <= clockTime)
{
temp=FIRST;
if(FIRST->next==NULL)
{
temp->next=NULL;
myEnqueue(temp);
FIRST=NULL;
break;
}
else
{
FIRST=FIRST->next;
temp->next=NULL;
myEnqueue(temp);
}

}
}
if(inProcess!=NULL)
{
inProcess->next=NULL;
myEnqueue(inProcess);
inProcess=NULL;
}
if(FRONT->proTime > qunatomTime )
{
FRONT->proTime = FRONT->proTime - qunatomTime;
clockTime=clockTime + qunatomTime;
printf("\n| %d at Time %d | -> ",FRONT->pName,clockTime);
inProcess=myDequeue();
}
else
{
clockTime = clockTime + FRONT->proTime;
printf("\n| %d at Time %d | -> ",FRONT->pName,clockTime);
FRONT->proTime=0;
free(myDequeue());
}
}while(clockTime < totalTime);
}

void printQueue()
{
head=FRONT;
while(head!=NULL)
{
printf(" %d -> ",head->pName);
head=head->next;
}
}

void myEnqueue(struct Process *temp)
{
if(FRONT==NULL && REAR==NULL)
{
FRONT=temp;
REAR=temp;
}
else
{
REAR->next=temp;
REAR=REAR->next;
}
}

struct Process * myDequeue()
{
if(FRONT!=NULL)
{
temp=(struct Process *)malloc(sizeof(struct Process)*1);
temp->next=NULL;
temp->pName=FRONT->pName;
temp->arrTime=FRONT->arrTime;
temp->proTime=FRONT->proTime;

if(FRONT->next==NULL && FRONT==REAR)
{
REAR=NULL;
FRONT=NULL;
}
else
{
FRONT=FRONT->next;
}
return temp;
}
else
{
return NULL;
}
}

void printLL()
{
head=FIRST;
while(head!=NULL)
{
printf("| %d |->",head->pName);
head=head->next;
}
}

int getTotalTime()
{
int totalTime=0;
head=FIRST;
while(head!=NULL)
{
totalTime=totalTime+head->proTime;
head=head->next;
}
return totalTime;
}

void createLL()
{
temp=(struct Process *)malloc(sizeof(struct Process)*1);
temp->next=NULL;
printf("\n Enter Process Name : ");
scanf("%d",&temp->pName);
printf("\n Enter Arrival Time : ");
scanf("%d",&temp->arrTime);
printf("\n Enter Process Time : ");
scanf("%d",&temp->proTime);

if(FIRST==NULL)
{
FIRST=temp;
}
else
{
head=FIRST;
while(head->next!=NULL)
{
head=head->next;
}
head->next=temp;
}
}

struct Process * sortLL(struct Process *head)
{
struct Process *myTemp=NULL,*myHead=NULL;
while(head!=NULL)
{
myTemp=(struct Process *)malloc(sizeof(struct Process)*1);
myTemp->next=NULL;
myTemp->arrTime=head->arrTime;
myTemp->pName=head->pName;
myTemp->proTime=head->proTime;

if(myHead==NULL)
{
myHead=myTemp;
}
else
{
temp=myHead;
while(temp->arrTime<myTemp->arrTime && temp->next!=NULL)
{
temp = temp->next;
}
if(temp->next!=NULL)
{
myTemp->next=temp->next;
temp->next=myTemp;
}
else
{
temp->next=myTemp;
}
}
head=head->next;
}
return myHead;
}

Comments