hi
this is a program for simulation the scheduling in processes between RAM and CPU the program in c language
and i hope that you will engoy it and i will be ready for more help
algorithm uses in scheduling between RAM and CPU
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
#define TRUE 1
#define FALSE 0
struct QType
{
int time;
int a_time;
int priority;
int executions_no;
int p_id;
struct QType * next;
};
struct QType *front;
struct QType *rear;
///////////////////////////////////////////////////////////////////////
int awt,sum_time,wt,p_no,Quantum; //external variables
int arr[30];
///////////////////////////////////////////////////////////////////////
int empty()
{
if (front == NULL && rear == NULL)
return TRUE;
else return FALSE;
}
///////////////////////////////////////////////////////////////////////
void addQ(int t_time,int t_a_time,int t_priority)
{
struct QType * ptr = (struct QType *) malloc(sizeof(struct QType));
ptr->time=t_time;
ptr->a_time=t_a_time;
ptr->priority=t_priority;
ptr->executions_no=0;
ptr->next=NULL;
if (empty())
{
rear = ptr;
front = ptr;
}
else
{
rear->next=ptr;
rear = ptr;
}
}
/////////////////////////////////////////////////////////////////////
int delQ()
{
int item;
if (empty())
{
printf("Empty\n");
return(-1);
}
else
{
struct QType * ptr;
ptr = front;
front=front->next;
item=ptr->time;
free(ptr);
return(item) ;
}
}
////////////////////////////////////////////////////////////////////////
void showQ()
{
struct QType * sFront = front ;
printf("\nQ = [") ;
while( sFront != NULL )
{
printf("%d,",sFront->time);
sFront = sFront->next;
}
printf("\b]\n");
}
//////////////////////////////////////////////////////////////////////
void sortQ()
{
int p,t,a,id;
struct QType *i,*j=front;
for(i=front;i!=NULL;i=i->next)
for(j=i;j!=NULL;j=j->next)
{
if(i->time>j->time)
{
p=i->priority;
t=i->time;
a=i->a_time;
id=i->p_id;
i->priority=j->priority;
i->time=j->time;
i->a_time=j->a_time;
i->p_id=j->p_id;
j->priority=p;
j->time=t;
j->a_time=a;
j->p_id=id;
}
}
}
/////////////////////////////////////////////////////////////////////////
void priority_sortQ()
{
int p,t,a,id;
struct QType *i,*j=front;
for(i=front;i!=NULL;i=i->next)
for(j=i;j!=NULL;j=j->next)
{
if(i->priority<j->priority)
{
p=i->priority;
t=i->time;
a=i->a_time;
id=i->p_id;
i->priority=j->priority;
i->time=j->time;
i->a_time=j->a_time;
i->p_id=j->p_id;
j->priority=p;
j->time=t;
j->a_time=a;
j->p_id=id;
}
}
}
/////////////////////////////////////////////////////////////////////
void choose()
{
int i,time,a_time,priority;
clrscr();
printf("\nWhat's the number of the processes you want to execute? ");
scanf("%d",&p_no);
printf("\nEnter the time Quantum for the Round Robin Scheduling ");
scanf("%d",&Quantum);
for(i=1;i<=p_no;i++)
{
printf("\n///////////////////////////////////////////////////////////");
printf("\nEnter the time needed for process No. %d ",i);
scanf("%d",&time);
printf("\nEnter the process arrival time ");
scanf("%d",&a_time);
printf("\nEnter the priority of this process ");
scanf("%d",&priority);
addQ(time,a_time,priority);
rear->p_id=i;
arr[i]=time; //for the Round Robin sch.
}
}
/////////////////////////////////////////////////////////////////////////
void FCFS()
{
int x=p_no;
struct QType * F;
awt=wt=0;
F=front;
x=p_no-1;
while(F->next!=NULL)
{
wt+=F->time;
awt+=x*F->time;
printf("\nwt for process %d= %d ",++F->p_id,wt);
x--;
F=F->next;
}
showQ();
printf("\nawt= %0.2f Ms",(float)awt/p_no,"\n\n");
}
/////////////////////////////////////////////////////////////////////////
void SJF()
{
int i;
struct QType * F;
awt=wt=0;
front=rear=NULL;
for(i=1;i<=p_no;i++)
{
addQ(arr[i],0,0);
rear->p_id=i;
}
F=front;
sortQ();
while(F->next!=NULL)
{
wt+=F->time;
awt+=wt;
F=F->next;
printf("\n the waiting time for %d process is %d",F->p_id,wt);
}
printf("\nawt= %0.2f Ms",(float)awt/p_no,"\n\n");
}
/////////////////////////////////////////////////////////////////////////
void PRIORITY()
{
int x,i;
struct QType * front1=front;
sum_time=awt=0;
x=p_no-1;
printf("\nPRIORITY\nThe procecess will execute in this order ");
while(front1->next!=NULL)
{
priority_sortQ();
ex: front1=front;
if(can_exe(front1->a_time))
{
sum_time+=front1->time;
awt+=x*front1->time;
awt=awt-front1->a_time;
x--;
printf("%d ",delQ());
}
else
{
addQ(front->time,front->a_time,front->priority);
delQ();
goto ex;
}
}
// delQ();
printf("\nawt= %0.2f Ms",(float)awt/p_no,"\n\n");
}
/////////////////////////////////////////////////////////////////////////
int can_exe(int item)
{
struct QType *ptr=front;
int x=0;
for(ptr=front;ptr!=NULL;ptr=ptr->next)
{
if(item<=sum_time)
x=1;
}
return x;
}
/////////////////////////////////////////////////////////////////////////
void ROUND_ROBIN()
{
int temp,i=0;
sum_time=awt=wt=0;
front=rear=NULL;
for(i=1;i<=p_no;i++)
{
addQ(arr[i],0,0);
rear->p_id=i;
}
printf("\nROUND ROBIN\n");
while(front!=NULL)
{
if(front->time>Quantum)
{
temp=front->time;
front->executions_no++;
front->time=Quantum;
addQ(temp-front->time,0,0);
rear->executions_no=front->executions_no++;
rear->p_id=front->p_id;
sum_time+=front->time;
delQ();
}
else
{
wt=sum_time-(front->executions_no*Quantum);
printf("\nThe waiting time for the process %d = %d",front->p_id,wt);
sum_time+=front->time;
awt+=wt;
delQ();
}
}
printf("\nThe avg. waiting time is %0.2f",(float)awt/p_no);
}
/////////////////////////////////////////////////////////////////////////
void main()
{
int ch;
char stop;
choose();
printf("\n1- PRESS TO VIEW THE REPORT ");
printf("\n2- Enter new other data ");
printf("\n3- Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nFCFS//////////////////////////////////////\n");
FCFS();
printf("\n\n\n\nPRIORITY//////////////////////////////////\n");
PRIORITY();
printf("\n\n\n\nSJF///////////////////////////////////////\n");
SJF();
printf("\n\n\n\nROUND_ROBIN////////////////////////////////\n");
ROUND_ROBIN();
printf("\n\n\n\n\n\n\n\n\n\nENTER ANY NO. TO EXIT ");
scanf("%d",wt);//working as getche() function//
break;
case 2:
choose();
break;
case 3:
break;
}
}
infamous 26 Junior Poster in Training
jerry@83 0 Newbie Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.