Вы находитесь на странице: 1из 6

#include<stdio.

h>
#include<stdlib.h>

struct node{
int prnum;
int atime;
int btime;
int wtime;
int tatime;
int xstime;
int xetime;

struct node *ptr;


};

typedef struct node NODE;

void BsortLL(NODE*);
void ProcessLL(NODE*);
void PrintLL(NODE*);
void PrintGchart(NODE*);

int main(void)
{
FILE *fp;
NODE *start;
NODE *temp;
NODE *NEW;
int prnum, atime, btime;

start = (NODE*)malloc(sizeof(NODE));
start->ptr = NULL;

temp = start;

fp = fopen("fcfs.data","r");

if( fp == NULL)
{
printf("\nError opening file");
exit(EXIT_FAILURE);
}

while( 1 )
{
fscanf(fp,"%d %d %d",&prnum,&atime,&btime);
if( feof(fp) )
{
break;
}

NEW = (NODE*)malloc(sizeof(NODE));

if( NEW == NULL )


{
printf("\nError allocating memory");
fclose(fp);
exit(EXIT_FAILURE);
}

NEW->ptr = NULL;
NEW->prnum = prnum;
NEW->atime = atime;
NEW->btime = btime;
temp->ptr = NEW;
temp = temp->ptr;

//printf("\n%d\t%d\t%d",NEW->prnum,NEW->atime,NEW->btime);

} // End of while loop

/****sort the list according to arrival time*****/


BsortLL(start);

/****Print all scheduling info****/


ProcessLL(start);
PrintLL(start);

/****Print Gantt Chart****/


PrintGchart(start);

fclose(fp);
printf("\n");

free(start);

exit(EXIT_SUCCESS);

} //End of main()

/**** BsortLL() ****/


void BsortLL(NODE* LL)
{
NODE *temp1;
NODE *temp2;
int prnum_buf,atime_buf,btime_buf;

temp1 = LL->ptr;

while( temp1->ptr != NULL)


{
temp2 = temp1->ptr;

while(temp2 != NULL)
{
if(temp1->atime > temp2->atime)
{

prnum_buf = temp1->prnum;
atime_buf = temp1->atime;
btime_buf = temp1->btime;

temp1->prnum = temp2->prnum;
temp1->atime = temp2->atime;
temp1->btime = temp2->btime;

temp2->prnum = prnum_buf;
temp2->atime = atime_buf;
temp2->btime = btime_buf;
}
temp2 = temp2->ptr;
}

temp1 = temp1->ptr;

}
}

/*** ProcessLL() ****/


void ProcessLL(NODE* LL)
{
NODE* temp = LL->ptr;
int nprocess = 0 ,w1,a1;
int sigmawt, sigmatat;
int xstime0,xstime1; //the absolute time at wwhich execution starts
int xetime0,xetim1; //xecution end time

xstime0 = temp->atime;
temp->xstime = xstime0;
xetime0 = xstime0 + temp->btime;
temp->xetime = xetime0;

temp->wtime = 0;
temp->tatime = 0 + temp->btime;
nprocess++;

sigmawt = 0;
sigmatat = temp->tatime;

while( temp->ptr !=NULL )


{

temp = temp->ptr;

a1 = temp->atime ;

if( a1 >= xetime0 )


{
w1 = 0;
xstime1 = a1;

else
{
xstime1 = xetime0;
w1 = xstime1 - a1;

temp->wtime = w1;
temp->tatime = temp->wtime + temp->btime;

sigmawt = sigmawt + w1;


sigmatat = sigmatat +temp->tatime;

xstime0 = xstime1;
xetime0 = xstime0 + temp->btime;

temp->xstime = xstime0;
temp->xetime = xetime0;

nprocess++;
}

printf("\n");
printf("Average waiting time = %3d",sigmawt/nprocess);
printf("\n");
printf("Average turn-around time = %3d",sigmatat/nprocess);
printf("\n");

} //End of ProcessLL()

/**** PrintLL() ****/

void PrintLL(NODE *LL)


{
NODE *temp = LL->ptr;

printf("%15s%15s%15s%15s%15s","Pr.No.","Arrival time","Burst time","Waiting time","Turn-aroud


time");
printf("\n");

while( temp != NULL )


{
printf("\n");
printf("%15d",temp->prnum);
printf("%15d",temp->atime);
printf("%15d",temp->btime);
printf("%15d",temp->wtime);
printf("%15d",temp->tatime);

temp = temp->ptr;
}

printf("\n");

/****PrintGchart()******/

void PrintGchart(NODE *LL)


{
NODE* temp;
temp = LL->ptr;

printf("\n\n");

while (temp != NULL)


{
printf("[%d]",temp->xstime);
printf("____P%d____",temp->prnum);

if( ( temp->ptr != NULL ) && ( temp->xetime < temp->ptr->xstime ) )


{

printf("[%d]",temp->xetime);
printf("____");
}

if ( temp->ptr == NULL )
{
printf("[%d]",temp->xetime);
}

temp = temp->ptr;
}

printf("\n\n");

Оценить