-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart3.c
181 lines (149 loc) · 4.12 KB
/
part3.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
/*##########################################################
## COP4610 – Principles of Operating Systems – Summer C 2017
## Prof. Jose F. Osorio
## Student: Nicolette Celli – 4174075
##
## Project: Multithreaded Programming
## Specs:
## Due Date: 07/9/2017 by 11:55pm
## Module Name:
##
## I certify that this program code has been written by me
## and no part of it has been taken from any sources.
##########################################################*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <omp.h>
// Function declarations
void professor();
void student(int id);
void answer_start();
void answer_done();
void enter_office();
void leave_office();
void question_start();
void question_done();
// Global variables
int num_students, capacity, current_id, asking_id;
int students_in_office, current_students; // keep track for professor
omp_lock_t waiting, asking, ask_question, answer_question;
void professor() {
//printf("%d\n", omp_get_thread_num());
while (current_students != 0) {
// professor answers questions until the students are satisfied
omp_unset_lock(&ask_question);
omp_set_lock(&answer_question);
answer_start();
answer_done();
omp_unset_lock(&ask_question);
omp_set_lock(&answer_question);
}
}
void student(int id) {
//printf("%d\n", id);
int questions = (id % 4) + 1;
// wait for space
omp_set_lock(&waiting);
while (students_in_office == capacity);
omp_unset_lock(&waiting);
// enter office
current_id = id;
students_in_office++;
enter_office();
int i;
for (i = 0; i < questions; i++) { // ask question
omp_set_lock(&asking);
omp_set_lock(&ask_question);
asking_id = id;
question_start();
omp_unset_lock(&answer_question);
omp_set_lock(&ask_question);
question_done();
omp_unset_lock(&answer_question);
omp_unset_lock(&asking);
}
students_in_office--;
current_students--;
current_id = id;
//printf("here, total students: %d\n", current_students);
//printf("in office: %d\n\n", students_in_office);
leave_office();
}
void answer_start() {
printf("Professor starts to answer question for student %d.\n", asking_id);
}
void answer_done() {
printf("Professor is done with answer for student %d.\n", asking_id);
}
void enter_office() {
printf("Student %d enters the office.\n", current_id);
}
void leave_office() {
printf("Student %d leaves the office.\n", current_id);
}
void question_start() {
printf("Student %d asks a question.\n", asking_id);
}
void question_done() {
printf("Student %d is satisfied.\n", asking_id);
}
int main(int argc, char *argv[]) {
// Too many or few arguments
if (argc != 3) {
printf("usage: task1-3 <number_of_students> <capacity>\n");
exit(-1);
}
else {
num_students = atoi(argv[1]);
capacity = atoi(argv[2]);
// validates number of students
if (num_students <= 0) {
printf("Number of students should be an integer greater than 0\n");
exit(-1);
}
// validates office capacity
if (capacity <= 0) {
printf("Office capacity should be an integer greater than 0\n");
exit(-1);
}
//omp_set_num_threads(num_students);
// Create professor thread
//professor();
omp_init_lock(&waiting);
omp_init_lock(&asking);
omp_init_lock(&ask_question);
omp_set_lock(&ask_question); // wait
omp_init_lock(&answer_question);
omp_set_lock(&answer_question); // wait
current_students = num_students;
omp_set_num_threads(num_students + 1);
int tid;
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
if (tid == 0) {
professor();
}
else {
student(tid - 1);
}
if (current_students == 0) { //no students in office
// End of simulation
printf("Office hours are closed!\n");
exit(0);
}
}
/*int i;
for (i = 0; i < num_students; i++) {
// Create all student threads
student(i);
}*/
omp_destroy_lock(&waiting);
omp_destroy_lock(&asking);
omp_destroy_lock(&ask_question);
omp_destroy_lock(&answer_question);
}
return 0;
}