Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/synchprobs/drivers.c
2092 views
1
/*
2
* 08 Feb 2012 : GWA : Please make any changes necessary to test your code to
3
* the drivers in this file. However, the automated testing suite *will
4
* replace this file in its entirety* with driver code intented to stress
5
* test your synchronization problem solutions.
6
*/
7
8
#include <types.h>
9
#include <lib.h>
10
#include <thread.h>
11
#include <test.h>
12
#include <current.h>
13
#include <synch.h>
14
15
/*
16
* 08 Feb 2012 : GWA : Driver code for the whalemating problem.
17
*/
18
19
/*
20
* 08 Feb 2012 : GWA : The following functions are for you to use when each
21
* whale starts and completes either mating (if it is a male or female) or
22
* matchmaking. We will use the output from these functions to verify the to
23
* verify the correctness of your solution. These functions may spin for
24
* arbitrary periods of time or yield.
25
*/
26
27
void
28
male_start(void)
29
{
30
kprintf("male %s starting\n", curthread->t_name);
31
}
32
void
33
male_end(void)
34
{
35
kprintf("male %s ending\n", curthread->t_name);
36
}
37
void
38
female_start(void)
39
{
40
kprintf("female %s starting\n", curthread->t_name);
41
}
42
void
43
female_end(void)
44
{
45
kprintf("female %s ending\n", curthread->t_name);
46
}
47
void
48
matchmaker_start(void)
49
{
50
kprintf("matchmaker %s starting\n", curthread->t_name);
51
}
52
void
53
matchmaker_end(void)
54
{
55
kprintf("matchmaker %s ending\n", curthread->t_name);
56
}
57
58
/*
59
* 08 Feb 2012 : GWA : The following function drives the entire whalemating
60
* process. Feel free to modify at will, but make no assumptions about the
61
* order or timing of threads launched by our testing suite.
62
*/
63
64
#define NMATING 10
65
66
struct semaphore * whalematingMenuSemaphore;
67
68
int
69
whalemating(int nargs, char **args)
70
{
71
72
int i, j, err=0;
73
74
(void)nargs;
75
(void)args;
76
77
whalematingMenuSemaphore = sem_create("Whalemating Driver Semaphore", 0);
78
if (whalematingMenuSemaphore == NULL) {
79
80
// 08 Feb 2012 : GWA : Probably out of memory, or you broke our
81
// semaphores! Panicing might be an overreaction, but why not?
82
83
panic("whalemating: sem_create failed.\n");
84
}
85
86
// 13 Feb 2012 : GWA : Students are smarter than me.
87
whalemating_init();
88
89
for (i = 0; i < 3; i++) {
90
for (j = 0; j < NMATING; j++) {
91
switch(i) {
92
case 0:
93
err = thread_fork("Male Whale Thread",
94
male, whalematingMenuSemaphore, j, NULL);
95
break;
96
case 1:
97
err = thread_fork("Female Whale Thread",
98
female, whalematingMenuSemaphore, j, NULL);
99
break;
100
case 2:
101
err = thread_fork("Matchmaker Whale Thread",
102
matchmaker, whalematingMenuSemaphore, j, NULL);
103
break;
104
}
105
if (err) {
106
panic("whalemating: thread_fork failed: (%s)\n",
107
strerror(err));
108
}
109
}
110
}
111
for (i = 0; i < 3; i++) {
112
for (j = 0; j < NMATING; j++) {
113
P(whalematingMenuSemaphore);
114
}
115
}
116
sem_destroy(whalematingMenuSemaphore);
117
118
// 13 Feb 2012 : GWA : Students are WAY smarter than me, including Nikhil
119
// Londhe.
120
whalemating_cleanup();
121
122
return 0;
123
}
124
125
/*
126
* 08 Feb 2012 : GWA : Driver code for the stoplight problem.
127
*/
128
129
/*
130
* 08 Feb 2012 : GWA : The following functions should be called by your
131
* stoplight solution when a car is in an intersection quadrant. The
132
* semantics of the problem are that once a car enters any quadrant it has to
133
* be somewhere in the intersection until it call leaveIntersection(), which
134
* it should call while in the final quadrant.
135
*
136
* As an example, let's say a car approaches the intersection and needs to
137
* pass through quadrants 0, 3 and 2. Once you call inQuadrant(0), the car is
138
* considered in quadrant 0 until you call inQuadrant(3). After you call
139
* inQuadrant(2), the car is considered in quadrant 2 until you call
140
* leaveIntersection().
141
*
142
* As in the whalemating example, we will use the output from these functions
143
* to verify the correctness of your solution. These functions may spin for
144
* arbitrary periods of time or yield.
145
*/
146
147
void
148
inQuadrant(int quadrant)
149
{
150
kprintf("car %s in quadrant %d\n", curthread->t_name, quadrant);
151
return;
152
}
153
154
void leaveIntersection()
155
{
156
kprintf("car %s left the intersection\n", curthread->t_name);
157
return;
158
}
159
160
#define NCARS 10
161
162
struct semaphore * stoplightMenuSemaphore;
163
164
int
165
stoplight(int nargs, char **args)
166
{
167
(void)nargs;
168
(void)args;
169
int i, direction, turn, err=0;
170
char name[32];
171
172
stoplightMenuSemaphore = sem_create("Stoplight Driver Semaphore", 0);
173
if (stoplightMenuSemaphore == NULL) {
174
175
// 08 Feb 2012 : GWA : Probably out of memory, or you broke our
176
// semaphores! Panicing might be an overreaction, but why not?
177
178
panic("stoplight: sem_create failed.\n");
179
}
180
181
// 13 Feb 2012 : GWA : Students are smarter than me.
182
stoplight_init();
183
184
for (i = 0; i < NCARS; i++) {
185
186
direction = random() % 4;
187
turn = random() % 3;
188
189
snprintf(name, sizeof(name), "Car Thread %d", i);
190
191
switch(turn) {
192
case 0:
193
err = thread_fork(name, gostraight, stoplightMenuSemaphore,
194
direction, NULL);
195
break;
196
case 1:
197
err = thread_fork(name, turnleft, stoplightMenuSemaphore, direction,
198
NULL);
199
break;
200
case 2:
201
err = thread_fork(name, turnright, stoplightMenuSemaphore, direction,
202
NULL);
203
break;
204
}
205
}
206
207
for (i = 0; i < NCARS; i++) {
208
P(stoplightMenuSemaphore);
209
}
210
211
sem_destroy(stoplightMenuSemaphore);
212
213
// 13 Feb 2012 : GWA : Students are WAY smarter than me, including Nikhil
214
// Londhe.
215
stoplight_cleanup();
216
217
return 0;
218
}
219
220