-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbouncer.c
63 lines (51 loc) · 1.22 KB
/
bouncer.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
#include <stdio.h>
#include "tinycsp.h"
extern struct list RQ;
int main (void) {
// Declare channels
static channel ch;
static process main, producer, consumer;
process *current_process;
// Set up the empty queue
list_init(&RQ);
// Set up the first process.
printf ("Adding main\n");
add_to_front(&main);
// Initialize channels
CHAN_INIT (ch);
// Set the labels for the processes.
SET_LABEL (main);
main.running = 1;
SET_LABEL (producer);
SET_LABEL (consumer);
printf ("Jumping to main...\n");
goto *(main.label);
PROC (producer);
int counter = 0;
FOREVER()
counter = counter + 1;
WRITE (ch, counter);
printf ("P %d\n", counter);
ENDFOREVER()
PROCEND (producer);
PROC (consumer);
int local = 0;
FOREVER()
READ (ch, local);
local += 1;
printf ("C %d\n", local);
ENDFOREVER()
PROCEND (consumer);
PROC (main);
// PAR
// Add everyone to the party.
// Set up parameters
add_to_back (&producer);
producer.running = 1;
add_to_back (&consumer);
consumer.running = 1;
// At PROCEND, we get removed from the runqueue, and
// running is set to 0.
PROCEND (main);
return 0;
}