-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproiect_10_RR(1).c
220 lines (167 loc) · 4.12 KB
/
proiect_10_RR(1).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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <time.h>
#define NR_THREADS 3
int count;
int wait_t;
int timp_total;
typedef struct
{
pthread_t id;//id-ul threadului
int status;
int aux_q;
int timp_executie;
int creste;
int timp_initial;
sem_t run;//are un semafor propriu
} thread;
typedef struct {
int quantum;//pt round robin
int nr_threads;
int size_q;
thread* q[101] ;//coada de prioritati
thread* threads[101];//vector de threaduri
thread* running;//threadul care ruleaza in acel moment
} scheduler;
scheduler s;
void *thread_routine(void *args)
{
thread *t;
t = (thread *)args;
start_waiting:
if( sem_wait(&t->run))
{
perror(NULL);
return errno;
}
t->aux_q = 0;
t->creste =0;
while( t->aux_q < s.quantum && t->timp_executie > 0 )
{
t->timp_executie--;
t->creste++;
t->aux_q ++;
}
timp_total = timp_total + t->creste;
update_scheduler();
if( t->timp_executie > 0) // nu s - terminat procesul
goto start_waiting;
printf("\n------------------------\n");
printf("Arrival Time: %d\n",0);
printf("Completion Time:%d\n",timp_total );
printf("Burst Time: %d\n", t->timp_initial);
printf("Turn Around Time:%d\n",timp_total);
printf("Waiting Time:%d\n",timp_total- t->timp_initial);
wait_t = wait_t + timp_total - t->timp_initial;
}
void init_thread()
{
thread* t;
t = malloc(sizeof(thread));
t->timp_executie = rand() %10 +1; //sa nu aiba timp de executie 0
t->status =0;
t->timp_initial = t->timp_executie;
//cream structura de thread
//ii initializam semaforul, va pleca cu S=0
if(sem_init(&t->run, 0, 0)){ //
perror(NULL);
return errno;
}
s.threads[s.nr_threads++]=t;
s.q[s.size_q++]=t;
if( pthread_create(& (t->id), NULL, thread_routine, (void *)t)){
perror(NULL);
return errno;
}
}
void start_thread(thread *t)
{
if(sem_post(&t->run)){
perror(NULL);
return errno;
}
t->status = 1;
}
void update_scheduler()
{
thread* next;
thread* current;
current = s.running;
if (current == NULL) {
next = s.q[0]; // ordona dupa timp_sosire--->q
s.running = next;
start_thread(next);
}
else
{
if( current->timp_executie == 0) //procesul s-a terminat
{
int i = 0;
for(i = 0; i < s.size_q-1; i++)
s.q[i] = s.q[i+1]; //il scoatem din coada
current->status = 0;
s.size_q--;
if(s.size_q >0) //
{ next = s.q[0];
s.running = next;
start_thread(next); }
}
else
{ //procesul mai are de executat dar s a terminat cuanta
printf(" threadul cu timpul%d\n", current ->timp_initial);
if( current->aux_q == s.quantum)
{
int i=0;
for(i=0;i<s.size_q-1;i++)
s.q[i] = s.q[i+1];
s.q[s.size_q-1]= current; // il punem la finalul cozii
current->status=0;
next = s.q[0];
s.running = next;
start_thread(next);
}
}
}
}
//}
void destroy_thread(thread *t)
{
if(sem_destroy(&t->run)){ //distrugem semafor
perror(NULL);
return errno;
}
free(t); //eliberam memoria threadului
}
void s_end( )
{
int i;
for (i = 0; i < s.nr_threads; i++) { //asteptam threadurile
if( pthread_join(s.threads[i]->id, NULL))
{
perror(NULL);
return errno;
}
}
for (i = 0; i < s.nr_threads; i++)
destroy_thread(s.threads[i]);
}
int main()
{ int i;
s.quantum = 3;
s.size_q = 0;
s.nr_threads = 0;
s.running = NULL;
for (i=0; i<NR_THREADS; i++)
{
init_thread();
}
update_scheduler();
s_end();
printf("\n=========================\n");
printf(" Timpul total in care ruleaza procesele este %d\n", timp_total);
printf(" Timpul mediu de astptare:%f\n", (double)wait_t / s.nr_threads);
}