Вы находитесь на странице: 1из 448

.

Linux.
c .




.
.
.
.
. , . , .
.
. , .
.

32.973.2-018.2
004.451
95

.
Linux. . .: , 2011. 448 .: .
ISBN 978-5-49807-794-9

, GNU/Linux. , , , ,
.
, , , ,
Linux . . , , , .

Prentice Hall PTR.


. .
, , , . , ,
, .

ISBN 978-5-49807-794-9

2007 Pearson Education, Inc.

ISBN 0132198576 (.)

, 2011
,
, 2011

, 194044, -, . ., 29.
005-93, 2; 95 3005 .
24.09.10. 70100/16. . . . 36,12. 2000. 0000.
CtP . . . .
197110, -, ., 15.



, .


.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2. .. . . . . . . 53
3. .. . . . . . . . . . . . . . . . . . . 96
4. .. . . . 124
5. Linux.. . . . 172
6. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
7. .. . . . . . . . . . . . . . . . 261
8. IPC
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
9. .. . . . . . . . . . . . . . . . . . . 326
10. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380

.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.2. ?.. . . . . . . . . 23
1.3.
?.. . . . . . . . . . . . . . . . . . . . . . . . . 24
1.3.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.3.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.4. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.4.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.4.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.4.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.5. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.5.1. : ?.. . . . . . . . . . . . . . . 34
1.5.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1.6. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

1.6.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.6.2. .. . . . . . . . . . . . . . . . . . . . . . 36
1.6.3.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.6.4. RPM GPG.. . . . . . . .
1.6.5. , .. . . . . .
1.7. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7.1. .. . . . . . . . . . . . . . . . . . . . . . . .
1.7.2. RPM.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7.3. Debian .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.8. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.8.1. Apt: Advanced Package Tool.. . . . . . . . . . . . . . . . . . . . . .
1.8.2. Yum: Yellowdog Updater Modified.. . . . . . . . . . . . . . . . . .
1.8.3. Synaptic: GUI- APT.. . . . . . . . . . . . . . . . . .
1.8.4. up2date: Red Hat.. . . . . . . . . . . . . .
1.9. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

38
38
41
42
43
45
46
47
48
49
49
51
51

2. .. . . . . . . . . . . . 53
2.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2. .. . . . . . . . . . . . . . . . . .
2.2.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2.2. make .. . . . . . . . . . . . . . . . . . . . . . . . .
2.2.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2.4. .. . . . . . . . . . . . . . . . . . . . . . . .
2.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3.1. GNU- .. . . . . . . . . . . . . . . . . . .
2.3.2. : configure.. . . . .
2.3.3. : make.. . . . . . . . . . . . . . . . . . . . . . . . .
2.3.4. : make install .. . . . . . . . . . . . . . . . .
2.4. .. . . . . . . . . . . . . . . . . . .
2.4.1. , Makefile.. . . .
2.4.2. .. . . . . . . . . . .
2.4.3. .. . . . . . . . . . . . . . . . . . . .
2.4.4. .. . . . . . . . . . . . . . . .
2.4.5. .. . . . . . . . .
2.4.6. . . . . . . . . . . . . .
2.5. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........

53
53
54
55
71
72
74
74
75
76
77
78
78
80
81
83
85
93
95

3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

10
3.2.1. man.. . . . . . . . . . . . . . . . . . . . .
3.2.2. man.. . . . . . . . . . . . . . . . . . . .
3.2.3. man: apropos.. . . .
3.2.4. man:
whatis .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2.5. man.. . . . . . . . .
3.2.6. man.. . . . . . . . .
3.2.7. GNU- info.. . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2.8. info.. . . . . . . . . . . . . . . . . . . . . . . . .
3.2.9. info .. . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2.10. info.. . . . . . . . . . . . . . . . . .
3.2.11. .. . . . . . .
3.3. .. . . . . . . . . . . . . . . .
3.3.1. /usr/share/doc.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.2. .. . . . . . . . . . . . . . .
3.3.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.1. TeX/LaTeX/DVI.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.2. Texinfo.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.3.DocBook .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.4.HTML.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.5.PostScript.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.6.Portable Document Format (PDF).. . . . . . . . . . . . . . . . . .
3.4.7.troff.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5. .. . . . . . . . . . . .
3.5.1.www.gnu.org.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5.2.SourceForge.net.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5.3. The Linux Documentation Project..
3.5.4.Usenet.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5.5. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5.6. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6. Linux.. . . . . . . . . . . . . . . . . . . . . . . . .
3.6.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.3. .. . . . . . . . . . . . . . . . . . . . . . . . .
3.7. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.7.1., .. . . . . .
3.7.2.- .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . 97
. . . . . . . . . . 97
. . . . . . . . . . 99
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

101
102
103
105
105
108
108
109
110
110
110
112
113
113
113
114
114
115
115
116
116
116
117
117
118
119
119
119
119
121
122
122
122
123

4. .. . . . . . . . . . 124
4.1... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

11

4.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
4.2.1.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
4.2.2. : vi Emacs.. . . . . . . . . . . . . . . . . . 127
4.2.3.Vim: vi.. . . . . . . . . . . . . . . . . . . . . . . 127
4.2.4. Emacs .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
4.2.5. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
4.2.6. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
4.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
4.3.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
4.3.2. .. . . . . . . . . . . . . . . . . . . . 156
4.3.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
4.3.4. diff patch .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
4.3.5. .. . . . . . . . . . . . . . . . . . . . . . . 159
4.4.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
4.4.1.
indent .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
4.4.2. astyle.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
4.4.3. cflow .. . . . . . . . . . . . . . . . 165
4.4.4. ctags .. . . . . . . . . . . . . . . . 165
4.4.5. cscope .. . . . . . . . . . . . . 166
4.4.6.
Doxygen.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
4.4.7. .. . . . 168
4.5... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
4.5.1., .. . . . . . . . . . . . . . . 169
4.5.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
4.5.3.-.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

5. Linux .. . . . . . . . 172
5.1... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
5.2. .. . . . . . . . . . . . . . . . . . . . . 172
5.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
5.3.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
5.3.2., .. . . . . . . . . . . . . . . . . . . . . . . . 175
5.3.3. .. . . . . . . . . . . . . . . . 176
5.3.4. nice .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
5.3.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
5.3.6. .. . . . . . . . . . . . . . . . . . . . 179
5.3.7. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

12

5.3.8. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
5.4. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
5.4.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
5.4.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . 191
5.4.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
5.4.4. / .. . . . . . . . . . . . . . . . . . . . . . 197
5.5. / .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
5.5.1. Linus Elevator ( noop).. . . . . . . . . . . . . 203
5.5.2. /
(Deadline I/O Scheduler).. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
5.5.3. /
(Anticipatory I/O Scheduler).. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
5.5.4. /
(Complete Fair Queuing I/O Scheduler).. . . . . . . . . . . . . . . . . . . . . . . . . 204
5.5.5. /.. . . . . . . . . . . . . . . . 205
5.6. .. . . . . . . . . . . . . . . . . . . . 206
5.6.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
5.6.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
5.7. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
5.7.1., .. . . . . . . . . . . . . . . 229
5.7.2.API-, .. . . . . . . . . . . . . . . . 229
5.7.3.- .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
5.7.4. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

6. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
6.1... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
6.2. ? .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
6.2.1. fork vfork.. . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
6.2.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
6.2.3. clone.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
6.3. exec.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
6.3.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
6.3.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . 236
6.3.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . 236
6.4. wait .. . . . . . . . . . . . . . . . . . . . . . . . . 238
6.5. , .. . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
6.5.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
6.5.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
6.5.3. .. . . . . . . . . . . . . . . . . . . . . . 246
6.6. .. . . . . . . . . . . . . 246
6.7. procfs .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

13

6.8. .. . . . . . . . . . . . . . . . . . . . . . . . . . 252
6.8.1. ps.. . . . . . . . 252
6.8.2.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
6.8.3. ps pgrep.. . . . . . . 256
6.8.4.
pmap.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
6.8.5. .. . . . . . . . . . . . . . 258
6.9... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
6.9.1. API-,
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
6.9.2., .. . . . . . . . . . . . . . . 260
6.9.3.-.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

7. .. . . . . . . . . . . . . . . . . . . . . . . . 261
7.1... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
7.2. IPC .. . . . 261
7.2.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
7.2.2. ,
IPC .. . . . . . . . . . . 262
7.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
7.3.1. API-
POSIX .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
7.3.2. API-
System V.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
7.4... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
7.4.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
7.4.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
7.4.3. .. . . . . . . . . . . . . . . . . . . . . . 272
7.4.4. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
7.4.5. sigqueue
sigaction.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
7.5. ().. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
7.6... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
7.6.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
7.6.2.
socketpair .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
7.6.3. /, .. . . 284
7.6.4. /, .. . . 289
7.7. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
7.7.1. System V.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

14

7.7.2. POSIX.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294


7.7.3. POSIX
System V .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
7.8. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
7.8.1. API- POSIX.. . . 301
7.8.2. API- System V.. . 304
7.9... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
7.9.1. API-, .. . 306
7.9.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
7.9.3.- .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

8. IPC
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
8.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2. .. . . . . . . . . . . . . .
8.2.1. lsof.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2.2. fuser.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2.3. ls.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2.4. file.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2.5. stat.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.3.1. strings.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.3.2. xxd .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.3.3. hexdump.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.3.4. od.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.4. System V IPC.. . . . . . . . . .
8.4.1. System V.. . . . . . . . . . . . . . . . . . . . . . .
8.4.2. System V .. . . . . . . . . . . . . . . . . . . . . . .
8.4.3. System V.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.5. POSIX IPC .. . . . . . . . . . . . . . . .
8.5.1. POSIX .. . . . . . . . . . . . . . . . . . . . . . . . .
8.5.2. POSIX .. . . . . . . . . . . . . . . . . . . . . . . . .
8.5.3. POSIX.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.6. .. . . . . . . . . . . . . . . . . . . . . . . . .
8.7. () .. . . . .
8.7.1. FIFO.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.7.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.8.
IPC.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.9. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.9.1. , .. . . . . . . . . .
8.9.2. -.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

308
308
308
310
310
310
311
311
311
312
313
313
314
314
316
317
318
318
319
319
320
321
321
322

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

323
324
324
325

15

9. .. . . . . . . . . . . . . . . . . . . . . . . . . . . 326
9.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
9.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
9.2.1. , .. . . 326
9.2.2.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
9.2.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
9.2.4.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
9.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
9.3.1. : time.. . . . . . . . . . . . . . . . . . . 350
9.3.2.
x86info. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
9.3.3. Valgrind .. 353
9.3.4. ltrace .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
9.3.5. strace
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
9.3.6. :
gcov gprof.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
9.3.7. OProfile.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
9.4. .. . . . . . . . . . . . . . . . . . . . . . 370
9.4.1.
SMP.. . . . . . . . . . . . . . . . . . . . . . . . . . 370
9.4.2.
SMP.. . . . . . . . . . . . . . . . . . . . . . . . . . 375
9.5. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
9.5.1. , .. . . . . . . . . . . . . . . . . 378
9.5.2. , .. . . . . . . . . . . . . . . . . . . . . . 379
9.5.3. , .. . . . . . . . . . . . . . . 379
9.5.4. -.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
9.5.5. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
10.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
10.2. : printf .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
10.2.1. , printf .. . . . . . . . . . . 381
10.2.2. printf .. . . . . . . . . . . . . 385
10.2.3. printf.. . . 393
10.3. GNU- gdb.. . . . . . . . . . . . . . . . . . . . . 393
10.3.1. gdb.. . . . . . . . . . . . . . . . . . . . 394
10.3.2. .. . . . . . . . 395

16

10.3.3. .. . . . . . . . . . . . . . . . . . 402
10.3.4. gdb .. . . . 411
10.3.5. .. . . . . . . . . . . . . . . . . . . . . . . . . 412
10.3.6. gdb.. . . . . . . . . . . 415
10.3.7. .. . . . . . . . . . . . . 417
10.4. .. . . . . . . . . . . . . . . . . . . . . . . . 418
10.4.1. .. . . 418
10.4.2. .. . . . . . . . . . . . . . . . 419
10.4.3.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
10.4.4.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
10.4.5. .. . . . . . . . . . . . 421
10.4.6.
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
10.5. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
10.5.1. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
10.5.2. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
10.5.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
10.5.4. , glibc.. . . . . . . . . . . . . . 429
10.5.5. Valgrind .. . . . . 432
10.5.6. Electric Fence.. . . 437
10.6. .. . . . . . . . . . . . . . . . . . . . . . . . . . . 439
10.6.1. .. . . . . . . . . . . . . . . . . . . 439
10.6.2. .. . . . . 443
10.6.3. .. . . . . . . 443
10.6.4. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
10.6.5. procfs .. . . . . . . . . . . . . . 445
10.7. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
10.7.1. , .. . . . . . . . . . . . . . 448
10.7.2. - .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
10.7.3. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448


, Linux. ls, grep, find
sort , ++,
Linux. Linux ,
. , ,
: ? .
, Linux

.
, ,
/proc,
Linux
.
, ,
, , , -
, . , ,
Linux ( UNIX!).
. ,
. ,
.
.
(Arnold Robbins),


Linux .
UNIX, ,

. , ,
.
- .
.
, .
,
. , grep ,
. ,
( , ). ,
,
grep.
, , , : Linux
,
. ,
.
, .
, , .
. ,
.
.
, ,
API,
. ,
Linux,
. :
. ,
,
, ,
.
MAN . GNU Linux
,
.
,

19

,
.
GNU/Linux , .
- 10000 ,
, .
. ,
, . ,
, , ,

Linux .
, ,
. Linux
, , URL-,
.
,
.


Linux-
,
Linux.
Windows, Linux
, .
,

. ,
Linux.


,
Linux Journal ,
Linux.
Linux.
- ,
. .
, -
, ,
, .
. , , ,
.
- . ,
Linux.

20

. Linux
,
, ,
Windows.
, ,
, .


.
, . ,
, ,
.
, -
, .
, .


1

. ,
, .
, ,
.
2,
, .
,
.
make. ,
,
GNU autoconf. ,
.
, ,
.
3
,
Linux, .
.
4,
, ,
, . ,
, , ,
.

21

,
.
5 Linux
. ,
Linux.
,
.
6 ,
.
. ,

API, .
7, ,
IPC.
, 8.
IPC
API,
.
8 IPC

, IPC.
7,
,
.
9, ,
, ,
. , ,
.

.
10
.
, Valgrind Electric Fence. ,
gdb , .


,
. , ,
. .
, ,
, .
(Arnold Robbins) .

. ,
.
(Debra Williams
Cauley) , ,
. , , ,
.
, , (Mark Taub) ,
.


(John Fusco) GE Healthcare,
, ,
Linux.
UNIX-
Linux 2.0. ,
Embedded Systems Programming Linux Journal.


,
gromakovski@minsk.piter.com ( ,
).
!
http://www.piter.com
.

1.1.
,
. , ,
.
, .

, ,
, , , . ,

. , , ,
.

.

1.2.
?

, Open Source Initiative
(OSI) (www.opensource.org).
,
GNU Project, (Richard Stallman).
Open Source Initiative
,
.

24

1.


. ,

. : ,
. , (
GNU Public License) ,
,
.
, .

.
. ,
.

1.3.

?

,
. ,
,
. ,
.
: , ,
.
. ,
( ),
. ,
, , ,
. ,
. -
, .
, ,
,
. -
, .
, ,
.

1.3.1.
,
- . Linux

1.3.

25

- DVD, ,
. -
, .

, .

, .
,
-. ,
,
.
, ,
.
, .

. www.freshmeat.net.
, ,
, . , ,
www.freshmeat.net
71. : 71
!
www.freshmeat.net ,
. ,
, Linux,
, , .
, Linux,
,
Open Source Initiative ( www.freshmeat.net
). 12
. ,
, , ,
. ,
, AbiWord, ,
. ,
, , , OpenOffice,
. , OpenOffice ,
, Office/
Business::Office Suites. : , ,
.

1.3.2.
,
.
, .

26

1.


. .
: , ;
, .
,
: ,
, ,
.
,
Linux TAR.
, ,
tar. gzip,
. TAR-
.

1.4.


