forked from yhzhang0128/egos-2000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ult.c
121 lines (93 loc) · 2.62 KB
/
ult.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
/*
* (C) 2023, Cornell University
* All rights reserved.
*/
/* Author: Robbert van Renesse
* Description: course project, user-level threading
* Students implement a threading package and semaphore;
* And then spawn multiple threads as either producer or consumer.
*/
#include "app.h"
/** These two functions are defined in grass/context.S **/
void ctx_start(void** old_sp, void* new_sp);
void ctx_switch(void** old_sp, void* new_sp);
/** Multi-threading functions **/
struct thread {
/* Student's code goes here. */
};
void thread_init(){
/* Student's code goes here */
}
void ctx_entry(void){
/* Student's code goes here. */
}
void thread_create(void (*f)(void *), void *arg, unsigned int stack_size){
/* Student's code goes here. */
}
void thread_yield(){
/* Student's code goes here. */
}
void thread_exit(){
/* Student's code goes here. */
}
/** Semaphore functions **/
struct sema {
/* Student's code goes here. */
};
void sema_init(struct sema *sema, unsigned int count){
/* Student's code goes here. */
}
void sema_inc(struct sema *sema){
/* Student's code goes here. */
}
void sema_dec(struct sema *sema){
/* Student's code goes here. */
}
int sema_release(struct sema *sema){
/* Student's code goes here. */
}
/** Producer and consumer functions **/
#define NSLOTS 3
static char *slots[NSLOTS];
static unsigned int in, out;
static struct sema s_empty, s_full;
static void producer(void *arg){
for (;;) {
// first make sure there's an empty slot.
// then add an entry to the queue
// lastly, signal consumers
sema_dec(&s_empty);
slots[in++] = arg;
if (in == NSLOTS) in = 0;
sema_inc(&s_full);
}
}
static void consumer(void *arg){
for (int i = 0; i < 5; i++) {
// first make sure there's something in the buffer
// then grab an entry to the queue
// lastly, signal producers
sema_dec(&s_full);
void *x = slots[out++];
printf("%s: got '%s'\n", arg, x);
if (out == NSLOTS) out = 0;
sema_inc(&s_empty);
}
}
int main() {
printf("User-level threading is not implemented.\n");
/*
thread_init();
sema_init(&s_full, 0);
sema_init(&s_empty, NSLOTS);
thread_create(consumer, "consumer 1", 16 * 1024);
thread_create(consumer, "consumer 2", 16 * 1024);
thread_create(consumer, "consumer 3", 16 * 1024);
thread_create(consumer, "consumer 4", 16 * 1024);
thread_create(producer, "producer 2", 16 * 1024);
thread_create(producer, "producer 3", 16 * 1024);
producer("producer 1");
thread_exit();
*/
return 0;
}