-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.c
156 lines (108 loc) · 2.84 KB
/
scheduler.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int *queue;
int size;
int space;
int head = 0;
int tail = 0;
FILE *fp;
pthread_mutex_t mutex;
pthread_cond_t queue_not_full = PTHREAD_COND_INITIALIZER;
pthread_cond_t queue_not_empty = PTHREAD_COND_INITIALIZER;
void *task(void *arg);
void *cpu(void *arg);
void enqueue(int);
int dequeue(void);
void print_queue();
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: ./main <size>\n");
return 1;
}
size = atoi(argv[1]);
if (size < 1 || size > 10) {
printf("Usage: ./main <1 - 10>");
return 2;
}
space = size;
queue = (int *) malloc(sizeof(int) * size);
fp = fopen("task_list.txt", "r");
if (fp == NULL) {
printf("Error: failed to open file \"task_list\"\n");
return 3;
}
pthread_t taskt, cpu_1, cpu_2, cpu_3;
if (pthread_mutex_init(&mutex, NULL) != 0) {
printf("Error: mutex init has failed\n");
return 4;
}
pthread_create(&taskt, NULL, task, NULL);
pthread_create(&cpu_1, NULL, cpu, (void *) 1);
pthread_create(&cpu_2, NULL, cpu, (void *) 2);
pthread_create(&cpu_3, NULL, cpu, (void *) 3);
pthread_join(taskt, NULL);
pthread_join(cpu_1, NULL);
pthread_join(cpu_2, NULL);
pthread_join(cpu_3, NULL);
pthread_mutex_destroy(&mutex);
free(queue);
fclose(fp);
return 0;
}
void *task(void *arg) {
while (!feof(fp)) {
printf("TASK:\n");
int cpu_burst;
fscanf(fp, "%d", &cpu_burst);
printf("\t+ %d\n", cpu_burst);
enqueue(cpu_burst);
}
for (int i = 0; i < 3; i++)
enqueue(-1);
}
void *cpu(void *arg) {
int cpu_no = (int) arg;
while (1) {
printf("CPU #%d:\n", cpu_no);
print_queue();
int number = dequeue();
printf("\t- %d\n", number);
if (number < 0)
break;
sleep(number);
}
printf("CPU_%d: Exiting\n", cpu_no);
return (NULL);
}
void enqueue(int cpu_burst) {
pthread_mutex_lock(&mutex);
while (space == 0)
pthread_cond_wait(&queue_not_full, &mutex);
pthread_mutex_unlock(&mutex);
*(queue + (tail++ % size)) = cpu_burst;
pthread_mutex_lock(&mutex);
space--;
pthread_cond_signal(&queue_not_empty);
pthread_mutex_unlock(&mutex);
}
int dequeue(void) {
pthread_mutex_lock(&mutex);
while (space == size)
pthread_cond_wait(&queue_not_empty, &mutex);
pthread_mutex_unlock(&mutex);
int cpu_burst = *(queue + (head++ % size));
pthread_mutex_lock(&mutex);
space++;
pthread_cond_signal(&queue_not_full);
pthread_mutex_unlock(&mutex);
return cpu_burst;
}
void print_queue() {
printf("\t");
for (int i = 0; i < size; i++) {
printf("-> %d ", *(queue + i));
}
printf("\n");
}