. ,
. Windows, , ,
PKZip. Linux
( , , PKZip,
). ,
( gzip bzip2).
UNIX.
Linux , ,
, ,
. , , TAR-
, .
.
, .
, (,
//).
.1.1
Linux . TAR.
tape archive,
tape.
tar
.
cpio, .
, pax POSIX,
TAR, CPIO PAX.
PAX,
.

27

1.4.
1.1.

tar

cpio

RPM;

ar

Debian; . AR

ar,
,
,
Debian.
, ZIP,
PKZip, ,
LHA.
Linux .
ZIP, , ,
Microsoft.

1.4.1.
,
. ; .1.2
.
1.2.

.tar

TAR,

.tar.gz, .tgz

TAR, gzip

.tar.bz2

TAR, bzip2

.tar.Z, .taz

TAR, UNIX compress

.ar, .a

AR,

.cpio

CPIO,

,
file. ,

. ,
. , TAR- c
foo.x. .
:
$ file foo.x
foo.x: gzip compressed data, from UNIX, max compression

28

1.

, gzip, ,
TAR. gzip
file z:
$ file -z foo.x
foo.x: tar archive (gzip compressed data, from UNIX, max compression)

.
,
,
.

1.4.2.
,
, t
. TAR-
Debian cron:
$ tar -tzvf data.tar.gz
drwxr-xr-x root/root
drwxr-xr-x root/root
drwxr-xr-x root/root
-rwsr-xr-x root/root
drwxr-xr-x root/root
-rwxr-xr-x root/root

0
0
0
22460
0
25116

2001-10-01
2001-10-01
2001-10-01
2001-10-01
2001-10-01
2001-10-01

07:53:19
07:53:15
07:53:18
07:53:18
07:53:18
07:53:18

./
./usr/
./usr/bin/
./usr/bin/crontab
./usr/sbin/
./usr/sbin/cron

v
, ls.
,
. ( ), 0.

.
.1.3 ,
.
.
1.3.

TAR
TAR,
gzip
TAR,
bzip2
CPIO

tar -tvf filename


tar -tzvf filename

tar -tjvf filename

cpio -tv < filename

CPIO stdin stdout



, . ,

29

1.4.

//
, .
. .
,
,
.
. .1.4
.
1.4.

, , ..

d
c

,
.
/dev;

,
.
/dev;

, . ,
,

,
. ,
r, w x.
, . ,
w , . .1.5
.
1.5.

rwx
rwr-x
--x

,
, ,
,

, setuid, setgid
sticky. ,
.

30

1.

setuid , , ,
,
. , ,
.
root setuid,
,
. , ? setuid
.
setgid ,
,
.
, , .
setgid, ,
.
, setuid setgid , ,
x . x ,
, ,
.
setuid setgid
. x s,
, setuid
. S,
setuid, .
, ,
. ,
root,
. Linux root
, - .
, root,
,
root.
setuid, setgid
x . s
, setgid
. S, ,
setgid,
.
cron
, crontab setuid,
root. .1.6
.
sticky, .
-
, . Linux sticky
, .
,

31

1.4.

, .
,
. ,
, ,
, . ,
sticky. ,
. , root
. sticky
/tmp.
1.6.

-rwxr-xr-x




, ,

-rw-r-xr-x

-rwsr-xr-x
-rwSr-xr-x

-rwxr-sr-x
-rwsr-sr-x
-rwsr-Sr-x

sticky
t T. :
-rwxrwxrwt ,
sticky;
-rwxrwx--T
, sticky.

1.4.3.
, ,
. .1.7
.

32

1.
1.7.

TAR

tar -xf filename

TAR, gzip

tar -xzf filename

TAR, bzip2
CPIO

tar -xjf filename

AR

ar x filename

cpio -i -d < filename

,
,
- . , , CPIO
. , ,
CPIO, /etc,
. , CPIO
, /etc/hosts.
, /etc/hosts, ,
. , :
cpio -t < foo.cpio
/etc/hosts

/ ,
/etc/hosts, - .
, ,
, ,
. GNU --no-absolutefilenames, hosts ./etc/hosts.
, CPIO
RPM, ,
.
, TAR-, UNIX,
. GNU- TAR Linux
/ , TAR.
, TAR- UNIX, GNU- TAR
. , /
, .

1.5.
,
.

33

1.5.

,
. ,

. , , KDE GNOME,
, ,
. , , , ,
, .

. ,
, .
,
. , .
.
upgrade,
.

.
. , ,
, .
,
, ,
.
: RPM (
RPM Package Manager1) Debian.
.1.8. , RPM
Red Hat Fedora, Suse. Debian
Debian
(Knoppix, Ubuntu .). pkgtool,
Slackware, portage,
Gentoo.
1.8. Linux

Red Hat

RPM

Fedora

RPM

Debian

Deb

Knoppix

Deb

Ubuntu

Deb

Gentoo

portage

Xandros

Deb

Mandriva ( Mandrake)

RPM

mepis

Deb

Slackware

pkgtool

Red Hat Package Manager.

34

1.

, .
, ,
.

1.5.1. :
?
Linux 32- Intel-
,
. ,
TAR.
, .
Linux
,
.
,
. ,
,
.
,
,
.
.1.9 ,
RPM.
, GNU, . -
- ,
. i386,
Pentium Pentium II.
, Linux
80386, .
1.9.

i386

i486
i586

i686

ix86

; gss 80386. ,
PentiumI
. ,
80486 ( )
. GNU-
i586 Pentium I.
Pentium
GNU- i686 Pentium Pro,
PentiumII .
PentiumII
, Pentium

1.6.

x86_64

IA64

ppc
ppc64
sparc
sparc64
mipseb

35

AMD Opteron Intel Pentium4 EM64T.


, 32-, 64- . 64- ,
32- ,
Opteron
EM64T 32- Linux
64- Itanium. ,
Intel Hewlett-Packard,

PowerPC G2, G3 G4, Apple
Macintosh Apple iMac
PowerPC G5 Apple iMac
SPARC, Sun
64- SPARC, Sun
MIPS, SGI

1.5.2.
Linux
, Linux .
,
, ,
.
.
, , :
;
( ) ;
,
;
;
(,
?);
.

1.6.
-
.
Linux,
, Windows, ,
.

: ,

36

1.

. ,
,
.
:
.
, .
Linux
.
root,
sudo.
,
. ,
,
.
.

1.6.1.
,
, . Linux
,
. ,
,
. ,
root,
. ,
,
,
.
, ,
, .
,
root.
. , rpm
. Debian,
.

1.6.2.

. ,
.
,
.
, ,

1.6.

37

.
,
.
, , .
,
.
, ,
.
(, 128-),
, .
,
, :

;
, ,

, .
md5sum
MD51, 128- .
md5sum .
, .
,
:
$ md5sum foo.tar bar.tar
af8e7b3117b93df1ef2ad8336976574f *foo.tar
2b1999f965e4abba2811d4e99e879f04 *bar.tar

md5sum,
:
$ md5sum
$ md5sum
foo.tar:
bar.tar:

foo.tar bar.tar > md5.sums


--check md5.sums
OK
OK

, 32
( 4). ,
, .
MD5 , ,
MD5.
, , MD5,
. MD5 ,
. , ,
MD5 .
1

MD5 Message Digest 5.

38

1.

1.6.3.

MD5,
, ,
. ,
, ,
, . ,
, .
,
, .
, , : .
-, .
- ,
.
.
, -
. .

GNU Privacy Guard
(GPG). GPG .1.1.

.1.1. GPG

1.6.4. RPM
GPG
RPM GPG-
. RPM ,

1.6.

39

MD5, , RPM.
RPM,
, .
GPG RPM.
, MD5
, .
rpm checksig, , ,
. , GPG, rpm
MD5. GPG,
gpg ok.
:
$ rpm checksig *.rpm
abiword-2.2.7-1.fc3.i386.rpm: sha1 md5 OK
abiword-plugins-impexp-2.2.7-1.fc3.i386.rpm: sha1 md5 OK
abiword-plugins-tools-2.2.7-1.fc2.i386.rpm: sha1 md5 OK
firefox-1.0-2.fc3.i386.rpm: (sha1) dsa sha1 md5 gpg OK
dpkg-1.10.21-1mdk.i586.rpm: (SHA1) DSA sha1 md5 (GPG) NOT OK (MISSING KEYS:
GPG#26752624)

,
RPM, GPG firefox dpkg.
firefox, dpkg. , firefox
,
rpm . . dpkg GPG,
. , ,
. rpm
.
firefox ,
Red Hat Fedora. Fedora
, Red Hat
. .
RPM
, , Red Hat
. dpkg
, Mandrake,
Fedora.
.
,
, dpkg ,
rpm , .
, , rpm 4.3.2
. ,
,
root. GPG
.
. ,

40

1.

, -,
. ,
. ,
GPG , ,
.

, GPG, .
Mandrake. :
$ rpm -qip dpkg-1.10.21-1mdk.i586.rpm
Name
: dpkg
Version
: 1.10.21
Vendor: Mandrakesoft
Release
: 1mdk
Build Date: Thu May 20 07:03:20 2004
Host: n1.mandrakesoft.com
Packager
: Michael Scherer <misc@mandrake.org>
URL
: http://packages.debian.org/unstable/base/dpkg.html
Summary
: Package maintenance system for Debian

.
URL- mandrakesoft.com mandrake.org. , 2004 , , ,
. Mandrake Mandriva,
. , , .
, , : .
$ rpm --checksig dpkg-1.10.21-1mdk.i586.rpm
dpkg-1.10.21-1mdk.i586.rpm: ...
... (GPG) NOT OK (MISSING KEYS: GPG 78d019f5)

.
Mandriva. Google http://
mandriva.com. ,
, , . . , Mandriva .
Google,
, .
www.keys.pgp.net, 0x78d019f5.
Search results for '0x78d019f5'
Type bits/keyID
cr. time exp time key expir
pub 1024D/78D019F5 2003-12-10
uid MandrakeContrib <cooker@linux-mandrake.com>
sig sig3 78D019F5 2003-12-10 __________ __________ [selfsig]
sig sig3 70771FF3 2003-12-10 __________ __________ Mandrake Linux
<mandrake@mandrakesoft.com>

1.6.

41

sig sig3 26752624 2003-12-10 __________ __________ MandrakeCooker <cooker@linuxmandrake.com>


sig sig3 45D5857E 2004-09-22 __________ __________ Fabio Pasquarelli (Lavorro) <fabiopasquarelli@tin.it>
sig sig3 17A0F9A0 2004-09-22 __________ __________ Fafo (Personale)
rec.r96@tin.it
sub 1024g/4EE127FA 2003-12-10
sig sbind 78D019F5 2003-12-10 __________ __________ []

Mandrake, ,
Mandrake. 78D019F5,
PGP . ,
78D019F5.txt :
rpm --import 78D019F5.txt

,
.
:
rpm --checksig dpkg-1.10.21-1mdk.i586.rpm
dpkg-1.10.21-1mdk.i586.rpm: (sha1) dsa sha1 md5 gpg OK

gpg

OK , .

, .
pgp.net. , , . ,
.

1.6.5. ,

, ,
, , ,
.
, .
, -
, ,
.
,
.
Linux ,
. ,
, .
.
, ,
, OpenSSH.
,

42

1.

, . ,
.
,
.
, ,
.
2.
, .

,

root,
. , .
.

, .
/usr/
sbin /sbin,
.
setuid setgid,
root.
, .
;
.
, -
.
,
.

1.7.

.
:
, ;

TAR, CPIO ;
, ;
, ,


;
.

, ,
, . ,
.

1.7.

43

,
, .
,
. Slackware, ,
.
, . RPM .
RPM ,
. ,
,
( Slackware).

.
. ,
,
. - ,
,
.
.
.
.


,
.
, ,

, .

.
, ,
. ,
, , , SourceForge.net.
, ,
. ,
.

1.7.1.
, .
,
Linux . ,
,
.
.
, ,
.

44

1.

, ,
, . ,
gcc, - foo.rpm.
RPM- :
$ rpm -qip foo.rpm

:
$ rpm -qi gcc

rpm RPM,
, p.
-,
.
, , , , ,
, .
, ,
.
. .1.10
RPM Debian.
1.10.

RPM

Debian

rpm -qpi _

dpkg -s _

rpm -qpl _

dpkg -L _

rpm -qp -scripts _

dpkg -e

rpm --checksig _

rpm -qp --requires _

dpkg -I

,
(, ,
)

rpm -qp --provides _

dpkg -I

.
,
- .
, ,
.
.
.1.11.
1.11.

RPM
rpm -qi
rpm qa

Debian
dpkg -s
dpkg list

45

1.7.

, -
, -

RPM
rpm -ql

Debian
dpkg -L

rpm -V

rpm -qf
_
rpm -q X

cd /;
md5sum -c <
/var/lib/dpkg/info/name.md5sums
dpkg -S _

X?

dpkg-query -W X

1.7.2. RPM
RPM ,
Linux. RPM ,
. ,
rpm --queryformat option (
--qf). , qf,
, :
$ rpm -querytags
HEADERIMAGE
HEADERSIGNATURES
HEADERIMMUTABLE
HEADERREGIONS
HEADERI18NTABLE
SIGSIZE
SIGPGP
SIGMD5
SIGGPG
PUBKEYS
...

, ,
rpm . , ,
, ,
:
$ rpm -qa --qf '%{vendor}' | sort | uniq -c
1 Adobe Systems, Incorporated
12 (none)
1 RealNetworks, Inc
838 Red Hat, Inc.
1 Sun Microsystems, Inc.

Fedora Core3 , 838


Red Hat, 12 . ,
GPG.

.

46

1.

, RPM,
. :
$ rpm -qp --scripts gawk-3.1.3-9.i386.rpm
postinstall scriptlet (through /bin/sh):
if [ -f /usr/share/info/gawk.info.gz ]; then
/sbin/install-info /usr/share/info/gawk.info.gz
/usr/share/info/dir
fi
preuninstall scriptlet (through /bin/sh):
if [ $1 = 0 -a -f /usr/share/info/gawk.info.gz ]; then
/sbin/install-info --delete /usr/share/info/gawk.info.gz
/usr/share/info/dirfi

,
( ..) (, /bin/sh). ,
.

1.7.3. Debian
Debian , RPM, dpkg
, rpm.
Debian . Debian
.deb, ,
ar. Debian,
. :
$ ar -t cron_3.0pl1-72_i386.deb
debian-binary
control.tar.gz
data.tar.gz

debian-binary ASCII-, .
control.tar.gz TAR,
, . data.tar.gz
TAR,
. ,
:
$ ar -x filename.deb

. data.tar.gz ,
.
, .
:
$ tar -tzf data.tar.gz
./
./usr/
./usr/bin/

1.8.

47

./usr/bin/crontab
./usr/sbin/
./usr/sbin/cron
./usr/sbin/checksecurity
./usr/share/
./usr/share/man/
./usr/share/man/man1/
...

control.tar.gz ,
, .
dpkg e:
$ dpkg -e cron_3.0pl1-72_i386.deb
$ ls ./DEBIAN/*
./DEBIAN/conffiles
./DEBIAN/control
./DEBIAN/md5sums
./DEBIAN/postinst
./DEBIAN/postrm
./DEBIAN/preinst
./DEBIAN/prerm

, preinst postinst
,
. prerm postrm
.

1.8.

. , X,
, .
X,
. ,
, , ,
..
.
X, ,
, .

, - .

(, Red Hat).

.

- , ,

48

1.

,
. ,
,
.
Debian Apt Advanced
Package Tool.
. Apt
RPM. ,

RPM.
RPM
. up2date Red Hat
Enterprise Server Fedora Core. Yum Yellowdog
Updater Modified1.

1.8.1. Apt: Advanced Package Tool


Apt
Debian, RPM.
dpkg,
, GPG. ,
RPM GPG. , Apt
,
, -
.
dpkg, Apt .
apt-get apt-cache. , ,
apt-get. ,
. apt-cache ,
, ,
. ,
, , .
apt-key ,
, .
, .
apt-setup ,
Apt - .
Ubuntu apt-setup
- Ubuntu, Debian .
/etc/apt/sources.list,
.
,
1

, Yellowdog
PowerPC, Yup,
RPM .

1.8.

49

. , Apt,
URL-.

1.8.2. Yum: Yellowdog Updater Modified


Yum RPM.
, Apt.
Apt, Yum .
Apt, Yum .
,
Apt.
yum , .
C . yum
,
.
Yum
GPG.
/etc/yum.conf /etc/yum.repos.d.
gpgcheck=1, yum ,
. Apt,
, URL-.
yum . ,
,
, :
$ yum list updates

, .
Apt
apt-get --dry-run -u dist-upgrade,
.

1.8.3. Synaptic: GUI- APT


Synaptic 1.0,
GUI-
Apt. , Ubuntu 861.
.
GUI-. Synaptic
,
. , , ,
, gcc c 3.3 3.4,
, FreeCell 1.0.1 1.0.2.

. , ,
. .1.2
Synaptic .

50

1.

.1.2. GUI- Synaptic

Apt, Synaptic ,
. Synaptic ,

, , .
Games and Amusements ( )
kasteroids, : Ubuntu
GNOME, kasteroids KDE-. ,
kasteroids,
. Synaptic ,
, .
.
, , , kasteroids
.
,
, ,
. Synaptic (
0.56),
.
.
, .
Synaptic GUI- Debian.
RPM-.
RPM- Apt Synaptic.

1.9.

51

1.8.4. up2date:
Red Hat
GUI- up2date Red Hat
Yum.
. - up2date
, , ,
, .
, ,
, up2date . Synaptic,
up2date GPG
, .
GUI- up2date .
, ; ,
, , .
, ,
.
up2date ,
Yum, Apt up2date. , Fedora Core4,
Yum.
, .
/etc/sysconfig/rhn/
sources. up2date ,
, RPM,
. RPM,
. DVD /mnt/dvd /etc/sysconfig/
rhn/sources :
dir fc-dvd /mnt/dvd/Fedora/RPMS

-, up2date
. :
$ up2date --install gcc

1.9.

. ,
, .
,
, .
,
.
.
.

52

1.

, .
.
:
dpkg Debian,
Debian , Ubuntu;
gpg GNU- ,
,
;
gzip, bzip2 GNU- ;
rpm ,
Red Hat Package Manager (RPM);
Red Hat, RPM Suse ;
tar, cpio, ar UNIX-,
.
- :
www.debian.org Debian,
, ;
www.gnupg.org GNU Privacy Guard,
gpg;
www.pgp.net , gpg
;
www.rpm.org RPM.

2.1.
,
, ,
. , make
- Linux.
,
, .
, GNU-
GNU-,
. ,
make.
,
.

2.2.

.
, ,
.
, . make
Linux.
,
. ,
make .
Linux
.

54

2.

2.2.1.
make . UNIX make .
,
.
, make .
make , .
,
. ,

Makefile, .
, .
make,
.
make, ,
. , , ,
make, ,
.
GNU- make , Linux.
.
,
make, . Makefile
.

Imake
Makefile
imake. XWindow
XWindow UNIX.
C ( imakefile)
Makefile, make.

. ,
imakefile .
imake
. ,
.
, imake.
, imake, .
imake XWindow
UNIX-, imakefile
XWindow.

GNU

GNU Project

2.2.

55

make. , imake,
, GNU
m4, .
,
.

make. GNU -
.


,
,
. ,

.
GNU :

.
, GNU-.
Cons1 (www.gnu.org/software/cons/dev/cons.html).
Perl (www.perl.org),
. GNU-
, Perl. GNU-
,
.
, ,
GNU-,
, .

2.2.2. make
make .
Makefile .
, make
, .
, Makefile.
,
GNU.

Makefile:
,
, Makefile .
:
1

Construction System.

56

2.

target: prerequisite
commands

, ,
. ,
.
, , .
. :
foo: foo.c
gcc -o foo foo.c
foo , foo.c . foo.c , foo, foo
, make gcc -o foo foo.c. foo foo.c,
. make :
, .
.
,
. :
# 1
program: object.o
gcc -o program object.o
# 2
source.c:
echo 'main() {}' > $@
# 3
object.o: source.c
gcc -c source.c -o $@
# 4
program2: program2.c
gcc -o program2 program2.c

, make ,
:
$ make
echo 'main() {}' > source.c
gcc -c source.c -o object.o
gcc -o program object.o

make Makefile
, .
, , 1.
:
1: program object.o:
yy object.o 3; 3 ;
yy object.o , program, program gcc;

2.2.

57

3: object.o source.c:

yy source.c 2; 2 ;
yy source.c , object.o, object.o gcc;
2: source.c :

yy source.c , echo.
, .
, Makefile .
, , 2 3 Makefile,
.
, 4 .
, make
. 4 , program2
, program2.c . , 4
. make program2,
4. Makefile
, .
make Makefile,
. , ,
.
, program2.c ,
. :
$ make program2.o

program2.o ,
program2.c.
make ,
, .
,
. all
Makefile. ,
, ,
Makefile:
program1: a.o b.o
gcc -o program1 a.o b.o
program2: c.o d.o
gcc -o program2 c.o d.o

make , program1
. ,
, :
$ make program1 program2

, make
.

58

2.

all Makefile, program1 program2


:
all: program1 program2
program1: a.o b.o
gcc -o program1 a.o b.o
program2: c.o d.o
gcc -o program2 c.o d.o

make make all,


program1, program2. all
, fred .
, make
. , .
, , ,
,
. : GNU- make
, :
.PHONY: all

make all
.

Makefile:
make
. :
VAR = value
VAR := value

, , :=
. ,
:
VAR = value
# !
VAR = $(VAR) more
# , :=
VAR := $(VAR) more
# GNU-,
VAR = value
VAR += more

GNU- make
: define endef. GNU-
:

2.2.

59

define VAR
value
endef

,
.

, .
ASCII-, ,
.

make
, . ,
= ,
.
:
, ;

;
, $();
define , , .

Makefile:
,
, , , :
$(MACRO)
${MACRO}

#
# ,

, ,
, .

.
,
, Makefile
1. ,
Makefile ,
1

,
.

60

2.

,
Makefile, .
Makefile ,
. Makefile
, Makefile
. Makefile:
FLAGS = first
all:
@echo FLAGS=$(FLAGS)
FLAGS = second
other:
@echo FLAGS=$(FLAGS)

, FLAGS
all, other, .
FLAGS , - .
FLAGS , make ,
, ,
, .

Makefiles

Makefile
, .
Makefile ,
, .
, ,
, .
, make ,
,
. make
, ,
Makefile.
- . make
Makefile!
:
$ echo "main() {}" > foo.c
$ make foo
cc
foo.c
-o foo

, make ,
foo.c. foo.

Makefile
. GNU- make ,

61

2.2.

, .
.

. , , GNU-
make ,
:
.c.o:
$(COMPILE.c) $(OUTPUT_OPTION) $<

: .o
, .. ,
. ,
,
.

, make p. COMPILE.c, ,
:
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
CC = gcc

, (CC)
; . make

.
Makefile, :
CFLAGS = -g

Makefile, ,
. , make
, Makefile
CFLAGS , :
$ make CFLAGS=-g foo
gcc -g
foo.c
-o foo


Makefile. , Makefile
,
. .2.1 ,
, .
2.1. ,

CC
CXX


gcc
g++


++

62

2.
2.1 ()

CFLAGS


none

CXXFLAGS

none

CPPFLAGS

none



++


, ++, , . :
-I, -D -U


.
C, C++, Fortran . .2.2
, ,
make p make.
2.2. ,

++
Assembler
Assembler6 ( , .S
.s,
, )
Pascal
Fortran
Fortran

.c
.cpp, .cc, .C
.s
.S

$(CC) -c $(CPPFLAGS) $(CFLAGS)


$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)
$(AS) $(ASFLAGS)
$(CPP) $(CPPFLAGS)

.p
.f
.F

$(PC) -c $(PFLAGS)
$(FC) -c $(FFLAGS)
$(FC) -c $(FFLAGS) $(CPPFLAGS)


GNU- make ,
, ,
.
.
, C++,
.cxx. ++,
GNU- make1.
, .
:
1

make ++, .C, .cpp .cc.

2.2.

63

.SUFFIXES: .cxx # let make know that this is a new extension


.cxx.o:
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

.SUFFIXES: make ,
, .cxx. , ,
make, , . GNU- make ,
.
.cxx :
%.o : %.cxx
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

, ,
.SUFFIXES:.
, , .
% Makefile,

. make ( ),
, , %, .




, make ,
.
$<,
. make,
. .2.3
,
.
2.3. , GNU- make

$@
$^
$+
$<
$?
$*



,

,
( ) . ,
, .c.o:, foo.o, foo.

64

2.

,
. ,
. ,
.
, .
Makefile :
program: $(OBJS)
$(CC) -o $@ $^

()
. ,
- ; program
, OBJS
. make ,
.

Makefile,
, :
program1: $(PROG1_OBJS)
$(CC) -o $@ $^
program2: $(PROG2_OBJS)
$(CC) -o $@ $^
# etc...


.
, .
,
. :
build_C_program=$(CC) -o $@ $^
program1: mod1.o mod2.o
$(build_C_program)
program2: mod3.o mod4.o
$(build_C_program)


build_C_program, , , build_C_program

. ,
, build_C_program,
.


,
,
UNIX- make:

2.2.

65

SRCS=foo.c bar.c
OBJS=$(SRCS:.c=.o)


make.
.
SRCS, .c .o
SRCS. foo.c bar.c,
, SRCS foo.o bar.o.
, ,
. Makefile ,
.
,
. SRCS
++? :
SRCS=foo.c bar.cpp
OBJS=$(SRCS:.c=.o)


,
bar.cpp, .c.
OBJS foo.o bar.cpp, , .
UNIX- make
:
CSRCS=foo.c
CXXSRCS=foo.cpp
OBJS=$(CSRCS:.c=.o) $(CXXSRCS:.cpp=.o)

,
.
GNU- make,
.


,
, . GNU-
make
. :
OBJS=$(SRCS:.c=.o)
OBJS:=$(patsubst %.c, %.o, $(SRCS)))

.

patsubst. , :
.
, . ,
:

66

2.

OBJS:=$(patsubst %.cpp, %.o, $(patsubst %.c, %.o, $(SRCS)))


.
.
:
OBJS:=$(addsuffix .o, $(basename $(SRCS)))

basename (
SRCS) . ,
: basename ,
. .o
basename addsuffix.
.
shell. ,

:
NAME = `date +%02d%02m%02y`.dat

, ,
. GNU- make
shell,
:
NAME = $(shell date +%02d%02m%02y).dat

,
, GNU- make
,
- . , .
, .
:
CURRENT_TIME=$(shell date +%T)
something:
@echo started $(CURRENT_TIME)
sleep 5
@echo finished $(CURRENT_TIME)

:
$ make
started 9:49:45
sleep 5
finished 9:49:45

, , .
, `date +%T`
, ,
, .
, , ,
. :

2.2.

67

CURRENT_TIME=`date +%T`

:
$ make
started 9:49:45
sleep 5
finished 9:49:50

, CURRENT_TIME ,
. CURRENT_TIME
, .
CURRENT_TIME , .
,
.
make.
.2.4.
2.4.

subst

$(subst from, to, text)

patsubst

$(patsubst frompattern, to-pattern,


filenames)

strip
findstring

$(strip string)
$(findstring match,
string)
$(filter patterns,
filenames)

filter

filter-out

$(filter-out patterns,
filenames)

sort

$(sort text)

word

$(word n, text)

wordlist

$(wordlist first, last,


text)
$(words text)
$(error message)

words
error

text
from, to

.
,
. ,
string
match, string;
,
filenames,
patterns. patterns
, , ,


filenames,
patterns. patterns
, , ,


text,
n- , text.
1
text, first
last
text
( ).

68

2.


,
, GNU- make
.
call:
$(call myfunction, arg1, arg2)

,
.
call , .
. myfunction
, ,
. :
myfunction = @echo $(1) $(2)
all:

$(call myfunction,hello,world)

Makefile :
$ make
hello world

. make
,
. , make ,
. ,
, .
, .
, ,
. :
$(call myfunction) # is the same as...
$(myfunction)

$(1), $(2) ..
.


GNU-
make. GNU- make
, .2.5.
2.5. , GNU- make

ifeq
ifneq
ifdef
ifndef

,
,

2.2.

69

: if equal if defined.
make not,
equal not equal, defined not defined.
else,
endif. :
conditional test
makefile text evaluated when test is true
else
makefile text evaluated when test is false
endif


make,
/.
,
.
, ifeq:
ifeq ($(shell uname -o),GNU/Linux)
CPPFLAGS += -DLINUX
endif

ifeq
. uname o
, GNU/Linux. Makefile
Linux, CPPFLAGS DLINUX.
, CPPFLAGS ,
, .
.
else :
ifeq ($(shell uname -o),GNU/Linux)
CPPFLAGS += -DLINUX
else
CPPFLAGS += -DOS_UNKNOWN
endif


, :
ifeq ($(shell uname -o),GNU/Linux)
all: linux_programs
else
all: bsd_programs
endif

,
.
,
,
:

70

2.

ifeq ($(A),0) # A isn't defined yet


all:
@echo $(A) == 0
else
all:
@echo $(A) != 0
endif
A=0

Makefile :
$ make
0 != 0

,
, ifeq (false).
, ,
?
ifdef ,

. :
ifdef (debug)
CFLAGS += -g
else
CFLAGS += -O2
endif

, CFLAGS ,
.
CFLAGS
-O2. ,
make debug:
$ make debug=1

ifdef Makefile
, :
ifndef (A_VITAL_VARIABLE)
$(error A_VITAL_VARIABLE is not set)
endif



, .
.
VPATH. GNU- make
.
, VPATH
PATH. , ,

2.2.

71

.
: src bin .
Makefile ./bin :
VPATH=../src
foo: foo.c
$(CC) -o $@ $^

VPATH , make
../src, .
foo.c , .
make . ,
, foo.c ../src, ,
:
$ make
gcc -o foo ../src/foo.c

foo.c , make
, .
. , ,
, VPATH
.
GNU- make VPATH vpath
( ).
, make
.
++ /cxxsrc,
/csrc. Makefile
:
vpath %.cpp ./cxxsrc
vpath %.c ./csrc


vpath VPATH. ,
.
.
, GNU-,
.
, ,
vpath.

2.2.3.

,
. (ld),

72

2.

.
, .
. ,
.
, , v
. , ,
( ):
$ gcc -v -o hello hello.c
/usr/libexec/gcc/i386-redhat-linux/4.0.1/cc1 -quiet -v hello.c -quiet dumpbase
hello.c -auxbase hello -version -o /tmp/ccoxYr4k.s
as -V -Qy -o /tmp/ccw3IciH.o /tmp/ccoxYr4k.s
/usr/libexec/gcc/i386-redhat-linux/4.0.1/collect2 --eh-frame-hdr -m elf_i386dynamic-linker /lib/ld-linux.so.2 -o hello /usr/lib/gcc/i386-redhatlinux/4.0.1/../
../../crt1.o /usr/lib/gcc/i386-redhat-linux/4.0.1/../../../crti.o /usr/lib/gcc/i386redhat-linux/4.0.1/crtbegin.o -L/usr/lib/gcc/i386-redhatlinux/4.0.1 -L/usr/lib/gcc/
i386-redhat-linux/4.0.1 -L/usr/lib/gcc/i386-redhatlinux/4.0.1/../../.. /tmp/ccw3IciH.
o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-asneeded /usr/lib/gcc/i386-redhatlinux/4.0.1/crtend.o /usr/lib/gcc/i386-redhatlinux/4.0.1/../../../crtn.o

.
. ,
. , ,
.
collect2, ld
.
++ :
. c ,
. .
,
.

2.2.4.
,
. POSIX, ,
POSIX, .
. ,
, ;
.
(
), ,
.

2.2.

73

: .
,
ar.
.
. , ,

,
. -
, , ,
.
.
,
,
.
.
, . ,

- .
,
, .
,
ar.
, -
.
, .
, .
. , ,
. -,
. ,
, ,
.
,
,
,
, .
, ,
,
.

, .
,
. ,
,
;
.

74

2.


.so. Linux ( UNIX)
.
.so. .a,
ar, .
AR ( - ) ,
"lib". , foo, libfoo.a.
-lfoo,
libfoo.a .
lib,
libfoo.a libfoo.so.

2.3.
, ,
, .
,
++. ,
++.
++ , ,
, .
, ,
++.
GNU- Linux-
,
. , , GNU-
GNU-
Linux. ,
Linux, Solaris, IRIX,
Free BSD Windows. ,
;
. ,
.
.

2.3.1. GNU-
GNU- -
. ,
,

2.3.

75

.
GNU-,
configure,
Makefile. ,
.
GNU-:
$ ./configure
$ make
$ make install

. . ,
. GNU-
.
,
,
GNU-.

2.3.2. : configure
,
Makefile. GNU-
,
POSIX- ,
. configure ,
, .
,
.

configure Makefile.
.
Makefile , Makefile.in,
configure Makefile.
configure ,
,
. GNU-, configure,

, . ,
, .
,
, .
configure, GNU-
, , .
, , , ,
.
, .

76

2.

, configure,
(.2.6).
2.6. configure

--prefix
--bindir

--sbindir

--datadir
--libdir
--mandir
--programprefix
--programsuffix

, /usr/local
, ${prefix}/bin. ,

, ${prefix}/sbin. ,
;
${prefix}/share
;
${prefix}/lib
man;
${prefix}/man
, ${bindir}
${sbindir}. ,
--program-prefix ,

GNU-
, . ,
- , :
$ ./configure help

, .2.6, configure
, :
--enable-X
--disable-X
--with-X
--without-X (also -with-X=no)

X , ,
. ,
X, . ,
configure -with-fries ,
.

2.3.3. : make
make .

. all, GNU ,

2.3.

77

(.2.7). ,
,
.
2.7. Makefile, automake

all
clean
distclean

mostlyclean

install

uninstall

,
all
, .
, tar. distclean Makefile ,
configure
clean , ,
,

, . , uninstall

, ,
install

,
.

2.3.4. : make install


,
.
root.
/usr/local/bin.
, /usr/bin.
/usr/local ,
, ,
. configure
prefix.
~/usr .
:
$ ./configure prefix ~/usr

,
.
,
, .
, , ,
.

78

2.

uninstall,
, ,
. uninstall .
,
. install uninstall ,
.
, uninstall.

2.4.
,
, , ,
. ,
.
Makefile.
, ,
, .

2.4.1. , Makefile
, make,
. . Makefile
,
.
,
.


make
. , Makefile
, SHELL,
/bin/sh. ,
. , ,
.
, .

. make ,
. ,
, .
, , ,
. ,
1.
1

,
. .

2.4.

79

,
. ,
Makefile.
. ,
, make ,
.


, make
.
. make ,
.
,
. , .
, , ,
. ,
, Makefile:
all:
@echo Look Ma! No Tabs!

, GNU- make:
$ make
Makefile:2: *** missing separator. Stop.

, ?
,
. make ,
, .
make . ,
, .. make ,
, ,
99%- ,
.
. ,
. ,
, :
all:
env FOO="Look Ma! No Tabs!" printenv FOO

, Makefile?
$ make
make: Nothing to be done for 'all'.

, make = ,
. ,
$(env FOO). :
, .

80

2.

, VPATH
VPATH.
, .
, , , ,
(
, ). ,
.
, VPATH, make
, . make
,
. make VPATH .
VPATH .
,
#include,
. , :
#include "foo.h"

,
.
, VPATH .
,
. , #include :
#include <foo.h>

include,
, I, . VPATH
,
.

2.4.2.
GNU-
.
, . ,
,
.
, .
, , ,
.
autoconf ,
xyz .
: can't find xyz.
, ,
xyz .

2.4.

81

, .
configure,
. , ,
. ,
. ( autoconf),
configure,
.
, configure xyz.
, xyz
. configure .
, .
,
configure. -
,
. :
CPPFLAGS
I;
LDFLAGS
L.

2.4.3.
configure

Makefile. , configure
Makefile, ,
,
. .
Makefile configure,
, Makefile.in.
:
Makefile , .

Makefile
GNU-
Makefile ,
, - . , ,
include Makefile.

Makefile.

Makefile, README , ,
.

.

82

2.

, , ,
,
.
Makefile, , ,
.
.

, make
Makefile ,
. make
, .
,
- .
, ,
, .2.8.
2.8.



,

, , ,

,
.
Makefile,

. .
make:
t1 t2 t3:
@if [ "$@"="t1" ]; then echo $@ is target 1; else echo $@ is not target 1;
fi

: t1, t2 t3.
, :
$ make t1 t2
t1 is target
t2 is target
t3 is target

t3
1
1
1

?
, . .
test,
.
= , test ,
. , if true.

2.4.

83

@
stdout. .
make n,
, , .
, @, Makefile
:
$ make -n t1 t2 t3
if [ "t1"="t1" ]; then echo t1 is target 1; else echo t1 is not target 1; fi
if [ "t2"="t1" ]; then echo t2 is target 1; else echo t2 is not target 1; fi
if [ "t3"="t1" ]; then echo t3 is target 1; else echo t3 is not target 1; fi

,

Makefile. n, ,
.

. ,
,
. ,
. ,
Makefile.
Makefile
-x.
.
SHELL:
$ make SHELL="/bin/sh -x" t1 t2 t3
+ [ t1=t1 ]
+ echo t1 is target 1
t1 is target 1
+ [ t2=t1 ]
+ echo t2 is target 1
t2 is target 1
+ [ t3=t1 ]
+ echo t3 is target 1
t3 is target 1

.
.
test ,
,
. , ,
, .

2.4.4.
,
, .

84

2.


. /++
,
.
:
1 void foo()
2 {
3
int x,y,z;
4
5
x=1
6
y=2;
7
z=3;
8 }

5 .
:
$ gcc -c foo.c
foo.c: In function 'foo':
foo.c:6: error: parse error before "y"


IDE (Integrated Development Environment) ,
Emacs Vim. ,
: , .
IDE,
, .
, -
. ,
5, , 6. - ,
, ,
6.
, ,
++, :
$ g++ -c foo.c
foo.c: In function 'int foo()':
foo.c:6: error: expected ';' before "y"

, ,
++,
.
,
. :
$ make CC=g++ foo.o

++
, .
++ ++

2.4.

85

. ++
, . , ,
, ,
.
, , ,
,
,
.
, , .
, ,
.

2.4.5.
.
, .
lint,
.
, .
lint ,
, .
,
,
. , GNU-
.
, lint.
,

. ,
, printf.
, ,
, .
-, Linux,
IA32,
. ,
,
. ,
.
GNU- /++
Wall,
, GNU.
, GNU-,
. , ,
, ,
. ,

86

2.

, GNU-,
configure:
$ CFLAGS=-Wall ./configure

:
Wall
,
( ++ ).

. ,
, ,
,
.
,
.
int.
, gcc
. :
$ echo "x=1;" > foo.c
$ gcc -c -Wall foo.c
foo.c:1: warning: type defaults to 'int' in declaration of 'x'
foo.c:1: warning: data definition has no type or storage class

ansi.

pedantic, ,
.
.
,
.
, , int,
,
. :
int foo(); //

, .
, ANSI.
,
. ,
.
, .
.
, Wall
, .
, ,
-Wstrict-prototypes.
, gcc (3.4.4) :

87

2.4.

foo.c:2: warning: function declaration isn't a prototype

, ,
, , .

,
Wall, , . GNU printf,
,
. ,
.2.9.
2.9. ,
printf
printf
fprintf
sprintf
snprintf

vprintf
vfprintf
vsprintf
vsnprintf

scanf
scanf
fscanf
sscanf

vscanf
vfscanf
vsscanf

,
. %s printf, ,
, .
, , , %s
, char * . ,

. , :
1
2
3
4
5

#include <stdio.h>
void foo(int x)
{
printf("%s", x);
}

Wall,
:
$ gcc -c -Wall foo.c
foo.c: In function 'foo':
foo.c:4: warning: format argument is not a pointer (arg 2)

, (
printf) . %s
, , char *, ,
int *, .
.
,
. ,
IA32 -
. IA32 ;

88

2.

, , .
,
printf .
, ,
, .
IA32 ,
. ,
long int int . ,
long %d (
long %ld). IA32
, long , int. 64-
. x86_64 (Opteron, Xeon/EM64T) int
32-, long 64-. , 64-
.
printf ,
scanf .
, scanf .
,
.
, scanf, .
, Wall,
.
-Wformat-nonliteral ,

, , .
, . ,
,
, .
-Wformat-y2k strftime strptime,
, printf.

, .

, GNU-
, , GNU-
. ,
,
.
Null pointer arguments ( NULL)


NULL . , NULL
.
glibc3.4.4, /++
.
Missing parentheses ( )

, ,

2.4.

89

, if. .
:

if ( x = y ) //versus...
if ( x == y )

1 ,
. ,
2. ,
.
, :

if ( (x = y) )

Missing braces ( )
, if/else .

1 void foo(int x, int y, int *z)


2 {
3
if ( x == y )
4
if ( y == *z ) *z = 0.0;
5
else
6
*z = 1.0;
7 }

, else
5 if 3, -
, else if 4.

:


$gcc -c -Wall foo.c


paren.c: In function 'foo':
paren.c:3: warning: suggest explicit braces to avoid ambiguous 'else'

,
if else:








1
2
3
4
5
6
7
8
9

if (x == y) {
if (y == *z) {
*z = 0.0;
}
}
else
{
*z = 1.0;
}

Uninitialized variables ( )

,
. ,

90

2.

.
,
. :







1 int foo(int y)
2 {
3
int x;
4
if ( y > 0 ) {
5
x = 1;
6
}
7
return x;
8 }

x , y <= 0.
.
,
( -O1, -O2 ..), :


$ gcc -Wall -c -O2 uninit.c


uninit.c: In function 'foo':
uninit.c:3: warning: 'x' might be used uninitialized in this function

,
, .
,
.
, gcc, .

.
gcc.

GNU-, C++
++ ,
, (,
), ++ . ,
- .
GNU- ,
++. Wall.
.
Nonvirtual destructors ( )

, .
-
. :



1 class Base {
2
char *m_ptr;
3 public:
4
Base() : m_ptr(0) { m_ptr = new char[1024]; };

91

2.4.

5
6
7
8
9
10
11
12
13
14
15

virtual char *ptr() { return m_ptr; };


~Base() { delete [] m_ptr; } ;

$ gcc -Wall -c polymorph.cpp


polymorph.cpp:1: warning: 'class Base' has virtual functions but nonvirtual
destructor

};
class Foo : public Base {
char *foo_ptr;
public:
Foo() { foo_ptr = new char[1024]; };
virtual char *ptr() { return foo_ptr; };
virtual ~Foo() { delete [] foo_ptr; } ;
};

Base ,
, Foo *.
, ~Foo
. Base - .
Reordered initializers ( )
, ,
, .
, .
, ,
. ,
:















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

struct Foo {
int m_two;
int m_one;
Foo( int one )
: m_one(one),
m_two(m_one+1) {};
};

$ gcc -c -Wall order.cpp


order.cpp: In constructor 'Foo::Foo(int)':
order.cpp:3: warning: 'Foo::m_one' will be initialized after
order.cpp:2: warning: 'int Foo::m_two'
order.cpp:6: warning: when initialized here

// declaration
// initializer

#include <iostream>
int main(int argc, char **argv[])
{
Foo f(1);
std::cout << f.m_one << std::endl;
std::cout << f.m_two << std::endl;
}

92

2.

5 6.
,
. , ,
m_two , , .
, m_two
m_one, ,
m_two.
, m_two m_one.
Deprecated features ( )
,
STL (Standard
Template Library) (, iostream.h <iostream>).
:
warning: #warning This file includes at least one deprecated or antiquated header.
Please consider using one of the 32 headers found in section 17.4.1.2 of the C++
standard. Examples include substituting the <X> header for the <X.h> header for
C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To
disable this warning use -Wnodeprecated1.

,
, .
, , #include
. .
.
, std
,
using.
, ++
++.
.
yy /
-Wno-deprecated.
yy
( )2.
yy
.

: .
32 17.4.1.2 ++.
<X> <X.h>, <iostream>
<iostream.h>.
, Wnodeprecated.
using namespace std .
, ,
.

2.4.

93

Incompatible ABI ( ABI)


-Wall, Wabi. ABI

(Application Binary Interface),



. ++,
, Fortran,
Fortran, .
, Fortran
ABI. ABI ,
.
ABI ,
.

ABI, ABI-
. ++
ABI.
, ABI , ++.
, ,
GNU- ++, ,
, ABI-
. ++, GNU-
3., c , 2., ,
ABI.

2.4.6.

, .
,
. , ,
, .
:
$ g++ -Wall -o main main.o -lfoo
main.o(.text+0x11): In function 'main':
: undefined reference to 'bar()'

, ,
,
,
.
,
++, -
, .
, ++
.

94

2.

++, ,
, .

.
, , - ,
, .
, .
nm. bar()
, ,
:
$ nm -u main.o | grep bar
U _Z3barv

_Z3barv , g++ 3.3.5


void bar(void). libfoo ,
, . ,
++ .
, ,
. ,

.
, ,
.
, :
main.c
1 extern void two();
2 int main(int argc, char *argv[]) { two(); }
one.c
1 void one(void) { }
two.c
1 extern void one(void);
2 void two(void) { one(); }

:
$ ar -clq libone.a one.o
$ ar -clq libtwo.a two.o
$ gcc -o main main.c -L. -lone -ltwo
./libtwo.a(two.o)(.text+0x7): In function 'two':
/home/john/src/linker/two.c:5: undefined reference to 'one'
collect2: ld returned 1 exit status

two() one(),
libtwo.a libone.a .
libone.a, l .
two() main(). one() ,
, one().

2.5.

95

libtwo.a two() main().


one(). ,
- one().
.
:
$ gcc -o main main.c -L. -ltwo -lone

.
, , , ,
.

2.5.

. make ,

++.

.
:
configure , GNU-
Makefile, make;
cons Perl,
make;
make UNIX- ++;
scons cons Python,
cons.
- :
www.gnu.org/software/autoconf GNU autoconf,
GNU;
www.gnu.org/software/make GNU make;
www.python.org Python;
www.scons.org scons.

3.1.
: .
, . ,
.
,
, .
, .

3.2.

UNIX
1.
, .
, , man
. GNU
Texinfo, ,
info.
man info,
,
.
man info
,

.
1


,
, .

3.2.

97

3.2.1. man

HTML PDF,
. , ,
Linux-,
. man ,
PDF-.
,
man , .
man
troff. , -
. , troff, HTML,
, .

HTML PDF. , , man
PostScript, t:
$ man -t man | lp -Pps

,
PostScript, .
man. Red Hat
, RPM, man,
Debian- man-db.
,
man. man-db
man,
.

3.2.2. man
Linux .
Filesystem Hierarchy Standard (www.pathname.com/fhs),
, , man.
.3.1.
3.1. Linux

1
2
3
4
5
6
7
8




/dev
(, /etc)

98

3.

man
. sync, , 1,
sync 2. man sync.
,
. ,
man, .
man sync,
:
$ man sync

man sync, , sync


, 2.
man ,
:
$ man 2 sync

,

. Perl.
3 Fedora.
POSIX ( ).
man sync,
2.
man, , :
$ man -a sync

man ,
Q man
.
, ,
. ,
sync 2 sync(2), sync 3
sync(3).
man, .
, , ,
.
, . , man
8 (System Administration Commands (
)), 3 (Programming Libraries (
)). ,
( ?). ,
man accept, 3,
man accept man accept Common
UNIX Printer System, 8. , ,
, , .
whatis:

3.2.

$ whatis accept
accept (8)
accept (2)

99

- accept/reject jobs sent to a destination


- accept a connection on a socket

man ,
. ,
8, 2. , man
2.
man
, .
Fedora Ubuntu ( Debian) /etc/man.config,
Knoppix ( Debian) /etc/manpath.config.
mandb
~/.manpath.

(. 3.1).

.3.1. khelpcenter man

3.2.3. man:
apropos
,
. : apropos whatis .

100

3.

propos UNIX- ,

, ,
. apropos
, .
, ().
, apropos NAME
,
.
NAME, apropos .
. ,
, ,
.
, man manual (
), command () .. .
.
,
. , compression ()
,
apropos compression,
: bzip2 gzip. ,
Fedora Core 4:
$ apropos compression
Compress::Zlib
(3pm) - Interface to zlib ...
pbmtopsg3
(1) - convert PBM ...
SSL_COMP_add_compression_method (3ssl) - handle ...
zlib
(3) - compression/decompression ...

, compression ()
compress () compressor (), ,
, gzip bzip2 .
.
whatis, :
$ whatis gzip bzip2
gzip (1)
bzip2 (1)

- compress or expand files


- a block-sorting file compressor, v1.0.2

apropos Red Hat


. , mandb- apropos Debian
, ,
.
1. :
$ apropos 'mag[tn]'
mt (1)
mt-gnu (1)
1

- control magnetic tape drive operation


- control magnetic tape drive operation

regex(7).

101

3.2.

rmt (8)
rmt-tar (8)
xmag (1x)

- remote magtape protocol module


- remote magtape protocol module
- magnify parts of the screen

mag[tn] magtape ( )
magnify (), ,
. mandb- apropos
Debian
e. , ,
compress (), compressor (
). .3.2
.
3.2.



mandb-

man
, .
man ( ),
. ,
, .

3.2.4. man:
whatis
, man
, ,
, . , ,
, readdir, man readdir,
2 , :
This is not the function you are interested in. Look at readdir(3) for the POSIX
conforming C library interface. This page documents the bare kernel system call
interface, which can change, and which is superseded by getdents(2)1.

, man ,
, , POSIX.
, . ,
man , whatis:
1

, . POSIX
readdir(3), .
,
getdents(2).

102
$ whatis readdir
readdir
readdir

3.

(2)
(3)

- read directory entry


- read a directory

man 3,
:
$ man 3 readdir

whatis ,
, mandb-
. ,
, zip,
:
$ whatis -w "*zip"
funzip (1)
gunzip (1)
gzip (1)
unzip (1)
zip (1)

filter for extracting from a ZIP ..


compress or expand files
compress or expand files
list, test and extract compressed ...
package and compress (archive) files

man
. man
a, man,
. ,
. , man -a read Fedora
Core4 man.
man
, - .

3.2.5. man
man Linux ,
7 1. man NAME,
,
.
apropos.
, man NAME.
,
.
SEE ALSO man
. , -
man, ( ).
, ,
. , man ,
.
1

. man(7).

3.2.

103

SEE ALSO .
- ,
man.
ENVIRONMENT ,
.
,
.
,
, .
ENVIRONMENT .
, , CONFORMING
TO, , .
, .
, Linux
. ,
. , bcopy
, memcpy, CONFORMING TO
bcopy, , 4.3BSD. memcpy
4.3BSD, , ISO C (ISO 9899),
.
man BUGS. ,
,
,
. BUGS
, ,
.
, .
, , , .

3.2.6. man
,
, .
1,
:
$ whatis intro
intro
(1) - Introduction to user commands
intro
(2) - Introduction to system calls
intro
(3) - Introduction to library functions
intro
(4) - Introduction to special files
intro
(5) - Introduction to file formats
intro
(6) - Introduction to games
intro
(7) - Introduction to conventions and miscellany section
intro
(8) - Introduction to administration and privileged commands
1

intro(2).
.

104

3.

, ,
, ,
. ,
, ,
. 7, ,
, .
.3.3 7,
.
3.3. 7 ,

ascii(7)

boot(7)

bootparam(7)

charsets(7)

hier(7)

man(7)

operator(7)

regex(7)
suffixes(7)

units(7)

ASCII . ,
, ,
Ctrl+G
.
,
Linux
,
.
, , ,
, Linux,
. , (i18n)
Filesystem Hierarchy Standard,
Linux.
, :
/bin /usr/bin?
,
, ,
man?
, troff, ,
man Linux
. ,
.
:
if ( 1 & 2 == 2 ) printf(bit one is set\n);
,
Linux .
.
. ,
, SI6
(SI Systme International dunits
). , ,
SI, 1 1000000, 1048576?
, .

,
.

3.2.

units(7)

uri(7), url(7), urn(7)

105

SI ,
. , , . ,
1048576 ( M).

, ,
,
.
,
.
man,
URL- . ,
URL, , , , -

, man, .3.3,
7,
, .
, man.

3.2.7. GNU- info


GNU-
info. GNU- man

info.
GNU- info Texinfo.
troff, Texinfo HTML XML,
. TeX (-
) Scribe,
. Texinfo
,
. troff, Texinfo
HTML
.

3.2.8. info
GNU- info GNU-
Emacs. .3.2 , info
Emacs. Emacs,
. info. ,
man, info
, , less,
man. man info
. , vi,
less,

106

3.

, vi. , info
Emacs
. info --vi-keys, vi
, info
, vi less.

.3.2. Emacs info

, info.
man, .
, info,
. info info, info,
info. info
info-stnd. man ,
.
, info .
man, info. info
info, man.
, Emacs
info,
pinfo (http://pinfo.alioth.debian.org). ,
man, . info,
,

3.2.

107

, .
,
, vi.
GUI-,
info. URL-
info:topic. GNOME gnome-help,
man info. !
KDE khelpcenter,
info (.3.3).

.3.3. khelpcenter info

info , ,
. ,
, :
$ info "(make)Quick Reference"

, ,
- , .
pinfo info
, khelpcenter,
. ,
man, info :
$ info --subnodes some-topic | less

108

3.

--subnodes info
stdout. less
man.

3.2.9. info
man, info
-apropos. info ,
-apropos,
, info ,
apropos, man.
man, . man,
. ,
,

.
info w,
, whereis, - ,
info ,
, man.

3.2.10. info
GNU-
man info, GNU
man ,
gcc. Texinfo-
. man gcc3.4.4, , 54000,
info 158000. info
, man
.
,
info. .3.4 .
3.4. info,
info
coreutils

cpp

,
. UNIX
- ,
.
(Arnold Robbins) Linux Journal, Opening the software toolbox (
)
, , . man
. , info

3.2.

info
gcc

ld

libc

109

,
man gcc, .
, ,
, . ,
info

man info
, .

3.2.11.

Linux
: GNOME KDE.
,
.
GNOME gnome-help ( yelp),
2.10.0
GNOME.
.
, .
HTML-, README
HTML. Mozilla Firefox
. ,
? Mozilla Firefox
URL-:
$ firefox file:///usr/share/doc/someproject/README.html

, file: (
). yelp
, Mozilla Firefox.
, ,
,
:
$ yelp README.html

, yelp
HTML- , man
man.
yelp, khelpcenter
HTML-. khelpcenter

110

3.

yelp.
khelpcenter
KDE-, man info
GNU. info, , ,
, man .
SEE ALSO man
man, .
. khelpcenter
. , ,
.

3.3.

, , ,
man info.
, . README
. ,
- - , .

3.3.1. /usr/share/doc
Filesystem
Hierarchy Standard, man info.

, README.
,
,
. ,
, README.
HTML - man.
. ,
Texinfo, , DocBook.
Debian doc-linux,
/usr/share/doc HOWTO The
Linux Documentation Project (www.tldp.org) ( ).
-
.

3.3.2.

. ,

3.3.

111

, .
Linux ,
-,
, . Linux
,

.
man,
SEE ALSO . ,
DHCP dhcpd man.
, dhcpd DHCP.
,
dhcpd.conf, .

man, SEE ALSO .
man
:
SEE ALSO
dhclient(8), dhcrelay(8), dhcpd.conf(5), dhcpd.leases(5)

, man,
. , man
.

man apropos, .
, , .
, ,
man,
.
apropos
, .
, man.
man makewhatis,
cron. ,
man
. , ,
. -
, , ,
.
mandb- man , (
) mandb. ,
mandb ,
makewhatis. cron. info
. info
apropos- --apropos.
apropos, .

112

3.

3.3.3.
1 .
.
- .
, . ,
/usr/bin diffstat ,
. , man diffstat,
. :
$ rpm -qf /usr/bin/diffstat
diffstat-1.38-2

,
, /usr/bin/diffstat.
diffstat-1.38-2.
, :
$ rpm -qi diffstat
...
Summary
: A utility which provides statistics based on the output
of diff.
Description : ...

, , ,
, , ,
- .
README HTML-,
.
man.
, .
diffstat-1.38-2 , - man
:
$ rpm -ql diffstat
/usr/bin/diffstat
/usr/share/man/man1/man1
/usr/share/man/man1/man1/diffstat.1.gz

man1
man1, man .
diffstat,
, .
. , ,
man, :
$ man /usr/share/man/man1/man1/diffstat.1.gz

, man ,
. Debian-
undocumented(7), man,
.

3.4.

113

3.4.
Linux .
,
, ,
.
troff, man.
, .
, LaTeX, ,
.
GNU- Texinfo,

. , HTML,
.
? : .

3.4.1. TeX/LaTeX/DVI
TeX LaTeX UNIX. LaTeX
TeX.
TeX LaTeX,
. TeX,
, ,
,
. TeX ,
,
.
, TeX -
.
TeX DVI ( Device
Independent ).
xdvi, kdvi evince, DVI
.
TeX LaTeX .tex ,
. TeX
latex;
tex. LaTeX
, ,
LaTeX, , .
, Makefile.

3.4.2. Texinfo
Texinfo GNU-.
GNU-

114

3.

info Emacs . ,
info, /usr/share/info,
Texinfo. Texinfo
makeinfo. Texinfo
,
info. .info, Texinfo
.texi .texinfo.
,
Texinfo,
. Texinfo,
, , /usr/share/doc.
,
, info.
Texinfo
makeinfo. info, HTML, DocBook, XML
.

3.4.3.DocBook
DocBook SGML ( Standard Generalized
Markup Language),
. , DocBook
, SGML Document Type Definition (DTD).
SGML
XML. DocBook , SGML:
, .

, .
DocBook DTD SGML, XML, DocBook
.sgml. DocBook
, .

3.4.4.HTML
- HTML
, Texinfo,
DocBook troff. -, ,
, vi Emacs.
,
, JavaScript -.
yelp khelpcenter -
-. HTML-
/usr/share/doc.

HTML, man . ,

3.4.

115

NTP (Network Time Protocol)


HTML, .3.4.

.3.4. HTML- NTP

3.4.5.PostScript
PostScript, Adobe,
Portable Document Format (PDF).
PostScript - UNIX.
( , )
PostScript, ,
PDF.
GNU Ghostscript
PostScript UNIX. Ghostscript ,
PostScript, PostScript (
) . Ghostscript
PostScript , .

3.4.6.Portable Document Format (PDF)


PDF
, ,
. ,

116

3.

,
. ,
, .
Ghostscript PDF-,
Adobe Acrobat Reader. , .
xpdf 1 ,
. GNOME- evince ,
, xpdf. GNOME evince
PostScript, PDF DVI.

3.4.7.troff
troff man.
, UNIX2.
groff GNU- troff, ,
man. ,
:
$ man intro
$ gzip -dc /usr/share/man/man1/intro.1.gz | groff -man -Tascii | less

man man
gzip.
groff DVI- troff,
.
HP PCL PostScript. ,
UNIX. DVI,
, .

3.5.


, .
, .
,
.

3.5.1.www.gnu.org
Linux,
.
1
2

. www.foolabs.com/xpdf.
, , roff(7) .
, .

3.5.

117

, , .
GNU C (glibc), ,
, 1300 .
60 Texinfo
GNU.

3.5.2.SourceForge.net
,
GNU, SourceForge.net,
, .
, , . ,
strace, Ubuntu, SourceForge.net.
$ dpkg -s strace
Package: strace
Maintainer: Roland McGrath <frob@debian.org>
Description: A system call tracer
strace is a system call tracer, i.e. a debugging tool ...

SourceForge.net,
,
man. , SourceForge.net
, .
RPM-, ,
URL- RPM:
$ rpm -qi strace
Name
Packager
URL
Summary

:
:
:
:

strace
Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
http://sourceforge.net/projects/strace/
Tracks and displays system calls ...

RPM,
,
. Debian
SourceForge.net .

3.5.3. The Linux


Documentation Project
www.tldp.org,
. man,
Linux.
,
, , -
. .

118

3.

, HOWTO, Guides (), FAQ


( ) , , man.
, . Linux
, .
,
, - .
HOWTO ,

,
. HOWTO ,
. HOWTO ,
,
.
FAQ
Usenet, 1.
, .
, FAQ ,
,
, , .
FAQ, -,
FAQ TLDP ,
(
). ,
FAQ.
FAQ The Linux
Documentation Project www.tldp.org/FAQ/LDP-FAQ/index.html.

3.5.4.Usenet
Usenet , .
Usenet ,
. ,
,
. ,
.
,
Usenet Google
. ,
- . , ,
, , .
- , Usenet,
. , .
,
.
1

, , FAQ Frequently Asked Questions.

3.6. Linux

119

, Usenet
. Usenet
.

3.5.5.
, .
,
, ,
( ).
, , .
,
,
.
(MARC)
http://marc.theaimsgroup.com.
Linux, , .

3.5.6.
- Linux Usenet ,
, .
, ,
,
.
.

3.6.
Linux
Linux .
, .
, Linux,
.
Linux. , http://kernel.org,
. ,
,
.

3.6.1.
.
2.6
, ,

120

3.

. , , README,
. What Is Linux
( Linux),
. ,
,
Makefile. , ,
:
$ make help
Cleaning targets:
clean
- remove most generated files but keep the config
mrproper
- remove all generated files + config + various
backup files
Configuration targets:
config
- Update current config utilising a line-oriented
program
menuconfig
- Update current config utilising a menu based
program
xconfig
- Update current config utilising a QT based
frontend
gconfig
- Update current config utilising a GTK based
frontend
oldconfig
- Update current config utilising a provided
.config as base
randconfig
- New config with random answer to all options
defconfig
- New config with default answer to all options
allmodconfig
- New config selecting modules when possible
allyesconfig
- New config where all options are accepted with
yes
allnoconfig
- New minimal config
Other generic targets:
all
- Build all targets marked with [*]
* vmlinux
- Build the bare kernel
* modules
- Build all modules
modules_install - Install all modules
dir/
- Build all files in dir and below
dir/file.[ois] - Build specified target only
rpm
- Build a kernel as an RPM package
tags/TAGS
- Generate tags file for editors
cscope
- Generate cscope index
Static analysers
buildcheck
checkstack
namespacecheck

- List dangling references to vmlinux discarded


sections
and init sections from non-init sections
- Generate a list of stack hogs
- Name space analysis on compiled kernel

3.6. Linux

121

Kernel packaging:
rpm-pkg
- Build the kernel as an RPM package
binrpm-pkg
- Build an rpm package containing the compiled
kernel & modules
deb-pkg
- Build the kernel as an deb package
Documentation targets:
Linux kernel internal documentation in different formats:
xmldocs (XML DocBook), psdocs (PostScript), pdfdocs (PDF)
htmldocs (HTML), mandocs (man pages, use installmandocs to
install)
Architecture specific targets (i386):
* bzImage
- Compressed kernel image (arch/i386/boot/bzImage)
install
- Install kernel using
(your) ~/bin/installkernel or
(distribution) /sbin/installkernel or
install to $(INSTALL_PATH) and run lilo
bzdisk
- Create a boot floppy in /dev/fd0
fdimage
- Create a boot floppy image
make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build
make O=dir [targets] Locate all output files in "dir", including
.config
make C=1
[targets] Check all c source with $CHECK (sparse)
make C=2
[targets] Force check of all c source with $CHECK
(sparse)
Execute "make" or "make all" to build all targets marked with [*]
For further info see the ./README file

, Kconfig,
.
,
make config, make menuconfig ..
Kconfig ,
.

3.6.2.
Linux
, .
modinfo ,
.
.
modinfo ,
.
, .
- , ,

122

3.

modinfo, , .
,
.

3.6.3.
,
Documentation.
700, ,
DocBook. DocBook
. , , ,
:
$ make pdfdocs
$ make mandocs
$ make psdocs

# C PDF-
# C man
# C PostScript-

,
, .

3.7.

Linux , .

. ,
Linux.
.

3.7.1.,

man UNIX-.
apropos

man.
whatis whatis (
makewhatis) man.
info GNU-,
.
yelp GNOME,
-.
khelpcenter KDE.
evince GNU-
DVI, PDF, PostScript .

3.7.

123

makeinfo Texinfo

(HTML, DocBook .).


gs Ghostscript; GNU PostScript.
xpdf PDF-
X Window.

3.7.2.-
www.troff.org , troff.
www.pathname.com/fhs - Filesystem

Hierarchy Standard.
http://marc.theaimsgroup.com ,

.
www.foolabs.com/xpdf xpdf.
www.gnome.org/projects/evince evince.
pinfo.alioth.debian.org pinfo.

4.1.
,
.
, , ,
, . ,
,
.
.
.
,
, .
,
.
,
.
,
.

4.2.
,
,
.

. -
.
,
.

125

4.2.

,

IDE (Integrated Development Environment),

.
, . ,
, .
, .
. , ,
, .
Linux ,
.4.1.
4.1.

vi/Vim

Emacs

GUI-

Vi UNIX, POSIX. , ,
,
. Vim
vi ,
Linux
Emacs GNU-
.
, . Emacs
GNU/Linux
,
.
,
, WordStar
GUI-
, .
:

- .
,


vi Emacs. vi
POSIX, ,
Linux- UNIX-. Emacs

Linux-. Emacs
UNIX-,
.

126

4.

4.2.1.

,
, , ,
.
.
-
,
, .
.4.2.
4.2.

, {}, () [],
.
, .
, ,

.

,
, ,
, , .
- ,
,
.

,

. , ,

.



.
,
, ,
.
,

127

4.2.

ctags etags
.
. Emacs vi
,
, ,

, .
,

4.2.2. :
vi Emacs
vi Emacs UNIX (
Linux).
X,
.
( ),
. . vi Emacs
, ,
, GUI-
. - :
, GUI- 1.

4.2.3.Vim: vi
vi POSIX
UNIX.
.
, . Vim ,
Linux. Vim ( vi
Improved vi)
vi, ,
vi.


Vim
, Vim ,
.4.2,
1

,
.

128

4.

vi Linux.
,
. ,
.
. ,
- ,
,
.
.4.3
vi Vim. Vim '
,
. ,
vi, Vim, vi,
, Vim,
Vim.
4.3. vi Vim



vi

GUI-

Vim

vi ,
. ,
, ,
GUI- Emacs,
, .
vi : ,
Ex. ,
( ) .
, ,
vi.

, ,
vi ,
!.
. - , - ()
. ,
, , .
, vi
. ,

4.2.

129

. ,
vi, .
.4.1 vi,
.

.4.1. vi

,
,
Esc. ,
. , ,
Ex.



vi.
?... ,
. ,
j, . .
.
, , , ,
.

.
.
i a . ,
, ,
. , jkhl
, ,

.
, .

. , 40j,
40 . ,
.
,
vi. , Vim
.

130

4.


.4.4
. ,
, .

. jkhl,
.
4.4. ,

j
k
h
l
Enter
G

[[

]]
{mark}

/{expr}






. ,
.
,
. , 50G
50-


. , (), {}
[],
, { .

[[, ,
,
m. m

() , .
, , ,
-

. /,
.
Enter.
.
( ). . ws .4.18

131

4.2.

?{expr}

n
N
w
b

.
/, , .
,
,


. , ,
.4.4, ,
.
, .
.4.5.
4.5.

Ctrl+F
Ctrl+B
Ctrl+]

Ctrl+T



, . ctags,
Ex :ta
( Back
-)


,
,
. .
,
- . ,
- ,
i. .4.6
,
.
4.6.

i
a

,
,

132

4.
4.6 ()

I
A
o
O

,
, , , -

, .4.8, (d)
.4.12. Vim
( .4.7) ,
. vi Vim.
, 4s
vi ,
. Vim .
4.7.

c{motion}

{N}s

, ,
.
D (..4.10) A
,
motion,
.
, .4.4
() N ,
. c,
, . 5s
c51
() . ( )
4.8.

2cw
cta

5cta
5S

;

. ta


133

4.2.

,
,
. ,
(u), vi
. Vim
. vi ,
, Vim
,
. vi
,
. Vim
,
Ctrl+R. .4.9
, .
4.9.

,
.

Ctrl+R

( Vim)

m{letter}


. ,

Ctrl+L

. ,

zt

zz

zb

,
, vi
(register) ,
(clipboard). ,
. (cut)
(paste) vi () (yank) (put). ,
(delete) , .
,
. , .4.10,

134

4.

. vi,
, , , 5,
5 .
4.10. ,

d{motion}

y{motion}

p
P

,
.
()
,
.
motion.
dd.
().
,
.4.4

( ). motion.
yy.
, .4.4
() , ,

() , ,


motion , ,
. motion
, .4.4,
.4.11.
, .4.11,
, . ,
. .4.12
d y, ,
.
4.11.

f{char}
t{char}
F{char}
T{char}

,

f, ,

,

F, ,

135

4.2.
4.12.

dfa

dta

5yta
yy4p

dn

da

yG

y50G

d5l

5dd

,
, , .
: ,
a
, , , .
:
a
,

. : 1) yy
( ) 2) 4 (
)
,
.
d n
,
a. d a
,
. y
G. , G

,
50- , 50- .
y 50G
, ( l , ).
d 5l
. dd ,

, .4.12, y d
. ,
.

Ex
vi
. Ex ,
vi, ,
Ex, .
, Ex - ,
, . Ex
, .
, Ex -
. Ex

136

4.

, , ,
.
, , . ,
Ex .
.4.1 , Ex ,
(:). Enter,
Ex. Enter
, vi .
Ex :
, :
:[firstline][,lastline]command


, .
. ,
. ,
. , ,
. , , ,
25- 30- , d
:
:25,30d


. .4.13.
4.13. Ex

.
$
%
a
/{expr}/
?{expr}?
\/
\?
\&





, 1,$
a. , m
,
,


.4.14 Ex, ,
, vi. ,
, ,
. , :w
.
, :
10,20w foo.dat

137

4.2.

10- 20-
foo.dat. ,

.
, .
4.14. Ex

write

:w {filename}


.
. vi
. :w! ,
, . :w!

quit

:q

vi (
,
). :q!
, vi .
. :e

xit

:x

vi .
,
. :x! , ,
.
:wq

edit

:e {filename}

.
.
, vi , -
, .
,
:e!

delete

:d

map

:map {a} {b}

,
.

set

:set {argument}


Vim (..4.18).
.vimrc

help

:help

Vim.


.

vi, ,

vi,
Vim

138

4.

, , Ex,
sed ( ). ,
, .


Vim

.
, . , .
Vim
vi. Vim ,
, vi.
, , .4.15
.
4.15. , Vim

Ctrl+N/Ctrl+P

Ctrl+T/Ctrl+D

Ctrl+R

Ctrl+V

, . ,
Vim - , , ,
.
, Ctrl+N, Ctrl+P
( vi).
shiftwidth.
8 ,
set shiftwidth

, Vim.
:help
registers. . , , : %
, . ()

( vi).
, ,
vi.


,
Ex.
, ,
,
.

139

4.2.

Ex substitute,
subst , ,s.
Ex,
, .4.13.
substitute :
:s/search/replacement/flags

.
replacement flags . replacement

, .
(/), ,
. , vi ,
, .
vi :
:s/\/usr\/bin\/file1/\/usr\/bin\/file2\//


Vim ctags,
, Vim. ctags
:
$ ctags -R

,
, tags.
Vim . POSIX ,
ctags vi ,
Fortran, ctags Exuberant,
Linux, ,
, C++, Java Python.
vi
Ex. .4.16 ,
.
Vim, ,
,
vi. vi
, ++,
. ,
Vim ,
, Back
-.
4.16.

Ctrl+]

140

4.
4.16 ()

:tag name


:ta

Ctrl+T
:pop

:po

:tnext

:tn

:tprevious

:tp

:tselect name

:ts

:tags

. ,



( Vim). Ctrl+T, ,


( Vim).
,
, ++.

( Vim).
:tnext, ,

( Vim).
, ,

( Vim).

,

Vim GUI
,
.4.16, -.

IDE.
. Vim
IDE, ,
, .
,
IDE, Vim Makefile. Vim
make :make,
. , Vim

,
. :make
Ex:
:make arguments

:make make .
, .
Vim ,
. , ,

141

4.2.

.4.17. ,
Vim. POSIX- vi make,
.4.17.
4.17. , Vim

:make arguments


:mak

:cnext

:cn

:cprev

:cp

:cfile filename

:cf

make

,

,




:cnext :cprev.

make

vi
, vi,
Ex :set. , vi
.exrc,
, Vim .vimrc.
, Vim .exrc
.vimrc. ,
vi. ,
.exrc , , ,
Vim, .vimrc.
vi .exrc source
.vimrc , .exrc:
:so ${HOME}/.exrc

.vimrc ,
Vim. .4.18
, .vimrc .exrc.
4.18. ,

tabstop

ts

set ts=4

( 8). ,
,

142

4.
4.18 ()

shiftwidth

sw

set sw=4

autoindent

ai

set ai

expandtabs

et

set et

wrapscan

ws

set ws

syntax

sy

sy on

makeprg

mp

set mp=ant

errorformat

efm

set efm=%f\ %d

,
( 8).
,
tabstop
: ai , noai
( )
, ,
tabstop. (ASCII- \011)
:
( ),

,
.

,
Ex.
,

.

( Vim).
. ,
set
( Vim).
, make,

( Vim).
Vim scanf- ,
, .

Vim,
:help efm

,
:set, (, :syntax).


GUI
Vim GUI-,
, GNOME gvim, KDE kvim1.
1

, KDE GNOME
, kvim gvim .

143

4.2.

GUI-
vi. , -
Vim, gvim (-y).
Vim
GUI, - .
,
GUI, vi.
, .
, , GUI-,
. ,
GUI-,
. .4.19 ,
KDE GNOME.
4.19. , GUI- Vim

Knoppix (KDE)
Ubuntu (KDE)
Ubuntu (GNOME)
Fedora (GNOME)

vim-gtk
kvim
vim-gnome
vim-X11


Vim
,
.4.2, , .
.4.20 ,
.
Vim .
Vim
. ,
,
: :help . ,
Vim vi .
, :g.
4.20. Vim


, %
.

Ex :syn on :syn off

Ctrl+N/Ctrl+P

144

4.
4.20 ()


,
substitute
Ex

:set ai :set noai
,
, Ctrl+],
Ctrl+T

4.2.4. Emacs
Emacs GNU.
vi, .
Emacs ,
Lisp, ( , Lisp)
. Emacs
,
, ,
.
, Emacs ,
.


Emacs
, Emacs ,
.4.2, - .
, Emacs ,
- .
, .

? ?
Emacs , vi
. Emacs
,
. . ,
Emacs , ,
, ,
. ,
. Emacs
.
vi, , Emacs
. Ex
. Emacs

145

4.2.

Ctrl 1.
, ASCII,
. , Ctrl+G ASCII BEL (\007), .
Ctrl+G, .
Emacs Ctrl+G .
,
GUI-, gnome-terminal,
. gnome-terminal
,
GUI-, Emacs
2. Emacs GUI, .
Emacs :
. ,
, CC, Emacs ,
C, C++, Java .
, , -
info. Emacs CC,
, .
vi,
, .
, .
info ,
.4.21.
4.21. , Emacs

gnu

K&R
bsd
whitesmith

Stroustrup
ellemtel
linux
python
java
1
2

, Emacs,
Free Software Foundation
, Kerninghan Ritchie
Allman; K&R
, , Whitesmith , PDP-11
++, Stroustrup
Ellemtel Telecommunication
Systems Laboratories,
, Linux
,
Python
, Java

PC Alt.
, ,
Edit () gnome-terminal.

146

4.

Emacs gnu,
, .
, , , - .
, , .

Emacs
Emacs
. Ctrl, Alt Esc.
, .4.22, ,
Emacs. , Emacs
Alt .
PC , Alt .
Esc Ctrl ,
Esc ASCII (\033). : Ctrl
Alt . ,
Esc, . ,
, (
), Esc. , M-x
Esc x.
Emacs ,
. ,
Escx M-x . -
, . ,
Backspace delete-backward-char.
, Backspace
:
M-x delete-backward-char
4.22. , Emacs

C-character
-character
Esc character

Ctrl

Alt ( )

Esc,
; -character

,
, -
(, delete-backward-char).
M-x Emacs . Emacs
.
- Tab.
, Emacs ,

147

4.2.

Enter. ,
. -
, M-x,
, ,
.


vi, Emacs ,
.
.4.23.
PC-,
, Page Up Page
Dn. Emacs
. Emacs
, C-u .

:
C-u 5 M-f

Emacs.
4.23. Emacs

C-b
C-f
C-n
C-p
C-v
M-v
M-f
M-b

(: )
(: )
(: )
(: )


(: )
(: )

,
Emacs
, , , ,
, .
vi, Emacs .
Emacs (kill), (paste)
(yank), :
(yank text from the clipboard). , vi, , Emacs
(yank) , vi.
- ,
.
C-@.

148

4.

. ,
, .4.24.
4.24. Emacs

C-@

C-Space

C-k
M-k
C-w

ESC w

C-Ins

C-y
C-_

C-x u

,

() ,

,

,
,
C-@
, ,
C-@
( ) ,


Emacs ,
.4.25. ,
. ,
Esc.
4.25. Emacs

C-s
C-r
Esc C-s
Esc C-r

(
)

( )


Emacs
Emacs ,
vi.
etags, Exuberant Ctags. etags
, ,
TAGS. Emacs

149

4.2.

. ,
Emacs, .4.26.
Emacs ,
- .
, review,
:
C-x r m review

review ,
. , :
C-x b m review
4.26. , Emacs

M-.

Esc-.

.
. ,
, Return

M-*

Esc-*

C-u M-.

,
(, C++)

C-u M-.

C-x r name

C-x b name

.
Emacs , ,
.


Emacs , .
, ,
, ,
, .
GUI- Emacs,
, GUI
.
Emacs .
, F10,
. , ,
- ,
, , .
.4.2 ,
Emacs.

150

4.

.4.2. Emacs

Emacs
Emacs ,
Lisp. , ,
Emacs,
Lisp. Emacs
.emacs.
Lisp,
Emacs.
Lisp , info
Emacs, -
. ,
Lisp .
, ,
, , , .
K&R,
( )
. .emacs
:
1. (defun my-c-style ()
2.
(c-set-style "k&r")
3.
(turn-on-font-lock)
4.
(setq c-basic-offset 4))
5.
6. (add-hook 'c-mode-common-hook 'my-c-style)
7. (setq indent-tabs-mode nil)

4.2.

151

my-c-style,
CC. ,
2, k&r. 3
font-lock Emacs. , 4
c-basic-offset,
. my-c-style,
. 6 my-c-style
, , Emacs
CC. ,
. 7 ,
,
indent-tabs-mode nil.

GUI
Linux GUI-
Emacs ( Xemacs),
. Xemacs Emacs,
. GUI-,
Emacs - , ,
nw (no windows ) (
emacs-nox). , , X,
Emacs GUI- ,
,
.
GUI- Emacs.
, , ,
. Emacs
GUI-, , . GUI- Emacs ,
GUI-, .

4.2.5.
Emacs vi ,

.
, - .
vi :
POSIX,
. , , vi
, UNIX, .
vi, Vim. ,
Linux, .
vi Vim,
vi. , Elvis, Vile

152

4.

Nvi. vi
bvi.
vi.
,
vi .
bvi .4.3.

.4.3. bvi

Emacs ,
, . .
Emacs ,
, .
Lisp. ,
Emacs .
Emacs
, .
Pico Pine,
UNIX- -.
Pico , , ,
. ,
,
. .
Pine 1
GNU- Nano. GNU
Pico, Free Software
1

. www.washington.edu/pine.

153

4.2.

Foundation , Pico
General Public License (GPL). Nano , Pico,
. .4.27 .
, Vim, (
GUI-).
GUI-. .4.28 ,
.
.4.2.
4.27.

Vim
Joe

vi
Emacs, Pico, WordStar

Zile

Emacs

Jed

Emacs, WordStar .

Nano

Pico

vi
. joe jstar
WordStar; jmacs Emacs, jpico
Pico.
Zile Zile Is Lossy Emacs (Zile
Emacs). Zile ,
Emacs
.jedrc.
Jed ,
,

GNU-
Pico

6.3.71
3.1
2.2
0.99.16
1.2.4-3

Vim
Joe
Zile
Jed
Nano

4.28. ,

4.2.6.

. ,
, , ,
, .

154

4.

,

.
.4.4
, . ,
GUI- ,
.

.4.4.

4.3.

. -,
. ,
, ,
.
,
-. ,
, ,
.
(super undo).
,
,
, .

155

4.3.

4.3.1.

.4.5. A F ,
.
.
,
D F. , ,
, .
,
.
,
. F
, .
.4.5 ,
, .

, .
,
.
.4.29.

.4.5.
4.29.

RCS

CVS

Revision Control System


CVS (Concurrent Version System) . RSC ,

Concurrent Version System
RCS,
.
, ,

156

4.
4.29 ()

Subversion
GNU arch

monotone

CVS,

CVS , Subversion.
, Subversion. ,

,
: SHA1

4.3.2.

, ,
.


, ,
. ,

.
,
. CVS Subversion , GNU arch , monotone .

/
.

. .
,
.



. , ,
, . ,
,
, .


,
.

4.3.

157

, .

,
. , (
).

.

. , ,
.4.5.
,
, ,
.


CVS .
. , ,
, ,
- ,
. ,
.

,
,
.
. ,
, .


, .
, , .
1.1, 1.2 .. ,
monotone 40- .
, , , .
release_1.0,
, ,
.

4.3.3.
,
.
, .

158

4.

4.3.4. diff
patch
diff,
. ,
- .
cmp. ,
diff, .
diff
(), patch.
, ,
diff, ,
. diff,
:
$ cat -n before.txt
1 This is a line to be deleted
2 This is a line that will be changed
3 This is a line that will be unchanged
$ cat -n after.txt
1 This is a line that has been changed
2 This is a line that will be unchanged
3 This is a line that has been added
$ diff
1,2c1
< This
< This
--> This
3a3
> This

before.txt after.txt
is a line to be deleted
is a line that will be changed
is a line that has been changed
is a line that has been added

,
. ,
, <. ,
, >.
, ,
patch.
, patch.
, , before.txt,
( new.txt).
before.txt after.txt new.txt.
:
$ cp before.txt new.txt
$ diff before.txt after.txt > mypatch.txt
$ cat mypatch.txt | patch new.txt

4.3.

159

, .
, new.txt
patch, :
$ patch -R new.txt < mypatch.diff

R patch ,
. diff
patch ,
.

diff r .

. .
, diff ,
, .
diff , ,
stderr.
N,
diff , , .
, .
.

4.3.5.

diff ,
. ,
, , .
GNU- diff ,
,
.

, , .
diff , - ,
(), , :
$ diff src1 src2
1c1
< const char *somechars=":,-;+.({)}";
--> const char *somechars=":,-;+.{()}";

.
, ?
. , Vim
:
$ vim -d src1.c src2.c

160

4.

, , GUI-
gvimdiff, .4.6.
,
. . ,
Vim gvimdiff, , -
gvimdiff.

.4.6. gvimdiff
( s )

, GUI-
, xxdiff1, sourceforge.
net.
, . GUI-
, .

. ,
.
,
.
, ,
.
GNU- ,
CVS Subversion, diff3. diff3
- , :
1

. http://sourceforge.net/projects/xxdiff.

4.3.

161

$ diff3 myfile original yourfile

myfile yourfile ,
. .4.7 ,
.
, .
:
1 void foo(void)
2 {
3
printf(" .\n");
4
5
printf(" .\n");
6
7
printf(" .\n");

,
3 :
printf(" .\n");

, 7 :
printf(" .\n");

.4.7. diff3

162

4.

, .
, ,
. diff3
, , ,
,
:
$ diff3 me.c orig.c you.c
====1
1:3c
printf("
2:3c
3:3c
printf("
====3
1:7c
2:7c
printf("
3:7c
printf("

.\n");
.\n");

.\n");
.\n");

====1 ====3 ,

. ,
1 me.c, 2 orig.c, 3 you.c. ,
, . diff
. ,
diff3.
, diff3 :
$
1
2
3
4
5
6
7
8

diff3 -m me.c orig.c you.c | cat -n


void foo(void)
{
printf(" .\n");
printf(" .\n");
printf(" .\n");
}

, ,
, .
,
,
.
xxdiff,
. .4.8 ,
.
, ,
. , -

4.4.

163

.4.8. xxdiff

, diff3.
xxdiff #ifdef
, :
1
2
3
4
5
6
7
8
9
10

void foo(void)
{
#if defined( ME )
printf(" .\n");
#elif defined( ORIG )
printf(" .\n");
#elif defined( YOU )
printf(" .\n");
#endif
}

CVS ,
, , ,
. ,
, ,
: .

4.4.

,
Emacs , .
Emacs ,

164

4.

.
.
, -
, ,
. ;
; -
. , ,
.

,
:
. - ,
.
, , , .
,
.

4.4.1.
indent
UNIX cb,
. ,
.
,
. ,
vi, . ,
, ,
:
!%cb


. ,
.

, .
, ,
-
, .
Linux cb indent,
. ,
++, .
cb, .
,
, :
$ indent *.c

4.4.

165

indent ,
Emacs ( .4.21), K&R, GNU BSD.

80 ,
, indent .

4.4.2. astyle

astyle1. indent, astyle ++,
Java ( ) C#.
, Emacs,
K&R, GNU Linux, ANSI.

4.4.3.
cflow
,
,
, . ,
.
POSIX- cflow ,
. ,
. GNU POSIX cflow2, POSIX,
.

4.4.4.
ctags
,
Exuberant Ctags. ctags
,
, x.
, :
$ ctags -x ex4-5.c
afunc
function
mainfunc
function
recurs
function
xfunc
function
zfunc
function
1
2

5
9
7
3
1

ex4-5.c
ex4-5.c
ex4-5.c
ex4-5.c
ex4-5.c

void
void
void
void
void

. http://sourceforge.net/projects/astyle.
. www.gnu.org/software/cflow.

afunc(void) { afunc(); }
mainfunc()
recurs(void) { recurs(); }
xfunc(void) { zfunc(); }
zfunc(void) { afunc(); }

166

4.

cflow ctags,
, ctags ,
. cflow ,
ctags . -, ctags
, ++, cflow .
-, ctags
--ckinds.

4.4.5.
cscope
cscope .
,
ncurses.
cscope, .
cscope, .4.9.

.4.9. scope

4.4.6.
Doxygen
Doxygen ,
.
++
( HTML), . ,

4.4.

167

, Doxygen
. , :
/**
** .
*/
void func(void)
{
}

Java- javadoc. Doxygen


javadoc. ,
, ,
.
Doxygen HTML, LaTeX, PDF, RTF,
man (, troff).
Graphviz (dot)1 UML-
++. ,
UML. ,
.
Doxygen Doxyfile,
.
.4.30 ,
Doxyfile.
Doxygen
. -
, , .

. ,
,
.
4.30. Doxygen

USE_PDFLATEX
PDF_HYPERLINKS
GENERATE_HTML
GENERATE_TREEVIEW
GENERATE_LATEX
GENERATE_RTF
GENERATE_MAN

PDF- LaTeX-; GENERATE_


LATEX ( NO)
PDF- ; USE_
PDFLATEX ( NO)
HTML- ( YES)

HTML ( NO)
LaTeX (
YES)
RTF- ( NO)
man ( NO)

. www.graphviz.org.

168

4.
4.30 ()

HAVE_DOT
UML_LOOK

dot Graphvis
( NO)
UML- ( NO)

4.4.7.

GNU Compiler Collection (GCC)
. .
, gcc,
cpp,
gcc.

M.
,
. , ,
. MM.
strace1:
$ gcc -MM -I ./linux syscall.c
syscall.o: syscall.c defs.h ./linux/syscall.h ./linux/dummy.h \
./linux/syscallent.h ./linux/errnoent.h

,
Makefile, .
,
. Makefile
, , .
, syscall.c
dummy.h, syscall.c;
syscall.h.
, I.
, ,
. ,
MG, ,
.

d
(.4.31), (
1

. http://sourceforge.net/projects/strace.

169

4.5.

E).
, :
$ echo | gcc -E -dM 4.31. , -d

-dM

-dD

-dN

-dI

#define
, .
, dM. GNU-
, , .
, ,
,
, dD, ,
.

#include;

4.5.
,
.
, ,
.
, Linux: Vim
Emacs.
.
, ,
.
(), ,
.
,
,
.

4.5.1.,

:
Vim vi,

;
Emacs GNU-.

170

4.

vi Emacs.
, :
vi: Elvis, nvi, Vile;
Emacs: Zile, joe, jed.
vi Emacs
,
GUI. - - .
GUI-
, vi Emacs:
GNOME: Gedit;
KDE: Kate, Kwrite;
X (): NEdit, SciTE.
,
.
: diff, diff3,
patch, xxdiff, vimdiff, gvimdiff.
: Subversion, CVS, monotone, GNU
arch.
,
:
indent;
astyle;
cflow;
ctags.

4.5.2.
CameronD. et al. Learning GNU Emacs. 3ded. Sebastopol, Calif.: OReilly Media,

Inc., 2004.
DoughertyD. and Robbins A. Sed and awk. 2ded. Sebastopol, Calif.: OReilly
Media, Inc., 1997.
Friedl J. E. F. Mastering Regular Expressions. 3ded. Sebastopol, Calif.: OReilly
Media, Inc., 2006.
LambL. and Robbins A. Learning the vi Editor. 6th ed. Sebastopol, Calif.: OReilly
Media, Inc., 1998.

4.5.3.-
:
Emacs www.gnu.org/software/emacs;
Vim www.vim.org.

4.5.

171

:
bvi http://bvi.sourceforge.net;
gedit www.gnome.org/projects/gedit;
JED www.jedsoft.org/jed;
joe http://joe-editor.sourceforge.net;
Kate www.kate-editor.org;
nano www.gnu.org/software/nano;
NEdit www.nedit.org;
SciTE www.scintilla.org/SciTE.html;
vile http://invisible-island.net/vile;
WordStar www.wordstar.org;
Zile http://zile.sourceforge.net.
:
astyle http://astyle.sourceforge.net;
cflow www.gnu.org/software/cflow;
cscope http://cscope.sourceforge.net;
Doxygen www.stack.nl/~dimitri/doxygen;
Exuberant Ctags http://ctags.sourceforge.net, http://xxdiff.sourceforge.net.
:
arch www.gnuarch.org/arch;
cvs www.nongnu.org/cvs;
monotone http://venge.net/monotone;
Subversion http://subversion.tigris.org;
xxdiff http://xxdiff.sourceforge.net.



Linux

5.1.
,
Linux,
. , ,
, Linux.
, ,
.
Linux,
.
, , .
32-
, -
. , 32-
4 , ,
, ,
.
, , .

. ,
,
.
Linux , .
/
Linux2.6 .

5.2.

: .
, , ,

5.2.

173

, .
, ,
. ,
. , ?
.
.
Linux ,
,
.
, ,
. , ,
, ,
, , . ,
, .
,

. ,
. ,
.

.
. ,
.
, , .
,
, ,
.
.5.1
. 1 ,
. (
1/3) ,
. 0xC0000000
. ,
.
.
. POSIX-, open, close, read,
ioctl write, .
, , .
.
,
.
, read :
#include <syscall.h>
...
n = syscall(SYS_read, fd, buffer, length);

174

5. Linux

.5.1. 32-


SYS_read.
syscall.h. , Linux,
, .
.
syscall ,
.
, Linux ,
(Application Binary Interface,
ABI), Linux.

/ BIOS.
(Application Programming Interface, API),
, ABI-
, .

.
Linux API-
, . , ,
, Linux2.2, ,
2.6,
.

5.3.

175

5.3.
DOS CP/M
.
, ,
. .

.
,
.
.
,
, ,
, .
.
Linux
,
.

5.3.1.
Linux .
.
, -
, ,
.
,
.
, ,
. /
. , '
.
.
,
, .
.

5.3.2.,
Linux ( ),
,
.
, , , . ,
, .

176

5. Linux

, , .
.
, ,
. sched_yield,
.
,
. , read write,
, .
,
.
, ,
. ,
, . ,
,
.

5.3.3.

,
Linux, . ,
,
. , ,

.
, .
Linux

.
, ,
. .

. ,
.
.


,
.
1.
,
, .
1

nice, .

5.3.

177

ps
.
ps , . ps ,
.
. , .

5.3.4. nice
, ,
.
, ,
.

.
, .

nice.
nice,
; ,
.
nice
, .

nice,
nice.

5.3.5.
,
.
, ,
, .
. ,

, .
,
, .
,
.

178

5. Linux

, ,
.
, .
.
,
.
.
Linux
, POSIX 1003.1.
100 ,
(039).
Linux
41 139 ( , 40, ).
,
, ,
. -
nice, .
, .
,
. POSIX
: FIFO .

FIFO
FIFO first in, first out, ,
. FIFO
, , ,
, . FIFO
, ,
.
cruncher FIFO,
.



, FIFO, ,
.
,
. , ,
,
.
,
'
, .
,
.

179

5.3.

5.3.6.

chrt.
fork exec POSIX-.
,
POSIX-:
int sched_setscheduler(pid_t pid, int pol, const struct sched_param*p);
int pthread_setschedparam(pthread_t thread, int policy,
const struct sched_param *param);

sched_setscheduler
. pthread_setschedparam

.
sched_param. ,
.5.1.
sched_param, ,
priority,
.
POSIX-:
int sched_get_priority_min(int policy);
int sched_get_priority_max(int policy);

POSIX
, Linux
(SCHED_FIFO SCHED_
RR). , (SCHED_
OTHER), sched_setscheduler .
, , , errno
EINVAL.
nice nice setpriority. :
setpriority , ,
nice.
5.1. , POSIX

SCHED_FIFO
SCHED_RR
SCHED_OTHER

FIFO

Linux

Linux 1 99
POSIX, sched_setscheduler.
, Linux
,
. , ,

180

5. Linux

0 139. , , 1,
, 41.

5.3.7.
.
,
ps /proc (
ps). .5.2 .
5.2. ,

(Running)

(Interruptible)

(Uninterruptible)
(Stopped)
(Zombie)

R
S
D
T
Z


,
,

-
(, ptrace)
, wait ( )

,
()
, ,
. ,
.
. ,
. ,
ps, :
$ ./cruncher & ./cruncher & ./cruncher &
$ ps -C cruncher -p $$ -o pid,state,cmd
PID S CMD
2588 S bash
2657 R /bin/sh ./cruncher
2658 R /bin/sh ./cruncher
2659 R /bin/sh ./cruncher


, ps,
, .
, R, .
, ( ).
, ,
. ,
(bash) ().

5.3.

181

. , , ,
wait, ps.
bash, ,
. , , ,
( ). , SIGTERM
SIGQUIT . ,
- /
, , .
,
( ) ,
, .
,
, , . ,
,
.
, ,
.
,
.
, ,
. ,
,
. ,
.
,
- .
,
. ,
, . , ,
, .
,
kill -9, ,
.
(, , ).

wait
, ,
wait.
, , ,
.
, , ,
, - . -
1,
ps. ,
1

182

5. Linux

, init,
wait ( ).
-?
: -?
, , ,
.
. ,
( ), .
, ,
, (SIGCHLD) , . , wait,
.
, init,
-.


. , ,
Ctrl+Z ,
.
SUSP,
, . Linux
( UNIX) ,
SIGTSTP1.
2.
;
man signal(7).
, SIGCONT.
-
. ,
, ,
, . Linux
. , SIGTERM SIGKILL
, 3.
1
2
3

SIGSTOP. SIGTSTP , SIGSTOP .


.stty(1).
SIGTERM, SIGKILL
, .

5.3.

183

.
, .
, , ,
, ,
. , ,
. ,
SIGTTIN,
(),
. , SIGTTIN,
,
fg. , .
, ,
. ,
SIGTTIN :
$ read x &
[1] 5851

bash- read
.
, SIGTTIN,
. ,
T ( (Stopped)):
$ jobs -x ps -p %1 -o pid,state,cmd
PID S CMD
5851 T bash

SIGTTIN,
SIGCONT,
. ,
.
,
, .
tostop ( terminal output stop),
. ,
.
tostop stty,
.

5.3.8.
.
,
. time ,
.
bash ,

184

5. Linux

/bin/time.
bash-:
$ time sleep 1
real
user
sys

0m1.042s
0m0.000s
0m0.020s

$ time dd if=/dev/urandom of=/dev/null count=1000


1000+0 records in
1000+0 records out
real
user
sys

0m0.527s
0m0.000s
0m0.500s

sleep ,
1.
1,042.
, .
dd 1000
/dev/urandom /dev/null.
527, 500 . ,
/dev/urandom . , dd
, ,
27.
-
,
. .
,
/.



POSIX (clock_t)

. . ANSI-
ANSI- clock,
Linux.
, ,

.
clock , CLOCKS_PER_SEC,
, GNU
1000000. Linux clock
,
. ,

185

5.3.

, clock_t,
sysconf1:
sysconf(_SC_CLK_TCK);
sysconf ( ),
clock_t .
, ,
,
clock_t.
ANSI- clock ,
. ,
, clock . ,
,
,
. - clock Linux-
, ANSI .
, Linux .
POSIX- times clock_t,
-. CLOCKS_PER_SEC
times clock_t
. .
times :
clock_t times(struct tms *buf);


, - . Linux
.

, .
, times, struct tms,
. tms
:
struct tms {
clock_t tms_utime; /*
clock_t tms_stime; /*
clock_t tms_cutime; /*
*/
clock_t tms_cstime; /*
*/
};

*/
*/

tms_utime ,

. tms_stime ,
1

, POSIX ANSI- CLK_TCK


, - sysconf(_SC_CLK_TCK).

186

5. Linux

. ANSI clock ,
. tms_cutime tms_cstime
, ,
-,
wait.
times getrusage,
BSD:
int getrusage(int who, struct rusage *usage);

times, getrusage
, , .
, .
POSIX, POSIX1003.1,
clock_gettime, .
clock_gettime :
int clock_gettime(clockid_t clk_id, struct timespec *tp);

, , . timespec
:
struct timespec {
time_t
tv_sec;
long
tv_nsec;
};

- ,
. API-
, .
clockid_t. getrusage,
clock_getres .
clock_getres :
int clock_getres( clockid_t clk_id, struct timespec *res);

timespec.
API- , POSIX
, CLOCK_REALTIME.
.5.3 ,
.
5.3. , clock_gettime

CLOCK_REALTIME

POSIX
;
Coordinated Universal Time (UTC)

,
ANSI- time


SC_CLK_
TCK

187

5.3.

CLOCK_MONOTONIC

CLOCK_PROCESS_
CPUTIME_ID

CLOCK_THREAD_
CPUTIME_ID

, ,
( )

,
.
ANSI- clock,



.
,
,
;

, ,



SC_CLK_TCK

ANSI-
clock.
,
clock_getres, 1,

2.6.14
1/100

,
clock_getres, 1,

.
, clock_getres,
1,
2.6.14
1/100

clock_getres
,
. clock_gettime ,
wait .


.
,

. .
,
HZ. HZ
.
. ,
IA32 100.
GNU clock_t.
.5.4
100,
Linux.
5.4.

Linux

,
100

,
10

188

5. Linux
5.4 ()

NTSC
PAL

- ()

,
60
50
80

,
16,67
20
12,5

. .5.2
, ,
menuconfig.
, , ,
clock_t, .
USER_HZ .
sysconf(_SC_CLK_TCK). , HZ
, . ,
,
.

.5.2.


bash ,
, time,
-
.
,
.

5.4.

189

,
getrusage, ,
. getrusage ,
RUSAGE_SELF RUSAGE_CHILDREN. , ,
, RUSAGE_CHILDREN,
,
. wait,
.
, .
, , , ,
.
getrusage ,
- .
,
time. bash-
/usr/bin/time. ,
, bash-
, time ,
getrusage.
time,
, :
$ time sleep 1
real
user
sys

0m1.007s
0m0.000s
0m0.004s

, :
$ \time sleep 1
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+199minor)pagefaults 0swaps

rusage,
, Linux.
time : real (
), user ( ) sys
( ).
, real, ,
, .
, .

5.4.
.
,
. ,

190

5. Linux

, ,
, .
,
- . ,
, , .
, ,
.
API- Linux
UNIX .
POSIX
Linux.
.
.
,
open, ,
.

5.4.1.
.
,
, .
, .
,
, ,
, .



,
. .
,
. , , ,
, .
,
.

.
.
,
, mmap lseek1.
1

lseek,
. 1 0,
, ,
.

5.4.

191

mem .
, , ,
.5.5. , /dev/null,
- .



. ,
.
,
, -.
(Logical Block Addressing,
LBA),
.
, ,
.



. .
, ,
eth0, ioctl
. .
, man
netdevice(7), .


,
.
.
, .
, . , ,
XFS
. ioctl
.

5.4.2.
,
.
, ,
.
,
.
, .

192

5. Linux

.ko ( kernel object )


insert. (
) rmmod.
,
:
$ make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install

/lib/
modules. depmod,
.

5.4.3.
,
, . ,
.
,
. Linux2.4 ,
, 16-,
, 8. Linux2.6
32-, 12 ,
20 .
mknod,
( ),
. -
,
. , ,
,
.
. , /dev/mem,
1 1,
:
$ mknod /dev/mem c 1 1

/dev , ,
1.

. ,
, -.
, .
mem,
(. .5.5).
. , - ,
1

,
nodev; .mount(8).

5.4.

193

,
.
/dev , .5.5.
, , /dev/mem open mem
1. ,
, .

. open
, ,
. , ,
,
,
. /dev/mem /dev/kmem
root,
mem.
, .
5.5. , mem

mem
kmem
null

1
2
3

port

zero
full

5
6
7

random

urandom

kmsg

10
11



. ,
,
/ (
)

/dev/core, /proc/kcore
ENOSPC

; ,
.
.random(4)
random, ,
, ,
.
.random(4)
mem

syslog

,
root
.
, /dev/mem
, . ,
root ,

194

5. Linux

, /mnt/floppy, nodev mount


/etc/fstab.


-,
. ,

.
IDE- (/dev/hd). Linux2.6
6 , 14
. , IDE-
16384 (214) ,
63 (26 1);
.
, ,
, . (
0) .
ls:
$ ls -l /dev/hd[ab]*
brw------- 1 john disk
brw-rw---- 1 root disk
brw------- 1 john disk
brw-rw---- 1 root disk
brw-rw---- 1 root disk

3, 0 Dec 21
3, 1 Dec 21
3, 64 Dec 21
3, 65 Dec 21
3, 66 Dec 21

10:00
03:59
10:00
03:59
03:59

/dev/hda
/dev/hda1
/dev/hdb
/dev/hdb1
/dev/hdb2

ls ,
, .
, IDE- 3,
hda.
/dev/hda 3 0. 1
1,
/dev/hda1. hda .
hdb, 64.
.5.6.
SCSI- , IDE-,
, 4
, 15.
16, SCSI- 65536 (216)
. SCSI- : 16
+ .
5.6. IDE- ( Linux2.6 )

0
0

0
1

= 64 -
+
0
/dev/hda
1
/dev/hda1

195

5.4.

1
1
1

0
1
2

= 64 +
64
65
66


/dev/hdb
/dev/hdb1
/dev/hdb2


(
) , , . ,
,
,
. , ,
,
. , , mem
.
.
Linux2.6,
256
.
, ,
,
. .
Linux2.6 . ,
,
1024. ,
.
,
.

www.lanana.org/docs/devicelist.
Documentation/devices.txt.
, IDE
, .
86 IDE-. ,
, .
/dev
. ,
/dev,
. , ,
.


Linux2.4 ,
, /dev.
, ,

196

5. Linux

. ,
, , ,
, ,
. Linux
.
devfs,
/dev.
, .
, /dev ,
.
, .
udev hotplug. hotplug

, .
hotplug ;
. ,
,
,
. ,
.
udev /dev
, ,
. ,
hotplug1.
udev
man udev(8).
sysfs. sysfs hotplug,
,
. sysfs
procfs, .
(kobjects kernel objects),
2.6, sysfs 2.4 .
sysfs /sys,
.
, ,
/proc procfs.
sysfs procfs,
(
)
.
procfs
, sysfs ,
.
.
1

. www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html.

5.4.

197

5.4.4. /
, ,
. ,
: , .
, .
, /
, .

/
,
, .

.

,
.
, , , read write,
,
1.
.
, ,
. .

, .
, ,
mmap.
.
mmap
read write. mmap,
MAP_FAILED, errno ENODEV.

,
/
,
.
:
.
.

. ,
, .
.
,
1

198

5. Linux

.
, 1.
mkfs,
t.
, , ,
Windows, :
$ mkfs -t vfat /dev/fd0

,
mount:
$ mount -t vfat /dev/fd0 /mnt/floppy

mount ,
, t .
, :
(, /dev/fd0) (
, /mnt/floppy).
() , , .
, .
, .
,
,
tar. tar ,
, ,
, ,
. ,
, .


,
. Linux
,
2.
, ,
.
,
. , ,
,
.
.
,
, .
, ,
1
2

. man fd(4).
,
Linux.

5.4.

199

.
, , ,
, .
.
,
,
.
, ,
,
.
, ,
,
.
,
,
.
vmstat:
$ vmstat
procs -----------memory---------- ...
r b
swpd
free
buff
cache
1 0
0 93412
5736
38096
4 ramdisk.
$ dd if=/dev/zero of=/dev/ram0 bs=1k count=4096
4096+0 records in
4096+0 records out
$ vmstat
procs -----------memory---------- ...
r b
swpd
free
buff
cache
0 0
0 89272
9832
38096
4 .

vmstat ,
, 1.
4 /dev/zero ( ) ramdisk-
/dev/ram0 ( ).
,
, .
vmstat , 5736 9832,
4096. ramdisk ,
, ,
, - .
.
, ,
:
1

. free(1), procps. . /proc/meminfo.

200
$ mkfs -t ext2 /dev/ram0
mke2fs 1.37 (21-Mar-2005)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
4096 inodes, 16384 blocks
...

5. Linux

$ vmstat
.
procs -----------memory---------r b
swpd
free
buff
cache
1 0
0 88792
10164
38272
$ mount /dev/ram0 /mnt/tmp

$ vmstat

procs -----------memory--------- .
r b
swpd
free
buff
cache
0 0
0 88792
10168
38272

2 .
$ dd if=/dev/zero of=/mnt/tmp/zero.dat bs=1k count=2048
2048+0 records in
2048+0 records out
$ vmstat

procs -----------memory--------- 2 .
r b
swpd
free
buff
cache
1 0
0 86572
10200
40324

,
.
, .
.
.
,

.
sync
fdatasync.

,
.
, ,
, .

, ,
. ,

5.4.

201

/, '
.
, , ,
- . ,
, .
,
. , ,
, .
,
/. ,
, , ,
. ( ) ,
.
ramdisk tmpfs
ramdisk ,
,
. , ,
, ramdisk ,
.
ramdisk .
,
, ,
, . ramdisk ,
,
tmpfs.
tmpfs ,
. tmpfs .
,
ramdisk .
tmpfs

(/dev/shm) .



, .
,
, -,
.

202

5. Linux

pdflush, ,
.

pdflush ,
, . , , ,
: . ,

. . ,
init , ,
,
, .
, ,
. ,
/proc/PID/maps, .
- maps .

,
.
, , , .
, , ,
,
/. pdflush .
, .

fsync, fdatasync sync.
sync sync .
( )
/
,
pdflush.

5.5. /
/
.
.
. /

. ,
,

5.5. /

203


. , -,
/
.
Linux /
,
().
,
.
, ,
, .
.
, ,
.

5.5.1. Linus Elevator


( noop)
Linux2.4
/, Linus Elevator.
/ , ,
,
,
. , -
/, .
, :
,
.
, , ,
, .
, , .
Linus Elevator , ,
, .
,
.
write , .
,
/. :
, ,
,
.
, ,
. , ,
(
), .

204

5. Linux

,
.
,
/.
/.
, ,
, , /,
, .
/
.
Linux2.6
/. Linus Elevator -
noop,
/ ,
.

5.5.2. /
(Deadline I/O Scheduler)
,
noop, ,
. ,

( ). ,

.
, noop.

5.5.3.
/ (Anticipatory I/O Scheduler)
,
, ,
6 ,
/. ,
.
.

5.5.4.
/
(Complete Fair Queuing I/O Scheduler)
/,
Linux. / ,

5.5. /

205

. /,
, , ,
,
/.

5.5.5.
/
Linux2.4 2.6 /
,
elevator.
( lilo.conf
grub.conf). ,
.5.7, Documentation/
kernel-parameters.txt.
2.6 elevator
.
. ,
,
, /sys/block/{}/queue/scheduler, :
$ cat /sys/block/hdb/queue/scheduler
noop [anticipatory] deadline cfq
5.7. /, Linux 2.6

noop

deadline

as

cfq

Linus Elevator Linux2.4 .



noop, ,
/
( )
/ (Anticipatory
I/O Scheduler); deadline, ,
6
/
(Complete Fair Queuing I/O Scheduler); deadline, , /
,

, hdb
/ (Anticipatory
I/O Scheduler). ,
. , ,
cfg, :
$ echo cfq > /sys/block/hdb/queue/scheduler

206

5. Linux

.
, , deadline,
cfq. , RAM-
-, noop.

5.6.


, Linux, ,
,
.
. ,
,
.
32- ,
,
. 32-
,
Linux. 64- ,
, .
64- ,
, 32-
.

5.6.1.
,
, ,
. , - ,
, (),
. ,
,
,
.
.5.3.
Linux
,
.
.
,
. ,
, , ,
.

5.6.

207

MMU (Memory Management


Unit).

.5.3. ,

MMU
MMU
,
. , MMU, .
MMU.
MMU- ,
,
. ,
MMU- : ,
.5.4.

208

5. Linux

, 4
,
PowerPC IA32. 20,
12.

.5.4. ,

, MMU-
, , .
,
.
,
, MMU- . MMU-
, .
,
. ,
(SIGSEGV).
,
.

, .
, /, Linux
. Linux

5.6.

209

,
,

, .
,
. - ,

,
.

TLB (Translation Lookaside Buffer)

,
. ,

.
, , U-

,
,
. ,

:
.

,
.
, ,
,

TLB

L-
,

(Translation Lookaside Buffer).

, . L-
, ,

. ,

512

128

L-. .

5.5

L-.
L- ,
, ,
. L-
,

,
.

u L- .

210

5. Linux

.5.5. TLB


DRAM,
-, ,
- DRAM-.
L1,
(
8 32 ), . ,
.
, L1 .
,
. , , x86
L1.

-, ,
. L2,
L1, ,
,
.

5.6.

211

L2
L3,
-, .
Intel L3
Xeon. ,
L4 L5 .
.
.

L1,
. ,
.
4 ,
32 62 .
.
, .
, ,
(, 64
). ,
;
.
L1 .
L2 L3 ,

. , ,
, ,
.
.
, .5.6,
,
,
.
,
.
, ,
3 ,
,
.
, .
,
. .

.
,
. ,

212

5. Linux

.5.6. :

,
. ,
,
,
.

1.
, ,
,
. - ,
- . ,
,
.

,
.
1

/
/, .

5.6.

213

,
. ,
.
,
. ,
,
,
1.

, ,
, .
,
.
.

-

-
madvise. API- ,
. ,
,
,
. ,
,
:
madvise( pointer, size, MADV_WILLNEED | MADV_SEQUENTIAL);



.
, ,
. ,
,
.
, .
,
.
(Symmetric Multiprocessing, SMP)

.

,
1

BIOS
.

214

5. Linux

. ,
(Direct Memory Access, DMA),
.
, , ,
.
DMA- ,
, , ,
().
.
, , ,
, .
DMA-, ,
.
() .
,
.

, mmap.
, () .
, , ,
.
, ,
, .
POSIX msync,
,
() .



. , ' ,
, .

, .
,
,
.
, (swap),
.
, . ,
, ,
,
.
.
(paging).
, ,
,

5.6.

215

' , .

, ,
.
,
.
-
(Out-Of-Memory Killer (OOM);
).
,
/.
' ,

.
,
, :
.
,
swapon swapoff. Linux
,
.
a,
/etc/fstab,
.
.
mkswap,
.
,
.

.


Linux
.
,
. ,
.
,
.

.
,
.
. 32-
3

216

5. Linux

1 .
.
32- 3 .
, ,
,
. 5.1.
5.1. pause.c: ,
int main()
{
return pause();
}

5.1 - ,
, .
.
( ) /proc/PID/maps,

pmap, procps:
$ ./pause &
[1] 6321
$ pmap 6321
6321:
./pause
004d0000
104K
004ea000
4K
004eb000
4K
004ee000
1168K
00612000
8K
00614000
8K
00616000
8K
08048000
4K
08049000
4K
b7f08000
4K
b7f1a000
4K
bfb05000
88K
ffffe000
4K
total
1412K

r-x-- /lib/ld-2.3.5.so
r---- /lib/ld-2.3.5.so
rw--- /lib/ld-2.3.5.so
r-x-- /lib/libc-2.3.5.so
r---- /lib/libc-2.3.5.so
rw--- /lib/libc-2.3.5.so
rw--[ anon ]
r-x-- /home/john/examples/mm/pause
rw--- /home/john/examples/mm/pause
rw--[ anon ]
rw--[ anon ]
rw--[ stack ]
----[ anon ]

pmap
. ,
.
pmap , ,
. , ,
, 1,4 , '
(/lib/libc-2.3.5.so).
(ld-2.3.5.so),
112 .
4, .
: libc 1,1

5.6.

217

, , ,
, ,
1,1 .
.
,
,
. , ,
, , ,
.
, 616000, ,
8048000 ( 122 ).
,
,
.
.
8086,
. 5.2 , 5.1,
8086. ,

.
5.2. pause.s: , 80x86
.text
# _start .
.global _start
.type _start, @function
# .
sighdlr:
ret
_start:
# BSD signal() syscall; : signal(SIGCONT,sighdlr)
movl $sighdlr, %ecx
movl $18, %ebx
movl $48, %eax
Int

$0x80

# 3- , sighdlr
# 2- , 18 = SIGCONT
# 1- , 48 = BSD
signal()
#

# pause()
movl $29, %eax
int $0x80
#

# 1- , 29 = pause()
#

218

5. Linux

# , SIGCONT.
movl $0,%ebx
movl $1,%eax
int $0x80

# 2- ,
# 1- , 1 = exit()
#

:
$ gcc -nostdlib -o pause pause.s

:
$ ./pause &
[1] 6992
$ pmap 6992
6992:
./pause
08048000
4K
08049000
4K
bf8f5000
88K
ffffe000
4K
total
100K

r-x-rwx-rwx------

/home/john/examples/mm/pause
/home/john/examples/mm/pause
[ stack ]
[ anon ]

, , ,
Linux exec.
, . exec

8049000.
, , ,
, .
pmap , ,
. exec
. exec ,
, 88 .
, ffffe000, Linux2.6
,
IA32. ,
.
PAE (Physical Address Extension),
Intel
, , DIMM-, .
, .
, . , ,
32- 32- ,
232 , 4 . 32-
, , - 4 ,
, .

5.6.

219

, ,
4 .
, -
.
64- . .
, ,
,
. Intel ,
64- .
PAE (Physical Address Extension), Intel, 64 (236 )
20 24 .
, - 12 . , 36 .
32- , 4 .
MMU (Memory Management Unit)
,
24-
. , , , 64 (236 ).

5.6.2.
,
.
,
- .
, ,
,
- .
.

,

:
.
.
, 1
, malloc
? , 5.3,

220

5. Linux

.
1 , malloc .
5.3. crazy-malloc.c:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
void *ptr;
int n = 0;
while (1) {
// 1
ptr = malloc(0x100000);
// ,
if (ptr == NULL)
break;
n++;
}
// ?
printf(" %d \n", n);

// , .
pause();

5.3 32- 160


, . ,
? malloc ,
3 ! pause,
:
$ ./crazy-malloc &
[1] 2817
malloced 3056 MB
( 3056 )
$ jobs -x pmap %1
2823:
./crazy-malloc
000cc000
4112K rw--[ anon ]
004d0000
104K r-x-- /lib/ld-2.3.5.so
004ea000
4K r---- /lib/ld-2.3.5.so
004eb000
4K rw--- /lib/ld-2.3.5.so
004ee000
1168K r-x-- /lib/libc-2.3.5.so
00612000
8K r---- /lib/libc-2.3.5.so
00614000
8K rw--- /lib/libc-2.3.5.so
00616000
8K rw--[ anon ]
006cf000 124388K rw--[ anon ]
08048000
4K r-x-- /home/john/examples/mm/crazy-malloc

5.6.

221

08049000
4K rw--- /home/john/examples/mm/crazy-malloc
08051000 2882516K rw--[ anon ]
b7f56000 125424K rw--[ anon ]
bfa43000
84K rw--[ stack ]
bfa58000
5140K rw--[ anon ]
ffffe000
4K ----[ anon ]
total 3142980K

. (heap) ,
++ .
,
GNU- . , ..
. (The C Programming Language,
Prentice Hall),
.
, ,
, .

brk.
,
.
.
,
. ,
, .5.7.
, , , ,
2 4, .
1, .
,

. 2 4 . ,
, ,
, . ,
.

, , GNU-
,
, 5.4.
GNU-
,
mmap.
, .5.7,
, 1.
1

,
, .

222

5. Linux

.5.7.

,
, -
.
.
.

mmap mallopt.
man,
info, libc, :
$ info libc mallopt

mallopt SVR4,
.
GNU- .5.8.
5.8. GNU- mallopt()

M_MMAP_THRESHOLD


.
, ,
mmap

5.6.

M_MMAP_MAX

223



mmap ,
. . mmap

. crazy-malloc
,
160 .
,

mmap. strace, ,
malloc mmap, :
mmap2(NULL,1052672,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0)

mmap2
MAP_ANONYMOUS.
, - -1.
,
, mmap2
, . ,
, .
, , , ,
.
,
, .
5.3 ,
,
, ,
5.4.
5.4. crazy-malloc2.c:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
void *ptr;
int n = 0;
while (1) {
// 1
ptr = malloc(0x100000);
//,

224

5. Linux

if (ptr == NULL)
break;
// .
memset(ptr, 1, 0x100000);
printf(" %d \n", ++n);
}
// , .
pause();
}

