You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/src/tutorial-basics.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Throughout the tutorial, you will learn how to use coroutines, channels, and soc
22
22
## Step 1: Setting up the stage
23
23
24
24
Start by including the libdill header file. Later we'll need some functionality from the standard library, so include those headers as well:
25
-
25
+
26
26
```c
27
27
#include<libdill.h>
28
28
#include<assert.h>
@@ -171,7 +171,7 @@ At this point, the client cannot crash the server, but it can block it. Do the f
171
171
3. At yet another terminal, open a new telnet session.
172
172
4. Observe that the second session hangs without even asking you for your name.
173
173
174
-
The reason for this behavior is that the program doesn't even start accepting new connections until the entire dialog with the client has finished. What we want instead is to run any number of dialogues with clients in parallel. And that is where coroutines kick in.
174
+
The reason for this behavior is that the program doesn't even start accepting new connections until the entire dialogue with the client has finished. What we want instead is to run any number of dialogues with clients in parallel. And that is where coroutines kick in.
175
175
176
176
Coroutines are defined using the `coroutine` keyword and launched with the `go()` construct.
177
177
@@ -197,13 +197,13 @@ int main(int argc, char *argv[]) {
197
197
assert(s >= 0);
198
198
s = suffix_attach(s, "\r\n", 2);
199
199
assert(s >= 0);
200
-
int cr = go(dialog(s));
200
+
int cr = go(dialogue(s));
201
201
assert(cr >= 0);
202
202
}
203
203
}
204
204
```
205
205
206
-
Let's compile it and try the initial experiment once again. As can be seen, one client now cannot block another one. Excellent. Let's move on.
206
+
Let's compile it and try the initial experiment once again. As can be seen, one client now cannot block another one. Excellent. Let's move on.
207
207
208
208
## Step 4: Shutdown
209
209
@@ -244,7 +244,7 @@ int main(int argc, char *argv[]) {
244
244
rc = hclose(ls);
245
245
assert(rc == 0);
246
246
247
-
return 0;
247
+
return 0;
248
248
}
249
249
```
250
250
@@ -258,7 +258,7 @@ Now try to compile this step and run it under valgrind. (Don't forget to compile
==179895== All heap blocks were freed -- no leaks are possible
263
263
```
264
264
@@ -270,7 +270,7 @@ File descriptors can be a scarce resource. If a client connects to the greetserv
270
270
271
271
To deal with the problem, we are going to timeout the whole client/server dialogue. If it takes more than *10* seconds, the server will kill the connection at once.
272
272
273
-
One thing to note is that libdill uses deadlines rather than the more conventional timeouts. In other words, you specify the time instant by which you want the operation to finish rather than the maximum time it should take to run it. To construct deadlines easily, libdill provides the `now()` function. The deadline is expressed in milliseconds, which means you can create a deadline one minure in the future as follows:
273
+
One thing to note is that libdill uses deadlines rather than the more conventional timeouts. In other words, you specify the time instant by which you want the operation to finish rather than the maximum time it should take to run it. To construct deadlines easily, libdill provides the `now()` function. The deadline is expressed in milliseconds, which means you can create a deadline one minute in the future as follows:
274
274
275
275
```c
276
276
int64_t deadline = now() + 60000;
@@ -296,7 +296,7 @@ This can be achieved by calling `bundle_wait()` on the `dialogue()` coroutine bu
Copy file name to clipboardExpand all lines: website/tutorial-basics.html
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -113,7 +113,7 @@ <h2 id="step-3-making-it-parallel">Step 3: Making it parallel</h2>
113
113
<li>At yet another terminal, open a new telnet session.</li>
114
114
<li>Observe that the second session hangs without even asking you for your name.</li>
115
115
</ol>
116
-
<p>The reason for this behavior is that the program doesn't even start accepting new connections until the entire dialog with the client has finished. What we want instead is to run any number of dialogues with clients in parallel. And that is where coroutines kick in.</p>
116
+
<p>The reason for this behavior is that the program doesn't even start accepting new connections until the entire dialogue with the client has finished. What we want instead is to run any number of dialogues with clients in parallel. And that is where coroutines kick in.</p>
117
117
<p>Coroutines are defined using the <code>coroutine</code> keyword and launched with the <code>go()</code> construct.</p>
118
118
<p>In our case, we can move the code performing the dialogue with the client into a separate function and launch it as a coroutine:</p>
<p>One thing to remember about canceling coroutines is that once a coroutine is canceled all the blocking operations within the coroutine, such as reading from a socket or sleeping, will start returning <code>ECANCELED</code> error. The coroutine should then deallocate all its resources and exit.</p>
176
176
<p>Looking at our <code>dialogue</code> coroutine it turns out that it already does that. It responds to any error, including <code>ECANCELED</code> by closing the socket handle and exiting.</p>
177
177
<p>Now try to compile this step and run it under valgrind. (Don't forget to compile libdill itself with <code>--enable-valgrind</code> and <code>--disable-shared</code> options!) Here's what you'll get:</p>
==179895== All heap blocks were freed -- no leaks are possible</code></pre>
183
183
<p>To get some background on how object lifetimes are supposed to be managed in libdill read the article about <ahref="structured-concurrency.html">structured concurrency</a>.</p>
184
184
<h2id="step-5-deadlines">Step 5: Deadlines</h2>
185
185
<p>File descriptors can be a scarce resource. If a client connects to the greetserver and lets the dialogue hang without entering a name, one file descriptor on the server side is, for all intents and purposes, wasted.</p>
186
186
<p>To deal with the problem, we are going to timeout the whole client/server dialogue. If it takes more than <em>10</em> seconds, the server will kill the connection at once.</p>
187
-
<p>One thing to note is that libdill uses deadlines rather than the more conventional timeouts. In other words, you specify the time instant by which you want the operation to finish rather than the maximum time it should take to run it. To construct deadlines easily, libdill provides the <code>now()</code> function. The deadline is expressed in milliseconds, which means you can create a deadline one minure in the future as follows:</p>
187
+
<p>One thing to note is that libdill uses deadlines rather than the more conventional timeouts. In other words, you specify the time instant by which you want the operation to finish rather than the maximum time it should take to run it. To construct deadlines easily, libdill provides the <code>now()</code> function. The deadline is expressed in milliseconds, which means you can create a deadline one minute in the future as follows:</p>
0 commit comments