Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> struct task{//process int id;//pid int bt;//burst time int at

ID: 3775498 • Letter: #

Question

#include <stdio.h>
struct task{//process int id;//pid int bt;//burst time int at;//arrival time int pr;//priority }; typedef struct task Task; typedef Task *TaskPtr;
struct qnode{//a node in the run/ready queue Task data;//process struct qnode *nextPtr; }; typedef struct qnode Qnode; typedef Qnode *QnodePtr;
void enqueue(QnodePtr *headPtr, QnodePtr *tailPtr,Task task); Task dequeue(QnodePtr *headPtr, QnodePtr *tailPtr); int isEmpty(QnodePtr headPtr);
void enqueue(QnodePtr *headPtr, QnodePtr *tailPtr,Task task){ QnodePtr newNodePtr = malloc( sizeof( Qnode)); if(newNodePtr !=NULL){ newNodePtr->data = task; newNodePtr->nextPtr = NULL; } QnodePtr current = *headPtr, prev = NULL; while(current!=NULL && task.pr>=(current->data).pr){ prev = current; current = current->nextPtr; } if(prev==NULL){ newNodePtr->nextPtr= *headPtr; *headPtr=newNodePtr; } else{ newNodePtr->nextPtr=prev->nextPtr; prev->nextPtr=newNodePtr; } if(newNodePtr->nextPtr==NULL){ *tailPtr = newNodePtr; } }
Task dequeue(QnodePtr *headPtr, QnodePtr *tailPtr){ Task value; QnodePtr tempPtr; value = (*headPtr)->data; tempPtr = *headPtr; *headPtr = (*headPtr)->nextPtr; if(*headPtr == NULL){ *tailPtr = NULL; } free (tempPtr); return value; }
int isEmpty(QnodePtr headPtr){ return headPtr == NULL; }
///////////////////////////////////////////////
struct event{//an event event int type;//event type 0:arrival, 1: departure int time;//event time Task task;//the process }; typedef struct event Event; typedef Event *EventPtr;
struct eventQnode{//an node in the events list Event data;//the event struct eventQnode *nextPtr; }; typedef struct eventQnode EventQnode; typedef EventQnode *EventQnodePtr;

void enqueueevent(EventQnodePtr *headPtr, EventQnodePtr *tailPtr,Event e); Event dequeueevent(EventQnodePtr *headPtr, EventQnodePtr *tailPtr); int isEmptyEQ(EventQnodePtr headPtr); void displayEvents(EventQnodePtr currentPtr);
void enqueueevent(EventQnodePtr *headPtr, EventQnodePtr *tailPtr,Event se){ EventQnodePtr newNodePtr = malloc( sizeof( EventQnode)); if(newNodePtr !=NULL){ newNodePtr->data = se; newNodePtr->nextPtr = NULL; } EventQnodePtr current = *headPtr, prev = NULL; while(current!=NULL && se.time>(current->data).time){//find the insert position in order of time prev = current; current = current->nextPtr; } while(current!=NULL && se.time==(current->data).time && se.type<(current->data).type){//then find the insert position in order of event's type prev = current; current = current->nextPtr; }
if(prev==NULL){ newNodePtr->nextPtr= *headPtr; *headPtr=newNodePtr; } else{ newNodePtr->nextPtr=prev->nextPtr; prev->nextPtr=newNodePtr; } if(newNodePtr->nextPtr==NULL){ *tailPtr = newNodePtr; } }
Event dequeueevent(EventQnodePtr *headPtr, EventQnodePtr *tailPtr){ Event value; EventQnodePtr tempPtr; value = (*headPtr)->data; tempPtr = *headPtr; *headPtr = (*headPtr)->nextPtr; if(*headPtr == NULL){ *tailPtr = NULL; } free (tempPtr); return value; }
int isEmptyEQ(EventQnodePtr headPtr){ return headPtr == NULL; }
void displayEvents(EventQnodePtr currentPtr){ if(currentPtr==NULL) printf("The event list is empty ... "); else{ printf("The event list is: "); Event tempevent; while(currentPtr!=NULL){ printf(" time: %d, type : %d : task(id:%d,bt:%d,pr:%d) ", (currentPtr->data).time, (currentPtr->data).type, (currentPtr->data).task.at, (currentPtr->data).task.bt,(currentPtr->data).task.pr); currentPtr=currentPtr->nextPtr; } } }

///////////////////////////////////////////////
const int MAXTASKS = 10; const int MAXBURSTTIME = 70; const int IAT = 30; const int MAXPR = 10;
void main(){ QnodePtr rqheadPtr=NULL, rqtailPtr=NULL;//the run/ready queue EventQnodePtr eventsQheadPtr=NULL, eventsQtailPtr=NULL;//the event queue/list
Task task;//the process structure Event event;//the event structure int prevat = 0, i;//set the previous arrival time to zero for(i=0;i<MAXTASKS;i++){//generate all arrivals and insert them in the event list //fill up the info of the process structure task.id=i; task.at=rand()%IAT+prevat; prevat=task.at; task.bt=rand()%MAXBURSTTIME+1; task.pr=rand()%MAXPR; //fill up the info of the event structure event.type=0;//event type is 0:arrival event.time=task.at;//event time event.task=task;//note that the process is encapsulated in an event structure enqueueevent(&eventsQheadPtr,&eventsQtailPtr,event);//insert the event in the events list } displayEvents(eventsQheadPtr);//Display the events list of all arrivals before starting the simulation

int clock=0;//the sim clock time is currently 0 int idle=1;//CPU is initially idle Event currentEvent;//to hold the current event int wt=0;//waiting time int idletime=0;//CPU idel time printf(" Time: %d: Simulation is started ... ",clock); while(!isEmptyEQ(eventsQheadPtr)){ currentEvent = dequeueevent(&eventsQheadPtr,&eventsQtailPtr);//get an event from the events list clock=currentEvent.time;// if(currentEvent.type==1){//Departure logic idle=1; task = currentEvent.task; printf(" Time: %d : Departure : task(id:%d,bt:%d,pr:%d) has finished. ", clock, task.id,task.bt,task.pr); } if(currentEvent.type==0){//Arrival logic task = currentEvent.task; printf(" Time: %d : Arrival : task(id:%d,bt:%d,pr:%d). ", clock, task.id,task.bt,task.pr); enqueue(&rqheadPtr,&rqtailPtr,task); } if(idle==1 && !isEmpty(rqheadPtr)){//Service logic idle = 0; currentEvent.task=dequeue(&rqheadPtr,&rqtailPtr); wt+=clock-currentEvent.task.at; currentEvent.type=1; currentEvent.time=clock+currentEvent.task.bt; enqueueevent(&eventsQheadPtr,&eventsQtailPtr,currentEvent); task = currentEvent.task; printf(" Time: %d : Serving : task(id:%d,bt:%d,pr:%d). ", clock,task.id,task.bt,task.pr); } }
printf(" Simulation has finished. "); printf(" Results: "); printf("Total tasks served = %d. ", MAXTASKS); printf("Average wating time for all processes = %f ",(double)wt/MAXTASKS); } Name: Simulation of CPU Scheduling (modified priority scheduling) In this assessment you need to simulate a modified version of the priority scheduling. Your simulation will distinguish types of priorities 0, I. and 3 each corresponding to a different real time stream of tasks. You need to generate the data of the tasks for these four types of real time streams as follows: Stream type 0: priority range 0 to 5 Stream type 1: priority range 6 to 10 Stream type 2: priority range l I to 15 Stream type 3: priority range 16 to 20 Produce a tracing file for this simulation ending up with the values for the following performance mreasures of interest for each stream type: average delay, average response time, average turnaround time. average IAT, and average burst time. In this simulation, you will use 1000 tasks. Use a serial number for the process pid in the range 0 to 999. Fix the seed of the rand function to 500 using srand function before generating the data for arrival time, burst time, then priority for each process in this order. The inter arrival time (IAT is in the range 0 to 90. while the burst time is in the range 50 to 200.

Explanation / Answer


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

struct task{//process
   int id;//pid
   int bt;//burst time
   int at;//arrival time
   int pr;//priority
};
typedef struct task Task;
typedef Task *TaskPtr;

struct qnode{//a node in the run/ready queue
   Task data;//process
   struct qnode *nextPtr;
};
typedef struct qnode Qnode;
typedef Qnode *QnodePtr;

void enqueue(QnodePtr *headPtr, QnodePtr *tailPtr,Task task);
Task dequeue(QnodePtr *headPtr, QnodePtr *tailPtr);
int isEmpty(QnodePtr headPtr);

void enqueue(QnodePtr *headPtr, QnodePtr *tailPtr,Task task){
   QnodePtr newNodePtr = malloc(sizeof(Qnode));
   QnodePtr current= headPtr,prev=NULL;

   printf(" Entered in Enqueue");


   if(newNodePtr !=NULL){
       newNodePtr->data = task;
       newNodePtr->nextPtr = NULL;
   }

   while(current!=NULL && task.pr>=(current->data).pr){
       prev = current;
       current = current->nextPtr;
   }
   if(prev==NULL){
       newNodePtr->nextPtr= headPtr;
       headPtr=&newNodePtr;
   }
   else{
       newNodePtr->nextPtr=prev->nextPtr;
       prev->nextPtr=newNodePtr;
   }
   if(newNodePtr->nextPtr==NULL){
       *tailPtr = newNodePtr;
   }
}

Task dequeue(QnodePtr *headPtr, QnodePtr *tailPtr){
   Task value;
   QnodePtr tempPtr;

   printf(" Entered in Dqueue");


   value = (*headPtr)->data;
   tempPtr = *headPtr;
   *headPtr = (*headPtr)->nextPtr;
   if(*headPtr == NULL){
       *tailPtr = NULL;
   }
   free (tempPtr);
   return value;
}

int isEmpty(QnodePtr headPtr){
   return headPtr == NULL;
}

///////////////////////////////////////////////

struct event{//an event event
   int type;//event type 0:arrival, 1: departure
   int time;//event time
   Task task;//the process
};
typedef struct event Event;
typedef Event *EventPtr;

struct eventQnode{//an node in the events list
   Event data;//the event
   struct eventQnode *nextPtr;
};
typedef struct eventQnode EventQnode;
typedef EventQnode *EventQnodePtr;


void enqueueevent(EventQnodePtr *headPtr, EventQnodePtr *tailPtr,Event e);
Event dequeueevent(EventQnodePtr *headPtr, EventQnodePtr *tailPtr);
int isEmptyEQ(EventQnodePtr headPtr);
void displayEvents(EventQnodePtr currentPtr);

void enqueueevent(EventQnodePtr *headPtr, EventQnodePtr *tailPtr,Event se){

   EventQnodePtr newNodePtr = malloc( sizeof( EventQnode));
   EventQnodePtr current = headPtr, prev = NULL;

   printf(" Entered in Enqueue Event");

   if(headPtr!=NULL){
       newNodePtr->data = se;
       newNodePtr->nextPtr = NULL;
       (*headPtr)->nextPtr=newNodePtr;
       tailPtr=newNodePtr;

   }

   while(current!=NULL && se.time>(current->data).time){
       //find the insert position in order of time
       prev = current;
       current = current->nextPtr;
   }
   while(current!=NULL && se.time==(current->data).time && se.type<(current->data).type){
       //then find the insert position in order of event's type
       prev = current;
       current = current->nextPtr;
   }

   if(prev==NULL){
       newNodePtr->nextPtr= *headPtr;
       *headPtr=newNodePtr;
   }
   else{
       newNodePtr->nextPtr=prev->nextPtr;
       prev->nextPtr=newNodePtr;
   }
   if(newNodePtr->nextPtr==NULL){
       *tailPtr = newNodePtr;
   }
}

Event dequeueevent(EventQnodePtr *headPtr, EventQnodePtr *tailPtr){
   Event value;
   EventQnodePtr tempPtr;

   printf(" Entered in Dqueue Event");


   value = (*headPtr)->data;
   tempPtr = *headPtr;
   *headPtr = (*headPtr)->nextPtr;
   if(*headPtr == NULL){
       *tailPtr = NULL;
   }
   free (tempPtr);
   return value;
}

int isEmptyEQ(EventQnodePtr headPtr){
   return headPtr == NULL;
}

void displayEvents(EventQnodePtr currentPtr){
   Event tempevent;
   if(currentPtr==NULL)
       printf("The event list is empty ... ");
   else{
       printf("The event list is: ");

       while(currentPtr!=NULL){
           printf(" time: %d, type : %d : task(id:%d,bt:%d,pr:%d) ",
           (currentPtr->data).time, (currentPtr->data).type, (currentPtr->data).task.at,
           (currentPtr->data).task.bt,(currentPtr->data).task.pr);
           currentPtr=currentPtr->nextPtr;
       }
   }
}


///////////////////////////////////////////////

const int MAXTASKS = 10;
const int MAXBURSTTIME = 70;
const int IAT = 30;
const int MAXPR = 10;

void main(){
   QnodePtr rqheadPtr=NULL, rqtailPtr=NULL;//the run/ready queue
   EventQnodePtr eventsQheadPtr=malloc(sizeof(Qnode)), eventsQtailPtr=NULL;//the event queue/list

   Task task;//the process structure
   Event event;//the event structure
   int prevat = 0, i;//set the previous arrival time to zero

   int clock=0;//the sim clock time is currently 0
   int idle=1;//CPU is initially idle

   Event currentEvent;//to hold the current event
   int wt=0;//waiting time
   int idletime=0;//CPU idel time


   clrscr();
   for(i=0;i<MAXTASKS;i++){//generate all arrivals and insert them in the event list
       //fill up the info of the process structure
       task.id=i;
       task.at=rand()%IAT+prevat;
       prevat=task.at;
       task.bt=rand()%MAXBURSTTIME+1;
       task.pr=rand()%MAXPR;

       //fill up the info of the event structure
       event.type=0;//event type is 0:arrival
       event.time=task.at;//event time
       event.task=task;//note that the process is encapsulated in an event structure

   //   (*eventsQheadPtr).data=event;
   //   eventsQheadPtr->nextPtr=NULL;

       enqueueevent(&eventsQheadPtr,&eventsQtailPtr,event);//insert the event in the events list
   }
   displayEvents(eventsQheadPtr);//Display the events list of all arrivals before starting the simulation

   printf(" Time: %d: Simulation is started ... ",clock);
   while(!isEmptyEQ(eventsQheadPtr)){
       currentEvent = dequeueevent(&eventsQheadPtr,&eventsQtailPtr);//get an event from the events list
       clock=currentEvent.time;//
       if(currentEvent.type==1){//Departure logic
           idle=1;
           task = currentEvent.task;
           printf(" Time: %d : Departure : task(id:%d,bt:%d,pr:%d) has finished. ", clock, task.id,task.bt,task.pr);
       }
       if(currentEvent.type==0){//Arrival logic
           task = currentEvent.task;
           printf(" Time: %d : Arrival : task(id:%d,bt:%d,pr:%d). ", clock, task.id,task.bt,task.pr);
           enqueue(&rqheadPtr,&rqtailPtr,task);
       }

       if(idle==1 && !isEmpty(rqheadPtr)){//Service logic
       idle = 0;
       currentEvent.task=dequeue(&rqheadPtr,&rqtailPtr);
       wt+=clock-currentEvent.task.at;
       currentEvent.type=1;
       currentEvent.time=clock+currentEvent.task.bt;
       enqueueevent(&eventsQheadPtr,&eventsQtailPtr,currentEvent);
       task = currentEvent.task;
       printf(" Time: %d : Serving : task(id:%d,bt:%d,pr:%d). ", clock,task.id,task.bt,task.pr);
       }
   }

   printf(" Simulation has finished. ");
   printf(" Results: ");
   printf("Total tasks served = %d. ", MAXTASKS);

   printf("Average wating time for all processes = %f ",(double)wt/MAXTASKS);
}