5.4
, . ,
, ,
:
$ ./crazy-malloc2
malloced 1 MB
malloced 2 MB
malloced 3 MB
...
malloced 74 MB
Killed
$


Out-Of-Memory Killer (
OOM). ,
.
.
,
, .
, (, ).
, /var/log/messages,
:
Out of Memory: Killed process 2995 (crazy-malloc2)

, malloc ,
. malloc
, .
, malloc , .
Linux , ,
, . ,
.
, .
,
root:
$ echo 2 > /proc/sys/vm/overcommit_memory

5.6.

225

,

, Out-Of-Memory Killer
,
. ,
,
.
, .
,
, ( ).
,
.
, .
,
Out-Of-Memory Killer,
. ,
- .



, .
. , , ,
ramdisk, () .

mlock munlock.
,
. ,
, , .

mlock munlock.
() , :
r = mlock( ptr , size );

POSIX-, mlock ,
-1.
, ,
, .
,
.

, .
, .
, ,
, .

226

5. Linux


mlockall. ,
MCL_CURRENT ,
MCL_FUTURE .
-
, .
:
r = mlockall( MCL_CURRENT | MCL_FUTURE );

, ,
, .
, brk
( malloc)
, ,
.
.
mlock, mlockall
, .
,
. mlockall (MCL_FUTURE),
,
.
,
. , malloc brk,
, , , , malloc
NULL. ,
, .
, mlock mlockall ,
munlock munlockall.
,

