Académique Documents
Professionnel Documents
Culture Documents
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
/* Global declarations */
char debugvar[10];
int DEBUG_TRUE;
int nprocess;
NODE *processList;
char filename[] = "sjf.txt";
if(argc > 2)
{
printf("\nError: Extra arguments to %s\n",argv[0]);
exit(EXIT_FAILURE);
}
else if(argc == 2)
{
if(strcmp("-d",(const char*)argv[1]))
{
printf("\nError: Invalid argument %s",argv[1]);
printf("\nOnly valid parameter can be '-d'\n");
exit(EXIT_FAILURE);
}
else
{
DEBUG_TRUE = 1;
}
}
else
{
DEBUG_TRUE == 0;
}
processList = readProcessData(filename,&nprocess);
printProcessData(processList);
simulateSJF(processList,nprocess);
return (0x0000);
}
*n = 0;
start = (NODE*)malloc(sizeof(NODE));
start->ptr = NULL;
temp = start;
fp = fopen(filename,"r");
if(!fp)
{
printf("\nCan not open file: %s",filename);
exit(EXIT_FAILURE);
}
while(1)
{
fscanf(fp,"%d %d %d",&p,&a,&b);
if(feof(fp))
{
fclose(fp);
break;
}
NEW = (NODE*)malloc(sizeof(NODE));
NEW->ptr = NULL;
NEW->arrival = a;
NEW->burst = b;
NEW->PID = p;
temp->ptr = NEW;
temp = NEW;
*n = *n + 1;
}
return start;
while(temp != NULL)
{
printf("\n%12d %12d %12d",temp->PID,temp->arrival,temp->burst);
temp = temp->ptr;
}
printf("\n");
}
/*
* While there are new or waiting processes
*/
while(inputQueue->ptr || waiting->ptr )
{
/*
* move all eligible processes to the waiting queue ;
*/
moveToWaiting(inputQueue,waiting,sysclock);
/*
* fetch the process to be executed during this slot
*/
next = getNext(waiting,current);
sysclock++;
/*
* no process to execute? Inset wait state in the 'terminated' queue
*/
if(next == NULL)
{
insertWaiting(terminated);
continue;
}
current = next;
current->remburst = current->remburst - 1;
/*
* Current process has zero reamaining burst time? move to terminated
*/
if(current->remburst == 0)
{
moveToTerminated(waiting,terminated,current);
}
printSchedulingInfo(terminated,n);
//////////////////////////////////////////////////////////////////////////////////////
// DEBUG
///////////////////////////////////////////////////////////////////////////////////////
if(DEBUG_TRUE)
{
printf("\nEntering 'moveToWaiting'");
printf("\nTime >> %d",ti);
printf("\nThe input queue is:");
printLL(inputQueue);
gets(debugvar);
///////////////////////////////////////////////////////////////////////////////////////
// END DEBUG
///////////////////////////////////////////////////////////////////////////////////////
/*
Reach end of waiting queue
*/
while(temp2->ptr != NULL)
{
temp2 = temp2->ptr;
}
while(temp1->ptr != NULL)
{
if((temp1->ptr)->arrival == ti)
{
temp2->ptr = temp1->ptr;
temp1->ptr = (temp1->ptr)->ptr;
temp2 = temp2->ptr;
temp2->ptr = NULL;
temp2->remburst = temp2->burst;
}
else
{
temp1 = temp1->ptr;
}
} // end of while
/////////////////////////////////////////////////////////////////////////////////////////
// DEBUG
////////////////////////////////////////////////////////////////////////////////////////
if(DEBUG_TRUE)
{
printf("\nLeaving 'moveToWaiting'");
printf("\nThe input queue is:");
printLL(inputQueue);
gets(debugvar);
/////////////////////////////////////////////////////////////////////////////////////////
// END DEBUG
/////////////////////////////////////////////////////////////////////////////////////////
}
/*
* waiting queue empty? return NULL
*/
if(temp == NULL)
{
return NULL;
/*
* else if current process is not null and it has remaining burst time return current;
*/
// The inner if() is reduntant: any process with zero burst time remaining is
// automatically sent to the 'terminated' queue;
//consider removing it.
if(current != NULL )
{
if(current->remburst != 0)
{
return current;
}
}
/* else
// assume first process has minimum burst;
*/
rvalue = temp;
minburst = temp->burst;
while(temp != NULL)
{
if(temp->burst < minburst)
{
minburst = temp->burst;
rvalue = temp;
}
temp = temp->ptr;
}
return rvalue;
while(temp->ptr != NULL)
{
temp= temp->ptr;
}
new = (NODE*)malloc(sizeof(NODE));
new->PID = -1;
new->ptr = NULL;
temp->ptr = new;
temp2 = waiting;
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// DEBUG
if(DEBUG_TRUE)
{
printf("\nInside function 'moveToTerminated '");
printf("\nwaiting queue is:");
printLL(waiting);
printf("terminated queue is:");
printLL(terminated);
}
//END DEBUG
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
while(temp2->ptr != NULL)
{
if(temp2->ptr == tomove) break;
temp2 = temp2->ptr;
}
temp2->ptr= tomove->ptr;
temp1 = terminated;
while(temp1->ptr != NULL)
{
temp1 = temp1->ptr;
}
temp1->ptr = tomove;
tomove->ptr = NULL;
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// DEBUG
if(DEBUG_TRUE)
{
printf("\nLeaving moveToTerminated\n");
printf("Waiting queue is:");
printLL(waiting);
printf("Terminated queue is: ");
printLL(terminated);
}
// END DEBUG
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
while(temp != NULL)
{
arrival = temp->arrival;
burst = temp->burst;
PID = temp->PID;
if(PID == -1)
{
systime++;
temp = temp->ptr;
continue;
else
{
waiting = 0;
}
temp = temp->ptr;
printf("\n");
printf("\n");
while(tmp != NULL)
{
printf(" %d ",tmp->PID);
tmp = tmp->ptr;
}
printf("\n");
}