Skip to content

Commit dbfad6b

Browse files
author
21lva
committed
final
1 parent fe02521 commit dbfad6b

18 files changed

+5059
-88
lines changed

Diff for: algorithms.h.gch

4 KB
Binary file not shown.

Diff for: constants.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef CONSTANTS_H
22
#define CONSTANTS_H
33

4+
#define NUMEPISODE 100
45
#define BYPROCESS 1
56
#define BYIO 0
67
#define BYTQ 2

Diff for: evaluation.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#include "evaluation.h"
2+
#define FFCFS "./testfile/fcfs.txt"
3+
#define FNSJF "./testfile/nsjf.txt"
4+
#define FPSJF "./testfile/psjf.txt"
5+
#define FNPRI "./testfile/npri.txt"
6+
#define FPPRI "./testfile/ppri.txt"
7+
#define FRR "./testfile/rr.txt"
28

39
void GanttChart(Result* result){
410
if(result==NULL)printf("fuck1\n");
@@ -21,7 +27,7 @@ void GanttChart(Result* result){
2127
printf("%d\n",cur->finishedT);
2228
}
2329

24-
void WTTT(Result* result){
30+
void WTTT(Result* result,int Alg){
2531
stack* Stack = result->finished;
2632
int ttt=0,count=0,twt=0;//total turnaound time, count, total waiting time
2733
while(stack_last(Stack)!=NULL){
@@ -35,6 +41,22 @@ void WTTT(Result* result){
3541
}
3642
printf("\ntotal waiting time :%3d , total turnaround time :%3d\n",twt,ttt);
3743
printf("average waiting time :%.1lf , average turnaround time :%.1lf\n",(double)twt/count,(double)ttt/count);
44+
FILE *f;
45+
if(Alg==1)
46+
f=fopen(FFCFS,"at");
47+
else if(Alg==2)
48+
f=fopen(FNSJF,"at");
49+
else if(Alg==3)
50+
f=fopen(FPSJF,"at");
51+
else if(Alg==4)
52+
f=fopen(FNPRI,"at");
53+
else if(Alg==5)
54+
f=fopen(FPPRI,"at");
55+
else
56+
f=fopen(FRR,"at");
57+
58+
fprintf(f,"%lf %lf %lf\n",(double)twt/count,(double)ttt/count,(result->totaltime-result->idle)/(double)result->totaltime);
59+
fclose(f);
3860
}
3961

4062
void CpuUtilization(Result* result){

Diff for: evaluation.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "mainheader.h"
55

66
void GanttChart(Result* result);
7-
void WTTT(Result* result);
7+
void WTTT(Result* result,int Alg);
88
void CpuUtilization(Result* result);
99

1010
#endif

Diff for: fcfs.c

-15
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ Result* FCFSA(process* parray[],int nump){
3838
first++;
3939
first%=nump+1;
4040
}
41-
/*
42-
if(IsAvailProcess(ready,1,time))
43-
{
44-
// printf("sdafsdffqfwefqwfqwf\n");
45-
ChangeRunning(running,ready);
46-
printf("id %d\n",running->Process->processID);
47-
}*/
4841
}
4942

5043
else if(RunningFinished(running)){
@@ -55,10 +48,6 @@ Result* FCFSA(process* parray[],int nump){
5548
first++;
5649
first%=nump+1;
5750
}
58-
/*
59-
if(IsAvailProcess(ready,1,time)){
60-
ChangeRunning(running,ready);
61-
}*/
6251
}
6352

6453
else if(RunningInterrupted(running,BYIO)){
@@ -69,10 +58,6 @@ Result* FCFSA(process* parray[],int nump){
6958
first++;
7059
first%=nump+1;
7160
}
72-
/*
73-
if(IsAvailProcess(ready,1,time)){
74-
ChangeRunning(running,ready);
75-
}*/
7661
else{
7762
running->Process=NULL;
7863
}

Diff for: main.c

+21-32
Original file line numberDiff line numberDiff line change
@@ -23,66 +23,55 @@ void config(int* IsRandom,int* Alg,int* numP,int* tq){
2323
}
2424

2525
int main(){
26-
int IsRandom,Alg,numP=3,tq=0,i;
27-
srand(time(NULL));
26+
int IsRandom,Alg,numP=3,tq=0,i,ii;
2827
config(&IsRandom,&Alg,&numP,&tq);
28+
process*** parray=(process***)malloc(sizeof(process**)*100);
29+
for(ii=0;ii<NUMEPISODE;ii++){
30+
srand(time(NULL)*(ii)+time(NULL)*ii*ii);
2931
printf("**********************************************************************\n");
3032
printf("Random : %d ,Algorithm : %s , number of process : %d, time quantum : %d\n",IsRandom,algarr[Alg],numP,tq);
3133
printf("**********************************************************************\n");
32-
process** parray = (process**)malloc(sizeof(process*)*numP);
34+
parray[ii] = (process**)malloc(sizeof(process*)*numP);
3335
printf("IF there are process that have same arrival time\nYou must type first coming process first\n");
3436
for(i=0;i<numP;i++){
3537
printf("*******insert value of %dth process******\n",i+1);
36-
parray[i]=make_process(IsRandom);
38+
parray[ii][i]=make_process(IsRandom);
3739
}
3840
Result* result=NULL;
3941
printf("##############################################################\n");
4042
for(i=0;i<numP;i++){
41-
printf("%d process\nCpuBT : %d IOT : %d ArivalT : %d Priority : %d\n",i+1,parray[i]->cpuBT,parray[i]->IOBT,parray[i]->arrivalT,parray[i]->priority);
42-
int lc=parray[i]->CpuIO.LeftCpu,li=parray[i]->CpuIO.LeftIO,j=0;
43+
printf("%d process\nCpuBT : %d IOT : %d ArivalT : %d Priority : %d\n",i+1,parray[ii][i]->cpuBT,parray[ii][i]->IOBT,parray[ii][i]->arrivalT,parray[ii][i]->priority);
44+
int lc=parray[ii][i]->CpuIO.LeftCpu,li=parray[ii][i]->CpuIO.LeftIO,j=0;
4345
while(lc>0||li>0){
4446
if(j%2==0)printf("CPU:");
4547
else printf("IO:");
46-
printf("[%d] ",parray[i]->CpuIO.TurnArray[j]);
47-
if(j%2==0)lc-=parray[i]->CpuIO.TurnArray[j];
48-
else li-=parray[i]->CpuIO.TurnArray[j];
48+
printf("[%d] ",parray[ii][i]->CpuIO.TurnArray[j]);
49+
if(j%2==0)lc-=parray[ii][i]->CpuIO.TurnArray[j];
50+
else li-=parray[ii][i]->CpuIO.TurnArray[j];
4951
j++;
5052
}
5153
printf("\n");
5254
}
53-
/*
54-
process** parray1 = (process**)malloc(sizeof(process*)*numP);
55-
process** parray2 = (process**)malloc(sizeof(process*)*numP);
56-
process** parray3 = (process**)malloc(sizeof(process*)*numP);
57-
process** parray4 = (process**)malloc(sizeof(process*)*numP);
58-
process** parray5 = (process**)malloc(sizeof(process*)*numP);
59-
copyProcess(parray,parray1,numP);
60-
copyProcess(parray,parray2,numP);
61-
copyProcess(parray,parray3,numP);
62-
copyProcess(parray,parray4,numP);
63-
copyProcess(parray,parray5,numP);*/
6455
printf("##############################################################\n");
65-
while(1){
66-
// printf("Choose Algorithm\n1. FCFS\n2. non-preemptive SJF\n3. preemptive SJF\n4. non-preemptive priority\n5. preemptive priority\n6. Round Robin : \n");
67-
// scanf("%d",&Alg);
56+
6857
if(Alg==FCFS)
69-
result = FCFSA(parray,numP);
58+
result = FCFSA(parray[ii],numP);
7059
else if(Alg==NONP_SJF)
71-
result = SJFA(parray,numP,0);
60+
result = SJFA(parray[ii],numP,0);
7261
else if(Alg==P_SJF)
73-
result = SJFA(parray,numP,1);
62+
result = SJFA(parray[ii],numP,1);
7463
else if(Alg==NONP_PRIORITY)
75-
result = PRIORITYA(parray,numP,0);
64+
result = PRIORITYA(parray[ii],numP,0);
7665
else if(Alg==P_PRIORITY)
77-
result = PRIORITYA(parray,numP,1);
66+
result = PRIORITYA(parray[ii],numP,1);
7867
else if(Alg==RR)
79-
result = RRA(parray,numP,tq);
68+
result = RRA(parray[ii],numP,tq);
8069
printf("##############################################################\n");
81-
break;
82-
}
8370
GanttChart(result);
84-
WTTT(result);
71+
WTTT(result,Alg);
8572
CpuUtilization(result);
73+
free(parray[ii]);
74+
}
8675
return 0;
8776
}
8877

Diff for: op

112 Bytes
Binary file not shown.

Diff for: process.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,13 @@ void make_turn(process* pp,int Is_random,int cputime,int iotime){
3030
tmpio-=target[index++];
3131
next=0;
3232
}
33-
xx++;
3433
}
3534
if(tmpcpu>0){
3635
target[index++]=tmpcpu;
37-
xx++;
3836
}
3937
if(tmpio>0){
4038
target[index++]=tmpio;
41-
xx++;
4239
}
43-
pp->CpuIO.X=xx;
4440
}
4541
else{
4642
int index=0,next=0,xx=0;
@@ -65,17 +61,13 @@ void make_turn(process* pp,int Is_random,int cputime,int iotime){
6561
tmpio-=target[index++];
6662
next=0;
6763
}
68-
xx++;
6964
}
7065
if(tmpcpu>0){
7166
target[index++]=tmpcpu;
72-
xx++;
7367
}
7468
if(tmpio>0){
7569
target[index++]=tmpio;
76-
xx++;
7770
}
78-
pp->CpuIO.X=xx;
7971
}
8072
}
8173