.
, ,
,
,
. ,
.
,
Out-Of-Memory Killer.
GNU- , sysconf.

:
num_pages = sysconf( _SC_AVPHYS_PAGES );

,
.

5.6.

227

MemFree /proc/meminfo.
,
, .

setrlimit,
,
. 5.5
crazy-malloc,
setrlimit, , .
5.5. crazy-malloc3.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

#include
#include
#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<limits.h>
<signal.h>
<unistd.h>
<sys/types.h>
<sys/stat.h>
<sys/resource.h>

int main(int argc, char *argv[])


{
void *ptr;
int n = 0;
int r = 0;
struct rlimit rl;
u_long pages, max_pages, max_bytes;
pages = sysconf(_SC_AVPHYS_PAGES);
/* max_bytes, */
max_pages = ULONG_MAX / sysconf(_SC_PAGE_SIZE);
if (pages > max_pages)
pages = max_pages;
max_bytes = pages * sysconf(_SC_PAGE_SIZE);
r = getrlimit(RLIMIT_AS, &rl);
printf(" %ld \n",
(u_long) rl.rlim_max / 0x100000);
/* . */
rl.rlim_cur = max_bytes;
r = setrlimit(RLIMIT_AS, &rl);
if (r) {
perror("setrlimit");
exit(1);

228
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 }

5. Linux

}
printf(" %ld \n", max_bytes / 0x100000);
while (1) {
// 1
ptr = malloc(0x100000);
// ,
if (ptr == NULL) {
perror("malloc");
break;
}
memset(ptr, 1, 0x100000);
printf(" %d \n", ++n);
}
// , .
printf("\n");
raise(SIGSTOP);
return 0;

crazy-malloc3 ,
Out-Of-Memory Killer, .
:
$ ./crazy-malloc3
current hard limit is 4095 MB
limit set to 53 MB
malloced 1 MB
malloced 2 MB
malloced 3 MB
...
malloced 50 MB
malloced 51 MB
malloc: Cannot allocate memory
paused

rlimit .
; .

, .

,
. ,
setrlimit, getrlimit (
27),
.
.

5.7.

229

5.7.

Linux. ,
.
, ,
,
.
Linux ,
. ,
.
, .
, .
, .
,
/ .
/
.

. ,
;
Out-Of-Memory Killer.

5.7.1.,

mkswap, swapon, swapoff

.
nice, renice, chrt .
pmap .
ps, time, times ,

.
strace ,

5.7.2.API-,

clock_getres, clock_gettime POSIX.
getrusage, times

.
mallopt API- GNU, malloc.
mlock, mlockall .

230

5. Linux

mmap, msync, madvise API-,

.
pthread_setschedparam
.
sched_get_priority_min/max
.
sched_setscheduler .
sysconf .

5.7.3.-
www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html
udev.
http://linux-hotplug.sourceforge.net ,
Linux- hotplug.

5.7.4.
CesatiM. and Bovet D. P. Understanding the Linux Kernel. 3ded. Sebastopol,

Calif.: OReilly Media, Inc., 2005.


KernighanB.W. and RitchieD. The C Programming Language. Englewood Cliffs,
N.J.: Prentice Hall, 1988.
Kroah-HartmanG., Corbet J. and Rubini A. Linux Device Drivers. Sebastopol,
Calif.: OReilly Media, Inc., 2005.
LoveR. Linux Kernel Development. 3ded. Indianapolis: Novell Press, 2005.
RodriguezC. S., Fischer G. and Smolski S. The Linux Kernel Primer: A Top-Down
Approach for x86 and PowerPC Architectures. Englewood Cliffs, N. J.: Prentice
Hall, 2006.

6.1.
5.
.
.
, , .
, ,
API-,
.

6.2. ?
Linux
.
, () .
init.
.

. init ,
.
, .
.
Linux .
,
UNIX-: fork vfork. Linux
, . clone.

6.2.1. fork vfork


fork
.
, . fork

232

6.

(pid_t), ,
. ,
,
fork. ,
( -1
). ,
, .
.
exec ( ),
.
vfork .
fork, , ,
. fork

. ,
exec. . ,
, init. init
,
. vfork
, init.
vfork , ,
exec , -
. , , ,
exec . , ,
man vfork(2).
UNIX- , fork
vfork vfork
, .

6.2.2.

.
. ,
,
,
. ,
- .
- ,
,
. , , .6.1.

, ,
.
,

6.2. ?

233

, .

.

. 6.1. :
,

, ,
init
. init
fork exec.
init. ,
, fork exec
init ( ).
xinetd, sshd ftpd.

6.2.3. clone
clone
Linux .
.
clone. API- POSIX
, .
clone ,
fork.
,
.
, .
, .

234

6.

6.3. exec
exec
. Linux exec ,

. man
exec(3)1. exec
, execve. ,
,
. execve :
int execve(const char *filename, char *const argv [], char *const envp[]);

execve ,
, , , ,
. , execve
. execve
. exec
exec, exec(3).
.
,
. , execve
(-1), errno EPERM.
,
, .
: ,
.

6.3.1.
,
, .
execve , errno
ENOEXEC ( exec).
.
.
#!,
, .
,
#!/bin/sh

, #!,
.
, execve -1.
1 :
1

, man 3 (),
2 ( ).

235

6.3. exec

argv[0] ;
argv[1] , ();
argv[2] argv .

Linux 128
1, ,
. , 128- ,
.
.


. . , , execve:
# !/bin/sh # !.
#!/bin/sh #.


execve, ENOEXEC.
, .
execve ,
.
, execve . , ,
, ,
, . ,
execve, , . !
.
bash , ,
- ENOEXEC, . 3.00.17
80 ( ), -ASCII .
ASCII, , .
,
Windows, .
execve .
, ENOENT (,
).
.
1

BINFMT_BUF_SIZE .

236

6.

6.3.2.
,
,
.
,
execve. ,
ELF1. ELF
Linux- a.out ( assembly output ), .

. , ,
Memory Management Unit (MMU),
, .
MIPS ECOFF,
Common Object File Format (COFF)
ELF.
,
. , ELF
, , 0x7f, 'ELF'.
ELF- ,
. , ( .o)
,
ELF. ELF-,
ELF-,
, ,
.
- , execve
.

6.3.3.
BINFMT_MISC ,
execve .

-, execve
. , Windows wine2 Java-
, jar-.
,
Windows-, wine Windows-,
.
Java- Java.
1
2

Executable and Linkable Format .


. www.winehq.org.

6.3. exec

237

BINFMT_MISC,
, Linux-
- .
execve, ,
, ,
, ,
Linux.
procfs,
:
$ mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc


:
$ ls -l /proc/sys/fs/binfmt_misc/
total 0
--w------- 1 root root 0 Feb 12 15:19 register
-rw-r--r-- 1 root root 0 Feb 11 20:06 status

register ,
status
.
.

register. ,
:
:name:type:offset:magic:mask:interpreter:flags

name ,
binfmt_misc . type ,
.
M, magic number ( ), E extention ().
(),
, .
(),
, .
DOS Windows.
offset, magic mask
. offset ,
, .
mask . ,
(
AND) , .
.
ASCII.
,
(escape-).

238

6.

Windows-
wine, :
$ echo ':Windows:M::MZ::/usr/bin/wine:' >

/proc/sys/fs/binfmt_misc/register

()
Z. ,
. , ,
, 1.

6.4.
wait
,
. ,
SIGCHLD. SIGCHLD ,
.
,
wait, :
pid_t
pid_t
pid_t
pid_t

wait(int *status);
waitpid(pid_t pid, int *status, int options);
wait3(int *status, int options, struct rusage *rusage);
wait4(pid_t pid, int *status, int options, struct rusage *rusage);

5,
.
,
,
.
Linux ( UNIX) ,
, wait.
wait , ,
, .
,
, init (pid 1).
, init
. -,
, init.
.6.1 wait .
,
Linux. int,
,
. wait.
1

, RPM- wine Fedora


, .

239

6.5. ,

waitpid wait4 ,

. wait wait3 pid
. , .
6.1. wait

rusage

wait

waitpid

wait3

wait4

,
,
,
wait, ,
, , . ,

,
waitpid, pid
.
,

, .
rusage struct,


wait3, ,
pid

waitpid, wait3 wait4 options,


:
WNOHANG
.
-1, ;
WUNTRACED , ,
,
(, ).

6.5. ,
5,
( ). ,

.

240

6.

,
.
, .
IA32 .6.2.

. 6.2. IA32

.6.2 ,
3 /1 ,
3 ,
1 , .

6.5. ,

241

,
.
,
, .
, - .
.6.2 .
; .
, . , ,
( 1 ),
.
.
.
, init,
, . ,
.6.2, .
,
exec. exec,
( )
, .

.
, .
,
. , , ,
,
, .
,
(
).
, , :
;
( );
;
.
, ,
,
. :
;
;
;
;
umask;
.

242

6.

6.5.1.
,
open.
,
. ,
,
.
.

: 0, 1 2. ,
, /.
.
, sshd, , ,

. sshd exec,
/
.
,
. open
flags, :
fd = open("foo",O_RDONLY);
fd = open("foo",O_WRONLY);
fd = open("foo",O_RDWR);

,
open . ,
,
. , open -1,
errno EACCESS ( ).
open , /
.
,
.
open.
,
. ,
,
. O_RDONLY,
O_WRONLY.
EBADF (
).
.
,
.
.

243

6.5. ,

,
. fork
exec1. ,
, .
. /proc
fd,
.

lsof
lsof2
. ,
,
. lsof
, . ,
/proc/pid/fd:
$ ls -l /proc/26231/fd
total 4
lrwx------ 1 root root
lrwx------ 1 root root
lrwx------ 1 root root
lrwx------ 1 root root
$ lsof -p 26231
COMMAND
PID USER
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root
bash
26231 root

64
64
64
64

FD
cwd
rtd
txt
mem
mem
mem
mem
mem
mem
mem
mem
0u
1u
2u
255u

Feb
Feb
Feb
Feb

17
17
17
17

19:40
19:40
19:40
19:40

TYPE DEVICE
DIR 253,0
DIR 253,0
REG 253,0
REG 253,0
REG 253,0
REG 253,0
REG 253,0
REG
0,0
REG 253,0
REG 253,0
REG 253,0
CHR 136,0
CHR 136,0
CHR 136,0
CHR 136,0

0 -> /dev/pts/0
1 -> /dev/pts/0
2 -> /dev/pts/0
255 -> /dev/pts/0
SIZE
4096
4096
686520
126648
1489572
16244
12924

NODE
542913
2
415365
608855
608856
608859
606897
0
48501472 801788
46552 606837
22294 862494
2
2
2
2

NAME
/root
/
/bin/bash
/lib/ld-2.3.5.so
/lib/libc-2.3.5.so
/lib/libdl...
/lib/libtermcap...
[heap] (stat: ...
/usr/lib/locale...
/lib/libnss_fil...
/usr/lib/gconv/...
/dev/pts/0
/dev/pts/0
/dev/pts/0
/dev/pts/0

, lsof
, . ,
FD ,
. mem txt,
1

,
shm_open, FD_CLOEXEC.
. ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof.

244

6.

, ()
mmap. ,
. ,
,
, - .
cwd rtd
. ,
(
).
lsof ,
. man,
lsof QUICKSTART.
.


, ,
, sysconf:
sysconf(_SC_OPEN_MAX);

sysconf
.
_SC_OPEN_MAX,
,
. , open
EMFILE ( ).
, , ,
.

6.5.2.
,
.
- , .
, , ,
, . LIFO (last
in, first out). ,
.
, ,
. /++
, ,
.
,
,
,
. ,
. /++

6.5. ,

245

, ,
, auto.
, auto
.
, static.
, static,
, ,
/ 1.
( )
, .
.
, ,
, .
,
. Linux
SIGSEGV.

,
(. ). ,
, , ,
-
.

( ) .
, ,
, .
Intel
Hyperthreading.

. ,
,
Hyperthreading
, .
,
, .

static , (
). ,
.

246

6.

() , .
, . , , ,
. ( )
.

setrlimit,
bash-
ulimit.
.

6.5.3.

,
.
. , ,
, .
,
, .
, ,
, ,
,
. ,
.
,
,
.
.
(),
. ,
.
root.

6.6.

setrlimit ,
.

247

6.6.

getrlimit. 5.
:
int setrlimit(int resource, const struct rlimit *rlim);
int getrlimit(int resource, struct rlimit *rlim);

, rlimit .
.
,
.
, . rlimit
:
struct rlimit {
rlim_t rlim_cur; /* */
rlim_t rlim_max; /* (
rlim_cur) */
};

,
; API-
.
fork , execve, :
pid_t pid = fork();
if ( pid == 0 ) {
struct rlimit limits = {};
getrlimit( RLIMIT_..., &limits);
.
setrlimit( RLIMIT_..., &limits );
exec( ... );
}

,
. ,
, .
.6.2.
6.2. , setrlimit getrlimit

RLIMIT_AS

( ),
.
. (
mmap)
ENOMEM. ,

SIGSEGV

248

6.
6.2 ()


RLIMIT_CORE

RLIMIT_CPU

RLIMIT_DATA

RLIMIT_FSIZE

RLIMIT_LOCKS
RLIMIT_MEMLOCK

RLIMIT_NOFILE
RLIMIT_NPROC

RLIMIT_RSS

RLIMIT_SIGPENDING
RLIMIT_STACK

.
,
, ,
, . .
SIGXCPU
, ,
SIGKILL
. brk sbrk, () ,
. errno
ENOMEM. glibc mmap,
brk ,
, .
, SIGXFSZ,
write truncate
EFBIG
,
; Linux 2.6
, ;
,

, . fork
errno EAGAIN
, , ;
Linux,
2.6.14
()
. . 7


,
. , , ,
SIGSEGV,
. ,
, , ,
open -1.
getrlimit
.
getrusage. , Linux

6.6.

249


- .
exit, getrusage
.
,
? .
-
.
( )
, .
, ,
.

,
. , ? 6.1,
,
.
6.1. insecure.c: ,
#include <stdio.h>
#include <stdlib.h>
// , .
unsigned char secret_message[] = {
0x8f, 0x9e, 0x8c, 0x8c, 0x88, 0x90, 0x8d, 0x9b, 0xc2, 0x8b, 0x90,
0x8f, 0xdf, 0x8c, 0x9a, 0x9c, 0x8d, 0x9a, 0x8b
};
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < sizeof(secret_message); i++) {
// , .
secret_message[i] ^= 0xff;
}
abort();
}

,
, ,
. ,
. ,
,
.
, .
,
. ,

.

250

6.

6.7. procfs
procfs ,
.
,
, procfs. Linux
: open, close, read write.
, procfs . ,
procfs,
.
procfs /proc.
.
,
.
, procfs,
ASCII.
/proc ,
, 123
/proc/123
. , /proc/self,
.
,
, , ,
. ,
.
.
/proc ,
,
.
. .6.3
, , .
6.3. , /proc/PID

auxv
cmdline

cwd
environ

,
gdb,
ASCII- NUL (0) ASCII,
argv,



,
ASCII- NUL ASCII. ,
envp,
(, PATH=xyz:abc)

ASCII


ASCII

251

6.7. procfs

exe
fd

maps

mem

mounts

oom_adj
oom_score

root

smaps

stat

statm

status
wchan

,

,
, . ,
,
, ()

, .
,

,

;
gdb
,
/etc/mtab.
oom_score
(. )
, OOM (Out-Of-Memory).
,
()

; /
,
chroot
()
,
. maps,

,

, scanf- ,
ps

,
stat
, stat,

, ( )

ASCII

ASCII

ASCII

ASCII

ASCII

Linux
procfs, /proc.
,
procfs.

252

6.

6.8.

procps1
procfs .
, , ps,
POSIX. ,
. ,
/proc.
/proc, .

6.8.1.
ps
ps procps ,
POSIX, .
, ,
,
(). , ps
,
.. , man
.
ps
,
. ,
,
, .
, ,
.
,
, ps
, , :
$ tty
/dev/pts/1
$ ps
PID TTY
21563 pts/1
21589 pts/1

?
TIME CMD
00:00:00 bash
00:00:00 ps

$ sleep 1000 &


[1] 21590
$ jobs l
[1]+ 21590 Running
1


.
1,
21590.

sleep 1000 &

. http://procps.sourceforge.net.

253

6.8.

$ ps
PID
21563
21590
21591

TTY
pts/1
pts/1
pts/1

TIME
00:00:00
00:00:00
00:00:00

CMD
bash
sleep
ps

TIME
00:00:00
00:00:00
00:00:00
00:00:00

CMD
bash
sleep
bash
ps


ps jobs.
.
,
.
ps ,
.

$ bash
$ jobs
$ ps
PID
21563
21590
21592
21609

TTY
pts/1
pts/1
pts/1
pts/1

, ,
ps. ,
,
. -1.
, :
$
F
0
0
0
0

ps -l
S
UID
S
500
S
500
R
500
R
500

PID
21563
21590
21623
21626

PPID C PRI
21562 0 75
21563 0 76
21563 96 85
21563 0 75

NI
0
0
0
0

ADDR SZ
- 1128
- 974
- 1082
- 1109

WCHAN
wait
-

TTY
pts/1
pts/1
pts/1
pts/1

TIME
00:00:00
00:00:00
00:00:26
00:00:00

CMD
bash
sleep
cruncher
ps

,
. .6.4
, ,
.

, , . ,
ps
.
6.4. , ps

F
S

UID

(. sched.h)
:
R ;
S ();
T ;
D ();
Z ,

254

6.
6.4 ()


PID
PPID
C





nice
Linux

,
( )

,
, /proc/stat ( 15 )

PRI
NI
ADDR
SZ
WCHAN
TTY
TIME
CMD

6.8.2.

procps ,
Linux, ps, .
, , o,
, ,
UNIX- .
,
. SGI , Sun , HewlettPackard . ps procps 3.2.6 236
(86
- ). man
.
.6.5
. ,
. ;
.
6.5. , ps


start, start_time,
lstart, bsdstart

etime

.
.
,
,
,

6.8.



time, cputime,
atime, bsdtime

size

m_size
pmem, %mem

majflt, maj_flt,
pagein
minflt, min_flt
sz, vsz, vsize

rss, rssize, rsz

lim
stackp

cpu

policy, class,
cls, sched

cp, %cpu, c, util


pri

255

, , ,
. bsdtime

;

, ; size
,
,
,

,

,
. sz
, vsz vsize
,

;
rss, setrlimit
;



SMP ,
;


:
TS (0) , ;
FF (1) , FIFO;
RR (2) ,


, . ,
(
0 39;
41 99;
, 40, Linux ).
,

256

6.
6.5 ()


- priority

opri, intpri

s, state, stat

tid, spid, lwp

wchan, wname

,

(
39 0,
1 99). , /proc/PID/stat/
priority ( 39 0
, 1 99
).
,

. state
D, R, S, T Z.
stat

.

,

6.8.3.
ps pgrep
, (
), , ,
. ,
.
:
$ ps -ef | grep myprogram

, pidof,
. procps,
SysVinit, .
/sbin
. SysVinit
, , ,
.

6.8.4.
pmap
pmap
, .

257

6.8.

/proc/PID/maps,
, :
$ cat &
[1] 3989
$ cat /proc/3989/maps
009db000-009f0000 r-xp
009f0000-009f1000 r-xp
009f1000-009f2000 rwxp
009f4000-00b15000 r-xp
00b15000-00b17000 r-xp
00b17000-00b19000 rwxp
00b19000-00b1b000 rwxp
08048000-0804c000 r-xp
0804c000-0804d000 rwxp
0804d000-0806e000 rwxp
b7d1e000-b7f1e000 r-xp
b7f1e000-b7f20000 rwxp
bfd1b000-bfd31000 rw-p
ffffe000-fffff000 ---p

