/
Unix_Network_Programming_v2.html
389 lines (358 loc) · 12.1 KB
/
Unix_Network_Programming_v2.html
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!DOCTYPE html>
<html>
<head>
<title>UNIX Network Programming Vol.2 Notes</title>
<!-- 2014-06-25 -->
<meta charset="utf-8">
<meta name="generator" content="Org-mode">
<link rel="Stylesheet" type="text/css" href="assets/css/style.css"/>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
<script type="text/javascript">
function show_org_source(){
/* document.location.href = rpl(document.location.href,"html","org.html"); */
var url = document.location.href.split('#')[0];
document.location.href = url.replace(/.html$/, '.org.html');
}
</script>
</head>
<body>
<div id="preamble" class="status">
<!--<body onload="prettyPrint()">-->
<nav>
<a href="index.html">Index</a>
<a href="about.html">About</a>
<a href="https://lotabout.github.io">Blog</a>
<a href="sitemap.html">SiteMap</a>
</nav>
</div>
<div id="content">
<h1 class="title">UNIX Network Programming Vol.2 Notes</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">Chap01: Introduction</a></li>
<li><a href="#sec-2">Posix IPC</a></li>
<li><a href="#sec-3">System V IPC</a></li>
<li><a href="#sec-4">Pipes and FIFOs</a></li>
</ul>
</div>
</div>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Chap01: Introduction</h2>
<div class="outline-text-2" id="text-1">
</div><div id="outline-container-sec-1-1" class="outline-3">
<h3 id="sec-1-1">Introduction</h3>
<div class="outline-text-3" id="text-1-1">
<p>
The evolution of IPC(interprocess of communication)
</p>
<dl class="org-dl">
<dt> pipe </dt><dd>unsed in programs or shells. Can be used only between
processes with common ancestors. <span class="underline">FIFO</span> broke this limit.
</dd>
<dt> System V message queue </dt><dd>can be used between processes no matter
they are related or not under <span class="underline">the same host</span>.
</dd>
<dt> Posix message queue </dt><dd>Almost the same to System V message queue.
</dd>
<dt> RPC(Remote procedure call) </dt><dd>Provide a mechanism for clients to
call some functions of the server.
</dd>
</dl>
<p>
We can see that the range of IPC increases with evolution. From
processes with common ancester to thoses in different hosts.
</p>
<p>
The evolution of Synchronization Mechanisms.
</p>
<ul class="org-ul">
<li>Hidden features of filesystem
</li>
<li>record locking
</li>
<li>System V semaphore.
</li>
<li>Posix semaphore
</li>
<li>mutex and condition variable.
</li>
<li>read-write lock.
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1-2" class="outline-3">
<h3 id="sec-1-2">Sharing Information Between Processes and Threads</h3>
<div class="outline-text-3" id="text-1-2">
<p>
There are 3 ways of sharing: through kernel(like sharing with files);
in kernel(pipes); without kernel(shared memory).
</p>
</div>
</div>
<div id="outline-container-sec-1-3" class="outline-3">
<h3 id="sec-1-3">Persistence of IPC Object</h3>
<div class="outline-text-3" id="text-1-3">
<p>
The <b>Presistence</b> of any type of IPC is how long an object of that
type remains in existence.
</p>
<dl class="org-dl">
<dt> process-persistent IPC </dt><dd>exists until last process with IPC object
open closes the object. (pipes and FIFOs.)
</dd>
<dt> kernel-persistent IPC </dt><dd>exists until kernel reboots or IPC object
is explicitly deleted. (System V message queue, semaphores, and
shared memories)
</dd>
<dt> filesystem-persistent IPC </dt><dd>exists until IPC object is explicitly deleted.
</dd>
</dl>
<p>
Note the difference between the place where IPC information was kept
and the existence of IPC object. For example, pipes are kept by the
kernel but exists with the existence of process.
</p>
<p>
Normally, no IPCs were designed with filesystem-presistence. First, no
processes survive the reboot. Second, requiring filesystem persistence
might degrade the preformance.
</p>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Posix IPC</h2>
<div class="outline-text-2" id="text-2">
</div><div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1">Introduction</h3>
<div class="outline-text-3" id="text-2-1">
<p>
There are 3 types of POSIX IPC:
</p>
<ul class="org-ul">
<li>Posix message queue
</li>
<li>Posix semaphores
</li>
<li>Posix shared memory
</li>
</ul>
<p>
They are common in: the pathnames used for identification, the
flags specified when opening or creatign, and the access permission.
</p>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">System V IPC</h2>
<div class="outline-text-2" id="text-3">
<p>
There are three types of System V IPC:
</p>
<ul class="org-ul">
<li>System V message queues,
</li>
<li>System V semaphores, and
</li>
<li>System V shared memory.
</li>
</ul>
</div>
<div id="outline-container-sec-3-1" class="outline-3">
<h3 id="sec-3-1">key_t Keys and ftok Function</h3>
<div class="outline-text-3" id="text-3-1">
<p>
System V IPC use <code>key_t</code> values for their names. The header
<sys/types.h> defines the <code>key_t</code> datatype as an integer, which is
normally at least 32 bits long.
</p>
<p>
The function <code>ftok</code> converts an <span class="underline">existing</span> pathname and an integer
identifier into <code>key_t</code> value.
</p>
<p>
Client and server are able to generate the same <code>key_t</code> value once
they agree on the <code>pathname</code> and <code>id</code>.
</p>
</div>
</div>
<div id="outline-container-sec-3-2" class="outline-3">
<h3 id="sec-3-2">ipc_perm Structure</h3>
<div class="outline-text-3" id="text-3-2">
<p>
It is used by the kernel to maintain the information for each IPC
object, similar to the information it maintains for files.
</p>
</div>
</div>
</div>
<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4">Pipes and FIFOs</h2>
<div class="outline-text-2" id="text-4">
</div><div id="outline-container-sec-4-1" class="outline-3">
<h3 id="sec-4-1">Introduction</h3>
<div class="outline-text-3" id="text-4-1">
<p>
The fundamental limitation of pipe is that they have NO name.
</p>
</div>
</div>
<div id="outline-container-sec-4-2" class="outline-3">
<h3 id="sec-4-2">Full Duplex Pipe</h3>
<div class="outline-text-3" id="text-4-2">
<p>
It depends on the system's support. Check first if you wants to
make use of it.
</p>
</div>
</div>
<div id="outline-container-sec-4-3" class="outline-3">
<h3 id="sec-4-3">FIFO</h3>
<div class="outline-text-3" id="text-4-3">
<ol class="org-ol">
<li>when calling <code>mkfifo</code> to create FIFO, it will create a new fifo or
return <code>EEXIST</code> if the named FIFO already exists.
</li>
<li>FIFO should be open by calling <code>open</code> system call.
</li>
<li>When reading from FIFO, it will block the process. So be careful
not to block both the server and the client. (reorder the calling
of <code>read</code> and <code>write</code>).
</li>
<li>Call <code>unlink</code> to remove FIFO from the filesystem. Note that kernel
keeps a reference count of the number of open descriptors that
refer to the pipe or FIFO. Thus, when calling <code>unlink</code>, it will not
affect open descriptors that had previously opened the pathname.
</li>
</ol>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<a href="#" class="back-to-top">Back to Top</a>
<!-- <hr class="comment"> -->
<!-- <div id="show_source"> -->
<!-- <input type="button" value="Show Org source" onClick='show_org_source()'> -->
<!-- </div> -->
</script>
<hr class="comment">
<section class="comment">
<!-- An element a visitor can click if they <3 comments! -->
<button class="btn show-comments">Load Disqus comments</button>
<div id="disqus_thread"></div>
<!-- The empty element required for Disqus to loads comments into -->
<!--<div id="disqus_thread"></div>-->
<!--<script type="text/javascript">-->
<!--/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */-->
<!--var disqus_shortname = 'lotaboutvimwiki'; // required: replace example with your forum shortname-->
<!--/* * * DON'T EDIT BELOW THIS LINE * * */-->
<!--(function() {-->
<!--var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;-->
<!--dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';-->
<!--(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);-->
<!--})();-->
<!--</script>-->
<!--<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>-->
</section>
<footer>
|采用<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/">CC BY-NC-SA 3.0</a>授权|由<a href="http://orgmode.org">Org Mode</a>自动生成<input type="button" value="Show Org source" onClick='show_org_source()'>|部署在<a href="https://github.com/lotabout/orgwiki">Github Pages</a>|
</footer>
<script src="assets/js/jquery-1.7.1.min.js"></script>
<script>
$(document).ready(function(){
var offset = 100;
var duration = 110;
$(window).resize(function () {
var toc= $('#table-of-contents')
if ($(window).width() <= 800) {
toc.css('position', 'static')
return
}
});
$(window).scroll(
function () {
console.log("scroll called.")
var toc= $('#table-of-contents')
if ($(window).width() <= 800) {
toc.css('position', 'static')
return
}
if ($(this).scrollTop() > duration) {
toc.css('left', toc.position().left + 'px')
toc.css('position', 'fixed')
$('.back-to-top').fadeIn(duration);
} else {
toc.css('left', '0px')
toc.css('top', '0px')
toc.css('position', 'relative')
$('.back-to-top').fadeOut(duration);
}
});
$('.back-to-top').click(
function(event) {
event.preventDefault;
$('html, body').animate({scrollTop: 0}, duration);
return false;
});
$('.show-comments').on('click', function(){
var disqus_shortname = 'lotaboutorgwiki'; // Replace this value with *your* username.
// ajax request to load the disqus javascript
$.ajax({
type: "GET",
url: "http://" + disqus_shortname + ".disqus.com/embed.js",
dataType: "script",
cache: true
});
// hide the button once comments load
$(this).fadeOut();
});
});
</script>
</div>
</body>
</html>