/*
FCFS CPU Scheduling Algorithm / Process Scheduling Algorithm
Author : Charudatta Ekbote ( Space Computer Education, SpaceComp Tutorials)
Note :
1) This program is developed to simulate FCFS Process Scheduling or CPU Scheduling Algorithm
2) Program demonstrates the short term scheduler's (STS) working.
3) Program is based on input from user (dynamic data).
4) Program is not checking basic validations for any inputs.
5) Program is desgined to work under ubuntu ( or any linux platform but not for window platform)
6) Program generates Gantt Chart and also outputs Average Turn Around Time, Waiting Time
*/
#include <stdio.h>
#include <stdlib.h>
int nJobs;
struct JobTable
{
int cpuBurst, arrivalTime, nextArrivalTime, completionTime, totalCpuUsed;
} *jt;
struct node
{
int pid, cpuBurst, priority;
struct node *next;
} *head, *end, *cj; // cj is current job selected by STS
void AppendToReadyQueue(int pid, int cpuBurst)
{
struct node *newnode;
newnode = malloc(sizeof(struct node));
newnode->pid = pid;
newnode->cpuBurst = cpuBurst;
newnode->next = NULL;
if( head == NULL )
head = end = newnode;
else
{
end->next = newnode;
end = newnode;
}
}
int time;
void DisplayReadyQueue()
{
struct node *temp = head;
printf("\033[%d;%df",1,15);
printf("@time = %d [", time);
while( temp )
{
printf("P%d(%d)->", temp->pid,temp->cpuBurst);
temp = temp->next;
}
printf("null] ");
}
void LoadJobsInReadyQueue()
{
int i;
for(i=0; i<nJobs; ++i)
{
if( jt[i].nextArrivalTime == time )
AppendToReadyQueue(i, jt[i].cpuBurst);
}
}
int IncompleteJobs()
{
int i;
for(i=0; i<nJobs; ++i)
{
if( jt[i].completionTime == 0 )
return 1;
}
return 0;
}
struct node * FCFS()
{
return head;
}
int DeleteFromReadyQueue()
{
struct node *temp = head;
head = temp->next;
int pid = temp->pid;
free(temp);
return pid;
}
void GetJobPoolData()
{
int i;
printf("enter total jobs in job pool : ");
scanf("%d",&nJobs);
jt = malloc(sizeof(struct JobTable) * nJobs);
for(i=0; i<nJobs; ++i)
{
printf("\nenter job%d details : \n",i);
printf("1st cpu burst : " ); scanf("%d",&jt[i].cpuBurst);
printf("arrival time : " ); scanf("%d",&jt[i].arrivalTime);
jt[i].totalCpuUsed = jt[i].cpuBurst;
jt[i].nextArrivalTime = jt[i].arrivalTime;
jt[i].completionTime = 0;
}
}
main()
{
int r=5,c=1,pid;
system("clear");
GetJobPoolData();
system("clear");
while( IncompleteJobs() )
{
LoadJobsInReadyQueue();
DisplayReadyQueue();
if( cj==NULL )
cj = FCFS();
printf("\033[%d;%df",r,c);
printf("%2d",time);
++time;
if( cj != NULL )
{
printf("\033[%d;%df",r-1,c);
printf("P%d",cj->pid);
--cj->cpuBurst;
if( cj->cpuBurst == 0 )
{
pid = DeleteFromReadyQueue();
cj = NULL;
int r = random()%4; // replace random()%4 with 0 to avoid next cpu burst generations
if( r == 0 )
jt[pid].completionTime = time;
else
{
jt[pid].cpuBurst = r;
jt[pid].totalCpuUsed += r;
jt[pid].nextArrivalTime = time + 2;
}
}
}
c+=4;
if( c>70 )
{
r += 4;
c = 1;
}
getchar();getchar();
}
printf("\033[%d;%df",r,c);
printf("%2d",time);
printf("\033[%d;%df",r-1,c);
printf("P%d",pid);
DisplayReadyQueue();
getchar();getchar();
int totalTat =0, totalWaitingTime =0,tat, waiting_time,i;
c= 1;
printf("\033[%d;%df",r+5,c);
printf("pid\tcpu_used\tarrival\t\tcompletion\ttat\twaiting\n");
for(i=0; i<nJobs; ++i)
{
tat = jt[i].completionTime-jt[i].arrivalTime;
waiting_time = tat - jt[i].totalCpuUsed;
printf("P%d\t%d\t\t%d\t\t1%d\t\t%d\t%d\n",i,jt[i].totalCpuUsed,jt[i].arrivalTime,jt[i].completionTime, tat, waiting_time);
totalTat += tat;
totalWaitingTime += waiting_time;
}
printf("\nAvg Turn Around Time : %f",(float) totalTat / nJobs );
printf("\nAvg Waiting Time : %f",(float) totalWaitingTime / nJobs );
getchar();getchar();
}
FCFS CPU Scheduling Algorithm / Process Scheduling Algorithm
Author : Charudatta Ekbote ( Space Computer Education, SpaceComp Tutorials)
Note :
1) This program is developed to simulate FCFS Process Scheduling or CPU Scheduling Algorithm
2) Program demonstrates the short term scheduler's (STS) working.
3) Program is based on input from user (dynamic data).
4) Program is not checking basic validations for any inputs.
5) Program is desgined to work under ubuntu ( or any linux platform but not for window platform)
6) Program generates Gantt Chart and also outputs Average Turn Around Time, Waiting Time
*/
#include <stdio.h>
#include <stdlib.h>
int nJobs;
struct JobTable
{
int cpuBurst, arrivalTime, nextArrivalTime, completionTime, totalCpuUsed;
} *jt;
struct node
{
int pid, cpuBurst, priority;
struct node *next;
} *head, *end, *cj; // cj is current job selected by STS
void AppendToReadyQueue(int pid, int cpuBurst)
{
struct node *newnode;
newnode = malloc(sizeof(struct node));
newnode->pid = pid;
newnode->cpuBurst = cpuBurst;
newnode->next = NULL;
if( head == NULL )
head = end = newnode;
else
{
end->next = newnode;
end = newnode;
}
}
int time;
void DisplayReadyQueue()
{
struct node *temp = head;
printf("\033[%d;%df",1,15);
printf("@time = %d [", time);
while( temp )
{
printf("P%d(%d)->", temp->pid,temp->cpuBurst);
temp = temp->next;
}
printf("null] ");
}
void LoadJobsInReadyQueue()
{
int i;
for(i=0; i<nJobs; ++i)
{
if( jt[i].nextArrivalTime == time )
AppendToReadyQueue(i, jt[i].cpuBurst);
}
}
int IncompleteJobs()
{
int i;
for(i=0; i<nJobs; ++i)
{
if( jt[i].completionTime == 0 )
return 1;
}
return 0;
}
struct node * FCFS()
{
return head;
}
int DeleteFromReadyQueue()
{
struct node *temp = head;
head = temp->next;
int pid = temp->pid;
free(temp);
return pid;
}
void GetJobPoolData()
{
int i;
printf("enter total jobs in job pool : ");
scanf("%d",&nJobs);
jt = malloc(sizeof(struct JobTable) * nJobs);
for(i=0; i<nJobs; ++i)
{
printf("\nenter job%d details : \n",i);
printf("1st cpu burst : " ); scanf("%d",&jt[i].cpuBurst);
printf("arrival time : " ); scanf("%d",&jt[i].arrivalTime);
jt[i].totalCpuUsed = jt[i].cpuBurst;
jt[i].nextArrivalTime = jt[i].arrivalTime;
jt[i].completionTime = 0;
}
}
main()
{
int r=5,c=1,pid;
system("clear");
GetJobPoolData();
system("clear");
while( IncompleteJobs() )
{
LoadJobsInReadyQueue();
DisplayReadyQueue();
if( cj==NULL )
cj = FCFS();
printf("\033[%d;%df",r,c);
printf("%2d",time);
++time;
if( cj != NULL )
{
printf("\033[%d;%df",r-1,c);
printf("P%d",cj->pid);
--cj->cpuBurst;
if( cj->cpuBurst == 0 )
{
pid = DeleteFromReadyQueue();
cj = NULL;
int r = random()%4; // replace random()%4 with 0 to avoid next cpu burst generations
if( r == 0 )
jt[pid].completionTime = time;
else
{
jt[pid].cpuBurst = r;
jt[pid].totalCpuUsed += r;
jt[pid].nextArrivalTime = time + 2;
}
}
}
c+=4;
if( c>70 )
{
r += 4;
c = 1;
}
getchar();getchar();
}
printf("\033[%d;%df",r,c);
printf("%2d",time);
printf("\033[%d;%df",r-1,c);
printf("P%d",pid);
DisplayReadyQueue();
getchar();getchar();
int totalTat =0, totalWaitingTime =0,tat, waiting_time,i;
c= 1;
printf("\033[%d;%df",r+5,c);
printf("pid\tcpu_used\tarrival\t\tcompletion\ttat\twaiting\n");
for(i=0; i<nJobs; ++i)
{
tat = jt[i].completionTime-jt[i].arrivalTime;
waiting_time = tat - jt[i].totalCpuUsed;
printf("P%d\t%d\t\t%d\t\t1%d\t\t%d\t%d\n",i,jt[i].totalCpuUsed,jt[i].arrivalTime,jt[i].completionTime, tat, waiting_time);
totalTat += tat;
totalWaitingTime += waiting_time;
}
printf("\nAvg Turn Around Time : %f",(float) totalTat / nJobs );
printf("\nAvg Waiting Time : %f",(float) totalWaitingTime / nJobs );
getchar();getchar();
}
No comments:
Post a Comment