00000000
00014000
00015000
00000000
00120000
00122000
00b19000
00000000
00003000
0804d000
00000000
b7f1e000
bfd1b000
00000000

fd:00
fd:00
fd:00
fd:00
fd:00
fd:00
00:00
fd:00
fd:00
00:00
fd:00
00:00
00:00
00:00

773010
773010
773010
773011
773011
773011
0
4702239
4702239
0
1232413
0
0
0

/lib/ld-2.3.3.so
/lib/ld-2.3.3.so
/lib/ld-2.3.3.so
/lib/tls/libc-2.3.3.so
/lib/tls/libc-2.3.3.so
/lib/tls/libc-2.3.3.so
/bin/cat
/bin/cat
[heap]
/usr/../locale-archive
[stack]
[vdso]

,

man proc(5).
. .
, s p
(shared) (private) .
, mmap.
.
() ,
/
, 1 , , /
.
, pmap,
:
$ pmap 3989
3989:
cat
009db000
84K
009f0000
4K
009f1000
4K
009f4000
1156K
00b15000
8K
00b17000
8K
00b19000
8K
08048000
16K
0804c000
4K
0804d000
132K
1

r-x-r-x-rwx-r-x-r-x-rwx-rwx-r-x-rwx-rwx--

/lib/ld-2.3.3.so
/lib/ld-2.3.3.so
/lib/ld-2.3.3.so
/lib/tls/libc-2.3.3.so
/lib/tls/libc-2.3.3.so
/lib/tls/libc-2.3.3.so
[ anon ]
/bin/cat
/bin/cat
[ anon ]

7.

258
b7d1e000
b7f1e000
bfd1b000
ffffe000
total

6.

2048K
8K
88K
4K
3572K

r-x-rwx-rw-------

/usr/lib/locale/locale-archive
[ anon ]
[ stack ]
[ anon ]

6.8.5.

skill pkill kill,
, ,
.
. .
skill
.
/proc/PID/stat. Linux /proc/PID/stat
15 1, ,
,
skill. ,
, 15, ,
, 15 ,
:
$ ./image_generator &
[1] ...
$ ./image_generator1 &
[2] ...
$ skill image_generator
[1] Terminated ./image_generator
[2] Terminated ./image_generator1


15

16

!

pkill ,
,
. ,
. pkill, skill,
/proc/cmdline, argv , .
,
, :
$ ./proc_abc &
[1] ...
$ ./abc_proc &
[2] ...
$ pkill abc
$ pkill ^abc

!

abc_proc.

TASK_COMM_LEN .

259

6.9.

$ pkill abc\$


proc_ abc. ($
.)

,
,
abc .
,
.
. ^abc ,
abc,
proc_abc. abc$
, abc.
abc_proc.

6.9.
,
. exec,
, Linux
. ,
, exec.

. ,
.

6.9.1. API-,

execve Linux,

. POSIX
,
. fork.
. exec(3).
fcntl .
FD_CLOEXEC.
fork .
.
kill .
setrlimit, getrlimit
.
sysconf , .
wait, waitpid, wait3, wait4
.

260

6.

6.9.2.,

pgrep ,

pmap .
ps ,

ulimit bash-

6.9.3.-
http://procps.sourceforge.net procps,


.
www.unix.org , Single UNIX Specification.
www.unix.org www.opengroup.org ,
, POSIX (IEEE Standard 1003.2)
( ).

7.1.
,
.
,
IPC (Interprocess communication),
.
, ,
, .
, ( )
.
, . ,
, , ,
.

,
.
6,
-
. .
,
.
IPC
.
,
. ,
IPC, ,
.

7.2. IPC

,
.

262

7.

, ,
.
. , , gcc,
, .
,
, , gcc v:
$ gcc -v -c hello.c
...
.../cc1 ... hello.c ... -o /tmp/ccPrPSPE.s
...
as -V -Qy -o hello.o /tmp/ccPrPSPE.s


.

.

,
, ,
. , ,
.

7.2.1.
: . , lockf
, .
lockf, .
: -
,
read write.
,
, .
GNU/Linux ,
mand,
, setgid.
- ,
.

7.2.2. ,

IPC
IPC
. , , ,
()
.
,
.

7.3.

263


IPC .

. ,
.
, ,
IPC- .

7.3.
,
/ -
, Linux.
,
.
.
, .
Linux UNIX-
.
API-
: System V POSIX. ,
. , ,
,
. ,
.
,
.
API-
. ,
,
,
. API- System V ,

. API- POSIX ,
, .

7.3.1.
API- POSIX
API- POSIX
.
.7.1. shm_open shm_unlink
open unlink, .
,
read write.

264

7.

shm_open shm_unlink Linux , ,


, .
API- POSIX ,
- API-
. ,
. 7.1
, ,
.
7.1. API- POSIX,

shm_open

. ,

open

shm_unlink


, shm_open.
unlink, , ,
. shm_unlink

mmap

() .
,
shm_open.
, . mmap
,

munmap

() ,
mmap. , , , mmap, ,

,

msync

,
() mmap, (
),

7.1. posix-shm.c: API- POSIX


1
2
3
4
5
6
7
8
9

/* posix-shm.c : gcc -o posix posix.c -lrt */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
// POSIX
#include <sys/file.h>
// open(2) "".
#include <sys/mman.h>
// mmap(2) "".
#include <sys/wait.h>

7.3.

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

void error_out(const char *msg)


{
perror(msg);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int r;
// shm_open '/'
// , Linux
// .
const char *memname = "/mymem";
//
const size_t region_size = sysconf(_SC_PAGE_SIZE);
// .
// ,
// , open(2).
int fd = shm_open(memname, O_CREAT | O_TRUNC | O_RDWR, 0666);
if (fd == -1)
error_out("shm_open");
// .
// ftruncate, write(2).
r = ftruncate(fd, region_size);
if (r != 0)
error_out("ftruncate");
// .
void *ptr =
mmap(0, region_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
0);
if (ptr == MAP_FAILED)
error_out("mmap");
// fd mmmap .
close(fd);
pid_t pid = fork();
if (pid == 0) {
// .
u_long *d = (u_long *) ptr;
*d = 0xdeadbeef;
exit(0);
}
else {
// .

265

266
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 }

7.

int status;
waitpid(pid, &status, 0);

// .
printf(" %#lx\n", *(u_long *) ptr);

// , .
r = munmap(ptr, region_size);
if (r != 0)
error_out("munmap");
// .
r = shm_unlink(memname);
if (r != 0)
error_out("shm_unlink");
return 0;

shm_open , .
, , .
.
write,

ftruncate. ,
,
, .
7.1
fork. ,
.
. ,
, ,

, :
$ gcc -o posix-shm posix-shm.c -lrt
$ ./posix-shm
$ child wrote 0xdeadbeef

,
,
. ,
, , ,
7.1. ,
, O_CREAT
shm_open, ftruncate.
,
.
, wait 7.1

267

7.3.

. . ,
, , :
. waitpid
, ,
. ,
, .
,
API- POSIX, Linux
Linux
, /dev/shm.
shm_open .
,
tmpfs. tmpfs
/dev/shm, shm_open . , , ,
,
. , tmpfs ,
, .
shm_open /dev/shm,
. , , .
, , , .

7.3.2.
API- System V
API- System V X Window,
.
API- POSIX. API- System V .7.2.
API-
, .
7.2. API- System V,

shmget
shmat
shmdt
shmctl

( shm_open)
( mmap)
() , shmat ( munmap)
, ()
, shmget ( shm_unlink)

268

7.

7.2 7.1,
API- System V. ,
, shmget
, ftruncate .
7.2. sysv-shm.c: API- System V
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

#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<unistd.h>
<sys/ipc.h>
<sys/shm.h>
<sys/wait.h>

void error_out(const char *msg)


{
perror(msg);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
// , ,
// shm_open()
key_t mykey = 12345678;
//
const size_t region_size = sysconf(_SC_PAGE_SIZE);
// .
int smid = shmget(mykey, region_size, IPC_CREAT | 0666);
if (smid == -1)
error_out("shmget");
// .
void *ptr;
ptr = shmat(smid, NULL, 0);
if (ptr == (void *) -1)
error_out("shmat");
pid_t pid = fork();
if (pid == 0) {
// .
u_long *d = (u_long *) ptr;
*d = 0xdeadbeef;
exit(0);
}

269

7.4.

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 }

else {
// .
int status;
waitpid(pid, &status, 0);
// .
printf(" %#lx\n", *(u_long *) ptr);
}
// , .
int r = shmdt(ptr);
if (r == -1)
error_out("shmdt");
// .
r = shmctl(smid, IPC_RMID, NULL);
if (r == -1)
error_out("shmdt");
return 0;

, shmget, ,
shm_open. shmid, shmget,
, shm_open.
, .
7.2 7.1,
:
$ gcc -o sysv-shm sysv-shm.c
$ ./sysv-shm
child wrote 0xdeadbeef

, API- POSIX,
, API- System V,
- . ipcs
System V.
.

7.4.
1
IPC,
. ,

1

Linux (Linux Programming by Example: The


Fundamentals).

270

7.

. ,
- ,
,
. ,
. , ,
malloc printf, .
,
.
,
.
. POSIX ,
. man
signal(2).
, ,
, , .
, , , ,
.
. ,
() .
, .

7.4.1.
UNIX, ,
API- ,
. ,
, , .
, ,
, , .
.

kill,
:
int kill( pid_t pid, int signal );

, kill
. , 0, ,
,
:
int r = kill(pid,0);

0
.

if ( r == 0 )
/* process exists! */
(/* ! */)

271

7.4.

else if ( errno == ESRCH )

- ,

errno.
/* */

kill, POSIX-, -1
, errno .
0 ,
. 0,
, ,
. kill
-1, errno ESRCH, ,
.

7.4.2.
API-: POSIX
System V. API- POSIX BSD
,
. API- System V ANSI
. , API-
POSIX.
API- POSIX System V
:
;
;
.
API- POSIX
.
. , SIGKILL SIGSTOP
, ,
.
.7.3 ,
, .
7.3.


( )

System V
old = signal(signo,handler)

POSIX
sigaction(signo,new,old)

old = signal(signo,SIG_IGN)

sigaction(signo,new,old)

old = signal(signo,SIG_DFL)

sigaction(signo,new,old)

272

7.

7.4.3.

,
. , ,
.
, .
, , .
POSIX sigset_t.
sigset_t
, :
int
int
int
int
int

sigemptyset(sigset_t *set);
sigfillset(sigset_t *set);
sigaddset(sigset_t *set, int signum);
sigdelset(sigset_t *set, int signum);
sigismember(sigset_t *set, int signum);





1,
signum;

0

sigaddset, sigdelset sigismember


. , .
,
. ,
sigset_t .
, sigprocmask
.
,
. sigprocmask
:
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

sigset_t.
, , ,

. how ,
.
:
SIG_BLOCK ,
. ,
,
;
SIG_UNBLOCK , , ,
.
,
;

273

7.4.

SIG_SET ,

. ,
.
, .
, sigprocmask
. ,
, .
7.3.
7.3. sigprocmask.c: sigprocmask
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<signal.h>
<unistd.h>

volatile int done = 0;


//
void handler(int sig)
{
// Ref signal(2) write() , printf() .
const char *str = "handled...\n";
write(1, str, strlen(str));
done = 1;
}

void child(void)
{
//
// SIGCHLD
21
printf(" \n");
22
exit(0);
23 }
24
25 int main(int argc, char *argv[])
26 {
27
// SIGCHLD,
// .
28
signal(SIGCHLD, handler);
29
sigset_t newset, oldset;
30
31
//
32
sigfillset(&newset);
33
34
// ,
35
//
36
sigprocmask(SIG_BLOCK, &newset, &oldset);
37

274
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 }

7.

//
pid_t pid = fork();
if (pid == 0)
child();
printf(" ""\n");
// "" .
int r = sleep(3);
// r == 0 , "" .
printf("! r=%d\n", r);
// ,
// .
sigprocmask(SIG_SETMASK, &oldset, NULL);
// .
while (!done) {
};
printf("\n");
exit(0);

7.3 ,
. SIGCHLD.
, ,
SIGCHLD SIGCHLD 3.
,
SIGCHLD.
, , :
$ ./sigprocmask
child exiting
parent sleeping
woke up! r=0
handled...


SIGCHLD.
SIGCHLD ,

"".
0 ,
"" 3 ;
- .

.

exiting

API- POSIX
.
:
int sigpending(sigset_t *set);
int sigsuspend(const sigset_t *mask);

7.4.

275

sigpending ,
, . 7.3
, , . ,
, , sigpending
,
. .
,
sigpending , .
, .

.

7.4.4.
,
. , , ,
?
, ,
, .
,
. ,
.
POSIX ,
,
. ,
, ,
, .

. SIGRTMIN SIGRTMAX.
,
. 7.4.
7.4. rt-sig.c:
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <signal.h>
5 #include <unistd.h>
6
7 volatile int done = 0;
8
9 //
10 void handler(int sig)
11 {
12
// Ref signal(2) write() , printf() .
13
const char *str = "handled...\n";
14
write(1, str, strlen(str));
15
done = 1;

276
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

7.

}
void child(void)
{
int i;
for (i = 0; i < 3; i++) {
// "" .
kill(getppid(), SIGRTMIN);
printf(" BANG!\n");
}
exit(0);
}
int main(int argc, char *argv[])
{
// SIGRTMIN, .
signal(SIGRTMIN, handler);
sigset_t newset, oldset;
// ,
//
sigfillset(&newset);
sigprocmask(SIG_BLOCK, &newset, &oldset);
//
pid_t pid = fork();
if (pid == 0)
child();
printf(" ""\n");
// "" .
int r = sleep(3);
// r == 0 , "" .
printf("! r=%d\n", r);
// ,
// .
sigprocmask(SIG_SETMASK, &oldset, NULL);
// .
while (!done) {
};
printf("\n");
exit(0);
}

7.4 7.3, ,
SIGRTMIN, SIGCHLD,

277

7.4.

, .
-
.
. , :
$ ./rt-sig
child BANG!
child BANG!
child BANG!
parent sleeping
woke up! r=0
handled...
handled...
handled...
exiting

,
. ,

kill.

7.4.5.
sigqueue sigaction
, kill,
sigqueue. ,
kill, ,
:
int sigqueue(pid_t pid, int sig, const union sigval value);

pid sig kill, value


. , ,
, , ,

RTSIGMIN RTSIGMAX,
.
,
sigqueue, ,
sigaction, ,
signal. sigaction
. 7.4,
signal sigaction :
struct sigaction sa = {
.sa_handler = handler,
.sa_flags = SA_RESTART
};


99

""

278

7.

sigemptyset(&sa.sa_mask);
sigaction(SIGRTMIN,&sa,NULL);



NULL

sigaction ,
sigaction. sa_mask ,
.
.
,
. sigaction
sigaction. ,
. sa_handler
System V, .
sa_sigaction ,
:
void handler(int sig, struct siginfo *si, void *ptr)

sigqueue,
. ,
, SA_SIGINFO sa_flags,
:
struct sigaction sa = {
.sa_sigaction = handler,
.sa_flags = SA_RESTART|SA_SIGINFO
};
sigemptyset(&sa.sa_mask);


99

SA_SIGINFO,
.
, , ,
SIGSEGV.
kill
sigqueue :
union sigval sv = {
.sival_int = 42
};
sigqueue(getppid(), SIGRTMIN, sv);


.
,

.

, , siginfo
, .
, Linux, POSIX-:
int
int

si_signo
si_code

SIGINT ..
(. )

279

7.4.

int

si_errno

pid_t
uid_t

si_pid
si_uid

void
int
long
union sigval

*si_addr
si_status
si_band
si_value

errno ,



,
(. )

band SIGPOLL

,
siginfo .
si_signo, si_errno si_code
POSIX. ,
si_pid si_uid
, .
si_code , .7.4,
. ,
- raise,
, si_code SI_USER. ,
sigqueue , , si_code
SI_QUEUE.
, , si_value,
,
sigqueue . , si_value
sigval, .
,
IPC
.
si_errno,
,
. si_band SIGPOLL,
IPC-.
7.4. , si_code
si_code

SI_USER

kill() raise()

SI_KERNEL

SI_QUEUE

sigqueue()

SI_TIMER

POSIX

SI_MESGQ

POSIX

SI_ASYNCIO

/ (AIO Asynchronous I/O)

SI_SIGIO

SIGIO (
Linux)

SI_TKILL

tkill() tgkill(); Linux

280

7.

7.5. ()
() .
: ,
, ,
, .
pipe,
. pipe
:
int pipe(int filedes[2]);

,
.
, .

.
,
, .
Linux UNIX ,
,
mkfifo mknod:
int mkfifo(const char *pathname, mode_t mode);
mkfifo mknod .
,
open, read write.
, , ;
.
, ,
, ,
.
.

7.6.

; .
, ,
.
, API-
,
.
1,
.
1

info- glibc: info libc sockets.

281

7.6.

7.6.1.

socket, ,
. socketpair,

. :
int socket(int domain, int type, int protocol);
int socketpair(int domain, int type, int protocol, int fd[2]);

, (domain),
(type) (protocol) ,
. socketpair, pipe,
.


domain, , ,
, , ,
- . ,
POSIX, GNU ,
,
. POSIX-,
, PF ( protocol family ). .7.5 .
, , PF_UNIX PF_INET. PF_UNIX
,
, PF_INET IP-1.
7.5. ,

PF_UNSPEC
PF_UNIX, PF_LOCAL
PF_INET
PF_INET6
PF_NETLINK
PF_PACKET

/
.
.
,
- IPv4
- IPv6

,


type ,
, ,
1

, IPv4-. IPv4 IP-,


32 , IPv6
128 .

282

7.

. .7.6
. ,
SOCK_STREAM. , ,
, ,
; .
protocol.
7.6.

SOCK_STREAM

.
, ,
. (out of band)

,
,
SOCK_STREAM, ,


SOCK_STREAM, ,

SOCK_DGRAM

SOCK_SEQPACKET

SOCK_RAW
SOCK_RDM


,
. POSIX ,
(.7.7).
<netinet/in.h>.
, .
/etc/protocols.
, /etc/protocols,
getprotoent(3)
, .
7.7.

IPPROTO_IP


ip

IPPROTO_ICMP

icmp

(Internet Protocol), .

,

(Internet
Control Message Protocol), ping

283

7.6.

IPPROTO_TCP


tcp

IPPROTO_UDP

udp

IPPROTO_IPV6

ipv6

IPPROTO_RAW

raw

(Transmission
Control Protocol),
SOCK_STREAM
(User
Datagram Protocol), ,
IPC,
,

SOCK_DGRAM
IP,
, ,
IPv6-
;

ip 0,
(PF_LOCAL).
0, .

7.6.2.
socketpair

socketpair, 7.5.
7.5. socketpair.c:
socketpair
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include
#include
#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<errno.h>
<unistd.h>
<sys/types.h>
<sys/socket.h>
<sys/wait.h>
<netdb.h>

int main(int argc, char *argv[])


{
int fd[2];
//
//
//
//

ref. socketpair(2)
( ) = PF_LOCAL
( PF_UNIX))
= SOCK_STREAM (. )

284
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 }

7.

// = ( )
int r = socketpair(PF_LOCAL, SOCK_STREAM, 0, fd);
if (r == -1) {
perror("socketpair");
}
pid_t pid = fork();
if (pid == 0) {
// .
char buf[32];
int n = read(fd[1], buf, sizeof(buf));
if (n == -1) {
perror("read");
}
printf(" %d '%s'\n", n, buf);
}
else {
// .
char msg[] = "Hello World";
int n = write(fd[0], msg, sizeof(msg));
if (n == -1) {
perror("write");
}
// .
int status;
wait(&status);
}
exit(0);
return 0;

7.5
socketpair. ,
(), , , .
, pipe : ,
.
,
.
,
.

7.6.3. /,

/,
socket.

285

7.6.

socketpair, , socket
. socketpair
, ,
,
.
socket,
. .7.1
. , socketpair
API-,
.

. 7.1. API- /


man Linux, ,
. ,
.7.1: select.

286

7.

1.
, ,
. select
:
int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout);

fd_set select,
. fd_set
, :
FD_SET(int fd, fd_set *set)
FD_CLR(int fd, fd_set *set)
FD_ISSET(int fd, fd_set *set)
FD_ZERO(fd_set *set)

fd fd_set.
fd fd_set.

fd fd_set.
fd fd_set.

, . 7.6
, . ,
, socket, listen,
. . accept

. ,
, .
7.6. server_un.c: ,
1
2
3
4
5
6
7
8
9
10

#include
#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<signal.h>
<stdlib.h>
<unistd.h>
<sys/types.h>
<sys/socket.h>
<sys/un.h>

// perror exit,
// -1
11 #define ASSERTNOERR(x,msg) do {\
12
if ((x) == -1) { perror(msg); exit(1); }} while(0)
13
14 // ,
// ()
15 #define SOCKNAME "localsock"
16
17 int main(int argc, char *argv[])
18 {
19
// ref. socket(2)
1

select poll.

287

7.6.

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

//
// ( ) = PF_LOCAL
// ( PF_UNIX)
// = SOCK_STREAM (. )
// = ( )
int s = socket(PF_LOCAL, SOCK_STREAM, 0);
ASSERTNOERR(s, "socket");
// ,
// .
struct sockaddr_un sa = {
.sun_family = AF_LOCAL,
.sun_path = SOCKNAME
};
// .
// , EADDRINUSE,
// unlink!
int r = bind(s, (struct sockaddr *) &sa, sizeof(sa));
ASSERTNOERR(r, "bind");
// .
// backlog-.
// . .
r = listen(s, 0);
ASSERTNOERR(r, "listen");
// sockaddr_un UNIX.
// .
struct sockaddr_un asa;
size_t addrlen = sizeof(asa);
// .
//
// , .
int fd = accept(s, (struct sockaddr *) &asa, &addrlen);
ASSERTNOERR(fd, "accept");
while (1) {
char buf[32];
fd_set fds;
// select .
FD_ZERO(&fds);
FD_SET(fd, &fds);
int r = select(fd + 1, &fds, NULL, NULL, NULL);

288

7.

67
68
69
70
71
72
73

ASSERTNOERR(r, "select");
//
int n = read(fd, buf, sizeof(buf));
printf(" %d \n", n);

74
75
76
77
78
79
80
81
82 }

// ,
// .
if (n == 0)
break;
}
// unlink.
unlink(SOCKNAME);
return 0;

7.7 ,
7.6. ,
socket, connect,
, .
7.7. client_un.c: ,
1
2
3
4
5
6
7
8
9
10
11
12
13

#include
#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<signal.h>
<unistd.h>
<sys/types.h>
<sys/socket.h>
<sys/un.h>

#define ASSERTNOERR(x,msg) do {\
if ((x) == -1) { perror(msg); exit(1); }} while(0)

// ,
// .
14 #define SOCKNAME "localsock"
15
16 int main(int argc, char *argv[])
17 {
18
// ust server_un.c
19
int s = socket(PF_LOCAL, SOCK_STREAM, 0);
20
21
ASSERTNOERR(s, "socket");
22
23
// AF_ macros
24
struct sockaddr_un sa = {
25
.sun_family = AF_LOCAL,
26
.sun_path = SOCKNAME

289

7.6.

27
28
29
30
31
32
33
34
35
36
37
38
39
40 }

};
// -1.
int r = connect(s, (struct sockaddr *) &sa, sizeof(sa));
ASSERTNOERR(r, "connect");
const char data[] = "Hello World";
r = write(s, data, sizeof(data));
printf(" %d \n", r);
return 0;

localsock.
UNIX sockaddr_un.
, .
ls,
s , :
$ ./server_un &
$ ls -l localsock
srwxrwxr-x 1 john john 0 Apr 29 22:28 localsock

, , ,
, 7.7. ( )
:
$ ./client_un
client wrote 12 bytes
server read 12 bytes
server read 0 bytes
[1]+ Done

./server_un

7.6.4. /,


, 7.7
7.6. , . ,
.
.
sys/un.h netinet/in.h.
, socket, PF_LOCAL
( PF_INET).
sockaddr_un sockaddr_in.
( AF_INET)
.

290

7.

unlink .

.
,
. ,
.

INADDR_ANY.
.
5000 :
struct sockaddr_in sa = {
.sin_family = AF_INET,
.sin_port=htons(5000),
.sin_addr = {INADDR_ANY} };

htons host to network short (


);
1.
INADDR_ANY sin_addr
in_addr,
, :
struct in_addr ifaddr;
int r = inet_aton("192.168.163.128",&ifaddr);

ifaddr sin_addr
struct sockaddr. ,
, INADDR_ANY .

7.7.

. ,
API-: System V POSIX.
, .
,
.
,
, .
, ,
, .
API- -. API-
System V '
, API- POSIX
.
1

.inet(3).

7.7.

291

7.7.1. System V

msgget,
,
API- System V .
,
.
msgget :
int msgget(key_t key, int msgflg);

key ,
. , msgget,
,
.
IPC_PRIVATE.
.
, msgget, ,
. ,
,
.
msgflag, msgget, ,
open. 9
, open.
IPC_CREAT IPC_EXCL. IPC_CREAT
; ,
. ,
IPC_CREAT IPC_EXCL, msgget ,
. ,
IPC_CREAT IPC_EXCL
open(2).
close,
, ,
. ,

.
msgctl,
, , , ,
:
int msgctl(int msqid, int cmd, struct msqid_ds *buf);

cmd, ,
IPC_RMID, buf NULL.
msgctl(2).

:

292

7.

int msgsnd(int qid, void *msg, size_t msgsz, int msgflg);


ssize_t msgrcv(int qid, void *msg, size_t msgsz, long typ, int msgflg);

System V
,
. , , . ,

,
.
7.8.
7.8. sysv-msgq-example.c: API- System V
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

#include
#include
#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<unistd.h>
<sys/wait.h>
<sys/stat.h>
<sys/file.h>
<sys/msg.h>
<sys/ipc.h>

// .
struct message {
long int mtype;
char mtext[128];
};
// .
int send_msg(int qid, int mtype, const char text[])
{
struct message msg = {
.mtype = mtype
};
strncpy(msg.mtext, text, sizeof(msg.mtext));
int r = msgsnd(qid, &msg, sizeof(msg), 0);
if (r == -1) {
perror("msgsnd");
}
return r;
}
// .
int recv_msg(int qid, int mtype, struct message *msg)
{
int r = msgrcv(qid, msg, sizeof(struct message), mtype, 0);
switch (r) {
case sizeof(struct message):

7.7.

38
/* okay */
39
break;
40
case -1:
41
perror("msgrcv");
42
break;
43
default:
44
printf(" %d \n", r);
45
}
46
return r;
47 }
48
49 void producer(int mqid)
50 {
51
// ,
// .
52
send_msg(mqid, 1, "type 1 first");
53
send_msg(mqid, 2, "type 2 second");
54
send_msg(mqid, 1, "type 1 third");
55 }
56
57 void consumer(int qid)
58 {
59
struct message msg;
60
int r;
61
int i;
62
for (i = 0; i < 3; i++) {
63
// -2 , 2 .
64
r = msgrcv(qid, &msg, sizeof(struct message), -2, 0);
65
printf("'%s'\n", msg.mtext);
66
}
67 }
68
69 int main(int argc, char *argv[])
70 {
71
// () .
72
int mqid;
73
mqid = msgget(IPC_PRIVATE, S_IREAD | S_IWRITE);
74
if (mqid == -1) {
75
perror("msgget");
76
exit(1);
77
}
78
79
pid_t pid = fork();
80
if (pid == 0) {
81
consumer(mqid);
82
exit(0);
83
}
84
else {
85
int status;
86
producer(mqid);
87
wait(&status);

293

294
88
89
90
91
92
93
94 }

7.

}
// .
int r = msgctl(mqid, IPC_RMID, 0);
if (r)
perror("msgctl");
return 0;

:
.
msgrcv, . -2
, ,
2 .
:
$ cc -o sysv-msgq-example sysv-msgq-example.c
$ ./sysv-msgq-example
'type 1 first'
'type 1 third'
'type 2 second'

, 1 .
,
. ,
, .
, (FIFO),
type msgrcv.
type msgrcv ,
, :
;

.
Linux MSG_EXCEPT,
- .
, MSG_EXCEPT flags
msgrcv, ,
N msgrcv ,
N.

7.7.2. POSIX
API- POSIX API- System V,
,
POSIX. ,
, ,
. , ,
() , . Linux
. API-

295

7.7.

POSIX ,
.

, ,
POSIX
POSIX creat
. mq_open
O_CREAT. mq_open :
mqd_t mq_open(const char *name, int oflag, ...);

open. POSIX
( ),
. Linux .
, oflag ,
open, O_CREAT, O_READ, O_WRITE O_RDWR.
O_CREAT, mq_open
.
, , open,
/ . ,
(, S_IREAD),
mq_open.
mq_open, O_CREAT,
mq_attr, :
struct
{
long
long
long
long
\
long
};

mq_attr
int
int
int
int

mq_flags;
mq_maxmsg;
mq_msgsize;
mq_curmsgs;

, O_NONBLOCK
,

int __pad[4];

mq_attr ,

.
, POSIX mq_getattr
.
mq_setattr, .
:
int mq_setattr(mqd_t mqdes, const struct mq_attr *iattr, struct mq_attr *oattr);
int mq_getattr(mqd_t mqdes, struct mq_attr *oattr);

mq_maxmsg mq_msgsize mq_open


. ,
, .

296

7.

, mq_setattr. mq_curmsgs
mq_open mq_setattr ;
, mq_getattr, .
, Linux-
mq_open, , close
.
POSIX mq_close:
int mq_close(mqd_t mqdes);

, Linux- mq_close
close.
mq_unlink,
unlink:
int mq_unlink(const char *name);

, , ,
. ,
, .

POSIX
, ,
System V-. mq_send mq_
receive, :
int mq_send(mqd_t mqdes, char *ptr, size_t len, unsigned prio);
ssize_t mq_receive(mqd_t mqdes, char *ptr, size_t len, unsigned *prio);

System V, , ,
System V, POSIX .
mq_send write
, . mq_send ,
,
. mq_receive ,
,
, .

, .

. ,
O_NONBLOCK.
mq_setattr.

7.7.3.
POSIX System V
System V POSIX .
System V

7.8.

297

,
. POSIX
,
,
, msg_receive
, ,
.
, System V

, POSIX- ,
,
, ,
. POSIX
.
, ,
( FIFO).

7.8.
, ,
;
.

,
.
.
API-,
: System V POSIX.
. -
. ,
.
, 1 0.

,
API- POSIX. 7.9 ,
.
,
.
7.9. hello-unsync.c: ,
1
2
3
4
5

#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<unistd.h>
<sys/file.h>

298
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

7.

#include
#include
#include
#include

<sys/times.h>
<sys/stat.h>
<semaphore.h>
<assert.h>

// busywait .
void busywait(void)
{
clock_t t1 = times(NULL);
while (times(NULL) t1 < 2);
}
/*
** . 1-
** 2- . ,
** ( "").
*/
int main(int argc, char *argv[])
{
const char *message = "Hello World\n";
int n = strlen(message) / 2;
pid_t pid = fork();
int i0 = (pid == 0) ? 0 : n;
int i;
for (i = 0; i < n; i++) {
write(1, message + i0 + i, 1);
busywait();
}
}

7.9
. ,
busywait , .

:
$ cc -o hello-unsync hello-unsync.c
$ $ ./hello-unsync
HWelolo rld
,

' 7.9 .7.2.


, write
. ,
.
-,
. 7.10
, API-
POSIX.

7.8.

299

. 7.2. 7.9:

7.10. hello-sync.c:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include
#include
#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<unistd.h>
<sys/file.h>
<sys/times.h>
<sys/stat.h>
<semaphore.h>
<assert.h>

// busywait .
void busywait(void)
{
clock_t t1 = times(NULL);
while (times(NULL) t1 < 2);
}
/*
** . 1-
** 2- .
** .

300
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

7.

*/
int main(int argc, char *argv[])
{
const char *message = "Hello World\n";
int n = strlen(message) / 2;
// .
sem_t *sem = sem_open("/thesem", O_CREAT, S_IRUSR | S_IWUSR);
assert(sem != NULL);
// , .
int r = sem_init(sem, 1, 0);
assert(r == 0);
pid_t pid = fork();
int i0 = (pid == 0) ? 0 : n;
int i;
// .
if (pid)
sem_wait(sem);
for (i = 0; i < n; i++) {
write(1, message + i0 + i, 1);
busywait();
}
// .
if (pid == 0)
sem_post(sem);
}

.7.3 ' .
API- POSIX ,
,
. , ,
, ( ),
.

. , ,
, sem_wait,
.

POSIX- sem_post.
.
:
$ cc -o hello-sync hello-sync.c
$ ./hello-sync
Hello World

7.8.

301

. 7.3. 7.10:
-

7.8.1.
API- POSIX
7.10
. POSIX
, .
() ,
7.11.
7.11. posix_sem.c: , POSIX
1 #include <stdio.h>
2 #include <string.h>

302
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

7.

#include
#include
#include
#include
#include
#include
#include

<stdlib.h>
<assert.h>
<errno.h>
<unistd.h>
<sys/file.h>
<sys/stat.h>
<semaphore.h>

int main(int argc, char *argv[])


{
const char *semname = "/mysem";
// ,
// .
// O_EXCL,
// ,
// , errno
// EEXIST.
sem_t *sem = sem_open(semname,
O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR,
0);
if (sem != SEM_FAILED) {
printf(" \n");
}
else if (errno == EEXIST) {
// ,
// O_EXCL
printf(" \n");
sem = sem_open(semname, 0);
}
assert(sem != SEM_FAILED);
int op = 0;
// : ,
if (argc > 1)
op = atoi(argv[1]);
if (op > 0) {
printf(" \n");
sem_post(sem);
}
else if (op < 0) {
printf(" \n");
sem_wait(sem);
}
else {
printf(" \n");

303

7.8.

49
50
51
52
53
54
55 }

}
int val;
sem_getvalue(sem, &val);
printf(" %d\n", val);
return 0;


sem_open. , sem_open, ,
open(2).

.
:
sem_t *sem_open( const char *name, int oflag, ...);

O_EXCL ,
sem_open , . ,
.
.

.

, , :
$ cc -o posix_sem posix_sem.c -lrt
$ ./posix_sem 1
created new semaphore
incrementing semaphore
semaphore value is 1

$ ./posix_sem 1
semaphore exists
incrementing semaphore
semaphore value is 2

$ ./posix_sem -1
semaphore exists
decrementing semaphore
semaphore value is 1

$ ./posix_sem 0
semaphore exists
not modifying semaphore
semaphore value is 1

$ ./posix_sem -1
semaphore exists
decrementing semaphore
semaphore value is 0


sem_post.

= 2 x sem_post 1 x sem_wait

304

7.

$ ./posix_sem -1
semaphore exists
decrementing semaphore

7.11 .
sem_close, , ,
, .
sem_unlink,
, .
API- POSIX
, :
Linux .
sem_open,
. sem_init,
:
int sem_init( sem_t *sem, int pshared, int value );

7.8.2.
API- System V
API- System V API-
,
, ,
, . 7.12
, 7.11.
7.12. sysv_sem.c: , System V
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include
#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<assert.h>
<errno.h>
<unistd.h>
<sys/stat.h>
<sys/sem.h>

int main(int argc, char *argv[])


{
// ftok
key_t semkey = ftok("/tmp", 'a');
// "" , 1.
// IPC_EXCL,
// ,
// , errno
// EEXIST.
int semid =
semget(semkey, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);

305

7.8.

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 }

if (semid != -1) {
printf(" \n");
}
else if (errno == EEXIST) {
// ,
// IPC_EXCL
printf(" \n");
semid = semget(semkey, 1, 0);
}
assert(semid != -1);
//
//
//
if

: " "
System V

(argc == 2) {
int op = atoi(argv[1]);
// ,
// ,
// .
struct sembuf sb = {
.sem_num = 0, // .
.sem_op = op, // ,
.sem_flg = 0 // (, IPC_NOWAIT)
};
// !
int r = semop(semid, &sb, 1);
assert(r != -1);
printf(" %d \n", op);

}
else {
printf(" \n");
}
printf("semid %d value %d\n", semid, semctl(semid, 0, GETVAL));
return 0;

,
API- POSIX,
. -, API- System V
semop wait post. -, API-
System V (wait
for zero), ,
,
.

306

7.

7.9.
.
API-
.
API-. ,
,
, API-.

7.9.1. API-,

API-,
.

flock .
ftok , System V IPC.
lockf .
select ,


shm_open, shm_unlink, mmap

POSIX.
shmget, shmat, shmdt, shmctl

System V.

kill, sigqueue .
sigaction, signal .
sigpending, sigsuspend .
sigprocmask, sigemtpyset, sigfillset, sigaddset, sigdelset, sigismember

()
mkfifo .
pipe .

bind, listen, accept, close ,

7.9.

307

connect

.
socket .


mq_open, mq_close, mq_unlink, mq_send, mq_receive, mq_setattr, mq_getattr

POSIX.
msgget, msgsend, msgrcv, msgctl
System V.

sem_open , sem_close , sem_post , sem_wait

POSIX.
semget, semop System V.

7.9.2.
GallmeisterB. POSIX 4 Programmers Guide. Sebastopol, Calif.: OReilly Media,

Inc., 1995.
RobbinsA. Linux Programming by Example, The Fundamentals. Englewood
Cliffs, N. J.: Prentice Hall, 2004.
StevensW. R. et al. UNIX Network Programming. Boston, Mass.: Addison-Wesley,
2004.

7.9.3.-
www.opengroup.org , ,

POSIX (IEEE Standard 1003.2) ( ).


www.unix.org , Single UNIX Specification.


IPC

8.1.
,
IPC.

- .

8.2.

, ,
. , ,
.
, ,
,
, errno
EMFILE.
, ,
. ?
procfs
. /proc/PID/fd
.
. ,
.

8.2.1. lsof
lsof.
,

309

8.2.

, .
,
. p ,
- :
$ lsof -p 16894
COMMAND
PID
cat
16894
cat
16894
cat
16894
cat
16894
cat
16894
cat
16894
cat
16894
cat
16894
cat
16894
cat
16894
cat
16894

USER
john
john
john
john
john
john
john
john
john
john
john

FD
cwd
rtd
txt
mem
mem
mem
mem
0r
1u
2w
3r

TYPE
DIR
DIR
REG
REG
REG
REG
REG
FIFO
CHR
CHR
REG

DEVICE
253,0
253,0
253,0
253,0
253,0
0,0
253,0
0,5
136,2
1,3
253,0

SIZE
4096
4096
21104
126648
1489572
48501472

167

NODE
575355
2
159711
608855
608856
0
801788
176626
4
1510
575649