@@ -117,7 +109,7 @@ process* make_process(int Is_random){
117109
make_turn(newp,Is_random,newp->cpuBT,newp->IOBT);
118110
}
119111
return newp;
120-
112+
}
121113

122114
void interrupt_process(process* target,int bywhom,int time){
123115
if(bywhom==BYPROCESS||bywhom==BYTQ){

Diff for: rr.c

+1-30
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ Result* RRA(process* parray[],int nump,int TimeQuantum){
4040
running->timeQuantum=TimeQuantum;
4141
}
4242

43-
/*
44-
if(IsAvailProcess(ready,1,time)){
45-
ChangeRunning(running,ready);
46-
running->timeQuantum=TimeQuantum;
47-
}*/
4843
}
4944

5045
else if(RunningFinished(running)){
@@ -55,12 +50,7 @@ Result* RRA(process* parray[],int nump,int TimeQuantum){
5550
first%=nump+1;
5651
running->timeQuantum=TimeQuantum;
5752
}
58-
/*
59-
if(IsAvailProcess(ready,1,time)){
60-
ChangeRunning(running,ready);
61-
running->timeQuantum=TimeQuantum;
62-
}*/
63-
}
53+
}
6454

6555
else if(RunningInterrupted(running,BYIO)){
6656
interrupt_process(running->Process,BYIO,time);
@@ -71,19 +61,13 @@ Result* RRA(process* parray[],int nump,int TimeQuantum){
7161
first%=nump+1;
7262
running->timeQuantum=TimeQuantum;
7363
}
74-
/*
75-
if(IsAvailProcess(ready,1,time)){
76-
ChangeRunning(running,ready);
77-
running->timeQuantum=TimeQuantum;
78-
}*/
7964
else{
8065
running->Process=NULL;
8166
}
8267
}
8368
else if(running->timeQuantum ==0){
8469
standby[last]=running->Process;
8570
interrupt_process(running->Process,BYTQ,time);
86-
//heap_insert(ready,running->Process);
8771
last++;
8872
last%=nump+1;
8973
if(last!=first){
@@ -92,23 +76,10 @@ Result* RRA(process* parray[],int nump,int TimeQuantum){
9276
first%=nump+1;
9377
running->timeQuantum=TimeQuantum;
9478
}
95-
/*
96-
if(IsAvailProcess(ready,1,time)){
97-
ChangeRunning(running,ready);
98-
running->timeQuantum=TimeQuantum;
99-
}*/
10079
else{
10180
running->Process=NULL;
10281
}
10382
}
104-
/*
105-
else if(IsPreemptive&&RunningInterrupted(running,BYPROCESS)){
106-
interrupt_process(running->Process,BYPROCESS,time);
107-
heap_insert(ready,running->Process);
108-
if(IsAvailProcess(ready,1,time)){
109-
ChangeRunning(running,ready);
110-
}
111-
}*/
11283
if(AllFinished(running,ready,ready)){
11384
break;
11485
}

Diff for: testfile/a.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
int main(){
4+
FILE *fcfs,*nsjf,*psjf,*npri,*ppri,*rr;
5+
fcfs=fopen("fcfs.txt","rt");
6+
nsjf=fopen("nsjf.txt","rt");
7+
psjf=fopen("psjf.txt","rt");
8+
npri=fopen("npri.txt","rt");
9+
ppri=fopen("ppri.txt","rt");
10+
rr=fopen("rr.txt","rt");
11+
}

Diff for: testfile/all.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5.982857142857143 17.082142857142856 0.822905257142857
2+
4.5789285714285715 15.525 0.7979731185714284
3+
3.5107142857142857 14.425714285714285 0.7700569757142861
4+
5.673571428571429 16.609642857142855 0.801424444285714
5+
5.2625 16.194285714285716 0.7854280585714281
6+
6.0075 17.010357142857142 0.8170534185714281

0 commit comments

Comments
 (0)