File tree 1 file changed +119
-2
lines changed
1 file changed +119
-2
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,11 @@ backgroundColor: white
33
33
- 早期的操作系统的主要同步机制
34
34
![ w:700] ( figs/basic-syncmutex.png )
35
35
36
+ <!--
37
+ Dijkstra 应译作“戴克斯特拉”
38
+ https://zhuanlan.zhihu.com/p/73390180
39
+ 维基百科 Edsger Wybe Dijkstra 页面(https://en.wikipedia.org/wiki/Edsger_W._Dijkstra),Dijkstra 可读作 /ˈdaɪkstrə/ 或 [ˈdɛikstra]
40
+ -->
36
41
---
37
42
38
43
### 信号量(semaphore)
@@ -56,16 +61,128 @@ backgroundColor: white
56
61
- 线程不会被无限期阻塞在P()操作
57
62
- 假定信号量等待按先进先出排队
58
63
59
- 自旋锁能否实现先进先出?
64
+ ** 问题 ** : 自旋锁能否实现先进先出?
60
65
61
66
![ bg right:30% 100%] ( figs/sema-train.png )
62
67
63
-
64
68
---
65
69
66
70
### 信号量在概念上的实现
71
+
72
+ <!--
67
73

74
+ -->
75
+
76
+ <font size =6 >
77
+
78
+ ```
79
+ Class Semaphore {
80
+ int sem;
81
+ WaitQueue q;
82
+ }
83
+ ```
84
+
85
+ <style >
86
+ .container {
87
+ display : flex ;
88
+ }
89
+ .col {
90
+ flex : 1 ;
91
+ }
92
+ </style >
93
+
94
+ <div class =" container " >
95
+
96
+ <div class =" col " >
97
+
98
+ ``` c++
99
+ Semaphore::P () {
100
+ sem--;
101
+ if (sem < 0) {
102
+ Add this thread t to q;
103
+ block (t)
104
+ }
105
+ }
106
+ ```
107
+
108
+ </div >
109
+
110
+ <div class =" col " >
111
+
112
+ ``` c++
113
+ Semaphore::V () {
114
+ sem++;
115
+ if (sem <= 0) {
116
+ Remove a thread t from q;
117
+ wakeup (t)
118
+ }
119
+ }
120
+ ```
121
+
122
+ </div >
123
+
124
+ </div >
125
+
126
+ </font >
127
+
128
+ ---
129
+
130
+
131
+ ### 信号量在概念上的实现
132
+
133
+ <font size =6 >
134
+
135
+ ```
136
+ Class Semaphore {
137
+ int sem;
138
+ WaitQueue q;
139
+ }
140
+ ```
141
+
142
+ <style >
143
+ .container {
144
+ display : flex ;
145
+ }
146
+ .col {
147
+ flex : 1 ;
148
+ }
149
+ </style >
150
+
151
+ <div class =" container " >
152
+
153
+ <div class =" col " >
154
+
155
+ ``` c++
156
+ Semaphore::P () {
157
+ while (sem <= 0) {
158
+ Add this thread t to q;
159
+ block (t)
160
+ }
161
+ sem--;
162
+ }
163
+ ```
164
+
165
+ </div >
166
+
167
+ <div class =" col " >
168
+
169
+ ``` c++
170
+ Semaphore::V () {
171
+ sem++;
172
+ if (sem <= 0) {
173
+ Remove a thread t from q;
174
+ wakeup (t)
175
+ }
176
+ }
177
+ ```
178
+
179
+ </div >
180
+
181
+ </div >
182
+
183
+ 问题:这个实现与上一个有什么不同?
68
184
185
+ </font >
69
186
70
187
---
71
188
You can’t perform that action at this time.
0 commit comments