NAME
/home/john
/
/bin/cat
/lib/ld-2.3.5.so
/lib/libc-2.3.5.so
[heap]
.../locale-archive
pipe
/dev/pts/2
/dev/null
/home/john/.bashrc

, ,
() . FD (
) ,
.
mmap, FD
, .
, .8.1
, FD.
8.1. , FD lsof

cwd
ltx
mem
mmap
pd
rtd
txt
{digit}r
{digit}w
{digit}u


(
)




( )
,

lsof ,
, .
lsof .
. lsof(8).

310

8. IPC

8.2.2. fuser

fuser. , ,
. fuser
:
$ fuser some-huge-file.txt
some-huge-file.txt: 17005

,
. fuser
k:
]$ fuser -k -KILL some-huge-file.txt
some-huge-file.txt: 17005
[1]+ Killed
cat some-huge-file.txt

8.2.3. ls
-l ls
. , , ,
, ,
.
, , :
$ ls -l /dev/log /dev/initctl /dev/sda /dev/zero
prw------- 1 root root
0 Oct 8 09:13 /dev/initctl
srw-rw-rw- 1 root root
0 Oct 8 09:10 /dev/log
brw-r----- 1 root disk 8, 0 Oct 8 04:09 /dev/sda
crw-rw-rw- 1 root root 1, 5 Oct 8 04:09 /dev/zero

(p)
(s)
(b)

(c)

8.2.4. file

, , ,
:
file /dev/log
/dev/log:
/dev/initctl:
/dev/sda:
/dev/zero:

/dev/initctl /dev/sda /dev/zero\


socket
fifo (named pipe)
block special (8/0)
/
character special (1/5)
/

,
. file
, ELF .

. .
. file(1).

311

8.3.

