Beruflich Dokumente
Kultur Dokumente
#include<stdio.h>
#include<stdlib.h>
/*
* Globals
*/
struct node {
int PID;
int ArrivalTime;
int BurstTime;
int Priority;
int RemBurst;
int WaitingTime;
/*
* function prototypes
*/
NODE *ReadProcessData(void);
void SimulatePriority(NODE*);
NODE *GetNextProcess(NODE*,int,int);
void makewait(NODE*,NODE*,int,int);
void SsortLL(NODE*);
void PrintLL(NODE*);
void PrintGChart(int[100][2],int);
int main()
{
NODE *start;
start = ReadProcessData();
SimulatePriority(start);
exit(EXIT_SUCCESS);
} // end of main
/**
* entry point of function ReadProcessData()
*/
NODE *ReadProcessData(void)
{
NODE *start;
NODE *temp;
NODE *NEW;
FILE *fp;
int pid,atime,btime,priority;
start = (NODE*)malloc(sizeof(NODE));
if(!start)
{
printf("\nError in memiry allocation");
exit(EXIT_FAILURE);
}
start->ptr = NULL;
temp = start;
fp = fopen("priority.txt","r");
if(fp == NULL)
{
printf("\nerror openong file \"priority.txt\"");
exit(EXIT_FAILURE);
}
while(1)
{
fscanf(fp,"%d %d %d %d",&pid,&atime,&btime,&priority);
if(feof(fp))
{
fclose(fp);
break;
}
NEW = (NODE*)malloc(sizeof(NODE));
if(!NEW)
{
printf("\nMemory allocation failed");
fclose(fp);
exit(EXIT_FAILURE);
}
NEW->ptr = NULL;
NEW->PID = pid;
NEW->ArrivalTime = atime;
NEW->BurstTime = btime;
NEW->Priority = priority;
NEW->RemBurst = btime;
NEW->WaitingTime = 0;
temp->ptr = NEW;
temp = temp->ptr;
nprocess++;
}
return(start);
} //end of ReadProcessData()
/*
* Entry point of function SimulatePriority()
*/
GanttArray[index][0] = -1;
GanttArray[index][1] = ctime;
index++;
}
GanttArray[index][0] = current->PID;
GanttArray[index][1] = ctime;
index++;
/*
* priority is now the priority of current
* process
*/
cpriority = current->Priority;
/*
* decrease remaining burst time
* of current process by one
* time slice
*/
current->RemBurst -= Tslice;
/*
* increment clock time
*/
ctime += Tslice;
/*
* check if the current process
* has no more burst remaining
*
*/
if( current->RemBurst == 0)
{
ncomplete++;
cpriority = 9999;
}
} //End of while
} //end of SimulatePriority()
/*
* Entry point of makewait()
*/
if(temp->RemBurst != 0)
{
/*
* if the process is not the
* one in execution
*/
if(temp != current)
{
/*
* make this process wait
* for a time slice
*/
temp->WaitingTime += Tslice;
} //end of if#3
} //end of if#2
} //end of if #1
temp = temp->ptr;
} //end of while
} //end of makewait()
/*
* entry point for GetNextProcess()
*/
NODE *GetNextProcess(NODE *list,int ctime, int priority)
{
NODE *temp;
NODE *next = NULL; //next process by default is NULL
temp = list->ptr;
if(temp->RemBurst != 0 )
{
/*
* if this process has a priority
* greater than or equal to
* the current priority
*/
if(temp->Priority <= priority )
{
/*
* then current process should
* be the next process
*/
next = temp;
priority = next->Priority;
} //end of if#3
} //end of if #2
} //end of if #1
temp = temp->ptr;
} //end of while
return(next);
} //end of GetNextProcess()
/*
* entry point for SsortLL()
*/
void SsortLL(NODE *list)
{
NODE *temp1 = list->ptr;
NODE *temp2;
NODE *swapbuf;
swapbuf = (NODE*)malloc(sizeof(NODE));
if(!swapbuf)
{
printf("\nError allocating memory to swapbuf");
exit(EXIT_FAILURE);
}
while(temp1->ptr != NULL)
{
temp2 = temp1->ptr;
while(temp2 != NULL)
{
if(temp2->ArrivalTime < temp1->ArrivalTime)
{
*swapbuf = *temp1;
*temp1 = *temp2;
*temp2 = *swapbuf;
}
temp2 = temp2->ptr;
}
temp1 = temp1->ptr;
}
} //End of SsortLL()
/*
* Entry point for PrintLL()
*/
printf("%15d",tatime);
avg_wtime += temp->WaitingTime;
avg_tatime += tatime;
temp = temp->ptr;
}
printf("\n");
} //end of PrintLL()
/*
* entrypoint for printGChart()
*/
printf("[%d]--P%d--",a[0][1],a[0][0]);
for(count=1 ; count<i; count++)
{
if(a[count][0] != a[count-1][0])
{
printf("[%d]",a[count][1]);
if(a[count][0] == -1)
{
printf("%4d","idle");
}
else
{
printf("--P%d--",a[count][0]);
}
}
}
printf("[%d]",a[i-1][1]+1);
printf("\n");
}