Modify the code of “FCFS Scheduling Algorithm”, to run processes according to th
ID: 3770902 • Letter: M
Question
Modify the code of “FCFS Scheduling Algorithm”, to run processes according to their priorities. Processes will be generated randomly between 8 and 12. Priority is randomly generated between -5 and 10. CPU burst time should be generated randomly between 12 and 35ms. If two or more processes have the same priority, the process that has shortest job will be executed first. You should print at the end: processes in their execution order, waiting time, and the total processing time. (7 points)
FCFS Code:
#include <stdio.h>
#include <stdlib.h>
struct queueNode {
int pid; // process id
int cpuBurst; // burst time
int wt; // waiting time
// refers to the next queue node
struct queueNode* nextPtr;
};
typedef struct queueNode QUEUE_NODE;
typedef QUEUE_NODE* QUEUE_NODE_PTR;
int main() {
QUEUE_NODE_PTR headPtr = NULL, tailPtr = NULL;
int pid, cpuBurst;
int i, n;
printf("enter number of Jobs: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
// generating a random number between 1 and 10
pid = i; // rand() % 10 + 1;
// generating a random number between 2 and 31
cpuBurst = rand() % 30 + 2;
// defining a variable nodePtr which will be used to store
// the instance of the created node
QUEUE_NODE_PTR nodePtr;
// creating a new instance of queue node by performing
// allocation in the memory
nodePtr = malloc(sizeof(QUEUE_NODE));
// checking if the node has been created
if (nodePtr /* != NULL */) {
// assigning the node's variables
nodePtr->pid = pid;
nodePtr->cpuBurst = cpuBurst;
nodePtr->nextPtr = NULL; // by default nextPtr is pointing to invalid address
// if this is the first iteration
// that means headPtr and tailPtr are null
if (i == 1) {
// assigning both head and tail to the created node
headPtr = tailPtr = nodePtr;
}
else {
// otherwise, setting the next node's pointer
tailPtr->nextPtr = nodePtr;
// moving the node to the last one
tailPtr = nodePtr;
}
}
else { // the memory is full
printf("no memory available ");
}
} // end for loop
if (!headPtr /* headPtr == NULL */) {
printf("cannot execute tasks, the queue is empty.");
Processes CPU Burst
P1 24 ms
P2 3 ms
P3 3 ms
AWT = WT(P1) + WT(P2) + WT(P3)
= 0 + 24 + 27
= 51 / 3 = 17
}
else {
printf(" %d node(s) has been created... Executing ready queue... ", n);
// defining a temp node which will be pointing by default
// to the first one
QUEUE_NODE_PTR currentPtr = headPtr;
int wt = 0, // stores the waiting time of each node
prev_burst = 0; // stores the cpu burst of the previous node
float t_wt = 0; // stores the total waiting time
int align = 0; // used to align the output
while (currentPtr /* currentPtr != NULL */) {
// calculating the total waiting time
wt = wt + prev_burst;
// setting the waiting time of the current node
currentPtr->wt = wt;
// printing the content of the node
printf("--> {pid:%d [bt: %d, wt: %d]} %*.*s",
currentPtr->pid,
currentPtr->cpuBurst,
currentPtr->wt,
align = align + 4);
// storing the burst time of the current node
// before moving to the next one
prev_burst = currentPtr->cpuBurst;
// calculating the total waiting time
t_wt = t_wt + wt;
// moving to the next node
currentPtr = currentPtr->nextPtr;
sleep(1);
} // end while loop
printf("--> {ready.queue is empty}
average waiting time (AWT) is: %.02f ", t_wt / n);
} // end else
return 0;
}
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<string.h>
int
n,cpuBurst[20],Twt,Ttt,A[10],Wt[10],w;
float
Awt,Att;
char
pid[20][20],c[20][20];
void
Getdata();
void
Calculate();
void
fcfs();
//GETTING THE NUMBER OF PROCESSES AND THE cpuBurstRST TIME AND ARRIVAL TIME FOR EACH PROCESS
void
Getdata()
{
int i;
printf (" Enter the number of processes: ");
scanf
("%d",&n);
for
(i=1;i<=n;i++)
{
fflush
(stdin);
printf
(" Enter the process name: ");
scanf
("%s",&pid[i]);
printf
(" Enter The cpuBurstrstTime for Process %s = ",pid[i]);
scanf
(
"%d"
,&cpuBurst[i]);
printf
(" Enter the Arrival Time for Process %s = ",pid[i]);
scanf
("%d",&A[i]);
}
}
printf(" ----------------------------------------------------------- ");
for(i=1;i<=n;i++)
printf("| %s ",pid[i]);
printf("| ");
printf
(" ----------------------------------------------------------- ");
printf
(" ");
for
(i=1;i<=n;i++)
printf
("%d ",Wt[i]);
printf
("%d",Wt[n]+cpuBurst[n]);
printf
(" ----------------------------------------------------------- ");
printf
(" ");
}
//CALCULATING AVERAGE WAITING TIME AND AVERAGE TURN AROUND TIME
void
Calculate()
{
int i;
//For the 1st process
Wt[1]=0;
for
(i=2;i<=n;i++)
{
Wt[i]=cpuBurst[i-1]+Wt[i-1];
}
for
(i=1;i<=n;i++)
{
Twt=Twt+(Wt[i]-A[i]);
Ttt=Ttt+((Wt[i]+cpuBurst[i])-A[i]);
}
Att=(float)Ttt/n;
Awt=(float)Twt/n;
printf
(" Average Turn around time=%3.2f ms ",Att);
printf
(" AverageWaiting Time=%3.2f ms",Awt);
}
//FCFS Algorithm
void
fcfs()
{
int i,j,temp, temp1;
Twt=0;
Ttt=0;
Printf(" FIRST COME FIRST SERVED ALGORITHM ");
for
(i=1;i<=n;i++)
{
for
(j=i+1;j<=n;j++)
{
if
(A[i]>A[j])
{
temp=cpuBurst[i];
temp1=A[i];
cpuBurst[i]=cpuBurst[j];
A[i]=A[j];
cpuBurst[j]=temp;
A[j]=temp1;
strcpy
(c[i],pid[i]);
strcpy
(pid[i],pid[j]);
strcpy
(pid[j],c[i]);
}
}
}
Calculate();
}
void main()
{
int ch;
clrscr();
Getdata();
fcfs();
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.