8.2.5. stat
stat stat,
.
,
stat, :
stat /dev/sda
File: `/dev/sda'
Size: 0
Blocks: 0 IO
Block: 4096
block special file
Device: eh/14d Inode: 1137
Links: 1
Device type: 8,0
Access: (0640/brw-r-----) Uid: (
0/
root) Gid: (
6/
disk)
Access: 2006-10-08 04:09:34.750000000 -0500
Modify: 2006-10-08 04:09:34.750000000 -0500
Change: 2006-10-08 04:09:50.000000000 -0500

stat, printf,

,
man stat(1).

8.3.
, ,
,
. ,
,
ASCII.
, , ,
, :
$ wc -w konnichiwa.txt
0 konnichiwa.txt
$ LANG=ja_JP.UTF-8 wc -w konnichiwa.txt
1 konnichiwa.txt


konnichiwa ( )
wc 0 ,



, .

8.3.1. strings
,
.
, , .

312

8. IPC

, -
cat.
strings
; , , .
n,
. ,
, .

8.3.2. xxd
xxd Vim
, , bvi.
, xxd . bvi,

ACSII-:
$ xxd floats-ints.dat
0000000: 4865 6c6c 6f20
0000010: 2020 2020 2020
0000020: e381 abe3 81a1
0000030: 2020 2020 0a0a
0000040: 0000 0000 0000
0000050: 0000 0000 0000
0000060: 0000 0000 0000
0000070: 0000 0000 0000
0000080: 0000 0000 0000
0000090: 0000 0000 0000
00000a0: 0000 0000 0000
00000b0: 0000 0000 0000
00000c0: 0000 0000 0000
00000d0: 0000 0000 0000
00000e0: 0000 0000 0000
00000f0: 0000 0000 0000
0000100: 0000 0000 0000
0000110: 0000 0000 0000
0000120: 0000 0000 0000
0000130: 0090 5f01 0091
0000140: 0000 c8af 4780
0000150: 47

576f
2020
e381
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
5f01
c8af

726c
0abf
af0d
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0092
4700

640d
e381
2020
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
5f01
c9af

2020
93e3
2020
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0093
4780

2020
8293
2020
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
5f01
c9af

Hello World.
........
..........
............
................
................
................
................
................
................
................
................
................
................
................
................
................
................
................
.._..._..._..._.
....G...G...G...
G

xxd 16- (),


g. ,
4 , g4. , ,
, . ,
32- , IA32, .
,
, , .
Little Endian ( ).

313

8.3.

, ,
xxd .
. , ,
(Big Endian)
, ,
.
, .
, ,
, xxd,
.

8.3.3. hexdump
hexdump, ,
. xxd, hexdump
16- ,
, ,
, xxd hexdump,
.
hexdump , xxd, ,
,
. hexdump
, 16- ,
.

8.3.4. od
od Unix . , od
.
t
(
).
:
$ od -tc floats-ints.dat
0000000
H
e
l
l
o
0000020
0000040 343 201 253 343 201
0000060
\n
0000100 \0 \0 \0 \0 \0
*

*)
0000460 \0 220
_ 001 \0
0000500 \0 \0 310 257
G
0000520
G
0000521

ASCII-
r
l
d \r
\n 277 343 201 223 343 202 223
241 343 201 257 \r
\n \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
(
W

221
_ 001
200 310 257

\0 222 _ 001
G \0 311 257

\0 223
_ 001
G 200 311 257

314

8. IPC

8.4.
System V IPC
System V IPC
ipcs ipcrm. ipcs
System
V IPC. ipcrm System V IPC,
.

8.4.1. System V
ipcs
( ),
.
, .
X Window System V IPC,
,
. :
$ ipcs -m -m

-m ,

------ Shared Memory Segments -------key
shmid
owner
perms
bytes
nattch
status
0x00000000 163840
john
600
196608
2
dest
0x66c8f395 32769
john
600
1
0
0x237378db 65538
john
600
1
0
0x5190ec46 98307
john
600
1
0
0x31c16fd1 131076
john
600
1
0
0x00000000 196613
john
600
393216
2
dest
0x00000000 229382
john
600
393216
2
dest
0x00000000 262151
john
600
196608
2
dest
0x00000000 294920
john
600
393216
2
dest
0x00000000 327689
john
600
393216
2
dest
0x00000000 360458
john
600
196608
2
dest
0x00000000 393227
john
600
393216
2
dest
0x00000000 425996
john
600
196608
2
dest
0x00000000 884749
john
600
12288
2
dest
0x00000000 2031630
john
600
393216
2
dest
0x00000000 2064399
john
600
196608
2
dest
0x00000000 2097168
john
600
16384
2
dest

,
. key ,
shmid-. nattch
,
. ipcs
p,
,
. :

8.4. System V IPC

315

$ ipcs -m -p
------ Shared Memory Creator/Last-op -------shmid
owner
cpid
lpid
163840
john
2790
2906
32769
john
2788
0
65538
john
2788
0
98307
john
2788
0
131076
john
2788
0
196613
john
2897
2754
229382
john
2899
2921
262151
john
2899
2921
294920
john
2907
2754
327689
john
2921
2923
360458
john
2893
2754
393227
john
2893
2754
425996
john
2921
2754
884749
john
2893
2754
2031630
john
8961
9392
2064399
john
8961
9392
2097168
john
8961
9392


cpid, ,
,
lpid. , nattch 2,
.
, ,
- ( ).
,
.
nattch 0 ,
ipcs, ,
icprm. ipcs
:
?
lsof. :
$ ipcs -m
------ Shared Memory Segments -------key
shmid
owner
perms
bytes
nattch
status
...
0xdeadbeef 2752529
john
666
1048576
3
, ?
$ ipcs -m p
------ Shared Memory Creator/Last-op -------shmid
owner
cpid
lpid
2752529
john
10155
10160
10155 10160.
, lsof.

316

8. IPC

$ lsof | head -1 ; lsof | grep 2752529


COMMAND
PID
USER
FD
TYPE
DEVICE
sysv-shm 10155
john DEL
REG
0,7
sysv-clie 10158
john DEL
REG
0,7
sysv-clie 10160
john DEL
REG
0,7

SIZE

NODE
2752529
2752529
2752529

NAME
/SYSVdeadbeef
/SYSVdeadbeef
/SYSVdeadbeef

, ,
, ipcs -i. ,
System V , shmid ( ). :
$ ipcs -m -i 32769



shmid-

Shared memory Segment shmid=32769


uid=500 gid=500 cuid=500
cgid=500
mode=0600
access_perms=0600
bytes=1 lpid=0 cpid=2406
nattch=0
att_time=Not set
det_time=Not set
change_time=Sat Apr
8 15:48:24 2006
$ kill -0 2406
bash: kill: (2406) No such process
$ ipcrm -m 32769

2406

8.4.2. System V
System V ipcs
q:
$ ipcs -q
------ Message Queues
key
msqid
0x00000000 131072
0x00000000 163841
0x00000000 196610
0x00000000 229379

-------owner
john
john
john
john

perms
600
600
600
600

used-bytes
0
0
0
132

messages
0
0
0
1

, key,
, msqid
. ,
.
0, ,
IPC_PRIVATE.
( msqid- 229379)
used-bytes messages.
,

8.4. System V IPC

317

. ipcs
i:
$ ipcs -q -i 229379
Message Queue msqid=229379
uid=500 gid=500 cuid=500
cgid=500
cbytes=132
qbytes=16384
qnum=1
send_time=Sun Oct
22
15:25:53
2006
rcv_time=Not set
change_time=Sun Oct 22
15:25:53
2006

mode=0600
lspid=12641
lrpid=0

, lspid lrpid
. , ,
, :
$ ipcrm -q 229379

ipcrm ,
,
, q.

8.4.3. System V
, ipcs
,
s:
$ ipcs s
------ Semaphore Arrays -------key
semid
owner
perms
0x6100f981 360448
john
600

nsems
1

, System V .
nsems .
, .
ipcrm:
$ ipcrm -s 360448


( ). ,
i:
$ ipcs -s -i 393216
Semaphore Array semid=393216
uid=500 gid=500
cuid=500
mode=0600, access_perms=0600
nsems = 1
otime = Tue May 9 22:23:30 2006

cgid=500

318

8. IPC

ctime = Tue May 9 22:22:23 2006


semnum
value
ncount
zcount
0
3
0
1

pid
32578

, stat
, ,
, . ncount
, .
zcount , ,
. pid
, , ;
, , .
ps ,
. wchan ,
. ,
,
$ ps -o wchan -p 32746
WCHAN
semtimedop

semtimedop .
, , ,
, . ,

.

8.5.
POSIX IPC
POSIX IPC .
POSIX ,
, Linux

IPC. IPC-
, ,
,
POSIX IPC.

8.5.1. POSIX
POSIX
. Linux POSIX
tmpfs, /dev/shm. ,
,
. ,
, .

8.5. POSIX IPC

319

, , ,
.
Linux

: open, close, mmap, unlink .. ,
Linux. POSIX
, ,

POSIX.

8.5.2. POSIX
POSIX Linux
mqueue. ,
.
POSIX , .
, , ,
/mnt/mqs, :
$ mkdir /mnt/mqs
$ mount -t mqueue none /mnt/mqs


mount,

root

,
POSIX .
. cat ,
, , :
$ ls -l /mnt/mqs
total 0
-rw------- 1 john john 80 Apr 9 00:20 myq
$ cat /mnt/mqs/myq
QSIZE:6
NOTIFY:0
SIGNO:0
NOTIFY_PID:0

QSIZE . ,
. NOTIFY, SIGNO NOTIFY_PID
mq_notify, .
POSIX
, rm
mqueue .

8.5.3. POSIX
POSIX Linux ,
tmpfs, . Linux,
API- System V, -
.

320

8. IPC

. ,

GNU (librt), glibc.
,
, .
glibc 2.3.5
, /dev/shm. mysem
/dev/shm/sem.mysem. API- POSIX
,
procfs; , , lsof fuser,
.
POSIX . sem_t,
GNU, ,
. ,
.

8.6.
, ,
ps,
() . ,
, .
,
o:
$ ps -o pending,blocked,ignored,caught
PENDING
BLOCKED
IGNORED
CAUGHT
0000000000000000 0000000000010000 0000000000384004 000000004b813efb
0000000000000000 0000000000000000 0000000000000000 0000000073d3fef9

BSD,
,
.
:
$ ps s
UID
PID
500
500
500
500
500
500

6487
6549
12121
17027
17814
17851

PENDING

BLOCKED

00000000
00000000
00000000
00000000
00000000
00000000

00000000
00000000
00010000
00000000
00010000
00000000

s.
IGNORED
CAUGHT ...
00384004
00384004
00384004
00000000
00384004
00000000

4b813efb
4b813efb
4b813efb
08080002
4b813efb
73d3fef9

...
...
...
...
...
...

, , ,
,
BLOCKED. ,

8.7. ()

321

. 1 0 N
N-1:
CAUGHT , ;
IGNORED ,
signal(N,SIG_IGN);
BLOCKED ,
sigprocmask;
PENDING , , .

strace.
,
. strace , ,
, .
strace ,
. , ,
, . , ,
, , ;
strace .

8.7.
()

netstat,
, /proc/net. FIFO
, - ,
.
FIFO /proc/pid/fd ,
FIFO.

8.7.1. FIFO
/proc/pid/fd FIFO
. ,
pipe (
, ):
$ ls -l !$
ls -l /proc/19991/fd
total 5
lrwx------ 1 john john
lrwx------ 1 john john
lrwx------ 1 john john
lr-x------ 1 john john
l-wx------ 1 john john

64
64
64
64
64

Apr
Apr
Apr
Apr
Apr

12
12
12
12
12

23:33
23:33
23:33
23:33
23:33

0
1
2
3
4

->
->
->
->
->

/dev/pts/4
/dev/pts/4
/dev/pts/4
pipe:[318960]
pipe:[318960]

322

8. IPC

pipe:[318960], 318960
. : pipe
,
, .
.
lsof
.

8.7.2.

netstat lsof. netstat

. , , ,
TCP- , :
$ netstat --tcp -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address
tcp
0
48 ::ffff:192.168.163.128:22

Foreign Address
State
::ffff:192.168.163.1:1344 ESTABLISHED

, lsof:
$ lsof -n -i tcp
COMMAND
PID
USER
FD
TYPE DEVICE SIZE NODE NAME
portmap
1853
rpc
4u IPv4
4847
TCP *:sunrpc (LISTEN)
rpc.statd 1871 rpcuser
6u IPv4
4881
TCP *:32769 (LISTEN)
smbd
2120
root
20u IPv4
5410
TCP *:microsoft-ds (LISTEN)
smbd
2120
root
21u IPv4
5411
TCP *:netbios-ssn (LISTEN)
X
2371
root
1u IPv6
6310
TCP *:x11 (LISTEN)
X
2371
root
3u IPv4
6311
TCP *:x11 (LISTEN)
xinetd
20338
root
5u IPv4 341172
TCP *:telnet (LISTEN)
sshd
23444
root
3u IPv6 487790
TCP *:ssh (LISTEN)
sshd
23555
root
3u IPv6 502673
...
TCP 192.168.163.128:ssh->192.168.163.1:1344 (ESTABLISHED)
sshd
23557
john
3u IPv6 502673
...
TCP 192.168.163.128:ssh->192.168.163.1:1344 (ESTABLISHED)

lsof pid-
. , sshd
. ,
, lsof,
, , netstat.
lsof ,
. TCP-, (wait), . ,
, TIME_WAIT. ,
lsof, ,
. , netstat,
, . TCP-
netstat tcp:

323

8.8.

$ netstat -n --tcp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
tcp
0
0 127.0.0.1:60526
127.0.0.1:5000

State
TIME_WAIT

,
, , ,
. , ,
,
.
netstat
, l,
p
:
$ netstat --unix -lp | grep /tmp/.ICE-unix/
unix 2 [ACC] STREAM LISTENING 7600 2408/gnome-session /tmp/.ICE-unix/2408

8.8.

IPC
Linux vfs,
.
, (, tmpfs
procfs), API- .
,
.

UNIX. , ,
, , ..
,
.
,
Linux .
,
, .
,
, .
, ,
IP-. ,
,
.
lsof
, .
NODE .

324

8. IPC

netstat ,
UNIX. ,
UNIX , .
. Linux
, lsof netstat
( Linux,
) , . netstat
,
lsof DEVICE.
TCP-, xinetd (
root):
$ lsof -i tcp -a -p $(pgrep xinetd)
COMMAND PID USER
FD
TYPE DEVICE SIZE NODE NAME
xinetd 2838 root
5u IPv4 28178
TCP *:telnet (LISTEN)

, xinetd telnet (
23). NODE TCP,
DEVICE .
,
procfs, :
$ ls -l /proc/$(pgrep xinetd)/fd
total 7
lr-x------ 1 root root 64 Oct 22
lr-x------ 1 root root 64 Oct 22
lr-x------ 1 root root 64 Oct 22
lr-x------ 1 root root 64 Oct 22
l-wx------ 1 root root 64 Oct 22
lrwx------ 1 root root 64 Oct 22
lrwx------ 1 root root 64 Oct 22

22:24
22:24
22:24
22:24
22:24
22:24
22:24

0
1
2
3
4
5
7

->
->
->
->
->
->
->

/dev/null
/dev/null
/dev/null
pipe:[28172]
pipe:[28172]
socket:[28178]
socket:[28175]

8.9.
,
IPC,
. System V IPC
, POSIX IPC
, .

8.9.1. ,

ipcs , ipcrm , System

V IPC.
lsof, fuser

8.9.

325

ltrace ,

, .
pmap .
strace .

8.9.2. -
http://procps.sourceforge.net procps,
pmap.
http://sourceforge.net/projects/strace strace.

9.1.
,
. ,

, .

, ,
,
.

9.2.
.
:
, ..
. ,
,
.
,
.

9.2.1. ,


, :
. ?

.

?

9.2.

327


, ,
, ,
. ,
,
.
,
, .
, , ,

.



, .
, ,
, .
,
.
,
, .
.
, , .
.
( hog) 9.1.
malloc, ,
.

GNU time. ,
bash- time. GNU-
, , :
$ \time ./hog 127
allocated 127 mb
Command terminated by signal 2
0.01user 0.32system 0:01.17elapsed 28%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+32626minor)pagefaults 0swaps


.
, /, .
, , hog
malloc,
.
.
,

328

9.

, ,
.
.
9.1. hog.c: , ,
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 int main(int argc, char *argv[])
7 {
8
if (argc != 2)
9
exit(0);
10
11
size_t mb = strtoul(argv[1], NULL, 0);
12
13
//
14
size_t nbytes = mb * 0x100000;
15
16
char *ptr = (char *) malloc(nbytes);
17
if (ptr == NULL) {
18
perror("malloc");
19
exit(EXIT_FAILURE);
20
}
21
22
// ( calloc())
23
size_t i;
24
const size_t stride = sysconf(_SC_PAGE_SIZE);
25
for (i = 0; i < nbytes; i += stride) {
26
ptr[i] = 0;
27
}
28
29
printf("allocated %d mb\n", mb);
30
pause();
31
return 0;
32 }

,
, .

,
. , ,
(
LRU least recently used).
,
, .
,

329

9.2.

, ,
.
,
; ,
.
,
hog, ,
:
$ free m

total
Mem:
250
-/+ buffers/cache:
Swap:
511

used
109
97
0

free
140
152
511

shared
0

buffers
0

cached
11

, 250 ,
140 , .
$ \time ./hog 200
allocated 200 mb
Command terminated by signal 2
0.03user 0.61system 0:03.94elapsed 16%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+51313minor)pagefaults 0swaps
, 51 313
!
$ free m

total
Mem:
250
-/+ buffers/cache:
Swap:
511

used
48
42
68

free
201
208
443

shared
0

buffers
0

cached
6

68 !

, hog
68 , time ,
.
, .
, ,
. , ,
, .
hog ,
, .
kswapd.
kswapd
. ,
, , .
, ,
.

330

9.

top. ,
procps, top. ncurses,

1.
top ps,
, top ,
ps. top ,
top,
. top:
top - 20:27:24 up 3:06, 4 users, load average: 0.17, 0.27, 0.41
Tasks: 64 total,
3 running, 61 sleeping, 0 stopped, 0 zombie
Cpu(s): 6.8% us, 5.1% sy, 0.8% ni, 80.8% id, 6.2% wa, 0.3% hi, 0.0% si
Mem:
158600k total,
30736k used,
127864k free,
1796k buffers
Swap:
327672k total,
10616k used,
317056k free,
16252k cached
PID
1
2
3
4
5
6
8
61
64
...

USER
root
root
root
root
root
root
root
root
root

PR
16
34
RT
10
13
10
20
10
10

NI
0
19
0
-5
-5
-5
-5
-5
-5

VIRT
1744
0
0
0
0
0
0
0
0

RES
96
0
0
0
0
0
0
0
0

SHR
72
0
0
0
0
0
0
0
0

S %CPU %MEM
R 0.0 0.1
S 0.0 0.0
S 0.0 0.0
S 0.0 0.0
S 0.0 0.0
S 0.0 0.0
S 0.0 0.0
S 0.0 0.0
S 0.0 0.0

TIME+
0:00.89
0:00.00
0:00.00
0:00.10
0:00.03
0:00.00
0:00.00
0:01.37
0:00.00

COMMAND
init
ksoftirqd/0
watchdog/0
events/0
khelper
kthread
kacpid
kblockd/0
khubd

, top
, .
Shift+G,
:
Choose field group (1 4):

,
. - ,
.
, ,
. Shift+A,
,
. :
1:Def Tasks:
Cpu(s):
Mem:
Swap:
1

22:26:14 up 1:41, 3 users, load average: 0.02, 0.01, 0.10


69 total,
1 running, 68 sleeping, 0 stopped, 0 zombie
0.7% us, 0.0% sy, 0.0% ni, 99.3% id, 0.0% wa, 0.0% hi, 0.0% si
256292k total,
174760k used,
81532k free,
8860k buffers
524280k total,
28120k used,
496160k free, 113608k cached

GNOME- gtop, Fedora


gnome-system-monitor. GUI-
.

331

9.2.

PID
4026
30696
3583
2 PID
30696
4054
4034
4033
3 PID
4026
4030
30696
3588
4 PID
4026
4030
30696
3588

USER
PR NI VIRT RES SHR S %CPU %MEM
TIME+ COMMAND
john
15
0 42232 11m 6504 S 0.7 4.7 0:05.13 gnome-terminal
john
16
0 1936 956 764 R 0.3 0.4 0:00.01 top
john
15
0 7056 660 396 S 0.0 0.3 0:04.98 sshd
PPID
TIME+ %CPU %MEM PR NI S VIRT SWAP RES UID COMMAND
4034
0:00.01 0.3 0.4 16
0 R 1936 980 956 500 top
4026
0:00.03 0.0 0.3 16
0 S 4276 3600 676 500 bash
4026
0:00.09 0.0 0.3 15
0 S 4276 3484 792 500 bash
4026
0:00.00 0.0 0.1 16
0 S 2068 1920 148 500 gnome-pty-helpe
%MEM VIRT SWAP RES CODE DATA SHR nFLT nDRT S PR NI %CPU COMMAND
4.7 42232 29m 11m 256 17m 6504 542
0 S 15
0 0.7 gnome-termin
0.5 4576 3248 1328
48 1604 896
47
0 S 16
0 0.0 gconfd-2
0.4 1936 980 956
48 268 764
0
0 R 16
0 0.3 top
0.3 4276 3460 816 580 260 624
10
0 S 16
0 0.0 bash
PPID UID USER
RUSER
TTY
TIME+ %CPU %MEM S COMMAND
3588 500 john
john
pts/1
0:05.13 0.7 4.7 S gnome-termina
1 500 john
john
pts/1
0:00.32 0.0 0.5 S gconfd-2
4034 500 john
john
pts/2
0:00.01 0.3 0.4 R top
3583 500 john
john
pts/1
0:00.29 0.0 0.3 S bash

top hog-.
, ,
,
hog.c 9.1. ( son-of-hog.c)
9.2. ,
top .
:
$ cc -O2 -o son-of-hog son-of-hog.c -lrt
$ ln -s son-of-hog hog-a
$ ln -s son-of-hog hog-b

librt clock_gettime

,
top

.
, .
swapon swapoff:
$ free m

total
Mem:
250
-/+ buffers/cache:
Swap:
511
$ sudo swapoff -a
$ sudo swapon -a
$ free m
total
Mem:
250
-/+ buffers/cache:
Swap:
511

used
free
shared
buffers
cached
246
3
0
14
165
66
183
4
507
4
(
root);
RAM

used
246
71
0

free
3
178
511

shared
0

buffers
14

cached
160

332

9.

178
.
, +/- buffers/cache free.
,
1.
hog-a 150 ,
:
$ ./hog-a 150 &
[1] 30825
$ touched 150 mb; in 0.361459 sec
$ free -m
total
used
free
Mem:
250
246
3
-/+ buffers/cache:
217
32
Swap:
511
0
511

shared
0

buffers
11

cached
17

, 150
( 361 ).
, ,
. SIGUSR1,
:
$ kill -USR1 %1
$ touched 150 mb; in 0.009929 sec

, 9 !

, .
hog , :
$ ./hog-b 150 &
[2] 30830
$ touched 150 mb; in 5.013068 sec
$ free -m
total
used
free
Mem:
250
246
3
-/+ buffers/cache:
235
14
Swap:
511
136
375

shared
0

buffers
0

cached
10

, ,
! , 361 , 5 .
, , hob-b
hog-a,
. , free ,
hob-b 136 . ,
!
:
1

. ,
.

9.2.

333

$ pkill -USR1 hog-b


touched 150 mb; in 0.019061 sec

, ,
hog-a. ,
top. p,
hog:
$ top -p $(pgrep hog-a) -p $(pgrep hog-b)

Shift+A,
. :
1:Def - 23:21:16 up 2:36, 2 users, load average: 0.00, 0.02, 0.01
Tasks:
2 total,
0 running,
2 sleeping,
0 stopped,
0 zombie
Cpu(s): 0.0% us, 0.0% sy, 0.0% ni, 100.0% id, 0.0% wa, 0.0% hi, 0.0% si
Mem:
256292k total,
252048k used,
4244k free,
692k buffers
Swap:
524280k total,
139760k used,
384520k free,
11908k cached
1

PID
30825
30830
2 PID
30830
30825
3 PID
30830
30825
4 PID
30830
30825

USER
PR NI VIRT RES SHR S %CPU %MEM
TIME+ COMMAND
john
16
0 151m 63m 172 S 0.0 25.3
0:00.36 hog-a
john
16
0 151m 150m 296 S 0.0 60.1
0:00.57 hog-b
PPID
TIME+ %CPU %MEM PR NI S VIRT SWAP RES UID COMMAND
4054
0:00.57 0.0 60.1 16
0 S 151m 1160 150m 500 hog-b
4054
0:00.36 0.0 25.3 16
0 S 151m 88m 63m 500 hog-a
%MEM VIRT SWAP RES CODE DATA SHR nFLT nDRT S PR NI %CPU COMMAND
60.1 151m 1160 150m
4 150m 296
9
0 S 16
0 0.0 hog-b
25.3 151m 88m 63m
4 150m 172
2
0 S 16
0 0.0 hog-a
PPID UID USER
RUSER
TTY
TIME+ %CPU %MEM S COMMAND
4054 500 john
john
pts/3
0:00.57 0.0 60.1 S hog-b
4054 500 john
john
pts/3
0:00.36 0.0 25.3 S hog-a

: 151
, , VIRT 1.
130 hog
10, nFLT
3. RES ( 1 2) ,
(
). SWAP, ,
.
9.2. son-of-hog.c: hog.c
1
2
3
4
5
6
7
8
9
10

#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<signal.h>
<time.h>
<unistd.h>
<sys/time.h>

void handler(int sig)


{

334
11
12 }
13
14

9.

//

// timespec
//
15 #define TIMESPEC2FLOAT(tv) ((double) (tv).tv_sec + (double) (tv).tv_nsec * 1e-9)
16
17 int main(int argc, char *argv[])
18 {
19
if (argc != 2)
20
exit(0);
21
22
//
23
signal(SIGUSR1, handler);
24
25
size_t mb = strtoul(argv[1], NULL, 0);
26
27
//
28
size_t nbytes = mb * 0x100000;
29
char *ptr = (char *) malloc(nbytes);
30
if (ptr == NULL) {
31
perror("malloc");
32
exit(EXIT_FAILURE);
33
}
34
35
int val = 0;
36
const size_t stride = sysconf(_SC_PAGE_SIZE);
37
38
// ,
// SIGUSR1
39
while (1) {
40
int i;
41
struct timespec t1, t2;
42
43
// t1
44
clock_gettime(CLOCK_REALTIME, &t1);
45
46
// !
47
for (i = 0; i < nbytes; i += stride) {
48
ptr[i] = val;
49
}
50
val++;
51
52
// t2
53
clock_gettime(CLOCK_REALTIME, &t2);
54
55
printf("touched %d mb; in %.6f sec\n", mb,
56
TIMESPEC2FLOAT(t2) - TIMESPEC2FLOAT(t1));
57
58
//
59
pause();

9.2.

60

61

rtur

62
,
, ,

hog -

hog -

. ,

,
HeH , .
, ,

top,

pklll -USRl hog-a


touched 150 ; l 14.286939 sec
pklll -USRl hogtouched 150 ; l 16.731990 sec
pklll -USRl hog-a
$ touched 150 ; l 16.944799 sec
$
$
$
$

hog-b ,


"

hog-b

, , .

hog

150

1 .
Pentium 4, 4 ,
38 400 . , 37
17 .
-
.
, ,
,

.
,

, , ,
,

. ,
,
.

9.2.2.

,
. -

, .

.
.

9.1

, !

Express (PCIe).

(Frontside s, FSB) -

9.

.
, ,

,
( ) .


(!

Express, AG ),

~-

lip8Jl

FSB

PCle

DDR2

...,

,~

I
PCle

,
()

!
.

9.1.

Intel

pel Express

FS-
FS- ,
,

. FS-
/ .

FSB-

-, .

, .

9.1,

, ,

(), ,
, FS-. ,

, -

9.2.

FS-, . ,

FSB- .

n SMP (Symmetric Multiprocessing).



. Linux
SMP 2.0.

, ,

S-. ,
,

SMP, ,

, .

, FS-,

. .
, ,
,

, .



, . ,

100 %,

, , ,
.

CPU_l dl , Linux,
, .
,

, cpu _ i dl .
, ,

, .

100 %.

,
.

.
. , .
,

- .

100 %

,
.

, - (
, ) -
- (, ).

9.

. , ,
, .
,

. ,

, 1 ,

n ( ,
) .
, , .
,

Intel

,
, ,

cTBoM .

9.2.3.

Intel.

, , ,
, ,

, .

, -
-, .


,
,

I-. ,
,

SBUS, ISA, VME

, .

, -- ,
,

.
, .

FS-, , ,
.
I- ,
. .

9.2,

, . ,

,
.

,
.

9.2.

339

.
,
,
.

, .

. 9.2.



. PCI Express
Intel Hypertransport AMD.
, .
, ,
.
, .
,
, Ethernet. (

340

9.

) , .
PCI Express ,
. , (,
), PCI Express
, .

, USB FireWire,

, ISA. -

. 15 ,
,
.
, .
,
,
. ,
,
, .

USB FireWire,
-.
, .
.
/proc/
interrupts. ,
.
, ,
, :
$ cat /proc/interrupts
CPU0
0:
27037
1:
10
2:
0
7:
1
8:
1
9:
0
11:
1184
12:
0
14:
6534
15:
269
NMI:
0
LOC:
27008
ERR:
0
MIS:

XT-PIC
XT-PIC
XT-PIC
XT-PIC
XT-PIC
XT-PIC
XT-PIC
XT-PIC
XT-PIC
XT-PIC

timer
i8042
cascade
parport0
rtc
acpi
uhci_hcd:usb1, uhci_hcd:usb2, uhci_hcd:usb3, eth0
VIA8233
ide0
ide1

9.2.

341


,
. ,
.
250,
250 .

PIC APIC
ISA, .
8259 PIC (Programmable Interrupt Controller),
(. .9.1). ,
, ,
. .
,
.
Pentium
APIC (Advanced
Programmable Interrupt Controller),

. Intel
APIC-, .
,
. APIC-

LAPIC (Local Advanced Programmable Interrupt Controller), APIC-
, ,
/ I/O APIC (I/O Advanced Programmable Interrupt Controller).
APIC-
, .
APIC-
BIOS ,
8259 PIC, .
Linux PIC-. /var/log/messages
:
localhost kernel: Local APIC disabled by BIOS -- you can enable it with "lapic"

, LAPIC-
, BIOS,
lapic. /etc/grub.conf :
Fedora Core (2.6.16np)
root (hd1,0)
kernel /vmlinuz-2.6.16np ro root=/dev/VolGroup00/LogVol00 rhgb quiet
lapic
initrd /initrd-2.6.16np.img

342

9.

APIC-
, .
,
PIC-. , APIC-
,
, .
, APIC-
.
BIOS, nolapic.



, . ,
, .
,
.
, .
PCI PCI-X
.
.
PCI Express , ,
.
PCI Express ,
(2,5 /).
, .
, .
,
,
.
.



lspci,
, .
,
, .
,
.
- lspci
, PCI-. PCI-
.
16- , ,
. lspci
,
.

9.2.

343

PCI , ,
.
bus. PCI
. 0 ( ).
.
lspci
, PCI-,
PCI Express.
, vv ().
,
.

9.2.4.

,
, .
top.
, ,
.
.
.

vmstat

vmstat ,
. ,
.
: vmstat, ,
, .
vmstat 1, :
$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu---r b swpd
free
buff cache
si
so
bi
bo
in
cs us sy id wa
1 1
0
3940 14740 147440
0
0
51
15 263
91 1 8 90 1
0 1
0
3764 15236 145516
0
0 2952
0 534
606 3 10 0 87
0 1
0 12368 16320 132804
0
0 1576
0 565
644 0 9 0 91
0 1
0
8216 16728 135116
0
0 2768
0 455
430 2 5 0 93
0 1
0
3984 16732 139260
0
0 4164
0 402
354 1 5 0 94
0 1
0 14452 16764 128484
0
0 3596 1020 438
397 1 5 0 94
0 1
0 11236 17796 129696
0
0 2196
0 588
695 2 9 0 89
0 1
0
8028 18724 130944
0
0 2220
0 556
610 2 7 0 91
0 1
0
4040 19604 132240
0
0 2180
0 532
601 1 12 0 87
1 1
0
4140 19744 131624
0
0 2964
0 491
506 1 5 0 94
0 1
0
4368 20024 131004
0
0 3484
932 481
488 1 6 0 93
0 1
0
9748 20212 124968
0
0 3840
0 502
535 1 8 0 91

344

9.

vmstat. ,
, vmstat
r b procs. r ,
, b
, .
, ,
( ).
/.
, b
, ,
.
vmstat.
memory swpd, free, buff cache.
, ,
.
.
S ( . vmstat(8)).
,
, ,
buffers cache.
free.
/ vmstat.
/
. swap
,
. ,
. si
( page in), so
(
page out).
. , ,
.
.
swap , io,
, /.
, ,
. /, ,
read write, ,
, mmap. bi
, bo .
.
, vmstat.
system cpu.
system in,
.
, .

345

9.2.


,
.
cs .
,

TLB- , 5.
,

.
, cpu ( ),
(us), (sy)
(id). (wa) ,
/.
vmstat,
2.6. 2.4
; , /,
.
wa , ,
/, ,
/. ,
, .

sysstat
sysstat ,
.
, .
sar ( system activity reporting)
. , ,
, vmstat, ,
.
sar
.
sar. vmstat, sar
, .
,
. :
$ sar 1 4
Linux 2.6.16np (redhat)
09:41:05
09:41:06
09:41:07
09:41:08
09:41:09
Average:

PM
PM
PM
PM
PM

CPU
all
all
all
all
all

06/01/2006
%user
6.00
0.99
95.92
5.94
26.75

%nice
0.00
0.00
0.00
0.00
0.00

%system
11.00
8.91
1.02
8.91
7.50

%iowait %steal
83.00
0.00
90.10
0.00
3.06
0.00
85.15
0.00
65.75
0.00

%idle
0.00
0.00
0.00
0.00
0.00

346

9.

sar ,
.

() . ,
( 1 ). , ,
, vmstat, ,
%nice %steal. %nice
, nice
( ). %steal
, 1 .
sar.
r,
, vmstat,
:
$ sar -r 1 4
Linux 2.6.16np (redhat)
05/31/2006
11:04:54 PM kbmemfree kbmemused %memused kbbuffers
11:04:55 PM
5504 250120 97.85 20708
11:04:56 PM
5504 250120 97.85 20708
11:04:57 PM
5504 250120 97.85 20708
11:04:58 PM
5504 250120 97.85 20708
Average:
5504 250120 97.85 20708

kbcached
128008
128008
128008
128008
128008

kbswpfree kbswpused %swpused kbswpcad


524280
0
0.00
0
524280
0
0.00
0
524280
0
0.00
0
524280
0
0.00
0
524280
0
0.00
0

B,
:
$ sar -B 1 5
Linux 2.6.16np (redhat)
05/31/2006
11:08:53 PM pgpgin/s pgpgout/s
fault/s majflt/s
11:08:54 PM
0.00
0.00
53.00
0.00
11:08:55 PM
0.00
0.00
31.00
0.00
11:08:56 PM
0.00
0.00
11.00
0.00
11:08:57 PM
0.00
0.00
11.00
0.00
11:08:58 PM
0.00
48.00
11.00
0.00
Average:
0.00
9.60
23.40
0.00

, vmstat,
, ( ) ,
.
, , - .
sar. sar,
top, ,
x. ,
,
, x :
1

,
. ,
Xen, QEMU Bochs.

347

9.2.

$ jobs -x sar -x 3942 -x 3970 1 1


Linux 2.6.16np (redhat)
06/01/2006
10:04:09 PM
PID minflt/s majflt/s
10:04:10 PM
3942 43231.00
0.00
10:04:10 PM
3970
0.00
0.00
Average:
PID minflt/s majflt/s
Average:
3942 43231.00
0.00
Average:
3970
0.00
0.00

%user
5.00
46.00
%user
5.00
46.00

%system
48.00
0.00
%system
48.00
0.00

nswap/s
0.00
0.00
nswap/s
0.00
0.00

CPU
0
0

sar ,
.
. minflt/s, majflt/s, ,
( ),
(%user) (%system). nswap/s
2.4 .
CPU ,
.
, .

SMP
, .
SMP
sar, .
x
, X
,
. ,
. ,
,
.
X,
,
.
sar sysstat1,
mpstat iostat. Fedora sysstat,
,
/var/log/sa.
.
, /, sar.
sar .
,
.
, , , .

. ,
1

. http://perso.wanadoo.fr/sebastien.godard.

348

9.

, ,
.
, /
p:
$ sar -d -p 1 1
Linux 2.6.16np (redhat)
06/01/2006
10:40:12 PM
DEV
tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
10:40:13 PM
hda
0.00
0.00
0.00
0.00
0.00 0.00 0.00 0.00
10:40:13 PM
hdb 457.00
8.00 8248.00
18.07
1.00 2.19 2.18 99.60
10:40:13 PM
hdc
0.00
0.00
0.00
0.00
0.00 0.00 0.00 0.00
10:40:13 PM nodev 1037.00
8.00 8288.00
8.00
3.54 3.41 0.96 99.60
10:40:13 PM nodev
0.00
0.00
0.00
0.00
0.00 0.00 0.00 0.00

sar
, n, .
,
sar.
:
DEV Ethernet;
EDEV Ethernet;
NFS NFS;
NFSD NFS;
SOCK ;
ALL .

. sar
I, :
$ sar -I 0 1 1
Linux 2.6.16np (redhat)
11:00:52 PM
INTR
11:00:53 PM
0
Average:
0

06/01/2006
intr/s
250.00
250.00

0,
. , ,
, 250 .

ALL,
. ,
, /proc/interrupts.
sar. sar
.
cron,
sysstat . cron,
sysstat, /etc/cron.d/sysstat
, .

349

9.3.

sysstat -
. ,
sa .
.
, ,
, .

iostat mpstat
sysstat
: iostat mpstat. ,
, ,
sar, .
,
,
.
d ,
sar, , p. ,
iostat , ,
/, :
# iostat
Linux 2.6.16np (redhat) 06/03/2006
avg-cpu: %user
%nice %system %iowait %steal
4.56
0.42
1.55
1.65
0.00
Device:
tps Blk_read/s
Blk_wrtn/s
hda
0.01
0.04
0.00
hdb
5.66
169.67
39.79
hdc
0.00
0.01
0.00
dm-0
9.42
169.51
39.60
dm-1
0.03
0.01
0.19

%idle
91.83
Blk_read
201
833342
28
832570
72

Blk_wrtn
0
195444
0
194512
928

sar,
.
,
.
() ,
.
/.

9.3.
,
, , -
? , ,
- - .
? ,

350

9.

.
, .
,

Intel, : ,

, .
, ,
, Intel.

9.3.1. : time
time, ,

.
, , ,
,
.

9.3.2.
x86info
x86info1 ,
cupid,

Intel. ,
Intel .
, , ,
.
, Intel
. :
$ x86info
x86info v1.17. Dave Jones 2001-2005
Feedback to <davej@redhat.com>.
Found 1 CPU
-------------------------------------------------------------------------Found unknown cache descriptors: 64 80 91 102 112122
Family: 15 Model: 1 Stepping: 2 Type: 0 Brand: 8
CPU Model: Pentium 4 (Willamette) [D0] Original OEM
Processor name string: Intel(R) Pentium(R) 4 CPU 1.70GHz
Feature flags:
fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflsh ds
acpi mmx fxsr sse sse2 ss ht tm
1

. http://sourceforge.net/projects/x86info.

9.3.

351

Extended feature flags:


Instruction trace cache:
Size: 12K uOps 8-way associative.
L1 Data cache:
Size: 8KB
Sectored, 4-way associative.
line size=64 bytes.
L2 unified cache:
Size: 256KB
Sectored, 8-way associative.
line size=64 bytes.
Instruction TLB: 4K, 2MB or 4MB pages, fully associative, 64 entries.
Found unknown cache descriptors: 64 80 91 102 112 122
Data TLB: 4KB or 4MB pages, fully associative, 64 entries.
The physical package supports 1 logical processors

Pentium 4
1,7 , . x86info
- .
Intel - Intel Application
Note 4851, .
/proc/cpuinfo,
. ,
. , ,
, , . ,
, , x86info.
, , /proc/cpuinfo L1,
L2 L3. Pentium 4
:
$ cat /proc/cpuinfo
processor
: 0
vendor_id
: GenuineIntel
cpu family
: 15
model
: 1
model name
: Intel(R) Pentium(R) 4 CPU 1.70GHz
stepping
: 2
cpu MHz
: 1700.381
cache size
: 256 KB
fdiv_bug
: no
hlt_bug
: no
f00f_bug
: no
coma_bug
: no
fpu
: yes
fpu_exception
: yes
cpuid level
: 2
wp
: yes
flags
: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36
clflush dts acpi mmx fxsr sse sse2 ss ht tm
bogomips
: 3406.70
1

. http://developer.intel.com.

352

9.

.
cache-miss.c, 9.3.
9.3. cache-miss.c: ,

1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <malloc.h>
6 #include <assert.h>
7
8 int main(int argc, char *argv[])
9 {
10
// 64 TLB- ( ,
// )
11
int num_pages = 64;
12
13
//
14
if (argc > 1) {
15
num_pages = atoi(argv[1]);
16
}
17
18
const size_t page_size = sysconf(_SC_PAGESIZE);
19
20
int num_bytes = num_pages * page_size;
21
int alloc_bytes = num_bytes;
22
23
if (alloc_bytes == 0) {
24
// ,
25
//
26
alloc_bytes = page_size;
27
}
28
29
//
//
30
char *buf = memalign(page_size, alloc_bytes);
31
assert(buf != NULL);
32
33
printf("%d pages %d KB\n", num_pages, num_bytes / 1024);
(printf("%d %d \n", num_pages, num_bytes / 1024);
34
35
// .
// ,
36
// ,
//
37
if (num_pages == 0) {
38
exit(0);
39
}
40
41
//
42
static volatile char store;

9.3.

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 }

353

/*
**
** , 1000000
*/
int i;
char *c = buf;
for (i = 0; i < 1000000; i++) {
store = *c;
c += page_size;
if (c - buf >= num_bytes)
c = buf;
}
return 0;

,
,
1 . ,
- .
.

9.3.3. Valgrind

Valgrind Intel (IA32
X86_64), 32- Power PC.
.
cachegrind,
- .
9.3:
$ cc -O2 -o cache-miss cache-miss.c


. ,
, .
1
, , .
,
. Valgrind
cachegrind :
$ valgrind --tool=cachegrind ./cache-miss 0
==18902== Cachegrind, an I1/D1/L2 cache profiler.
==18902== Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.

354
==18902==
==18902==
==18902==
==18902==
==18902==
==18902==
--18902---18902-0 pages 0
==18902==
==18902==
==18902==
==18902==
==18902==
==18902==
==18902==
==18902==

9.

Using LibVEX rev 1471, a library for dynamic binary translation.


Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
Using valgrind-3.1.0, a dynamic binary instrumentation framework.
Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
For more details, rerun with: -v
warning: Pentium 4 with 12 KB micro-op instruction trace cache
16 I- 32 B
KB
I
I1
L2i
I1
L2i

refs:
137,569
misses:
1,216
misses:
694
miss rate:
0.88%
miss rate:
0.50%

refs:

62,808

==18902==
==18902==
==18902==
==18902==
==18902==
==18902== L2

refs:

3,469

( 3,185 rd +

284 wr)

==18902== L2
==18902== L2

misses:
miss rate:

1,945
0.9%

( 1,735 rd +
( 0.9%
+

210 wr)
1.3% )

(47,271 rd + 15,537 wr)


L1 L2
D1 misses: 2,253 ( 1,969 rd + 284 wr)
L2d misses:
1,251
( 1,041 rd +
210 wr)
D1 miss rate:
3.5% (
4.1%
+
1.8% )
L2d miss rate:
1.9% (
2.2%
+
1.3% )

L2

,
. , .
,
.
,
( ,
, pid-).
, ,
, 62808. 47271 ,
15537 .
, , L1 (
D1 ) 2253 .
, ,
.
.
,
.
, ,
. ,
.
miss rate,

9.3.

355

.
3,5%.
, ,
- .
, 2, , ,
, L1:
$ valgrind --tool=cachegrind ./cache-miss 2

==18908== Cachegrind, an I1/D1/L2 cache profiler.
...
1 pages 4 KB
==18908==
==18908== I
refs:
10,136,737
==18908== I1 misses:
1,213
==18908== L2i misses:
692
==18908== I1 miss rate:
0.01%
==18908== L2i miss rate:
0.00%
==18908==
==18908== D refs: 2,062,373 (1,046,952 rd + 1,015,421 wr)
==18908== D1 misses: 2,236 ( 1,951 rd + 285 wr)
==18908== L2d misses:
1,252
(
1,041 rd +
211 wr)
==18908== D1 miss rate:
0.1%
(
0.1%
+
0.0% )
==18908== L2d miss rate:
0.0%
(
0.0%
+
0.0% )
==18908==
==18908== L2 refs:
3,449
(
3,164 rd +
285 wr)
==18908== L2 misses:
1,944
(
1,733 rd +
211 wr)
==18908== L2 miss rate:
0.0%
(
0.0% +
0.0% )

, , ,
2 .
1 , . ,
L1
.
1969 1951. , ,
,
.
284 285.

, ,
.
.
256 (64 ),
L2 , :
$ valgrind --tool=cachegrind ./cache-miss 64
...
64 pages 256 KB
==18914==
==18914== I
refs:
9,152,148
==18914== I1 misses:
1,176

356
==18914==
==18914==
==18914==
==18914==
==18914==
==18914==
==18914==
==18914==
==18914==
==18914==
==18914==
==18914==
==18914==

9.

L2i misses:
I1 miss rate:
L2i miss rate:

670
0.01%
0.00%

D
D1
L2d
D1
L2d

refs:
2,062,236
(1,046,866 rd + 1,015,370 wr)
misses:
1,002,231
(1,001,947 rd +
284 wr)
misses:
1,315
(
1,105 rd +
210 wr)
miss rate:
48.5% (
95.7%
+
0.0% )
miss rate: 0.0% ( 0.1% + 0.0% )

L2
L2
L2

refs:
misses:
miss rate:

1,003,407 (1,003,123 rd +
1,985 (
1,775 rd +
0.0% (
0.0%
+

284 wr)
210 wr)
0.0% )

L1
,
. - ,
L1, ,
,
.
256
L2, ,
L1.
L2 ,
, ,
. , 1003407
L2,
3449.
, .
,
. ,
L2d misses.
L2, ,
. ,
, '
. 0,0% L2 miss rate.
, ,
L2:
$ valgrind --tool=cachegrind ./cache-miss 256
...
256 pages 1024 KB
==18918==
==18918== I
refs:
9,140,561
==18918== I1
misses:
1,176
==18918== L2i misses:
670
==18918== I1
miss rate:
0.01%
==18918== L2i miss rate:
0.00%
==18918==

357

9.3.

==18918==
==18918==
==18918==
==18918==
==18918==
==18918==
==18918==
==18918==
==18918==

D
D1
L2d
D1
L2d

refs:
2,062,295
misses:
1,002,230
misses:
1,001,251
miss rate:
48.5%
miss rate: 48.5%

(1,046,911 rd + 1,015,384 wr)


(1,001,946 rd +
284 wr)
(1,001,041 rd +
210 wr)
(
95.7%
+
0.0% )
( 95.6% + 0.0% )

L2
L2
L2

refs:
1,003,406 (1,003,122 rd +
misses:
1,001,921 (1,001,711 rd +
miss rate:
8.9% (
9.8%
+

284 wr)
210 wr)
0.0% )

256 , 4
L2. ,
.
,
L2 1 . Valgrind

, D refs. ,
, L2d
95,6 %, 100%.
L2 (L2d
miss rate) 48,5%, , .
,
, ,
, .
L2
. ,
8,9%,
L2 ( , )
(9140561). ,
, . ,
, ,
,
.
Valgrind , .
,
. ,
Valgrind, ,
, .

, , Valgrind,
.

9.3.4. ltrace
ltrace
. strace, ,
, ltrace , .

358

9.

, strace
, ltrace ,
. ++
, ltrace
' .
ltrace ,
. ltrace
,
. ,
ltrace y .
ltrace ,
.

e.
, :
$ ltrace -T -e read,write dd if=/dev/urandom of=/dev/null count=1
read(0,
"\007\354\037\024b\316\t\255\001\322\222O\b\251\224\335\357w\302\351\207\323\n$\
032\342\211\315\2459\006{"..., 512) = 512 <0.000327>
write(1,
"\007\354\037\024b\316\t\255\001\322\222O\b\251\224\335\357w\302\351\207\323\n$\
032\342\211\315\2459\006{"..., 512) = 512 <0.000111>
1+0 records in
1+0 records out
+++ exited (status 0) +++


, T
( ). , ,
read write, 3 .
,
. .
ltrace ,
, ,
,
.

9.3.5. strace

strace ltrace
. , strace
. , ltrace,

. , ,

359

9.3.

.
ltrace, strace
. ,
ltrace.
, strace,
. ,
strace ,
,
.

. ,
, .
strace p:
$ strace -ttt -p 23210
Process 23210 attached - interrupt to quit
1149372951.205663 write(1, "H", 1)
= 1
1149372951.206036 select(0, NULL, NULL, NULL,
1149372952.209712 write(1, "e", 1)
= 1
1149372952.210045 select(0, NULL, NULL, NULL,
1149372953.213802 write(1, "l", 1)
= 1
1149372953.214133 select(0, NULL, NULL, NULL,
1149372954.217911 write(1, "l", 1)
= 1
1149372954.218244 select(0, NULL, NULL, NULL,
1149372955.221994 write(1, "o", 1)
= 1
Process 23210 detached

{1, 0}) = 0 (Timeout)


{1, 0}) = 0 (Timeout)
{1, 0}) = 0 (Timeout)
{1, 0}) = 0 (Timeout)

,
Hello World 1 ,
. ,
, . c,
.

9.3.6.
: gcov gprof
gcov gprof, UNIX, ,
. , ,
,
, .
, ,
,
. ,
,
.
. , GNU
,
- .

360

9.

gprof
gprof
, ,
. ,
,
. gcc
pg. ,
, pg,
. .
,
,
, ,
.
9.4 , ,
,
.
9.4. profme.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

#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<math.h>

/* -. */
double slow(double x)
{
return pow(x, 1.12345);
}
/* */
double slower(double x)
{
return 1.0 / x;
}
/* , */
double slowest(double x)
{
return sqrt(x);
}
int main(int argc, char *argv[])
{
int i;
double x;
/* */
for (i = 0; i < 3000000; i++) {
x = 100.0;

361

9.3.

32
33
34
35
36 }

x = slow(x);
x = slower(x);
x = slowest(x);
}

,
pg. ,
, .

, , ,
.
, , .
-02 pg:
$ cc -pg -O2 -o profme profme.c -lm
$ ./profme


gmon.out. ,
, gprof.
gprof .
:
$ gprof ./profme
Flat profile:
Each sample counts as 0.01
%
cumulative
self
time seconds
seconds
46.88
0.15
0.15
37.50
0.27
0.12
12.50
0.31
0.04
3.12
0.32
0.01
...

seconds.
calls
3000000
3000000

self
ns/call
50.00
40.00

3000000

3.33

total
ns/call name
50.00 slowest
40.00 slower
main
3.33 slow

gcov gprof
gcov
, ,
. ,
,
, ,
.
, , -
. , .

, , , .

. :

362

9.

, .
gcov.
,
, ,
, . ,
,
, .

.
9.5 ,
.
9.5. summer-proj.c:

1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <math.h>
6
7 volatile double x;
8
9 int main(int argc, char *argv[])
10 {
11
int i;
12
for (i = 0; i < 16000000; i++)
13
x = 1000.0;
14
15
/* 0 <= r < 16 */
16
int r = i & 0xf;
17
18
if (r <= 8) {
19
x = pow(x, 1.234); /*
20
}
21
else if (r <= 11) {
22
x = sqrt(x);
/*
23
}
24
else {
25
x = 1.0 / x;
/*
26
}
27
}
28 }

9/16 */
3/16 */
4/16 */


:
$ cc -g -O2 -ftest-coverage -fprofile-arcs summer-proj.c -o summer-proj -pg -lm


.
, .

9.3.

363

-ftest-coverage summer-proj.gcno
. -fprofile-arcs ,
summer-proj.gcda.
gcov1.
pg, gprof.
gcov :
$ ./summer-proj
summer-proj.gcda
$ gcov ./summer-proj
summer-proj.c.gcov
File 'summer-proj.c'
Lines executed:100.00% of 9
summer-proj.c:creating 'summer-proj.c.gcov'

, 100%,
.
. ,
summer-proj.c.gcov, , .
9.6.
9.6.
-:
-:
-:
-:
-:
-:
-:
-:
-:
-:
-:
-:
-:
-:
1:
-:
16000000:
16000000:
-:
-:
16000000:
-:
16000000:
9000000:
-:
7000000:
1

summer-proj.c.gcov: gcov
0:Source:summer-proj.c
0:Graph:summer-proj.gcno
0:Data:summer-proj.gcda
0:Runs:1
0:Programs:1
1:#include <stdio.h>
2:#include <string.h>
3:#include <stdlib.h>
4:#include <time.h>
5:#include <math.h>
6:
7:volatile double x;
8:
9:int main(int argc, char *argv[])
10:{
11:
int i;
12:
for (i = 0; i < 16000000; i++) {
13:
x = 1000.0;
14:
15:
/* 0 <= r < 16 */
16:
int r = i & 0xf;
17:
18:
if (r <= 8) {
19:
x = pow(x, 1.234); /* 9/16
*/
20:
}
21:
else if (r <= 11) {

.gcda

fbranch-probabilities.

364

9.

3000000:

22:

x = sqrt(x);

-:
-:
4000000:

23:
24:
25:

}
else {
x = 1.0 / x;

-:
-:
-:

26:
27:
28:}

/* 3/16
*/
/* 4/16
*/

}
}

,
, .
,
gcov. , 19
. , 19, 22 25 slow, slower
slowest 9.4, . ,
sqrt .
?
gprof:
$ gprof --no-graph -l summer-proj| head -10
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative
self
self
total
time seconds
seconds
calls Ts/call Ts/call name
34.00
0.17
0.17
main (summer-proj.c:22
20.00
0.27
0.10
main (summer-proj.c:25
16.00
0.35
0.08
main (summer-proj.c:19
11.00
0.41
0.06
main (summer-proj.c:18
6.00
0.43
0.03
main (summer-proj.c:21
...

@
@
@
@
@

8048926)
8048879)
80488c6)
80488b3)
8048870)

, , , gprof
nograph.
:
gprof: gmon.out file is missing call-graph data

,
, . :
19,
22 (sqrt).
, , gcov,
. ,
gcov,
, .
, ,
gprof. ,
, OProfile.

9.3.

365

9.3.7. OProfile
Linux
OProfile. ,
1. OProfile
,
gcov gprof.
OProfile
, ,
, ,
,
. ,
.
, .
,
.
. (oprofiled)
root.
, ,
.
, OProfile.
OProfile ,
,
. ,
OProfile.
:
opcontrol oprofiled;
opreport ,
oprofiled;
opgprof gmon.out, gprof;
opannotate .
opcontrol
oprofiled, .
opcontrol. opcontrol
,
.
, .
, opcontrol
,
opcontrol.

: Instrumentation
Support Profiling Support. ,
OProfile.

366

9.

opcontrol start.
opcontrol ,
.
opcontrol
.oprofile. OProfile
- , --start,
.
root,
.
dump,
.
,
, , . ,
.
,
dump.
opreport, opgprof opannotate.
opreport ,
root.
oprofiled ,
opcontrol --start. opcontrol dump
,
oprofiled .
opcontrol dump
. opcontrol stop, oprofiled
.
OProfile.
,
, . oprofiled
,
.
,
.
:
/ ;
( );
;
/ .


, .
,
- . , ,
9.4
:

367

9.3.

$ cc -o profme -O2 -lm


$ opcontrol --start no-vmlinux

Using default event: GLOBAL_POWER_EVENTS:100000:1:1:1
Using 2.6+ OProfile kernel interface.
Using log file /var/lib/oprofile/oprofiled.log
Daemon started.
Profiler running.
$ ./profme
,

$ opcontrol dump

,

opreport
$ opreport
Counted GLOBAL_POWER_EVENTS events (time during which processor is not stopped)
with a unit mask of 0x01 (mandatory) count 100000
GLOBAL_POWER_E...|
samples|
%|
-----------------16477 52.9807 libm-2.3.3.so
11597 37.2894 profme
1795 5.7717 no-vmlinux
325 1.0450 libc-2.3.3.so
162 0.5209 bash
140 0.4502 libglib-2.0.so.0.400.8
103 0.3312 oprofiled

opreport , ,
,
libm. , ,
. ,
,
.
, . OProfile
, opgprof gmon.out,
gprof. :
$ opgprof profme
$ gprof --no-graph profme
Flat profile:
Each sample counts as 1 samples.
%
cumulative
self
time
samples
samples
calls
70.37
8161.00 8161.00
22.78 10803.00 2642.00
5.40 11429.00
626.00
1.41 11593.00
164.00
0.03 11597.00
4.00
...

gmon.out

gmon.out

self
T1/call

total
T1/call name
slow
slower
slowest
main
_init

368

9.

.
, .
. ( )
, ,
, .
,
, .
, -
, . , oprofiled 8161
, slow,
, .
, OProfile ,
.
--no-graph, gmon.out,
opgprof, .
,
,
(-g).
9.5:
$ cc -g -O2 -o summer-proj summer-proj.c -lm
$ opcontrol --reset
Signalling daemon... done
$ opcontrol --start
Profiler running.
$ ./summer-proj
$ opcontrol -dump
$ opgprof ./summer-proj
$ gprof --no-graph -l ./summer-proj | head
Flat profile:
Each sample counts as 1 samples.
%
cumulative
self
time
samples samples
calls
46.81
3496.00 3496.00
20.10
4997.00 1501.00
14.82
6104.00 1107.00
8.19
6716.00
612.00
4.69
7066.00
350.00

self
T1/call
main
main
main
main
main



.
gmon.out gprof

total
T1/call name
(summer-proj.c:19
(summer-proj.c:12
(summer-proj.c:19
(summer-proj.c:22
(summer-proj.c:21

@
@
@
@
@

8
8
8
8
8

048444)
0483fb)
04842c)
048410)
0483ea)

.
.
OProfile gprof
opannotate.
opgprof:
$ opannotate --source ./summer-proj
/*

stdout
(. )

9.3.

369

* Command line: opannotate --source ./summer-proj


*
* Interpretation of command line:
* Output annotated source file with samples
* Output all files
*
* CPU: P4 / Xeon, speed 1700.38 MHz (estimated)
* Counted GLOBAL_POWER_EVENTS events (time during which processor is not
stopped) with a unit mask of 0x01 (mandatory) count 100000
*/
/*
* Total samples for file : "/home/john/examples/ch-07/prof/summer-proj.c"
*
*
7458 99.8527
*/
:#include <stdio.h>
:#include <string.h>
:#include <stdlib.h>
:#include <time.h>
:#include <math.h>
:
:volatile double x;
:
:int main(int argc, char *argv[])
:{ /* main total:
7458 99.8527 */
:
int i;
1554 20.8060 :
for (i = 0; i < 16000000; i++) {
27 0.3615 :
x = 1000.0;
:
:
/* 0 <= r < 16 */
6 0.0803 :
int r = i & 0xf;
:
115 1.5397 :
if (r <= 8) {
4678 62.6322 :
x = pow(x, 1.234); /* 9/16
*/
:
}
350 4.6860 :
else if (r <= 11) {
612 8.1939 :
x = sqrt(x);
/* 3/16
*/
:
}
:
else {
116 1.5531 :
x = 1.0 / x;
/* 4/16
*/
:
}
:
}
:}

, gcov gprof.
, ,
oprofiled
. ,

370

9.

.
, ,
100% ,
.
OProfile.
OProfile. , ,
,
. ,
, ,
:
$ opcontrol --list-events

.
,
.
OProfile1.

9.4.


, , ,
SMP,
2.
SMP ,
, ,
.

.
SMP ,

.

9.4.1.

SMP
SMP
, .
, . , ,
1
2

. http://oprofile.sourceforge.net.
Linux2.0, 1996, , SMP-
.

9.4.

371

,
.
SMP-,
,
.
, .



. ,

SMP.
RISC CISC. , ,
.
, .
, ,

.
CISC
(Complex Instruction Set Computers ),
, .
CISC Motorola 68000 In
tel 8086. Motorola 68000 Macintosh,
Intel 8086 IBM PC1. Intel 8086
IA32.
, IBM,
, ,

,
.
RISC (Reduced Instruction Set Computer
). , RISC
, .
RISC , PowerPC, SPARC MIPS.
. ,
CISC RISC ,

,
XX .

- .
, ,
.
1

8088 8- 8086.

372

9.


.
, .
,
.
( ).
,
.
.
,
.
,
, , . ,
,
.
,
20-
20 , .
, , , ,
20 , .
- .
- .
,
.
. ,
,
. ,
(.9.3).
:
if.
, , ,
.
,
.

.
RISC ,
,
CISC-
. , RISC-
, CISC.
, ,
, .
,
.

9.4.

373

. 9.3. :
, x = false,

. 5,
. ,
, .

, .

,

SMP
.
FSB-,
(. .9.1), .
FSB-,

. FSB-

.
Opteron AMD
, ,
,
. , Opteron FSB
Hypertransport,
.
, FSB-,

374

9.

AMD
.
, , FSB, , Hypertransport
.
Hypertransport ,
,
. Opteron
,
,
, .
, , .

SMT
Linux SMT (Symmetric
Multithreading) Hyperthreading,
Pentium Xeon,
Intel. , Hyperthreading,
.
SMT
,
, , .

.
, MMU, TLB . , , ,
,
.
SMT
CISC.
CISC- ,
, RISC, .

.
Hyperthreading ( SMT)
- ,
, . ,
, ,

.
SMT MMU,
, .



. , ,

9.4.

375

,
.
,
.
, , ,
.
, Intel
AMD, .
(,
). , ,
, , MMU ..
, , ,
SMP,
.
-
, ,
. , ,
, .
,
.
, , ,
. , , .

9.4.2.

SMP
,
, .
.

. ,

,
.

Linux
SMP
Linux
SMP 2.0. SMP-
,
.
, ,
.
.

376

9.

SMP-
, ( TLB-
) ,
TLB-. ,
SMT, ,

MMU . ,
, ,
.
,
, .
( ),
. , ,
, .
, , .
,
, .
, ,
.

.

. Linux 2.6.7
SMP
SMT1. ,
, ,
.



schedutils taskset,
.
, . , ,
taskset ,

SMP, 0 :
$ taskset 1 ./myprogram

,
1

taskset p
:
$ taskset -p 1234
pid 1234's current affinity mask: 1
1

Processor Type and Features


( ).

377

9.4.

Linux
, root
, , .


, ,
Linux
.
.
,
, -
. Linux
, ,
.
, ,
.

API-

, , ,
root.
,
GLIBC:
int sched_setaffinity(pid_t
int sched_getaffinity(pid_t

pid,
pid,

size_t setsize,
size_t setsize,

cpu_set_t *cpuset);
cpu_set_t *cpuset);

0,
-1. cpu_set_t ,
, setsize . cpu_set_t
, ,
unsigned long.
.
:
CPU_ZERO(p)
CPU_SET(n,p)

, p
n ,
p
CPU_CLR(n,p) n ,
p
CPU_ISSET(n,p) , n ,
p

setaffinity
, cpu_set_t.
root,
. .
GLIBC API- POSIX
pthreads.

378

9.

API-
GNU- Native POSIX Threads Library (NPTL)
. ,
,
.
, , -
, .
POSIX pthreads
, , NPTL,
, _np (-POSIX).
:
int pthread_setaffinity_np(pthread_t tid, size_t setsize, cpu_set_t *cpuset);
int pthread_setaffinity_np(pthread_t tid, size_t setsize, cpu_set_t *cpuset);

,
tid-.
,
pthread_self tid.
,
. pthread_
attr_t :
int pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t setsize, cpu_set_t
*cpuset);
int pthread_attr_getaffinity_np(pthread_attr_t *attr, size_t setsize, cpu_set_t
*cpuset);

,
.
attr pthread_create.
, .

9.5.

. ,
SMP, ,

.

9.5.1. ,
,

PCI, ,
, ,
.

9.5.

379

, , ,
.
, , ,
.

9.5.2. ,
,


.
.
,
.
SMP (Symmetric Multiprocessing),
SMT (Symmetric Multithreading) ,
,
.

9.5.3. ,

gprof, gcov

.
OProfile ( ,
).
strace, ltrace
.
time, top, vmstat, iostat, mpstat ,
.

9.5.4. -
http://sourceforge.net/projects/procps procps,


.
http://sourceforge.net/projects/strace strace.

9.5.5.
DowdK. and Severance C. High Performance Computing, RISC Architectures,
Optimization & Benchmarks. Sebastopol, Calif.: OReilly Media, Inc., 1998.

10

10.1.

, Linux.
-
,
.
,
.
, ,
. , ,
,
.
, gdb
.
GUI,
gdb, -
. , GUI-
gdb.
,
GUI- .
,
, .
, ,
.

10.2. : printf
printf -
. , printf,
,
. ,
.

381

10.2. : printf

,
. ,
,
.

10.2.1. ,
printf
, printf,
, .
, , ,
printf .
,
: ,
.
,
, ,
.

printf
printf
.
- .
X,
, :
$ time od -v /dev/zero -N200000
,
0606440
0606460
0606500
real
user
sys

000000 000000 000000 000000 000000 000000 000000 000000


000000 000000 000000 000000 000000 000000 000000 000000
0m1.257s
0m0.052s
0m0.092s

, 1,257 ,
.
, , ,
' , . -
,
. , /dev/null,
:
$ time od -v /dev/zero -N200000 > /dev/null
real
0m0.059s
user
0m0.048s
sys
0m0.012s

382

10.

,
,
.

printf
IPC 7
, .
, ,
.
,
, , .
:
for (i = 0; i < 3; i++) {
printf("Hello World\n");
sleep(1);
}

Hello World
3 . ,
,
. tee.
:
$ ./hello | tee hello.out

3 , ,
Hello World.
printf, . ,
, .
/ ,
FILE*.
,
. ,
.
,
. ,
, .
,
, printf .
,
,

fflush.


,
.

10.2. : printf

383

.
,
, (, ).

.
,
,
, , .

, .
,
setvbuf, :
int setvbuf(FILE *stream, char *buf, int mode, size_t size);

steam ,
, , stdout. buf size
. ,
,
.
NULL buf 0 size.
mode,
:
_IONBF .
. ;
_IOLBF .
;
_IOFBF .
.
stdout (_IOLBF),
. stdout
( ),
(_IOFBF). ,
. -
.
,
.
setvbuf:
setvbuf(stdout,NULL,_IOLBF,0);

_IONBF,
,
.

fflush.
,

384

10.


. , , , ,
, .
fflush .
fflush ,
.


,
, . .10.1
, ,
, .
,
fflush
.
, , ,
fsync.
, ,
, , .
, ,

, .
.

. 10.1. /

10.2. : printf

385

,
. ,
NFS.

. ,
, .
, ,

fsync fdatasync ,
:
int fsync(int fd);
int fdatasync(int fd);


, .
, ,
1. ,
fdatasync ,
fsync .
,
, ,
fflush setvbuf, ,
.
fileno.

10.2.2.
printf
, , ,
, printf .
,
, .



. ,
,
printf,
,
. :
1

,
,
.
.

386
#ifdef DEBUG
#define DEBUGMSG(...) printf(__VA_ARGS__)

10.

/* C99 / GNU */

#else
#define DEBUGMSG /* nop */
#endif


GNU-, 99.
-GNU- , 99,
,
:
#define DEBUGMSG(msg) printf msg

/*
*/
/* ... */
DEBUGMSG(("Hello World %d\n",123));
/* printf("Hello World
%d\n",123); */

, .
,
.
,
.
printf
,
.
:
#define DEBUGMSG(fmt,...) printf("%s %d " fmt, __FILE__,__LINE__, ## __VA_ARGS__)

##, ,
GNU-.
, GNU
. __VA_ARGS__
, ,
. __VA_ARGS__ , gcc
.
,
.
, "%s %d"
fmt , .

ANSI (1989 .).
.
printf- .

#define PHEX(x)printf("%#10x %s\n", x, #x)
...

10.2. : printf

387

PHEX(foo);
PHEX(bar);
PHEX(averylongname);

, #
. ,
#, .
. ,
PHEX(averylongname);


printf("%#10x %s\n", averylongname, "averylongname");


. , ,
,
, :
0x1 foo
0x2 bar
0xdeadbeef averylongname

,

. .
. ,
,
,
.
:
#define DEBUGMSG(...) \
do { \
printf(__VA_ARGS__);\
fflush(stdout);\
} while(0)

, .
,
. , .
do/while(0)

Linux.
,
. , ,
. ,
:

388

#define EXITMSG(msg)

10.

printf(msg); exit(EXIT_FAILURE)

, ,
, . :
if ( x != 0 ) EXITMSG("x is not zero \n");

:
if ( x != 0 ) printf("x is not zero \n");
exit(EXIT_FAILURE);

exit .
, , . , :
#define EXITMSG(msg)

{ printf(msg); exit(EXIT_FAILURE); }


. ,
, :
if ( x != 0 )
EXITMSG("x is not zero \n");
else
printf("no problem \n");

. :
if (x != 0)
{ printf("x is not zero \n"); exit(1); };
else
/*
! */
printf("no problem \n");

, , if. else , ,
, .
, do/while. do/while , , .
( while) .
, 0 (false), . :
#define EXITMSG(msg) \
do { printf(msg); exit(EXIT_FAILURE); } while(0)

, , ,
. , , .

10.2. : printf

389

-
,
, .
.
.
, ,
. ,
,
.

.
,
, .
inline,
++, 99 gcc.
, .
,
,
. ,
, , ,
.
,
, ,
,
.
, .
, 1.
,
-, . printf
,
. API-
stdarg.h. :
inline int myprintf( const char *fmt, ... )
{
int n;
va_list ap;
va_list ,
API-
va_start(ap, fmt);
,
( fmt)
n = vprintf(fmt,ap);
vprintf va_list
va_end(ap);
,

return n;
}
1

gcc , -03
-finline-functions.

390

10.

,
,
printf. ,
, , va_list
.
printf- . ,
v: vprintf, vsprintf, vfprintf ..
- printf ,
.
,

. , printf
.
, GNU ,
.
printf ,
. -
.
printf
. gcc
. vprintf
stdarg-
- , va_list,
. , myprintf vprintf
, ,
.
GNU printf
__attribute__.
stdio.h printf . myprintf
:
inline int myprintf( const char *fmt, ... )
__attribute__ ((format (printf, 1, 2)));

__attribute__ .
,
printf 1,
2. ,
__attribute__ GNU-
,
.

,
printf
, printf,
, , ,
, .

10.2. : printf

391

,
, :
warning: int format, long int arg (arg 2)
warning: long unsigned int format, int arg (arg 2)

int long,
. ,
gcc, 32-
long int. printf
, int,
double. , printf
,
, ,
.
,
, , .
,
.
64- . off_t, , API-
POSIX, lseek mmap. 32-, 64-
. 32- off_t,
GNU 64-
:
-D_FILE_OFFSET_BITS=64

64-
, . ,
-
.

gcc:
gcc 3.x: warning: int format, different type arg
gcc 4.x: warning: format '%x' expects type 'unsigned int', but argument 2 has type 'off_t'


, 64- 32-.
long long gcc 64-
32- . , long long 11:
printf("%lld\n", x);

.
x86_64 gcc 64- long long long.
, printf ,
, .
IA32, , gcc int64_t 99
long long, x86_64 gcc long. ,
IA32

392

10.

printf "%lld",
x86_64 "%ld".
.
, 64- 32- . ,
double 64-. ,
float double ,
float double, .
float
, float double,
32-.
gcc:
gcc 3.x: warning: int format, double arg (arg 2)
gcc 4.x: warning: format '%d' expects type 'int', but argument 2 has type 'double'

, float,
, ,
, double.
. , ,
, ,
.
gcc:
gcc
gcc
gcc
gcc

3.x:
3.x:
4.x:
4.x:

warning:
warning:
warning:
warning:

format argument is not a pointer (arg 2)


char format, different type arg (arg 2)
format '%s' expects type 'char *', but argument 2 has type 'int'
format '%s' expects type 'char *', but argument 2 has type 'int *'

,
,
. ,
.
, ,
.



, ,
, .
.
,
. ,
, .
printf
.
, , ,
.

10.3. GNU- gdb

393

,
,
. , ,
,
grep.
,
.
.
,
, ,
, .

10.2.3.
printf
, printf .
, ,
. ,
. ,
, ,
printf. ,
printf .
printf ,
, .

10.3.
GNU- gdb
gdb ,
. Linux
.
.
. gdb
, ,
, gdb . , ,
sh gdb, :
(gdb) sh
Ambiguous command "sh": sharedlibrary, shell, show.

, Tab, ,
.
, Tab ,
, , :

394
(gdb) b<Tab><Tab>
backtrace break
(gdb) b

10.

bt

b,
, backtrace, break bt1. gdb
, . -
, gdb
.

10.3.1. gdb
gdb
gdb .
:
$ gdb ./hello ...
$ gdb
(gdb) file ./hello
Reading symbols from /home/john/hello...done.


.

gdb.
gdb,
run.
, , .
, ,
. ,
.
gdb
:
set args set
. gdb ,
run,

run. , ,
set args;
run .
,
set args. ,
,
;
1

, b break, bt
backtrace.

10.3. GNU- gdb

395

start run, ,
, main .

;
step - .
,
, gdb .
, run start;
next step, , gdb
, ,
;
kill .
kill, .
signal
.

10.3.2.

, Ctrl+C.
gdb ,
SIGINT,
, .
,
.
gdb .
:
break, tbreak (break)
(tbreak). ,

, ;
, .
(.
);
watch , ,

( ).
,
(.
);
continue ,
. continue
(N), gdb
N1. :

396

10.

,
N- ;
signal .

, .
info signal;
info breakpoints

;
delete . -
info breakpoints
delete.

,

breakpoint b,
. , ,
, :
(gdb) b
gdb) b foo
(gdb) b foobar.c:foo
(gdb) b 10
(gdb) b foobar.c:10
(gdb) b *0xdeadbeef





foobar.c


foo
foo,
10
10 foobar.c
0xdeadbeef

.
,
.



. ,
nasty, setlen buflen.
if , :
(gdb) b nasty if setlen > buflen
Breakpoint 1 at 0x804843e: file nasty.c, line 8.
(gdb) run 100
Starting program: /home/john/examples/ch-10/debug/nasty 100
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0xffffe000
Breakpoint 1, nasty (buf=0x804a008 "", setlen=100) at nasty.c:8
8
return memset(buf, 'a', setlen);

.
,

10.3. GNU- gdb

397

, .
:
(gdb) b nasty if len > buflen
No symbol "len" in current context.

len main
nasty, - gdb
.
++.
:
(gdb) b nasty if main::len > buflen
Breakpoint 1 at 0x804845e: file nasty.c, line 8.

,
++.

C++
, ++, .
, ,
. , gdb
, .
, 10.1.
- ,
.
, ,
. gdb Tab
,

. , ,
, , ,
Tab . annoy<Tab>,
.
10.1. cppsym.c: C++
1 // "" ,
2 // ,
//
3 //
4
5 namespace inconvenient {
6
void *annoyingFunctionName1(void *ptr) {
7
return ptr;
8
};
9
void *annoyingFunctionName2(void *ptr) {
10
return ptr;
11
};
12
void *annoyingFunctionName3(void *ptr) {
13
return ptr;

398
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

};

10.

};
void *annoyingFunctionName3(int x) {
return (void *) x;
};

// , 'using' gdb...
using namespace inconvenient;
int main(int argc, char *argv[])
{
annoyingFunctionName1(0);
annoyingFunctionName2(0);
annoyingFunctionName3(0);
annoyingFunctionName3((int) 0);
}

, ,
.
. info,
:
(gdb) info function annoy

,
annoy
All functions matching regular expression "annoy":
File cppsym.cpp:
( cppsym.cpp:)
void *inconvenient::annoyingFunctionName1(void*);
void *inconvenient::annoyingFunctionName2(void*);
void *inconvenient::annoyingFunctionName3(int);
void *inconvenient::annoyingFunctionName3(void*);


. , ,
, Tab . ,
: Tab
(inconvenient),
, ,
gdb , .

, Tab,
:
(gdb) b 'inc<Tab><Tab>
inconvenient
inconvenient::annoyingFunctionName1(void*)
inconvenient::annoyingFunctionName2(void*)
inconvenient::annoyingFunctionName3(int)
inconvenient::annoyingFunctionName3(void*)

Tab .
.

10.3. GNU- gdb

399

Tab ,
, , :
(gdb) b 'incon<tab>
(gdb) b 'inconvenient::<Tab>

b inconvenient
b
inconvenient::annoyingFunctionName
(gdb) b 'inconvenient::annoyingFunctionName3<Tab><Tab>
inconvenient::annoyingFunctionName3(int)
inconvenient::annoyingFunctionName3(void*)
(gdb) b 'inconvenient::annoyingFunctionName3(

, ,
Enter. :
(gdb) b 'inconvenient::annoyingFunctionName3(void*)'
Breakpoint 2 at 0x804836f: file cppsym.cpp, line 13.

Tab
.


,
. gdb
watchpoint.
,
.
,
, .
,
.

, foo,
:
(gdb) watch foo

foo

,
. foo 123
, , 123,
. , watch
foo . foo ,
, :
(gdb) watch *foo

,
foo

,
foo!
,
, , ,
. ,

400

10.

, .
( ) ,
.

.
, foo 123:
(gdb) watch foo if foo == 123

.
. 10.2
,
.
.
ovrrun, memcpy.
- ,
. memcpy
,
.
buflen .
,
1 800000.
,
malloc. ,
strlen, .
,
.
,
.
ovrrun, msglen
buflen.
:
(gdb) b ovrrun if msglen > buflen

, , .
, msglen
gdb. gdb msglen
buflen, ,
. ovrrun
800000 , , ,
. ,
Pentium 4 1,7 ,
overrun 700 , gdb
. ,
2 17 .
, ,
;

10.3. GNU- gdb

401

, gdb .
:
(gdb) watch buf[buflen]

,
buf+buflen, .
,
,
, . ,
800000 gdb .
:
watch ,
, ;
rwatch ,
;
awatch ,
.
gdb , .
info watchpoints,
info breakpoints. ,
, delete.
10.2. overrun.c:

1
2
3
4
5
6
7
8
9

#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<time.h>

//
const char text[] = "0123456789abcdef";
// ,
//
void ovrrun(char *buf, const char *msg, int msglen)
{
// memcpy
//
char dummy[4096];
memset(dummy, msglen, sizeof(dummy));

10
11
12
13
14
15
16
17
18
19 }
20
21
22

// ""...
memcpy(buf, msg, msglen);
// malloc .
// malloc
// ( mmap).

402

10.

23

// malloc ,
// , 1
24
//
25 const int buflen = 13;
26
27 int main(int argc, char *argv[])
28 {
29
char *buf = malloc(buflen);
30
int i;
31
32
// ,
// .
33
srand(time(NULL));
34
35
// .
36
int n = 800000;
37
38
// 1 N
39
// , .
40
int thresh = RAND_MAX / n;
41
42
for (i = 0; i < n; i++) {
43
// ,
//
44
int len = (rand() < thresh) ? buflen + 1 : buflen;
45
ovrrun(buf, text, len);
46
}
47
48
// , .
49
int overran = (strlen(buf) > buflen);
50
if (overran)
51
printf("OVERRUN!\n");
52
else
53
printf("No overrun\n");
54
55
free(buf);
56
return overran;
57 }

10.3.3.
gdb

. ,
, :
print
, ,
.
++;

10.3. GNU- gdb

403

x examine (); print,

, ,
print .
,
;
printf .
.
, , ;
whatis , gdb
;
backtrace ,
;
up, down ,

;
frame up down,
, .
, backtrace;
info locals info,
.

print

print x (print p).
print ++
1, x ,
,
. x ,
, .
, , ,
gdb .
print x ,
. x
. gdb ,
,
:
(gdb) p/x foo
$2 = 0x1000
(gdb) x/d &foo
0x22eec4:
1

foo
&foo

4096

gdb , , ++.
. info gdb languages support.

404

10.

.10.1.
10.1. , print x

print



ASCII-


double
float,
.
32- IEEE double g,
IEEE float w

NUL--



ASCII-.
NUL

, x ,
, ,
. ,
:
(gdb) x/8bx &foo
0x22eec4:

0x00

8
&foo
0x10
0x00
0x00
0xd8
0xef
0x22
0x00

x ,
.
,
.10.2. x b,
. print ,
.

10.3. GNU- gdb

405

10.2. , x

b
h
w
g


(8 )
(2 )
(4 )
(8 )

gdb
, ,
. ,
, . ,
, , 8bx
, 8xb.

print
.10.1 10.2, .
, print
. gdb ,
, - gdb:
(gdb) p getpid()
$1 = 12903
(gdb) p kill(getpid(),0)
$2 = 0
(gdb) p kill(getpid(),9)

pid-


API- ( gdb
)
Program terminated with signal SIGKILL, Killed.
The program no longer exists.
The program being debugged stopped while in a function called from GDB.
When the function (kill) is done executing, GDB will silently
stop (instead of continuing to evaluate the expression containing
the function call).
print
, x
, .
:
double dblarr[] = {1,2,3,4};
float fltarr[] = {1,2,3,4};
int
intarr[] = {1,2,3,4};

x print gdb:
(gdb) p intarr
$5 = {10, 20, 30, 40}

406
(gdb) x/4wx intarr
0x8049610 <intarr>:
0x00000028
(gdb) x/2gx intarr
0x8049610 <intarr>:

10.

32-
( )
0x0000000a
0x00000014
0x0000001e
64-
( )
0x000000140000000a
0x000000280000001e

x
,
. , , IEEE float
4 , 8-
(g) ,
. IEEE double
8,
, double:
(gdb) p fltarr
$7 = {10, 20, 30, 40}
(gdb) x/4wf fltarr
0x8049600 <fltarr>:
(gdb) x/2gf fltarr
0x8049600 <fltarr>:
(gdb) x/4gf dblarr
0x80495e0 <dblarr>:
0x80495f0 <dblarr+16>:

p -

w ,
sizeof(float)
10
20
30
40
g
float
134217760.5625 34359746808
g
double
10
20
30
40

,
, . ,
, .
, ,
, - .
,
. print '
, .
, ,

:
(gdb) p *intarr
$4 = 10
(gdb) p intarr[1]
$5 = 20
(gdb) p intarr[1]@2
$6 = {20, 30}

,



@
, 1

10.3. GNU- gdb

407

.
NUL- ASCII, .
:
const char ccarr[] = "This is NUL terminated.\0Oops! you shouldn't see this.";
const char *ccptr = ccarr;
ccarr , NUL- ASCII. ccarr . ,
print , ,
x /s
:
(gdb) p ccarr
NUL- ASCII
$1 = "This is NUL terminated.\000Oops! you shouldn't see this".
(gdb) p ccptr
char NUL-
$2 = 0x8048440 "This is NUL terminated".
(gdb) x/s ccarr
/s
x ,
NUL-
0x8048440 <ccarr>:
"This is NUL terminated".

print

, :
(gdb) p (char*) ccarr
$3 = 0x403040 "This is NUL terminated."

, , , -
,
i x:
(gdb) x/10i main
0x401050 <main>:
0x401051 <main+1>:
0x401053 <main+3>:
0x401056 <main+6>:
0x401059 <main+9>:
0x40105e <main+14>:
0x401061 <main+17>:
0x401064 <main+20>:
0x401067 <main+23>:
0x40106a <main+26>:

push
mov
sub
and
mov
add
add
shr
shl
mov

%ebp
%esp,%ebp
$0x28,%esp
$0xfffffff0,%esp
$0x0,%eax
$0xf,%eax
$0xf,%eax
$0x4,%eax
$0x4,%eax
%eax,0xffffffe4(%ebp)

, ,
.

gdb
gdb ,
. ,
,

408

10.

, .
,
.

.
print, kill
print. ,
call:
(gdb) call getpid()
$1 = 27274
$1 , gdb
.
gdb ( ). gdb
, .
. getpid
kill, :
(gdb) call kill($1,0)
$2 = 0


, set.
, , gdb,
.
gdb
,
, set. ,
set
.

, C++
++ ,
. ,

. , :
template <class Typ>
void swapvals( Typ &a, Typ &b)
{
Typ tmp = a;
a = b;
b = tmp;
}

Tmp .
-
, . ,

10.3. GNU- gdb

409

, Typ. .
, double,
:
swapvals<double>(a,b);

swapvals,
double.
int, swapvals<int>,
.
,
. ,
gdb- info functions:
(gdb) info func swapvals
All functions matching regular expression "swapvals":
File templ.cpp:
void void swapvals<Foo>(Foo&, Foo&); gdb 6.3 prints 'void' twice, for some
reason.
void void swapvals<double>(double&, double&);
void void swapvals<int>(int&, int&);

,
. ,
, , .
.
, int,

:
(gdb) b 'void swap<Tab>
(gdb) b 'void swapvals<<Tab>
(gdb) b 'void swapvals<int>(int&, int&)'
Breakpoint 3 at 0x8048434: file templ.cpp, line 8.

,
- , rbreak.
,
, :
(gdb) rbreak swapvals
Breakpoint 2 at 0x8048456: file templ.cpp, line 8.
void void swapvals<Foo>(Foo&, Foo&);
Breakpoint 3 at 0x8048400: file templ.cpp, line 8.
void void swapvals<double>(double&, double&);
Breakpoint 4 at 0x8048434: file templ.cpp, line 8.
void void swapvals<int>(int&, int&);

,
, ,
, . .

410

10.

,
C++
++ ANSI,
,
. STL (Standard Template Library)
++, -
STL.
STL
. ,
.

, . ,
, .
, ++
.
. ,
, gdb.
.
++: vector.
vector ,
.
int:
int myarray[3];
std::vector<int> myvector(3);

,
++ vector,

,
, , , whatis:
(gdb) whatis myarray
type = int [3]

(gdb) whatis myvector
type = std::vector<int,std::allocator<int> > ,

++ size,
, .
gdb , :
(gdb) p myvector.size()
$1 = 3

: ++
, ().
, size,
size
. :

10.3. GNU- gdb

411

(gdb) p myvector.size()
Cannot evaluate function -- may be inlined

,
, .
,
.
,
. ,
,
vector<int>::size vector<float>::size.

display
display ,
.
,
, x.

10.3.4.
gdb
,
gdb :
$ gdb programname pid
pid ,
, programname .
,
.
, ,
, - .
,
gdb .
,
detach. ,
. gdb
attach.
file ,
.
, gdb,
, :
(gdb)file programname
(gdb)attach pid

, ,
, file
gdb , .

412

10.

10.3.5.
,
, .

.
:
SIGSEGV . ,
,
, ,
, ,
;
SIGFPE . ,
.
SIGFPE x86,
;
SIGABRT . abort assert;
SIGILL .
,
;
SIGBUS . .
, ,
, .
,
.
, (
), ,
.
:
$ gdb exec-filename core-filename

core,
,
, core.pid1.
,
gdb ,
. gdb
, , :
$ gdb seldomcrash core.27078
...
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0xffffe000
Core was generated by './seldomcrash'.
Program terminated with signal 8, Arithmetic exception.
1

sysctl -w kernel.core_uses_pid = 0.

10.3. GNU- gdb

413

...
#0 0x0804836c in main (argc=1, argv=0xbfc9acd4) at seldomcrash.c:10
10
return someint / 0;
, !
(gdb)

,
gdb file,
core-file.
,
gdb
,
.
.
, ,
, , , gdb .
, , ,
, backtrace,
, gdb no stack.

.
memset memcpy ,
. , ,
,
.
scanf, read .
, 10.3,
, .
,
1.
10.3. factorial.c: ,
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 // .
6 //
7 int factorial(int x)
8 {
9
int overflow;
10
static int depth = 0;
11
12
if (x <= 1)
13
return 1;
14
1


, SIGSEGV,
.

414
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

10.

if (++depth > 6) {
// n .
// SIGSEGV, ,
int n = 0x100;
memset((char *) &overflow, 0xa5, sizeof(overflow) + n);
}
}

return x * factorial(x - 1);

int main(int argc, char *argv[])


{
int n = 3;
printf(" > 7 \n");
if (argc > 1) {
n = atoi(argv[1]);
}
printf("%d! == %d\n", n, factorial(n));
return 0;
}

,
7 . 256 ,
.
:
$ ./factorial 7
Command line argument > 7 will cause stack corruption
7! == 5040
$ ./factorial 8
Command line argument > 7 will cause stack corruption
Segmentation fault (core dumped)
!
$ gdb ./factorial core
GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
...
Core was generated by `./factorial 8'.
Program terminated with signal 11, Segmentation fault.
...
#0 0xa5a5a5a5 in ?? ()

