-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj-4.c
127 lines (121 loc) · 1.93 KB
/
proj-4.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
//
// proj-4.c
// proj-4
//
// Created by Sean C Bennett on 10/12/15.
// Created by Shane T. Nelson on 10/12/15.
// Copyright © 2015 Sean C Bennett. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include "sem.h"
sem_t mutex;
sem_t rsem;
sem_t wsem;
unsigned int rwc = 0;
unsigned int wwc = 0;
unsigned int rc = 0;
unsigned int wc = 0;
// Reader CASE 3
void reader(void){
sleep(1);
printf("Entering reader\n");
while(1)
{
//Reader Entry
P(&mutex);
printf("Reader Entry\n");
if((wwc > 0) || (wc > 0))
{
rwc++;
V(&mutex);
P(&rsem);
V(&mutex);
rwc--;
}
rc++;
V(&mutex);
//READ
printf("Reading resource: Reader # %d \n", rc);
sleep(1);
//Reader Exit
P(&mutex);
printf("Reader Exit\n");
rc--;
if((rc == 0) && (wwc > 0))
{
V(&wsem);
}
V(&mutex);
}
return;
}
// Writer CASE 3
void writer(void) {
int i;
sleep(1);
printf("Entering writer\n");
while(1){
//Writer Entry
P(&mutex);
printf("Writer Entry\n");
if((rc > 0) || (wc > 0) || (rwc > 0) || (wwc > 0))
{
wwc++;
V(&mutex);
P(&wsem);
P(&mutex);
wwc--;
}
wc++;
V(&mutex);
//WRITE
printf("Writing to resource: Writer # %d \n", wc);
sleep(1);
//Writer Exit
P(&mutex);
printf("Writer Exit\n");
wc--;
if(rwc > 0)
{
for(i = 0; i <= rwc; i++)
{
V(&rsem);
}
}
else if(wwc > 0)
{
V(&wsem);
}
V(&mutex);
}
return;
}
// Main function
int main() {
int ret1, ret2, ret3;
TCB_t *RunQ = 0;
InitQueue(&RunQ);
ret1 = InitSem(&mutex, 1);
if(ret1 < 0)
{
printf("Error initializing access semaphore");
}
ret2 = InitSem(&rsem, 1);
if(ret2 < 0)
{
printf("Error initializing reader semaphore");
}
ret3 = InitSem(&wsem, 1);
if(ret3 < 0)
{
printf("Error initializing order semaphore");
}
start_thread(reader);
start_thread(reader);
start_thread(reader);
start_thread(writer);
start_thread(writer);
run();
return 0;
}