Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/synchprobs/problems.c
2093 views
1
/*
2
* Copyright (c) 2001, 2002, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
/*
31
* Driver code for whale mating problem
32
*/
33
#include <types.h>
34
#include <lib.h>
35
#include <thread.h>
36
#include <test.h>
37
#include <synch.h>
38
39
/*
40
* 08 Feb 2012 : GWA : Driver code is in kern/synchprobs/driver.c. We will
41
* replace that file. This file is yours to modify as you see fit.
42
*
43
* You should implement your solution to the whalemating problem below.
44
*/
45
46
// 13 Feb 2012 : GWA : Adding at the suggestion of Isaac Elbaz. These
47
// functions will allow you to do local initialization. They are called at
48
// the top of the corresponding driver code.
49
50
void whalemating_init() {
51
return;
52
}
53
54
// 20 Feb 2012 : GWA : Adding at the suggestion of Nikhil Londhe. We don't
55
// care if your problems leak memory, but if you do, use this to clean up.
56
57
void whalemating_cleanup() {
58
return;
59
}
60
61
void
62
male(void *p, unsigned long which)
63
{
64
struct semaphore * whalematingMenuSemaphore = (struct semaphore *)p;
65
(void)which;
66
67
male_start();
68
// Implement this function
69
male_end();
70
71
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
72
// whalemating driver can return to the menu cleanly.
73
V(whalematingMenuSemaphore);
74
return;
75
}
76
77
void
78
female(void *p, unsigned long which)
79
{
80
struct semaphore * whalematingMenuSemaphore = (struct semaphore *)p;
81
(void)which;
82
83
female_start();
84
// Implement this function
85
female_end();
86
87
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
88
// whalemating driver can return to the menu cleanly.
89
V(whalematingMenuSemaphore);
90
return;
91
}
92
93
void
94
matchmaker(void *p, unsigned long which)
95
{
96
struct semaphore * whalematingMenuSemaphore = (struct semaphore *)p;
97
(void)which;
98
99
matchmaker_start();
100
// Implement this function
101
matchmaker_end();
102
103
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
104
// whalemating driver can return to the menu cleanly.
105
V(whalematingMenuSemaphore);
106
return;
107
}
108
109
/*
110
* You should implement your solution to the stoplight problem below. The
111
* quadrant and direction mappings for reference: (although the problem is,
112
* of course, stable under rotation)
113
*
114
* | 0 |
115
* -- --
116
* 0 1
117
* 3 1
118
* 3 2
119
* -- --
120
* | 2 |
121
*
122
* As way to think about it, assuming cars drive on the right: a car entering
123
* the intersection from direction X will enter intersection quadrant X
124
* first.
125
*
126
* You will probably want to write some helper functions to assist
127
* with the mappings. Modular arithmetic can help, e.g. a car passing
128
* straight through the intersection entering from direction X will leave to
129
* direction (X + 2) % 4 and pass through quadrants X and (X + 3) % 4.
130
* Boo-yah.
131
*
132
* Your solutions below should call the inQuadrant() and leaveIntersection()
133
* functions in drivers.c.
134
*/
135
136
// 13 Feb 2012 : GWA : Adding at the suggestion of Isaac Elbaz. These
137
// functions will allow you to do local initialization. They are called at
138
// the top of the corresponding driver code.
139
140
void stoplight_init() {
141
return;
142
}
143
144
// 20 Feb 2012 : GWA : Adding at the suggestion of Nikhil Londhe. We don't
145
// care if your problems leak memory, but if you do, use this to clean up.
146
147
void stoplight_cleanup() {
148
return;
149
}
150
151
void
152
gostraight(void *p, unsigned long direction)
153
{
154
struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
155
(void)direction;
156
157
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
158
// stoplight driver can return to the menu cleanly.
159
V(stoplightMenuSemaphore);
160
return;
161
}
162
163
void
164
turnleft(void *p, unsigned long direction)
165
{
166
struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
167
(void)direction;
168
169
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
170
// stoplight driver can return to the menu cleanly.
171
V(stoplightMenuSemaphore);
172
return;
173
}
174
175
void
176
turnright(void *p, unsigned long direction)
177
{
178
struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
179
(void)direction;
180
181
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
182
// stoplight driver can return to the menu cleanly.
183
V(stoplightMenuSemaphore);
184
return;
185
}
186
187