(gdb) bt
,
#0 0xa5a5a5a5 in ?? ()
backtrace
#1 0xa5a5a5a5 in ?? ()
#2 0xa5a5a5a5 in ?? ()
#3 0xa5a5a5a5 in ?? ()
...

#21 0xa5a5a5a5 in ?? ()

#22 0xa5a5a5a5 in ?? ()
---Type <return> to continue, or q <return> to quit---q
Quit
(gdb)p factorial::depth

$1 = 7
-

10.3. GNU- gdb

415

,
gdb, , ,
. ,
backtrace (bt) . -
.
.
- .
depth, factorial,
++: factorial::depth.
, , factorial 7 .
, .
, .
, ,

.
, .

10.3.6.
gdb
.
gdb ,
.
,
, .
gdb ,
, , .
, .
, gdb
.
, .
info threads .
:
(gdb) info threads
4 Thread -1225585744 (LWP
at thread-demo.c:17
3 Thread -1217193040 (LWP
at thread-demo.c:17
2 Thread -1208800336 (LWP
at thread-demo.c:17
* 1 Thread -1208797504 (LWP
at thread-demo.c:45

6703) 0x0804854c in the_thread (ptr=0x2)


6702) 0x0804854c in the_thread (ptr=0x1)
6701) 0x0804854c in the_thread (ptr=0x0)
6698) main (argc=1, argv=0xbf940e84)

gdb
. , gdb
.
,

416

10.

. pthread_t,
API- pthreads.
gdb
. ,
gdb.
thread gdb-
.
, ,
. , ,
, gdb .
,
thread break. ,
foo 3, :
(gdb) break foo thread 3

, gdb , ,
, .
.
gdb- scheduler-lock

step next , ,
. ,
, .
, .
gdb . ,
gdb .
,
, set scheduler-lock.
:
set scheduler-lock off
set scheduler-lock on
set scheduler-lock step

off, . on
, gdb . ,
continue.
step step,
next, , next.

10.3. GNU- gdb

417

10.3.7.

gdb ,
,
. ,
- , , .
,
, .
,
, .
,
.
:

, ,
.
, ,
. ,
, . ,
, .
, volatile;

,
, . ,
.
,
gcc -fno-inline;

. ,
,

, ,
.
. gdb
, ,
gdb, objdump.
:
1 void vscale2(double *vec, int len, double arg)
2 {
3
int i;
4
5
// , ...
6
double a, b, c;
7
for (i = 0; i < len; i++) {

418
8
9
10
11
12
13
14
15 }

10.

// ,
a = arg * arg;
b = arg * a / 2;
c = a * b * arg;
vec[i] *= c;
}

, 6, ,
.

.
objdump ,
( ) ,
, ,
.
objdump .
,
, .
,
, .

10.4.

-
, .
Windows Microsoft
, UNIX- . ,
,
. ,
,
.
.

10.4.1.

Linux-
,
. ldd1.
Hello World:
1

ldd list dynamic dependencies .

10.4.

419

$ ldd hello
linux-gate.so.1 => (0xffffe000)
libc.so.6 => /lib/libc.so.6 (0xb7e32000)
/lib/ld-linux.so.2 (0xb7f69000)

, libc.so,
/lib/libc.so.6.
,
/lib/ld-linux.so.2 . linux-gate.so.1
, Intel.

sysenter sysexit, .

, .

.
,
, ,
, .

. ,
16- ,
. ,
, POSIX
API- . , ,
man dlopen(3).

10.4.2.

,
,
main. .
,
,
.
; ldd,
.

,
-shared fpic, :
$ cc -shared -fpic -o libmylib.so mylib1.c mylib2.c

-shared
, . fpic
, -

420

10.

. , ,
,
.

:
$ cc -o myprog myprog.o -L . -lmylib

-L ,
.
, ld-linux.so ,
. ,
:
$ ./myprog
./myprog: error while loading shared libraries: libmylib.so: cannot open shared
object file: No such file or directory

, ,
. , ,
. ,
. ,
-
.
soname
,
. , , soname,
. ,
, soname.

10.4.3.


ldlinux.so, /lib.
/lib
/usr/lib.
, LD_LIBRARY_PATH.
.

, soname,
/etc/ld.so.cache.

/sbin/ldconfig, , /etc/ld.so.conf.

ldconfig, . ,
,

421

10.4.

soname.
hello , Fedora Core 3,
libc.so.6 libc-2.3.6.so:
$ ls -l /lib/libc.so.6
lrwxrwxrwx 1 root root 13 Jul

2 16:03 /lib/libc.so.6 -> libc-2.3.6.so

libc.so.6 soname,
, libc-2.3.6.so ,
GNU C (glibc). ,
glibc , libc.so.6.
.
soname ,
.
glibc GNU-,
glibc.

10.4.4.

LD_LIBRARY_PATH ,
. myprog:
$ LD_LIBRARY_PATH=./ ./myprog


,
libmylib.so. , soname,
,
LD_PRELOAD :
$ LD_PRELOAD=libc.so.6 ./hello-world

libc.so.6
soname glibc


libc , hello-world.
,
, libc.
LD_PRELOAD ,
soname, /etc/ld.so.cache.

10.4.5.

, ,
, .
,

422

10.

, libc .
, ,
root. -
,
.
, setuid root,
,
. ,
, root.
, ,
. -
,
. ,
, ,
, .
,
, ,
. , , ,
(LD_LIBRARY_PATH ),

root, 1.

10.4.6.

Linux
. man
ld.so(8),
ld-linux.so.2.

, ,
.
man ld.so(8),
ldd, - ld-linux.so.2
.

,

ldd - ,
,
:
$ ldd hello
linux-gate.so.1 => (0xffffe000)
1

ld.so(8).

10.4.

423

libm.so.6 => /lib/libm.so.6 (0xb7f1b000)


libpthread.so.0 => /lib/libpthread.so.0 (0xb7f09000)
libc.so.6 => /lib/libc.so.6 (0xb7de0000)
/lib/ld-linux.so.2 (0xb7f4e000)

, ,
. , ,
. Hello World
, pthread,
:
$ gcc -o hello hello.c -lm -lpthread

,
. ,
.
, ,
. ,
.
,
, .
ldd,
. , libpthread.so libm.so
, ? u,
, 1:
$ ldd -u ./hello
Unused direct dependencies:
/lib/libm.so.6
/lib/libpthread.so.0


?
,
,
.
, .

, , ,
, .

, . , ,
,
. ,
, . ,
1

, u man,
ldd, ,
help.

424

10.

, . ,
, .
,
,
, .
32- ,
.



, ,
, - .
nm objdump
. , readelf.
, ,
, , -
.
,
soname . , ldconfig
.
, soname
.
ldconfig p.
soname - ()
, DYNAMIC.
nm , objdump readelf:
$ objdump -x some-obj-1.0.so | grep SONAME
SONAME
libmylib.so
$ readelf -a libmylib.so |grep SONAME
0x0000000e (SONAME)
Library soname: [libmylib.so]

,
.
.
. ++
,
, .
,
nm .
:
$ nm -uA *.o | grep foo

u
. A
,
grep, ,

10.5.

425

. ++, C,
()
.
, :
int foo(char p);
int foo(unsigned char p);

++ ,
,
1.
, , u.
C , ,
() :
$ nm -gCA lib*.a | grep foo
libFoolib.a:somefile.o:00000000 T foo(char)
libFoolib.a:somefile.o:00000016 T foo(unsigned char)

, objdump readelf
. nm
objdump :
$ objdump -t
objdump C
() .
readelf :
$ readelf s

nm objdump, 2.15.94 readelf



() .
binutils.

10.5.
,
.
, ,
. ,
, . ,
, . glibc
,
.
1

, const,
.

426

10.5.1.

10.

,
. ,

9 II

, .
SIGSEGV

, . .
,
.

, ,
, 11 ,

free. ,

, 9II
.

, SIGSEGV,

11

free!

9 II
,

TecTB. .

10.5.2.

,
, .

-
.

. ,

, -
- ,
. -,
, ,
.

, ,

, . ,
, ,

. T

32- ),

- .
, ,

,
( ).
--
, .

10.5.

427

, , - -
,

. , .
, .

10.5.3.

,
,
, .

,
, , ,

SIGSEGV.
: ,
.

.
, :
, . ,

,
, .

.
, ,

fl awfl nder 1

Python, ,


.
,

, ,

.
,

.
~>>-, . ,
,

.
, ,
.

, ,
, ,

.
. , ,

SI GSEGV,
. www.dwheeler.comjflawfinder.

428

10.

, ,

,
.


,
cTB . II

( ),

.
,
, , , .
,
.
, ,

SIGSEGV.
,
. ,
, .

.

,
.
- .
.

, .
,
, , II

free

.
, II

free.

free,

, ,
. ,
,

S IGSEGV.


++. ,

new de l ete -

,
.

GNU - new

de l ete

malloc

free

, , ,

de l ete. ++

. ,
.

new de l ete

, .


; .

10.5.

429

10.5.4.

glibc

GNU (9 II )

,
MALLOC _ _.

,
.
,

9 II

9 II

MALLOC_CHECK

MALLOC__

MALLOC_CHECK_=0 -

MALLOC_CHECK_=l - stderr ;
MALLOC_CHECK_=2 -

; .

MALLOC _ _ ,
9 II , .
,

2.

-
. ,
, , MALLOC _ _

2.

mtrace

mtrace - , 9 II ,

Fedora 9 II -utll s.

.
.

mtrace - .

, , mtrace

9 II , .
mtrace,

mtrace muntrace 9 II .
, MALLOC _
, 9II ,
mt race.
.
, ,

mtrace:

$ MALLOC TRACE=foo.dat ./ex-mtrace

mtrace
foo. dat

430

10.

leaking 0x603 bytes


leaking 0x6e2 bytes
leaking 0x1d8 bytes
( 0x1d8 )
( 0xd9f )
leaking 0xc3 bytes
leaking 0x22f bytes
$ mtrace ./ex-mtrace foo.dat
Memory not freed:
----------------Address
Size
0x0804a378
0x603
0x0804a980
0x6e2
0x0804b068
0x1d8
0x0804b248
0xd9f
0x0804bff0
0xc3
0x0804c0b8
0x22f

at
at
at
at
at
at

mtrace

Caller
/home/john/examples/ch-10/memory/ex-mtrace.c:23
/home/john/examples/ch-10/memory/ex-mtrace.c:23
/home/john/examples/ch-10/memory/ex-mtrace.c:23
/home/john/examples/ch-10/memory/ex-mtrace.c:23
/home/john/examples/ch-10/memory/ex-mtrace.c:23
/home/john/examples/ch-10/memory/ex-mtrace.c:23

mtrace ++, .
++,
. ,
, mtrace malloc, new. malloc
++,
.


memusage
memusage,
. Fedora
glibc-utils. ,
, .
,
ASCII-. :
$ memusage awk 'BEGIN{print "Hello World"}'


awk

Hello World
Memory usage summary: heap total: 3564, heap peak: 3548, stack peak: 8604
total calls
total memory
failed calls
malloc|
28
3564
0
realloc|
0
0
0 (in place: 0, dec: 0)
calloc|
0
0
0
free|
10
48
Histogram for block sizes:
0-15
21 75% ==================================================
16-31
3 10% =======
32-47
1
3% ==

431

10.5.

48-63
112-127
3200-3215

1
1
1

3% ==
3% ==
3% ==

, glibc-utils, memusage
man info. --help
memusage,
, mmap munmap
malloc free:
$ memusage --help
Usage: memusage [OPTION]... PROGRAM [PROGRAMOPTION]...
Profile memory usage of PROGRAM.
-n,--progname=NAME

-p,--png=FILE
PNG

-d,--data=FILE


-u,--unbuffered

-b,--buffer=SIZE

--no-timer

-m,--mmap
mmap
-?,--help

--usage

-V,--version

The following options only apply when generating graphical output:
-t,--time-based

-T,--total

--title=STRING

-x,--x-size=SIZE

-y,--y-size=SIZE

Mandatory arguments to long options are also mandatory for any corresponding
short options.
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

memusage
PNG1,
.10.2.
memusagestat PNG- ,
memusage d.
.
1

PNG Portable Network Graphics,


.

432

10.

. 10.2. , memusage

10.5.5. Valgrind

9 Valgrind1
,
. ,
valgrind -tool. valgrind
:
$ valgrind --tool=memcheck ./myprog

Valgrind ,
, .
. Valgrind
.
,
, .
, ,
.
1

. www.valgrind.org.

10.5.

433

Valgrind
valgrind ,
.

--leakcheck=full. 10.4
, Valgrind.
10.4. leaky.c:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *possible_leak(int x)
{
// valgrind ,
static char *lp;
char *p = (char *) malloc(x);
lp = p + x / 2;
return p;
}
int main(int argc, char *argv[])
{
//
char *p1 = malloc(0x1000);
//
char *p2 = possible_leak(0x1000);
return 0;
}

, Valgrind, .
10.4 p1 , .
,
. possible_leak.
, -
. ,
, ,
. Valgrind ,
:
$ valgrind --quiet --leak-check=full ./leaky
==22309==
==22309== 4,096 bytes in 1 blocks are possibly lost in loss record 1 of 2
==22309==
at 0x40044C9: malloc (vg_replace_malloc.c:149)
==22309==
by 0x804838D: possible_leak (leaky.c:9)
==22309==
by 0x80483E8: main (leaky.c:20)
==22309==
==22309==
==22309== 4,096 bytes in 1 blocks are definitely lost in loss record 2 of 2

434
==22309==
==22309==

10.

at 0x40044C9: malloc (vg_replace_malloc.c:149)


by 0x80483D5: main (leaky.c:17)

, possible_
leak .
, , .

Valgrind
Valgrind
. , ,
1 ,
glibc. 10.5 ,
.
10.5. new-corrupt.cpp: ++
1 #include <string.h>
2
3 int main(int argc, char *argv[])
4 {
5
int *ptr = new int;
6
memset(ptr, 0, sizeof(int) + 1);
// 1
7
delete ptr;
8 }

valgrind,
:
$ valgrind --quiet ./new-corrupt
==14780== Invalid write of size 1
==14780==
at 0x80484B9: main (new-corrupt.cpp:6)
==14780== Address 0x402E02C is 0 bytes after a block of size 4 alloc'd
==14780==
at 0x4004888: operator new(unsigned) (vg_replace_malloc.c:163)
==14780==
by 0x80484A9: main (new-corrupt.cpp:5)

, Valgrind
.
.

massif
massif Valgrind
.
. , 10.6,
funalloc , .
10.6. funalloc.c:
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 void func1(void)

435

10.5.

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

{
}

malloc(1024);

void func2(void)
{
malloc(1024);
}

//

//

int main(int argc, char *argv[])


{
srand(0);
int i;
for (i = 0; i < 256; i++) {
int r = rand() % 100;
if (r > 75) {
func2(); // 25%
}
else {
func1(); // 75%
}
//
usleep(1);
}
}

funalloc
, func1
75% .
, , func1
75% .

:
$ valgrind --tool=massif ./funalloc

: massif.PID.ps massif.PID.txt.
, :
Command: ./funalloc
== 0 ===========================
Heap allocation functions accounted for 97.9% of measured spacetime
Called from:
( :)
73.5% : 0x8048432: func1 (funalloc.c:8)
24.4% : 0x804844A: func2 (funalloc.c:13)
( )

,
.10.3.

436

10.

.
.
,
.

. 10.3. ,
massif

Valgrind
Valgrind-
,
. ,
.

. --log-file.
Valgrind .
.
.
,
, Valgrind
, .
memcheck, massif cachegrind, callgrind
helgrind. callgrind ,
gprof, cachegrind.
helgrind ,
. ,
,
.

437

10.5.

10.5.6.
Electric Fence
Electric Fence
, ,
glibc, .
Valgrind ,
Electric Fence ,

Memory Management Unit (MMU). MMU-
, Electric Fence
.
Electric Fence,
.
,
. - ef
LD_PRELOAD
. Valgrind, , ,
ef. 10.5,
Electric Fence, :
$ ef ./new-corrupt
Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
/usr/bin/ef: line 20: 23227 Segmentation fault
(core dumped) ( export
LD_PRELOAD=libefence.so.0.0; exec $* )

gdb
, , .
, gdb
Electric Fence .
,
Electric Fence:
$ g++ -g -o new-corrupt new-corrupt.cpp -lefence

libefence.a

gdb
LD_PRELOAD
gdb, :
$ gdb ./new-corrupt
...
(gdb) set environment LD_PRELOAD libefence.so
(gdb) run
Starting program: /home/john/examples/ch-10/memory/new-corrupt
Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0xffffe000
Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>

438

10.

Program received signal SIGSEGV, Segmentation fault.


0x080484b9 in main (argc=1, argv=0xbfb8a404) at new-corrupt.cpp:7
7
memset(ptr,0,sizeof(int)+1);
(gdb)

,
. ,
gdb Electric Fence ef.
, , Electric Fence 2.2.2,
ef, , gdb
6.3 0 malloc:
$ ef gdb ./new-corrupt
Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
ElectricFence Aborting: Allocating 0 bytes, probably a bug.
/usr/bin/ef: line 20: 23307 Illegal instruction
(core dumped) ( export
LD_PRELOAD=libefence.so.0.0; exec $* )

, EF_ALLOW_MALLOC
, 0, libefence
0 . Electric Fence
,
man efence(3).
Electric Fence
, . ,
, .
, :
char *buf = malloc(1024);
...
char *ptr = buf + 10;
,
ptr
...
*(ptr - 11) = '\0';
, : !

Electric Fence,
EF_PROTECT_BELOW
:
$ EF_PROTECT_BELOW=1 ef ./underrun
Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
/usr/bin/ef: line 20: 4644 Segmentation fault (core dumped) ( export LD_
PRELOAD=libefence.so.0.0; exec $* )

, ,
. SIGSEGV,
, .
Electric Fence ,
, ,

10.6.

439

SIGSEGV.

,
, . ,
,
.

. 1
,
10.2. Electric Fence
.
, ,
Electric Fence
, SIGSEGV,
. ,
Electric Fence, ,
. Electric Fence
,
.

10.6.

,
, .

, .

10.6.1.
, , ,
.
.

.
, ,
.
,
.
.
,
, .
,
.

440

10.

, ,
.

. ,
, , , ,
.
10.7 ,
(
).
10.7. trace-buffer.c:
1
2
3
4
5
6
7
8
9
10
11

#include
#include
#include
#include

<stdio.h>
<string.h>
<stdlib.h>
<stdarg.h>

// .
//
char tracebuf[4096] = "";
char *mstart = tracebuf;

// printf- .
// GNU __attribute__
12 //
13 int dbgprintf(const char *fmt, ...)
14
__attribute__ ((__format__(__printf__, 1, 2)));
15
16 // printf-
17 int dbgprintf(const char *fmt, ...)
18 {
19
int n = 0;
20
21
// ref. stdarg(3)
22
va_list ap;
23
va_start(ap, fmt);
24
25
// , snprintf
26
int nchars = sizeof(tracebuf) - (mstart - tracebuf);
27
28
if (nchars <= 2) {
29
//
30
mstart = tracebuf;
31
nchars = sizeof(tracebuf);
32
}
33
34
//
35
n = vsnprintf(mstart, nchars, fmt, ap);

10.6.

36
37
38
39
40 }
41
42 int
43 {
44
45
46
47
48
49

mstart += n + 1;
va_end(ap);
return n;
defective(int x)
int y = 1;
dbgprintf("defective(%u)", x);
if (x == 10) {
dbgprintf("time to corrupt the stack!");
// 128 (
// )
memset(&y, 0xa5, sizeof(y) + 128);

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

441

// , ,
//
dbgprintf("I'm still here; returning now.");
return 0;
}
return defective(x + 1);
}
int main(int argc, char *argv[])
{
defective(1);
dbgprintf("exiting...");
return 0;
}

dbgprintf printf- ,
,
. ,
, ,
. ,
, .
,
.
,
. 10,
. ,
. ,
. :
$ cc -g -o trace-buffer trace-buffer.c
./trace-buffer
Segmentation fault (core dumped)

442
$ gdb ./trace-buffer core.25347
...
#0 0xa5a5a5a5 in ?? ()

10.


!
bt (backtrace)

(gdb) bt
#0 0xa5a5a5a5 in ?? ()
#1 0xa5a5a5a5 in ?? ()
...

#22 0xa5a5a5a5 in ?? ()
---Type <return> to continue, or q <return> to quit---q
Quit
(gdb) x/15s &tracebuf
15 ,

tracebuf
0x8049720 <tracebuf>:
"defective(1)"
0x804972d <tracebuf+13>:
"defective(2)"
0x804973a <tracebuf+26>:
"defective(3)"
0x8049747 <tracebuf+39>:
"defective(4)"
0x8049754 <tracebuf+52>:
"defective(5)"
0x8049761 <tracebuf+65>:
"defective(6)"
0x804976e <tracebuf+78>:
"defective(7)"
0x804977b <tracebuf+91>:
"defective(8)"
0x8049788 <tracebuf+104>:
"defective(9)"
0x8049795 <tracebuf+117>:
"defective(10)"
0x80497a3 <tracebuf+131>:
"time to corrupt the stack!"
0x80497be <tracebuf+158>:
"I'm still here; returning now."
0x80497dd <tracebuf+189>:
""
0x80497de <tracebuf+190>:
""
0x80497df <tracebuf+191>:
""
(gdb)

,
defective, .
, gdb.

, .
, ,
,
. ,
. ,
strings,
ASCII-
, .
, 10.7, , .
, ,
. ,
. ,
, , .

.

10.6.

443

10.6.2.


gdb. ,

, . ,
, ,
. gdb
gstack,
, .
, :
$ gstack pid

gstack ,
,
gdb .
gstack , ,
, ,
.

10.6.3.

,
,
, .
, . ,
, , ,
.
.
,
.
bash, :
$ ulimit -c unlimited

, , /etc/rc.local,
, 6, ,
.

abort assert

. , ,
SIGABRT. abort :
raise(SIGABRT);

444

10.

, ,
(, )
.
exit, .
abort :
if ( ! program is sane ) abort();

ANSI C assert,
. assert ,
. false, abort.
,
:
assert(!insane);

insane true, :
$./foo
assert: foo.c:7: main: Assertion '!insane' failed.
Aborted (core dumped)

assert
NDEBUG. ,
, ,
. , ,
assert. :
.

gcore
,
. gdb
.
gdb gcore.
gdb, core.pid
. gdb
gcore,
gdb gcore;

.
gcore
,
o.

10.6.4.
,
. , ,
,

10.6.

445

SIGBART1. ,
.
,
(SIGUSR1, SIGUSR2)
,
.

SIGSTOP SIGCONT. , ,
SIGSTOP
, .
, :
raise(SIGSTOP);

Ctrl+Z
. ,
, , SIGSTOP:
$ kill -STOP pid

, SIGCONT,
. :
$ kill -CONT pid

, , , . ,
, , /proc.

10.6.5. procfs

, procfs ,
/proc.

; .
,
, .
/proc/self,
. ,
/proc/self, ,
.

ps, , procps.
, ,
1

,
SIGSEGV SIGBUS,
.

446

10.

,
/proc, .
, ,
, procfs.
,
.
,
- - .
, .


pmap,
- .
, . ,
, malloc mmap ,
.
, .
, , pmap,
/proc/PID/maps. pmap
.
procfs . consult proc(5).


,
, ,
. /proc/PID/
environ ,
NUL-.
strings:
$ strings -n1 /proc/PID/environ
MANPATH=:/home/john/usr/share/man
HOSTNAME=redhat
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000 ...

, ,
.
-n1 strings ,
4 .
1 , .
/proc/cmdline,
.
, argv , :
$ strings -n1 /proc/self/cmdline | cat -n
1 strings

argv[0]

447

10.7.

2
3

-n1
/proc/self/cmdline

argv[1]
argv[2]

- , 1
argv[0], .


/proc
. /proc/PID/exe
, , :
$ ls -l /proc/self/exe
lrwxrwxrwx 1 john john 0 Jul 30 11:53 /proc/self/exe -> /bin/ls

/proc/PID/cwd
.
, .
, ,
procfs .
, , ,
/proc/PID/fd. ,
.
, .
, readlink
, , :
$ ls -l /proc/self/fd |
total 4
lrwx------ 1 john john
l-wx------ 1 john john
lrwx------ 1 john john
lr-x------ 1 john john

tee /dev/null
64
64
64
64

Jul
Jul
Jul
Jul

30
30
30
30

12:00
12:00
12:00
12:00

0
1
2
3

->
->
->
->

/dev/pts/3
pipe:[33209]
/dev/pts/3
/proc/5487/fd

, 0 2 (
)
. tee, 1
pipe:[33209]. , , ,
33209 .
3 . , /proc/self/fd,
/proc/5487/fd.
6 lsof fuser,
.
, ,
ls /proc.

10.7.
,
.

448

10.

printf ,
, .
, GNU- gdb
, .
, gdb
, ,
.
, gdb- ,
++.
,
, . ,

, ,
.
, gdb.

, ,
. ,
.

10.7.1. ,

Electric Fence

,
Memory Management Unit.
gdb GNU-, .
mtrace glibc
.
Valgrind ,
.

10.7.2. -
http://duma.sourceforge.net ,

Electric Fence.
http://perens.com/FreeSoftware/ElectricFence

Electric Fence.
www.valgrind.org Valgrind.

10.7.3.
RobbinsA. GDB Pocket Reference. Sebastopol, Calif.: OReilly Media, Inc., 2005.