-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
172 lines (161 loc) · 4.62 KB
/
process.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "process.h"
#include <time.h>
//srand(time(NULL));
void randomize(){
srand(time(NULL));
int i=0;
for(i=0;i<(rand()%RAND_MAX);i++)(rand()%RAND_MAX);
}
void make_turn(process* pp,int Is_random,int cputime,int iotime){
int tmpcpu=cputime,tmpio=iotime;//variables used to check how much time we used for each operation
pp->CpuIO.TurnArray=(int*)malloc(sizeof(int)*(1+cputime+iotime));
int *target=pp->CpuIO.TurnArray;
if(Is_random){
//if the turn should be decided randomly
//the first operation must cpu operation
//although it is just 1 time unit
target[0]=rrandom(tmpcpu);
tmpcpu-=target[0];
int next=1,index=1,xx=0;
while(tmpcpu>0 && tmpio>0){
if(next==0){
target[index]=rrandom(tmpcpu);
tmpcpu-=target[index++];
next=1;
}
else{
target[index]=rrandom(tmpio);
tmpio-=target[index++];
next=0;
}
}
if(tmpcpu>0){
target[index++]=tmpcpu;
}
if(tmpio>0){
target[index++]=tmpio;
}
}
else{
int index=0,next=0,xx=0;
while(tmpcpu>0&&tmpio>0){
if(next==0){
printf("CPU type %dth operation, insert how much?? from 1 to %d : ",index+1,tmpcpu);
scanf("%d",&target[index]);
if(target[index]<=0||target[index]>tmpcpu){
printf("the value must be more than zero and smaller than or equal to %d",tmpcpu);
continue;
}
tmpcpu-=target[index++];
next=1;
}
else{
printf("IO type %dth operation, insert how much?? from 1 to %d : ",index+1,tmpio);
scanf("%d",&target[index]);
if(target[index]<=0||target[index]>tmpio){
printf("the value must be more than zero and smaller than or equal to %d",tmpio);
continue;
}
tmpio-=target[index++];
next=0;
}
}
if(tmpcpu>0){
target[index++]=tmpcpu;
}
if(tmpio>0){
target[index++]=tmpio;
}
}
}
process* make_process(int Is_random){
static int pidDist=0;//process id will be distributed as auto increment
process* newp = (process*)malloc(sizeof(process)*1);
if(Is_random){
newp->processID=++pidDist;
newp->cpuBT = rrandom(CPUBT_RANGE);
newp->IOBT = rrandom(IOBT_RANGE);
newp->arrivalT = rrandom(ARRIVALT_RANGE);
newp->priority = rrandom(PRIORITY_RANGE);
newp->gettingT = newp->arrivalT;
newp->finishedT=-1;
newp->CpuIO.NextType=0;
newp->CpuIO.Index=0;
newp->CpuIO.LeftCpu=newp->cpuBT;
newp->CpuIO.LeftIO=newp->IOBT;
printf("id : %d cpubt : %d iobt : %d arri : %d prio :%d\n",newp->processID,newp->cpuBT,newp->IOBT,newp->arrivalT,newp->priority);
make_turn(newp,Is_random,newp->cpuBT,newp->IOBT);
}
else{
newp->processID=++pidDist;
printf("insert cpu burst time : ");
scanf("%d",&(newp->cpuBT));
printf("insert I/O burst time : ");
scanf("%d",&(newp->IOBT));
printf("insert arrival time : ");
scanf("%d",&(newp->arrivalT));
printf("insert priority : ");
scanf("%d",&(newp->priority));
newp->finishedT=-1;
newp->gettingT = newp->arrivalT;
newp->CpuIO.NextType=0;
newp->CpuIO.Index=0;
newp->CpuIO.LeftCpu=newp->cpuBT;
newp->CpuIO.LeftIO=newp->IOBT;
make_turn(newp,Is_random,newp->cpuBT,newp->IOBT);
}
return newp;
}
void interrupt_process(process* target,int bywhom,int time){
if(bywhom==BYPROCESS||bywhom==BYTQ){
//in case of interrupted by a process or time slice expired
target->gettingT = time;
}
else{
//If the interruption is due to an I/O operation
target->gettingT = time + target->CpuIO.TurnArray[target->CpuIO.Index+1];
target->CpuIO.LeftIO -= target->CpuIO.TurnArray[target->CpuIO.Index+1];
target->CpuIO.Index+=2;
}
}
void remove_process(process* target){
free(target);
}
void turn_free(turn* target){
free(target->TurnArray);
free(target);
}
void process_free(process* target){
turn_free(&(target->CpuIO));
free(target);
}
void _FinishProcess(process* target, int time){
target->finishedT=time;
if(target->CpuIO.LeftIO!=0)target->finishedT+=target->CpuIO.LeftIO;
}
int Is_finished_process(process* target){
if(target->CpuIO.LeftCpu<=0){
return 1;
}
return 0;
}
/*
void copyProcess(process* parray1[],process* parray2[],int numP){
int i =0,j=0 ;
for(i=0 ; i<numP ; i++){
parray2[i]->processID=parray1[i]->processID;
parray2[i]->cpuBT=parray1[i]->cpuBT;
parray2[i]->IOBT = parray1[i]->IOBT;
parray2[i]->arrivalT = parray1[i]->arrivalT;
parray2[i]->gettingT = parray1[i]->gettingT;
parray2[i]->priority = parray1[i]->priority;
parray2[i]->finishedT = parray1[i]->finishedT;
parray2[i]->CpuIO.NextType = parray1[i]->CpuIO.NextType;
parray2[i]->CpuIO.Index = parray1[i]->CpuIO.Index;
parray2[i]->CpuIO.LeftIO = parray1[i]->CpuIO.LeftCpu;
parray2[i]->CpuIO.LeftCpu = parray1[i]->CpuIO.LeftCpu;
for(j=0 ; j<parray1[i]->CpuIO.X ; j++){
parray2[i]->CpuIO.TurnArray[j] = parray1[i]->CpuIO.TurnArray[j];
}
}
}*/