include include struct queueNode int pid int cpuburst int w
#include<stdio.h>
#include<stdlib.h>
struct queueNode
{
int pid;
int cpuburst;
int wt;
struct queueNode *nextPtr;
};
typedef struct queueNode QUEUENODE;
typedef QUEUENODE *QUEUENODEPTR;
int main()
{
QUEUENODEPTR headPtr=NULL, tailPtr=NULL;
int pid, cpuburst;
int i,n;
printf(\"Enter no of Jobs: \");
scanf(\"%d\",&n);
for(i=1;i<=n;i++)
{
pid=rand( ) % 10 + 1;
cpuburst=rand( ) % 30 + 2;
QUEUENODEPTR newPtr;
newPtr= malloc(sizeof(QUEUENODE));
//Under Windows use newPtr=new QUEUENODE; to create a new node
if(newPtr !=NULL)
{
newPtr->pid=pid;
newPtr->cpuburst=cpuburst;
newPtr->nextPtr=NULL;
if(i==1)
{
headPtr=newPtr;
tailPtr=newPtr;
}
else
{
tailPtr->nextPtr=newPtr;
tailPtr=newPtr;
}
}
else
printf(\"No memory Available \ \");
}
if(headPtr==NULL)
printf(\"The Queue is Empty: \ \");
else
{
QUEUENODEPTR currentPtr=headPtr;
printf(\"The Queue is \ \");
int WT=0, prevcpuburst=0;
float SUM=0;
while(currentPtr != NULL)
{
WT=WT+prevcpuburst;
currentPtr->wt=WT;
printf(\"%d (%d) -->\",currentPtr->cpuburst,currentPtr->wt);
prevcpuburst=currentPtr->cpuburst;
currentPtr=currentPtr->nextPtr;
SUM=SUM+WT;
}
printf(\"NULL \ \ \");
printf(\"AWT=%f\ \",SUM/n);
}
return 0;
}
Solution
// I have modified the code to Add Arrival time , For part (II) I have one question, Do I have to modify the Code
// which I wrote below or the One pasted Above, Please Comment so that I can do it accordingly
//Below is the code for part (I) , the process are arranged according to the waiting time in The Queue, the waiting time // for 1st Process will be 0 No matter at what time it arrives.
#include<stdio.h>
#include<stdlib.h>
struct queueNode
{
int arrivaltime;
int pid;
int cpuburst;
int wt;
struct queueNode *nextPtr;
};
typedef struct queueNode QUEUENODE;
typedef QUEUENODE *QUEUENODEPTR;
int main()
{
QUEUENODEPTR headPtr=NULL, tailPtr=NULL , temp=NULL;
int pid, cpuburst, arrivaltime;
int i,n;
printf(\"Enter no of Jobs: \");
scanf(\"%d\",&n);
for(i=1;i<=n;i++)
{
pid=rand( ) % 10 + 1;
arrivaltime=rand( ) % 5 + 1;
cpuburst=rand( ) % 30 + 2;
QUEUENODEPTR newPtr;
newPtr= malloc(sizeof(QUEUENODE));
//Under Windows use newPtr=new QUEUENODE; to create a new node
if(newPtr !=NULL)
{
newPtr->pid=pid;
newPtr->arrivaltime=arrivaltime;
newPtr->cpuburst=cpuburst;
newPtr->nextPtr=NULL;
if(i==1)
{
headPtr=newPtr;
tailPtr=newPtr;
}
else if (headPtr->arrivaltime >= newPtr->arrivaltime)
{
newPtr->nextPtr=headPtr;
headPtr = newPtr;
}
else
{
temp = headPtr;
for(;temp->nextPtr!=NULL &&
temp->nextPtr->arrivaltime < newPtr->arrivaltime; temp = temp->nextPtr);
newPtr->nextPtr = temp->nextPtr;
temp->nextPtr = newPtr;
}
}else{
printf(\"No memory Available \ \");
}
}
if(headPtr==NULL)
printf(\"The Queue is Empty: \ \");
else
{
QUEUENODEPTR currentPtr=headPtr;
printf(\"The Queue is \ \");
int WT=0, prevcpuburst=0, arrivaltym=0;
float SUM=0;
while(currentPtr != NULL)
{
if(currentPtr==headPtr)
arrivaltym=0;
else
arrivaltym = currentPtr->arrivaltime;
WT=WT+prevcpuburst-arrivaltym;
currentPtr->wt=WT;
printf(\"%d (%d) (%d)-->\",currentPtr->cpuburst,currentPtr->wt, currentPtr->arrivaltime);
prevcpuburst=currentPtr->cpuburst;
currentPtr=currentPtr->nextPtr;
SUM=SUM+WT;
WT=WT+arrivaltym;
}
printf(\"NULL \ \ \");
printf(\"AWT=%f\ \",SUM/n);
}
return 0;
}


