You are on page 1of 193

. . .

. .

(. . ).

, 2006 , , .
.
,
,

http://www.stolyarov.info

2006


 , 2006 , , - . ,
, , , .
,
, . ,
, , , ,
, .
, ,
:
1. ( )
,
;
2. ,
,
: (1)
PDF, , , , ,
; (2)

, -

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

681.3.06
32.973-018.2
81

-

. . .

. .
81 : . .:

. . . ( 05899 24.09.2001 .), 2006. 192 .


ISBN 5-89407-246-8
 ,   . . . III . , ,
.
Unix.
681.3.06
32.973-018.2

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

ISBN 5-89407-246-8

c . . , 2006

. 1:

1
1


, . 1.
, (
) .

: Microsoft Windows (Windows 95, Windows
98, Windows NT, Windows 2000 Windows XP), Unix
(SunOS/Solaris, FreeBSD, NetBSD, AIX, Linux ..), (VMS,
OS/360, Plan9, THE, MacOS...).
,
.
, ,
,
.
, ,
. : ,

MSDOS Windows3.1. Unix
, .
3

() ,
. ,
.
,
, .
,
. ,
, Unix.
:
-, Unix ()
; ,
, ,
, , , ,
, Windows.
-, ,
Unix, ; , ,
BSD Unix,
Windows.
-, Unix ,
. ,
, XWindow, .
, , Unix,
, ,
.
, Unix
; Unix
.
4

2
2.1

, 1623 .
 ,
, .   .
.
, 1960 .
, :
(XVI .), .
, .
, , , 1645 .
1642 19 . ;
.
,
. , . , ,
.
, , , , . ,
, .
1820 ,
. , , ; - ,
, .
(17921871) 1823 .

. . , .
5

1830 , , , .
. , . ,
1834 ,
, ,
. .
, .
17000
(
). ,
.
  . ,
,
.
, 1842 . 1843 , .
,
.
; , .
Ada .

2.2

1930
. , 1937 Z1, , . ,
Z2, . 1941 Z3, . 2200 ,
5-10 22 . Z3 .
6

,
1937 ; , .
Z3 1944
. Zuse Apparatebau. ,
Z4 . 1960 Z3
.
1950 Z4 ,
.
-,
, -, -.

, ,
, .
, Bell Labs .
1938 . 1940 ,
. ,
. , ,
.

2.3

()

1938 (
)
, . ,
, ABC (Atanaso Berry Computer),
- 1 .
. ,
Colossus (1943), . Colossus ; 1

ENIAC; , ENIAC

30 ,
.
ENIAC,
. . ENIAC
.
1946 , ; ,
ENIAC .
ENIAC .
ENIAC ; 1948 ,
. ,

, .
ENIAC, IAS (Immediate Address Storage). 1951 , 1952.
, IAS
,
.
, ,
, , . . 1945 , ,
EDVAC.
, ; , , 1936 . , IAS , , .
, IBM SSEC (1948), Manchester SSEM (1948),
BINAC (1949) .
, , ,
.
, (
8

) , , . ,
, . , , , .
, , , .
, , ; , , Colossus
ENIAC, IAS (, , IBM SSEC ,
, Colossus ENIAC ).
, , , , , .   (), ,
-1, ,
1951 , IAS .

2.4

( )

, ,
,
,
Bell Labs; 1956
.
( ) . . , , .
1955 1965
.
. , Digital Equipment Corporation 50000 PDP-8. , 9

,
.
, , , ;
( , 1954 1957
); , , .
( , ) FORTRAN Monitor System,
IBM 7094. FMS
, ;
( ) .
,
( ) .
. FMS IBM
1401; IBM 7094 , , ,
IBM 1401.
, , , .

2.5

( )

( , 1958) . , ,
.
1965 1980 .
, , ,
,
( ).
-, , , , 10

.
: , (, ) , .
;
.

2.6

( )

,
,
.
, . ,

IBM PC 1986 Intel
80386.
, .
IBM PC ; , ,
Google .

,
. , .
.

11

. , ,
.
-. , ,
.
. ; , ;
( ,
).
,
.
.
. ;
(, )2 .
.
. (, , ) . , (, ,
)
, . ,
,
; . , .

. ,
, ,

.
2

, ,

12

. 2:

2
4
4.1

,
, .
, , ,
.
, . , , ,
.
,
. , , ?

(, ):

, , , (

) .
, , ,
,
, ,
(. . 2).
13

()

. 3:

4.2

-.
, (, ).
- (, ,
, ).
(, ,
..) , , . , , ( ) ,
( ); ,
,
1 ; , ,
, .
( , , ) (. 3). , ,
, , -.
. -,
, 1

14

. 4:
.

-, .
, , ,
-. - , (.
. 4); , ,
, .
,

-, 2 , ,
, .
, ,
.

4.3

( , )
2

  , batch mode; batch  (,


, ).
, packet, .

15

, , ,
.
, , , , , ,
, 100x100, .

: (,
) ,
, ( 180
). , ,
: 
, ,
.
.
,
. ,
,
.
, , ;
, ..
, -,
( ,
). , ,
,
- ( ) ,
.
, Windows, ,
. , ,
; , , - (
-). , ,
, , .
16

. ,
; , Windows ,
, .

4.4

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

, ; ,

, , ,

. ,
3 ( ).

4.5

, ( )
.
- (. 3 4).
(.3) -
3

soft

hard.

17


( ),

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

.

4.5.1

, . 4, . -
. ,
; , , ,
(, ), -,
( ,
, ).
,
-, .
.
( ), . , ,
,
- . .

4.5.2

, . ,
.
18

,
.
,
, .
,
. , .

4.5.3

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

. ,
, / -.
: 4 .
.
, , ; ,
, . (, 5 )
, .
4

    (. kernel mode, supervisor mode ). 


 (. user mode ) (. nonprivileged.

.
5 . kernel

19

4.5.4

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

4.5.5

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

  ; 20

, .
. ,
.

5.1

()

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

6 :
1. , ,
 .
2. ,
,
;
 . .
3. ,
, ; .
6

; .

21

4. - ( ) ;
. , () , ,
;
.
5. ,
, .
, .

, IRET (interrupt return).
.
,
, .
, ( )
; ,
, .
-
, ( ) . , .
.
, , , .
,
.
, , , , . , 7 ,

, ;
7 ,

-,

22

, , ,
, (, , ) ,
.
, (
); , ()
, .
, ( -

)
.
5.2

()

, , : ,
?
, :
- ,
, , ; ,
, , .

- , , .
,
,
- .
.
, .
: . , .
, , , ;
( );
23

( , , ),
. , -
, ; ,
, ,
..
, , , . (
): ,
, 8 .

a .
, , ,
. , () ;
, , .
, (traps)
- .

5.3

. .

, , . ,
,
. , ,

.

() .

,
.
. -, , 8 , -

, , ;
[8].

24

. -,
(
,
). , ,
.
, .
, .
; , ( TRAP, SVC, INT
..). ,
, ( ,
,
).

, , 9 .

. .

, ,
. .
; , ,   , , ,
, ,
.
  , 
9

   , , ,
.


25

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

, , . ,

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

; ,
(, ), , . ,
 + , , , ,
.

:
, ; 26

, 10 .
,
. (
);
.
, .
, , -


.
6.1

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

.
IBM/360 VM/360. VM/360
OS/360, , IBM/360
. , VM/360
.

10

, . ,
 , ,
 .

27

. 6:

3
7

, , .
. 6.

.
; , ..
, .
- ,
. , , , , ( ). -
, . , , ,
, ,
.
28

1 , .
, . , . ,
, ,
.
, -, , . , , ,
ash-.
.
, , . ,
.
( ) -;
, ,

.
,
. (). , .
. , .

. - ; .
, . . .
1

   (). RAM (Random Access Memory),


 .


29

111111
000000
3
000000
111111
000000
111111
000000
111111
000000
111111
2
000000
111111
000000
111111
000000
111111
000000
111111
000000
111111
1
000000
111111
000000
111111

000000
111111
000000
111111

111111
000000
3
000000
111111

111111
000000
3
000000
111111

111111
000000
000000
111111
4
000000
111111
000000
111111
000000
111111
000000
111111
1
000000
111111
000000
111111

000000
111111
000000
111111

111111
000000
000000
111111
4
000000
111111
000000
111111
111111
000000 000000
111111
000000
111111
5
000000
111111
000000
111111
000000
111111

000000
111111
000000
111111

. 7:

8.1

,
.
1. . . ,

. , , .
2. . ;

, ()
.
3. . , ,
:
,
. , .
4. .
, (, ).
, , , ,

30

; ,
.
5. . , , ,
. . 7. 5, ,
, .
,
: ,
.
,
. , , , , ,
( ), ,
.. ,
. ( ,
),
( ,
), .

, , .

8.2

8.2.1

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

swapping

31

; .
, , (

); , ,
, , ,
.

8.2.2

,
, . ,
.
, , , , . . ( ) ,
.
. , ,
.
( )
,
, .
;
, ,
( ) (
).
32

111111
000000
000000
111111
2
000000
111111
000000
111111
000000
111111

111111
000000
000000
111111
1
000000
111111

111111
000000
000000
111111
2
000000
111111
000000
111111
000000
111111

111111
000000
000000
111111
1
000000
111111

. 8:

8.3

8.3.1

,
. , .
( )

(. 8).
( ) , . , 
. .
,  ,
. , ,  ,
,
.
,
, , :
, , . ,

.
 , . ,
.
33

,
, . :
. , .

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

8.3.2

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

( ,
; 34

. 9:

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

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

. - ,
,
;
35

,
, , ,
, 3 .
, .
, ,
.
() , , ,
; , , .
,
.
,
, , ,

, .
. , Intel
;

.

8.3.3


.
,
.
4 , (. 10).
, , 32- ,
512Mb (

229

),

217 =

131072 212 = 4096 , 4Kb ;


20
2
= 1048576 .
3

,
 frame;
 ,  ,
4

36

. 10:
, ,
( ,
)
, .
-
( !) . ,
, .

, (. 11).
,
. ,

,
. , , , ;
, , ,
.
. ,
37

. 11:
. ,
1Mb, ,
1Mb, , (
1Mb) . , , , ,
.
, 29 214
. Intel Pentium 4Kb,
2Mb 4Mb. , 4Kb (212 ) .
, 32- (4Gb ) 220 , .
, 4 , , 4Mb .
, : Mb,
, . ,
, .. ,

.
. , ;
, ..,
38


. 1 . 2


. 2 .

. 12:
(. 12).

; . , , 5 ,
.

32- 4Kb.
12 , 20 ,
10 , 10
6 .
210 = 1024 , 4
4096 , .
,
. ,
, , ;
. , . ,
5

32- 64-
i386- ; ,

39

. ,
,
, . ,
, ,
. , , ;

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

:
. , , , , ,
, ;
. , - .
, 7 .
( ).
,
, , .
( ).
,
( ).
,
(16
).
, 7


 

40

Translation Lookaside Buer, TLB, -

, (
- ), ,
, .
,
. ,
, .
, ,
, .
,
, .
, .
, , ;
. , , ,

, .

8.3.4

,
, . -
. ,
, . , , ,
, ( ).
- , , i386 .
; , - .

41

4
9

Unix

1960-x General Electrics,


(MIT)
Bell Laboratories ( AT&T) MULTICS.
MULTICS ; , Bell
Labs .
Bell Labs, MULTICS, . ,
.
Star Travel,
Bell Labs PDP-7.
, , , MULTICS,
PDP-7 .
,
PDP-7 [9].
UNICS ( MULTICS)
. , CS
X ( ).
,
PDP-11.
( , 

) .
BCPL, B.
,
. .
C.
1973 C.
: , . , , . C
Unix 30
42

- , Unix , , C
.
1974 ,
. PDP-11 , ,
.
AT&T: (
). Unix
.
,  ,
 (love, ken) [3].
Unix .
;
Interdata 8/32. C,
, , .
1977 .
Unix
. Unix, BSD,
, FreeBSD, NetBSD, OpenBSD BSDi,
(, BSD Berkley Software
Distribution). , Unix, 1974 ;
,
1975-1976 . BSD 1977 .
1984 AT&T
. AT&T Unix. Unix .
Unix, AT&T BSDi,
BSD. BSD Unix-.
1987 , AT&T, .
1993 , AT&T , Unix
(Unix Software Labs, USL) Novell; 18000 (!) , ,
43

, .
, Unix ,
Intel
Microsoft. 1986 Intel 80386 Unix; ,
BSD i386, ( - ) 1992
.
1984 ,
. . , 1987
BSD , AT&T.
,
. 1990-.
1991 Unix i386,
.
, .
Minix . i386, ,
. ,
; , [4].

Linux
. ,
. Freax.
( 0.01) 1991 ,
(1.0) 1994, 1996.
( ), Linux AT&T , BSD i386.
Linux , BSD
1 .
1

. , Internet FreeBSD Linux'. , , ( - ) FreeBSD

44


; , , . , GNU GPL,
.
Unix . Unix , . Linux ( ) ( )
FreeBSD. . ,
Unix, SunOS/Solaris ( Sun Microsystems).
Unix-,
.
POSIX 1003.1, Unix
Unix (
BSD System V).
, Unix ( , )
, 1970- .
X Window Unix.

10

Unix

Unix ,
-
Unix' .

10.1

Unix . , , ( ) ;
45

. 13: vt320 (Digital Equipment Corporation)


, , ( )
, .
; .
. , Unix : , Linux FreeBSD
, , 2 .
Unix
( , , ).
; , ,
, , , .
getty, ,
, . , () ,
() ;
, , .
,
. 2 Alt-Fn, n

. , 1, 2, 3, ..., 12

46

. , ,
.
.
Bourne
Shell.

10.2

Unix MSDOS WinXX,


, (- A:, C: ..),
, Unix
, (/).
. .
() , pwd:

$ pwd
/home/stud/s2003324
, ,
ls:

$ ls
Desktop

tmp

Unix , .. , a.b..c...d....e . , , ,  . ,
, ls -a:

$ ls -a
. .. .bash_history

Desktop

tmp

, .
, -F:

$ ls -aF
./ ../ .bash_history

Desktop/
47

tmp/

cp
mv
rm
mkdir
rmdir
touch
less








1:

, , .bash_history, . , . , ..


(.. , ;
/home/stud).
cd:

$ pwd
/home/stud/s2003324
$ cd tmp
$ pwd
/home/stud/s2003324/tmp
$ cd ..
$ pwd
/home/stud/s2003324
$ cd /usr/include
$ pwd
/usr/include
$ cd /
$ pwd
/
$ cd
$ pwd
/home/stud/s2003324
, cd
,
.
1.
48

10.3

,
'-'. , rm -r the_dir
the_dir .
, . . , , ( ,
;
cd). , ,
, , :

$ /bin/ls -l -a
$ /usr/local/bin/pine -f sent
, , , (-a, -l
..), , ..
. , , , , ,
 . ,
, (, -
just a file.txt), :

$ rm "just a file.txt"
$ rm just\ a\ file.txt

, .

10.4

Unix ,
,
.
,
,
.
, / .
, Bourne Shell,
-
. <, >, >>, >& | (. 2).
49

cmd1 > file1

cmd2

cmd3
cmd1
cmd4
cmd5

cmd1, file1. ,
, .
< file2
cmd2, file2
. ,
.
> file1 < file2 cmd3,
, .
| cmd2
cmd1
cmd2,
.
2> errfile

errfile.
2>&1 | cmd6

cmd5
cmd6
2: -

Unix less, , "


" PgUp, PgDn . . , .
less , , - , .
,

ls -lR | less
, .

10.5

, ,
ps:

$ ps
PID TTY

TIME CMD
50

2199 pts/5
2241 pts/5
$

00:00:00 bash
00:00:00 ps

, , .
, ps
( , FreeBSD Linux).
;
, ps ax , ps axu
3 .
.
, , kill 2736 2736,
; kill -9 2736 ,
.

10.6

,
/.

.
, . ls -lR /. ,
. , ,
,
, , . ,
&, :

$ ls -lR / >list.txt 2>/dev/null &


[1] 2437
$
/dev/null
,
.
3

Linux FreeBSD. , SunOS/Solaris, ps


51

, 1,
2437.

, . Ctrl-Z,
. bg4
, .
(.. ,
) .
fg5 .

10.7


, , (). ,
Bourne Shell,

#!/bin/sh
Bourne Shell . , ,
. . ,
, :

I=10
MYFILE=/tmp/the_file_name
MYSTRING="Here are several words"
, , ( ) ,
, , .
$, :

$ echo $I $MYFILE $MYSTRING


:
4

background
foreground

52

10 /tmp/the_file_name Here are several words



, :

$ echo ${I}abc
:

10abc
$(( )). ,

$ I=$(( $I + 7 ))
I .
test . , () , (). test . .

[ -f "file.txt" ]
# file.txt
[ "$I" -lt 25 ]
# I 25
[ "$A" = "abc" ]
# A abc
[ "$A" != "abc" ]
# A abc
, , :

if [ -f "file.txt" ]; then
cat "file.txt"
else
echo " file.txt "
fi
, , ,
test, . :
53

if gcc -Wall -g myprog.c -o myprog; then


echo " "
else
echo " "
fi
, . , 1 100:

I=0
while [ $I -le 101 ]; do
echo $I
I=$(( $I + 1 ))
done
Bourne
Shell [1].

10.8

Unix .
VAR=VALUE, VAR , VALUE .
:
, .
-.
PATH. ,
, , . HOME, ; LANG, ,
; EDITOR,
. ,
. , set .
. -, (,
, , VAR=VALUE),
:
54

$ echo $PATH
/usr/local/bin:/bin:/usr/bin
$ echo $HOME
/home/stud/s2003324
$ echo $LANG
ru_RU.KOI8-R
,
export:

$ PATH=$PATH:/sbin:/usr/sbin
$ export PATH

$ export PATH=$PATH:/sbin:/usr/sbin
, ,
, ,
.
unset
export:

$ unset MYVAR
$ export MYVAR
,
,
.
, . :

$ VAR=value command
, FreeBSD ,
, chfn,
, . vim, . ,
, :

$ EDITOR=joe chfn
joe.
55

5
11
11.1

, , , . ,
, .
ash-: , ,
ash- ( ,  ). ,
.
, ; ,
,
,
, ,
. , LATEX,
, ash-,
ash.
, (
) ,
; , .
. , . , ,
, , ,
,  (
). , 80Gb,
56

. 14: -
; 1.4Mb, ; 256Mb, . ,
IDE,
, ash-, ( ).

11.2

- (, , )
. 14. : , ,
, , ,
, . ,
, .
,
-; , ( , ), ,
-
.
, . ,
, , ,
, , -, 1 . , ,
prog1, ,
1

, , -

57

"Hello, world" (,

C
printf).

prog1 > file1


file1 , ,
-.

prog1 | prog2
- ( ) : ,
prog1, prog2,
, , .
- ()
, , ,
.

11.3

11.3.1

(
), .
, ,
. ,
,
.
, 55
(
, ) , ,
-.
,
. , .
58

11.3.2

,
.
, .
:
1. .
() . ,
,
, . Unix'
.
2. .
, ,
.
, .
. .
Windows, MS-DOS.
3. .
,
, ,
. , . ,
. Unix-, Linux, FreeBSD Solaris.
, , ..
.

11.4

() (. . 1 . 3). - , , -, .
, , ,
59

prog > file


printf("Hello, world");


write(1, "Hello, world", 12);


disk#1, part#2, sector#127563

ide#0, slave,

cyl.125, head 8, sec.20

. 15: -
, ,
. , , .

(. 15). ( ) .
- . ,
, , ( );
,
(, write()) .

write() ,
, . , ,
, , . , .
,
.
, -
,
. .
60



. 16: -

11.5

, ,
, , , : ,  , , ,

.
, - ,
.
. 16.
, . ,
- , - .
,
. ,
. , , , ..
, .
-. , ,
, , ,
. .
61

11.6

11.6.1

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

,
.
, , , , , , ,
.

,
, .
, ,
,
. , .

11.6.2


, - , , ,
. -.
- , ,
. ( ) , , , 62

.
, ,
. ( ) -, , ,
,
.
, - . , , ,
- ,
, /
. , - ,
, ,
, , (
, , ,
). , , , , , ,
, ,
.
, -.
,
. ,
, , ,
; , ,
.
- ,
, , .

11.6.3

-
( , ,
).
(, IN OUT MOV).

, .
,
MOV. , , PDP-11.
63


- ,
, , , . , ,
.
(
C++) .
, , , ,
. PDP-11 , .. 16-. ,
-, , , . ,
,
,
, .
, i386,
. - , .

11.6.4

,
, .
, ( ), . , , ,
- , (. 17); ,
Nh ,
Nc (), Ns , .
64

48

33
34

32
16
15

29

44

36

47

18

7
6

28

14

19

26

11
00
00
11
5 4

36

46

20

21

11

37
45

27

13

43

22

12

26

23
25

24

13

23

10
2

38

25

35
35

30
45

17

31

46

37

48

47

38

24

14
12 1
11
34
22
2
15
10
3
9
21
4 16
33
8
5
7
6
20
17
19
18
32
29
44
31
30

11
00
00
11

42

27

39

28
40

41
39
41

40

43
42

. 17:

.
, , , . ,
, .

11.6.5

, , , -.
, (
, ) .
4.5, .
, (
)
.
.
, -,
, , ,
, , (
) , , 65

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

11.7

11.7.1

, ,
. .
, A ;

, . , , A ,
B . , B
, .
,
,
A.
B ,
. . A
.
B ,
, C ,
.
,
, . A,
B C ,
.
66

( C , B )  , .
,
,
.
, , ,
,
, , , .

.
. A

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

11.7.2

, , , , , .
,
, 67

. , , ,
. , ,
, ; , ,
, ,
, ,
.
, . , A
, B ,
, , , . ,
A ; ,
B , :
. , B
, ,
,
: .
, : , ,
,
.

11.7.3

- ,
... ,

.

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

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

11.7.4


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

11.7.5

, , , .
() .
69

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

, .

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

, .

, ,
, , ,
.

. .

70

6
12
12.1

, , ,
1 .
,
, .
, , ,

. ,   ,
.
,
; , . , , .
.
, , 2
. ,
( , , ).
,
. ,
.
(). , ,
1 ,

; ,

2 directory ;  , 
, . ,  , .
directory folder
( ).
, ( ) ,
-, ,
.

71

( ), ..

( ,  )
( , , .. ,
). ; MS-DOS Windows
(\), Unix (/). ,
work, project, program.c,
Unix : /work/project/program.c.
, , (
). Unix, MS-DOS
Windows, .. ( ).

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

/home/work/projects/task/prog.c /home/fun/books/alice.txt.
/home/work/projects .
, , task/prog.c
../../fun/books/alice.txt.
- .
( , ) , , .

12.2

12.2.1

Unix

, Unix
.
72

, (
Windows
A:, C: .. Unix ).
, , , ,
(. mount point ),

. ,
, work, prog.c, /mnt/floppy,
: /mnt/floppy/work/prog.c.

12.2.2

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

() , ,
, (. index node,

i-node). , -

. .
, Unix
( 255 ) , ,
-. ,
Unix .
,
, , , ,
.
( , , ,
backspace .) , 127 (,
). ,
- ().
. , .
73

12.2.3

Unix , ,
, ,
.
, .

int link(const char *oldpath, const char *newpath);


oldpath , newpath . (. hardlinks ).
:
.
, ; ,
,
.
, , .
, .
, , unlink(),
, Unix.
Unix ,
, .
unlink() ,
. ,
( ),
.
, - ,
.
, ln. cp,
, .
.
, : ,

$ mkdir a; cd a; mkdir b; cd b; ln ../../a ./c


74

a, ,
.
, , a , -
( ).

,
.

12.2.4

Unix
. , ,
( ), - . ,
.
, .
, . ,
FAT (MSDOS, Windows )
(volume label).
Unix : - , , (FIFO) ,
, . ; .
(. symbolic link ) , .
, , .
,
. . , ,
. , , ,
,
.

int symlink(const char *oldpath, const char *newpath);


link(). unlink().
75

ln -s:

$ ln -s /path/to/old/name new_name

12.2.5

(. access permissions ) , (, ) .
12- . 9 ;
,
.
, .
, ls -l, :

$ ls -l /bin/cat
-rwxr-xr-x 1 root root

14232 Feb 4 2003 /bin/cat

-rwxr-xr-x
(  , , d ..) , , ( rwx, .. , ), ( r-x, ..
). , /bin/cat
,
root (.. ).
,
, .
( )
, (
) .
1, 2, 4;
, .., , 6 (4 + 2), 5 (4 + 1).
, /bin/cat 07553 .
3

, ; C ,

76

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

SetGid Bit

(02000)

Sticky Bit

(01000).

SetUid Bit,

SetUid Bit

(04000),

root) ,
SetGid Bit ,

(
). suid-

passwd.

Sticky Bit, , Unix -

, ,
; ,
.

SetGid Bit

, , -

,   ,
.

Sticky Bit

, ,

, ( ) .

chmod4 .
,
:

$ chmod 644 myfile.c


myfile.c ,
.

[ugoa][+-=][rwxsXtugo] u, g, o a , , (user), (group), (others)
(all). + , - , =
. r, w, x
4

Change Mode

77

, , , , s / Set- ( ), t
Sticky Bit. X () /
, ,
- .
chmod -R,
.
, chmod a+x myscript myscript ; chmod go-rwx *
, .

chmod -R u+rwX,go=rX ~
, ; , , .

, .
, . root .

12.3

12.3.1

, , , .

int open(const char *name, int mode);


int open(const char *name, int mode, int perms);
name , .
,
( , ).
mode , .
O_RDONLY ( ), O_WRONLY (
) O_RDWR ( ).
.
, , ,
. :
78

O_APPEND :
;
O_CREAT ,
;
O_TRUNC ,
, ; ;
O_EXCL ( O_CREAT)
; , , ;
.
(perms) , (
O_CREAT). . , ,
, umask.

( ,
),
0666, ( -

) , .
umask,
, .
open() -1 , . ,
, 5 .

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

, - .
5

,
. ,
descriptor , ( ) :
index node descriptor
.

79

0, 1 2 : ,
, .
.
,
-, , . .

12.3.2

(, , )

int read(int fd, void *buf, int len);


fd ; buf , ; len
, .
read() len .
, ,
, ,
. ,
len , buf .
1 . , .
, len.
0. read()
, , . , ,
.
( )

int write(int fd, const void *buf, int len);


fd ; buf , , ( );
len .
1 . , .
, len.
80

len, . ,
write(), .

12.3.3

. , ( ,
, , ).

int close(int fd);


fd , . 0
, 1 .
,
.

12.3.4

. ,
. read()
len, 100,
99, 100 199, 200 299
..
, , 6 .

int lseek(int fd, int offset, int whence);


fd, , .
offset , , whence
, .
whence, SEEK_SET, ; SEEK_CUR , SEEK_END
. , .
6

, .

81

. lseek(fd, 0, SEEK_SET) , lseek(fd, 0, SEEK_END) . lseek(fd, 0, SEEK_CUR) ,


, .
100 :

int rc;
char buf[100];
/* ... */
lseek(fd, -100, SEEK_END);
rc = read(fd, buf, 100);
, .
, , (, , ).

, . , ,
. , .. Unix  , -
.

12.4

12.4.1

Unix .
. , , , , , ...
, - (CD). , , .

. Unix,

cat /dev/cdrom > image.iso


, , , ,

cat myfile.ps > /dev/lp0


, , ,
.
82

, ,
. , ,
, . Unix ,
, lseek() write(). , , () .
, , , , .
, .
,
. , Unix , .

12.4.2

, ,
: ( ) .
-    .
- () (). , - , ;
, , .
- ( ), 7 , , . ,
. , /dev/null
, ; 
. /dev/zero ,
. /dev/random
,
..
7

83

-
. , ,
.
, -
.

12.4.3

, ,
read() write().
, - lseek(). , () , ,
, . ;
/ -, ,
, ,
.

-
. , , -

- .
, . , .
, ,
CD-ROM; ; ; , ..

int ioctl(int fd, int request, ...);


fd ,
request . , . , ,
84

int fd = open("/dev/cdrom", O_RDONLY|O_NONBLOCK);


ioctl(fd, CDROMEJECT);
ioctl(fd, CDROMCLOSETRAY);
, CD-ROM.

O_NONBLOCK ,

,
.

(
audio CD),

struct cdrom_ti cti;


cti.cdti_trk0 = 2;
cti.cdti_ind0 = 0;
cti.cdti_trk1 = 2;
cti.cdti_ind1 = 0;
ioctl(fd, CDROMPLAYTRAKIND, &cti);
.

85

7
13

13.1

, , .
, , :
1 ;
;
;
;
;
, (, );
.
, .
. , , .
,

 ;  , , ..

. 18:
86

, , , ,
, (, -). ,
: , ( ) (. . 18).
(, ,
,
, , ).
,
,
. ,
;
, , .
( sleep()) ( pause()).

13.2

2 .
; ,
, , .

, (, , mp3-). ,
/ . ,
: , (
) .

(/) , .
2

thread, lightweight process. , , ;


.

87

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

.
.

14
14.1

Unix

Unix ,
, :
1. (, ). , . ,
.
2. , .
3. , ,
(PSW), .
( , , - ),
.
4. -, -,
.
5. . , , ,
(. 19, ).
88

l s \0
argv

PATH=/bin:/usr/bin\0
envir
HOME=/home/john\0

- l \0
- a \0
NULL

/ u s r / l o c a l \0

NULL

TERM=xterm\0

ls -l -a /usr/local

. 19:
6. . , (. 19, ).
7. .
; ,
, .
8. . Unix , , ,
. ,
/foo, / /foo,
/bar /foo/bar. /foo .
,
, .
9. . .
10. umask. , ..  ,
. , umask
0077,
, .
11. ( ,
..).
12. .
uid ( ), gid ( ), euid egid (
). ; , , suid- ( ,
89

, ,
, ).
passwd ( ).
13. , ,
. pid . ppid (, ),
; , ppid 1.
.

14.2

14.2.1

Unix
3 .

int fork(void);
,
, :
1. (pid), , ;
2. ppid pid' ;
3.
fork() ;
4. ( ) , fork() (
- ),
fork() pid , 0.
, fork() ( ) ( , ..
3

Unix , clone()
Linux,

90

). , , , 4 .
, , , ,
,
, .
, ..

14.2.2

Unix .

int execve(const char *path, char* const argv[],


char* const envir[]);
path ,
(
, ). argv envir , , , . 19 (. . 89).
exec, execve().

int execv(const char *path, char* const argv[]);


execve(), , envir.
, ,
, , .
,
:

int execvp(const char *path, char* const argv[]);


4

, :

91

execvp() execv() , ,
path, ,
, PATH; ,
PATH /bin, ls
, .
, , . , .

int execl(const char *path, const char *argv0, ...);


int execlp(const char *path, const char *argv0, ...);
,
, .
, ,
NULL.
, ,
; , argv0
,
, argv0
path.
execl() execlp() ,
,
PATH, , execvp().
, ls -l -a /var. , , :

char *argv[] = { "ls", "-l", "-a", "/var", NULL };


execvp("ls", argv);
:

execlp("ls", "ls", "-l", "-a", "/var", NULL);


, exec
( exec) , . exec
( , ,
, ; ).
-1, :
.
92

, exec
5 , . exec
-.

14.2.3

void exit(int code);


code . , 0
, 1, 2, 3 ..
. ,
10, .
,
main(). , main() ( , Unix'
main() int).
, , ; .

14.2.4

,
( ,
, )
. 6 . , ,
-, , , ..,
, .
( - ) wait(). :

int wait(int *status);


5

, exec, close-on-exec fcntl(),


6 ,
init ( 1)

93

, , ( -1). , ,
( , ),
( wait, . ).
pid .
, , , , .
WIFEXITED,
WIFSIGNALED ( ),
WEXITSTATUS ( , ),
WTERMSIG ( , ).

int wait4(int pid, int *status, int options,


struct rusage *rusage);

, -1, ; . status , .
options 0, WNOHANG.
;  , 0. rusage ,
. -1
, 0 , WNOHANG
, pid , .

14.2.5


fork()+exec().

int pid;
pid = fork();
if(pid == -1) { /* */
perror("fork");
exit(1);
94

}
if(pid == 0) { /* */
execlp("ls", "ls", "-l", "-a", "/var", NULL);
perror("ls"); /* exec -> */
exit(1); /* */
}
/* */
wait(NULL); /* ,
*/
14.3

()

()

zombie

. 20:

, , . 20. ,
( ), , .
( ,
).
95


; .
, ,
. ,
, , .
, , ,
.
( ), . ,
(, ).
.
, , (, -),
, , .
, , , (
).

15

(race condition)

,
, , :

int main()
{
if(fork() == 0) { /* */
printf("I'm the child\n");
} else {
/* */
printf("I'm the parent\n");
}
return 0;
}
.
, printf(). , ( -).
96

I'm the parent,


I'm the child.
. ,
fork() . , , .
.

, ( ) 7 .

,
. ,

.

.

race condition.
.

97

8
16

16.1

int chdir(const char* path);


. ".." (, /usr/local/share/.. ,
/usr/local).

int chroot(const char* path);


-
. .
chroot() , root.

16.2

extern char **environ;


char *getenv(const char *name);


int setenv(const char *name, const char *value, int overwrite);
void unsetenv(const char *name);
getenv() , ,
name.
, NULL.

NULL

,
,

98

PATH.

setenv() , , , ,
overwrite;
, overwrite
, .
unsetenv() .

16.3

umask

umask

int umask(int mask);



umask.

16.4

open(), ( , , ..)
; , , open() .

close().
, , :

int dup(int fd);


int dup2(int fd, int new_fd);
dup() , -, fd. , , :
lseek(),
.
dup2() , ( new_fd).
, .
99

, , ,
, . :

int save1, fd;


fflush(stdout);

/*
*/
save1 = dup(1);
/* */
int fd = open("file.dat", O_CREAT|O_WRONLY|O_TRUNC, 0666);
/* */
if(fd == -1) { /* ... ... */ }
dup2(fd, 1);
/* c
*/
close(fd);
/* "" */
/* ... ...
,
( printf, puts ..),

*/
dup2(save1, 1);
close(save1);

/*
*/
/* */
/* */

. ,
C Shell

ls -l -a -R / > filelist
( , filelist,
). :

int pid, status;


pid = fork();
if(pid == -1) { /* ... ... */ }
if(pid == 0) {
/* */
int fd = open("filelist", O_CREAT|O_WRONLY|O_TRUNC, 0666);
if(fd == -1) exit(1);
dup2(fd, 1);
100

close(fd);
execlp("ls", "ls", "-l", "-a", "-R", "/", NULL);
perror("ls");
exit(1);

}
/* */
wait(&status);
if(!WIFEXITED(status) || WEXITSTATUS(status)!=0) {
/* ... ... */
}
16.5

uid, gid, euid, egid, pid, ppid, sid pgid


, , getuid(), getgid() ..
pid, ppid ( )
.
sid pgid
, , .
uid, gid, euid, egid, , .
.
,
.

17

Unix

Unix- -

. ,
, ; , , .
,
, .
1 .
,
1

, ; , ,
.

101

System V IPC

mmap

. 21:
.
, .
mmap() ,
2 . ,
, , .
,
( ) ( ).
, Unix, Unix . ,
( , , xterm, , , sshd
telnetd). , ( ) , .
, System V IPC 3 .
, .
, System V IPC 2

mmap(). .
3 V ; 
--

102

. [3] .
(
, ), (sockets). ,
; ,
TCP/IP
- ; , . Unix , , .
,

.

18
18.1

.
, . :
SIGTERM . .
SIGKILL . SIGTERM,
, . ,
SIGKILL

9.


. ,
SIGTERM, , 5 SIGKILL.  : , , ( )
.
SIGILL, SIGSEGV, SIGFPE SIGBUS ,
(103

,
, , ).
core-4 . (, ).
SIGSTOP SIGCONT , , . , SIGSTOP, SIGKILL,
, . SIGCONT ,
( ) .
SIGINT SIGQUIT
5 Ctrl+C Ctrl-\,
. , SIGQUIT core-.
SIGCHLD
.
SIGALRM
alarm(). , ,
. .
SIGUSR1 SIGUSR2
. , .

18.2

, , .

int kill(int target_pid, int sig_no);


4 Core-

core prog.core,
. . Core-
, , ,
..
5 . , SIGINT SIGQUIT ,
, , ( ).

104

sig_no .
SIG, SIGINT, SIGUSR1 ..
target_pid () () .
, ,
. -1, ,
kill(). , 

,
. .
, (uid == 0),
;
, . , kill(-1,
SIGTERM) SIGTERM , .

18.3

,
, core-, . (,
SIGCHLD) .
, SIGKILL SIGSTOP,
: -, .
-
void, ..

void handler(int s) {
/* ... */
}

signal():

typedef void (*sighandler_t)(int);


sighandler_t signal(int signo, sighandler_t hdl);
signo , hdl , . hdl SIG_IGN
( ) SIG_DFL ( ).
105

signal() , , SIG_ERR .
- , -
, - ( ,
).

( ,

Linux, ). Unix,
System V, (
-)
. BSD, , ,
.
-
,
- (, , SIG_DFL, ).
, Ctrl-C
, 25 .

#include <signal.h>
#include <stdlib.h>
volatile static int i = 0;
const char message[] = "Press it again, I like it\n";
void handler(int) {
signal(SIGINT, handler);
i++;
write(1, message, sizeof(message)-1);
}
int main() {
signal(SIGINT, handler);
while(i<25) pause(); /* ,
*/
return 0;
}
, pause()
.
106

while , ,

, .. .
volatile i ,
i ; , volatile, , .
, SIGINT
, ;
, , , , 
signal().
, , , ,
Press it again, I like it write()
printf(). ,  : ,
- ,
.
, ,
.
, printf()
.
, , sig_atomic_t (
int) . write().
signal().
,

sigaction(),

signal().

Unix- . , , .

18.4

sigaction()

alarm()

alarm()
SIGALRM
. :

int alarm(unsigned int seconds);


107

, .

SIGALRM; ,
, .
alarm() ,
SIGALRM.
, . ,
, .
SIGALRM, ,
,
.
, seconds , .

18.5

,
. , ,
, ..
, , , (, ), ,
(, read(), sleep() .), ,
; 
errno, EINTR.
, , , .

19

, . , ,
.
, , , .
108

. 22:

19.1

19.1.1

int pipe(int fd[2]);


int; pipe() , :
fd[0] , fd[1] .
, , ,
.
, , , fork(). ,
, .
. 22
. ( ) .
, , .
,
. ,
, ,
.
, .
C :

int fd[2];
pipe(fd);
if(fork()==0) { /* child #1 */
close(fd[0]);
109

/* ... */
write(fd[1], /* ..., */);
/* ... */
exit(0);

}
if(fork()==0) { /* child #2 */
close(fd[1]);
/* ... */
rc = read(fd[0], /* ..., */);
/* ... */
exit(0);
}
/* parent */
close(fd[0]);
close(fd[1]);
/* ... */

19.1.2

, . ,
, ( read() ) , -
, ,
, .
, , (
, ), read()
; read() ( , ) ,
, ,
read() , .
, , . , , (, Linux 4096
). , , write()
, - ,
, , .
, ,
, . ,
110

, .

, , (
read()) ,
0 (  ).
, , , ,
,
SIGPIPE. . write() -1,
,
SIGPIPE.

19.2

, , , ,
..
Unix.
.

( 0 1) , , .
 ,
, , . Unix, , ,
  ; ,
. , . ,
, , .
, ,
SIGPIPE. -
, ; ,
- ,
111

ls -lR

| grep '^d'

C, , :

int main() {
int fd[2];
pipe(fd);
/* */
if(fork()==0) { /* ls -lR */
close(fd[0]); /* */
dup2(fd[1], 1); /* . - */
close(fd[1]); /* fd[1] */
/* ls -lR */
execlp("ls", "ls", "-lR", NULL);
/* , */
perror("ls");
exit(1);
}
if(fork()==0) { /* grep */
close(fd[1]); /* */
dup2(fd[0], 0); /* . - */
close(fd[0]); /* fd[0] */
/* grep */
execlp("grep", "grep", "^d", NULL);
/* , */
perror("grep");
exit(1);
}
/*
*/
close(fd[0]); close(fd[1]);
/* */
wait(NULL); wait(NULL);
return 0;
}
19.3

(FIFO)

, ,
(FIFO), 112

. , , ; ,
,
,
, .. .
FIFO

int mkfifo(const char *pathname, int permissions);


, ( open() mkdir()). , ,
umask. -1 , 0
.
FIFO ;
, - FIFO
open() , ,
, .
FIFO:
- ,
.
,
.
, - .
, , , ,
,
 , , , SIGPIPE.
, ; ,
 , , ,
read() - . , - .
, ,
read() 0 ( ).

113

9
20

OC Unix .
,
, , , .

void *mmap(void *start, int length, int protection,


int flags, int fd, int offset);
mmap() open();
mmap() , ,
fd. offset length , ,
. , , , 1 .

int getpagesize();
protection mmap() . PROT_READ,
PROT_WRITE PROT_EXEC,
. , .
, ; , ,
. PROT_NONE,
.
protection , : ,  , open() O_RDONLY,
, , .
1 ,

mmap() , ,

114

flags MAP_SHARED,
MAP_PRIVATE ( , , ). ,

. MAP_ANONYMOUS,
( ); fd offset .
, mmap() MAP_ANONYMOUS, , fork()
, , ,
, .
start , .
; start NULL,
.
mmap()
. ,
char*.
mmap() MAP_FAILED, -1, void*.
:

int fd;
char *ptr;
fd = open("file.dat", O_RDWR);
if(fd == -1) { /* ... ... */ }
ptr = (char*) mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
if(ptr == MAP_FAILED) { /* ... ... */ }
ptr[25] 26 "file.dat",
ptr[25] = 'a' 'a'.
:

int *ptr;
ptr = (char*) mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS, 0, 0);
if(ptr == MAP_FAILED) { /* ... ... */ }
115

if(fork() == 0) {
/* ... ... */
} else {
/* ... ... */
}

( 1024 , , int
4 ). ptr,
ptr[77] = 120,
ptr[77] 120.
, mmap(),

int munmap(void *start, int lenght);


, ,
. ,
,

msync().

21

, Unix .
, , ( ), xterm. ,
( xterm),
( ). , ,
, .
(,
). xterm .
, , Ctrl-C. ,
SIGINT;
, . , ,
, Unix (
116

). , , . : Ctrl-C
3 (, Ctrl-A 1, Ctrl-B 2, ..). ,

SIGINT. , tcsetattr() , SIGINT
- , 3
.
xterm. , Ctrl-C
SIGINT xterm, ,
. , xterm,
, SIGINT,
, Ctrl-C.
XWindow,
.
,
3 , ,

SIGINT.
Ctrl-D. xterm
, , , EOF'
, 2 . xterm , Ctrl-D ( 4). ,
, read(),
, 0 (  ).
,
, , ( xterm), ,
. , ,
(master),
(slaves).
,

int getpt();
2 ,

xterm' cat, Ctrl-D, ,


, xterm

117

(
). ,
.
(. pseudoterminal slave, pts ).

int grantpt(int fd);


int unlockpt(int fd);

, . grantpt() , , , ,

uid

uid


open() ( ,
,
- ).
, open(). , ,
, , ,
exec .

char *ptsname(int master_fd);


master_fd , getpt().
, -, , :

int openpty(int *master, int *slave, char *name,


struct termios *termp, struct winsize *winp);

master

slave

, -

, , ,   .

name

termp

winp

. -

22

.
() 118

( ), , , ,
, , ..
Unix

int ptrace(int request, int pid, void *addr, void *data);


request
( , , ). .
:
() . fork(), ( ptrace()
PTRACE_TRACEME), exec , .
exec () SIGCHLD.
wait, ,
.
PTRACE_SINGLESTEP,
PTRACE_CONT, PTRACE_GETREGS, ..

ptrace() PTRACE_ATTACH. ,
; , SIGCHLD , ,
getppid()
.

119

10
23
23.1

.
. ISO OSI

( ) , 1 , .
, . ,
, , , , , .
Internet, TCP. ,
HTTP (hypertext transfer protocol).
(ISO/OSI), . ISO
International Standard Organization (, ), OSI Open Systems Interconnection (  ,
).
:
.
, ,
, ..
. ,
; , ,
. ,
. , ,
. , , ,
1

. ,
;
.

120

, ,
, . ,
..
. ,
, , ,
, , ,
.
,
. ;
, , ,
..
. , ..
. , , , , ..
. , , .

ISO/OSI All People Seem To Need Data Processing ( , ,


). (Application, Presentation, Session, Transport, Network, Datalink Physical).
, , .

23.2

. , (. socket) ,
2 . Unix , , ,
close().
(, , ) .
2

,
,
.

121

-. , Internet
TCP/IP
: ip- (4 , , 192.168.10.12) ( ).
Internet IPv6 ( IPv4), ip- 16 , 12ff:2001:0055:2eab:0767:1212:f1b1:a00a. , .
, Novell,
IPX/SPX.
: 4- , 6- ()
2- .
. ,
;
.
,
. ,
(, ,
) . ,
. , (. address family)
.
, ; , ,
, .
, .
: .
: , ,
, ( a priori ). , , (
). . ,
122

().
.
; ,
,
(
). ,
,
.. .
Unix

int socket(int address_family, int type, int protocol);


address_family . : AF_INET TCP/IP (
ip-/) AF_UNIX
( ).
type .
SOCK_STREAM SOCK_DGRAM
. ,
.
, . ,
0.
-1 ; , .

23.3

. bind()

bind():

int bind(int sockfd, struct sockaddr *addr, int addrlen);


sockfd , socket(); addr , ; ,
addrlen .
addr
sockaddr, , .
123

AF_INET struct sockaddr_in, IP- + . :


sin_family ( AF_INET).
sin_port , , , , . ,
htons()3 .
sin_addr IP-. sin_addr ,
s_addr, ip-
.
AF_UNIX struct sockaddr_un, . :
sun_family ( AF_UNIX).
sun_path 108 , .
bind() 0 , -1 . , , bind()
; , ( 1 1023) ,
- (, ).
bind() .
bind() sockaddr_XXX
: , .

23.4

.
socket() SOCK_DGRAM
. bind(),
(
), .
, ,
sendto() recvfrom():
3

htons() Host to Network Short, ..


.
.

124

int sendto(int s, const void *buf, int len, int flags,


const struct sockaddr *to, socklen_t tolen);
int recvfrom(int s, void *buf, int len, int flags,
struct sockaddr *from, socklen_t *fromlen);
s , buf
, , len (, , ).
flags ;
0.
sendto() to ,
, ( ). , , (sockaddr_in AF_INET
sockaddr_un AF_UNIX). tolen
. socklen_t int.
recvfrom() from ,
(..
, ). fromlen socklen_t,
recvfrom() ,
; recvfrom()
,
.
, UDP. IP/UDP ,
, , , .
;
. , , , [6].

23.5

. -

. ,
, , ,
, ( ).
125

. 23:
. -

,
- , . ,
,
4 .


, .
; , TCP,
TCP- TCP-.

23.5.1

, ,
, (. listening). ,
,
. , -, , (. 23).
, socket()
, , bind().
( . 23 L)
4

,
,

126

int listen(int sd, int qlen);


sd . qlen
. . , ,
,
( ). . qlen
, ,
. , qlen
. qlen 5,

,
 .

int accept(int sd, struct sockaddr *addr, socklen_t *addrlen);


sd . addr , , ( , ).
addrlen socklen_t, accept() , ;
accept() ,
. from fromlen
recvfrom().
accept() , (
-1 ). accept() ,
, ,
, .
,
- , : , , , , (, ).
. .
127

23.5.2

, ,
socket(). ;
, .

int connect(int sd, struct sockaddr *addr, int addrlen);


sd . addr
, (..
, ). , , (sockaddr_in AF_INET sockaddr_un AF_UNIX).
addrlen .
0 , -1 .

23.5.3


read() write(),
(
, accept(),
, connect()).

recv()
send(), read() write()
flags. , man Unix [2] [6].

int shutdown(int sd, int how);


sd , how .
how == 0 , how == 1 ,
how == 2 ( ).

close(). , .. shutdown() , ( )
, .
, , read()
recv() 0,  .
128

23.5.4

AF_INET

sockaddr_in sin_port.
.
. ,
( ,
), big-endian,
, , , little-endian5 .
,
, , (big-endian) ,
.. . , Unix
(host byte order) (network
byte order). ,
, ,
. :

unsigned
unsigned
unsigned
unsigned

long int htonl(unsigned long int hostlong);


short int htons(unsigned short int hostshort);
long int ntohl(unsigned long int netlong);
short int ntohs(unsigned short int netshort);

, n network (..
), h host ( ). , s , l . , , ntohl()
, .
IP-, , (,
"192.168.10.12"), inet_aton()
struct in_addr ( sin_addr sockaddr_in ):
5 

big-endians little-endians  , ,
. .
.


129

int inet_aton(const char *cp, struct in_addr *inp);


cp , IP-, inp , . ,
IP-, 0 .
, IP- char *serv_ip,
port .
sockaddr_in :

struct sockaddr_in addr;


addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if(!inet_aton(serv_ip, &(addr.sin_addr))) {
/* - IP- */
}
ip , ip. sin_addr
INADDR_ANY:

struct sockaddr_in addr;


addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;

23.5.5

 TCP-

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

SO_REUSEADDR.

bind()

int setsockopt(int sd, int level, int optname,


const void *optval, int optlen);

130

setsockopt():

sd

level

() ,

( ,

SOL_SOCKET)

optname

 ( ,

, , ) ;

SO_REUSEADDR.

, , , (

1,

optval

optlen

).

int,

optval

optlen

1
sizeof(int).

, :

int opt = 1;
setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

23.6

,
,
. , ,
. Unix

int socketpair(int af, int type, int protocol, int sv[2]);

af, type

protocol

, , , -

. ,

AF_UNIX, SOCK_STREAM 0. sv int,

. ,
, .
0 , -1 .

131

11
24
24.1

, -
. , .
( ).
, accept().
accept(), ,
accept(),
: , .
( ,
) , ; , ,
accept(), ,
.. .
,
, , - .
read() recv(),
,
, ( ) .
. , ,
, ,
,
.
, ,
.
.
, , ( accept()), ,
, . ,
132

,
: ,
, ,
-.
, . - - ,
, ,
, , , . ,
,
, , . ,
, , ;
, ( ), ,
, .
, , , ,
, , ,
.  . fcntl() ( , ) , read()
accept() , ( , ).
.
.
, . , , ( ,
)
, , , , ,
. ,
, ,
.
. : , ,
, ,
.
133

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

24.2

, 
accept(). ,  ()
. ,
. , ;
. , accept().
:

int ls;
struct sockaddr_in addr;
ls = socket(AF_INET, SOCK_STREAM, 0);
if(ls == -1)
{ /* ... ... */ }
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
134

addr.sin_addr.s_addr = INADDR_ANY;
if(-1 == bind(ls, &addr, sizeof(addr))
{ /* ... ... */ }
for(;;) {
socklen_t slen = sizeof(addr);
int cls = accept(ls, &addr, &slen);
if(fork() == 0) { /* */
close(ls);
/* ...
cls
,
addr
...
*/
exit(0);
}
/* */
close(cls);
/* , -
( ) */
while(wait4(-1, NULL, WNOHANG, NULL)>0);
/* */
}
24.3

-.

,
. , : ,
.
, , . ,
,
.

; ,
135

: ,
?
,
, , , .
.
, .
, , , ,
, ,
, , . , ,
. .
, , ,
. ,
, , , .
Unix select()
poll()1 . select() ; poll() ,
.
select() :
( , , ;
; );
;
.
:

int select(int n, fd_set


fd_set
fd_set
struct

*readfds,
*writefds,
*exceptfds,
timeval *timeout);

readfds, writefds exceptfds 1

, select() poll() . select()


, poll() .
, . , Solaris poll(), select() .
, kqueue(), .

136

, , ,
, . n ,
. max_d+1, max_d . , timeout ,
, ,
, .
  fd_set.
, , ,
, .
:

FD_ZERO(fd_set *set);
/*
FD_CLR(int fd, fd_set *set);/*
FD_SET(int fd, fd_set *set);/*
FD_ISSET(int fd, fd_set *set);
/*

*/
- */
- */
- -? */

timeval, ,
long. tv_sec , tv_usec
( ). , , - 5.3 :

struct timeval t;
t.tv_sec = 5;
t.tv_usec = 300000;
, ( ), ,
. , ,
NULL .
select() :
, ( ,
, ); -1.
, .
-1;
errno, EINTR.
137

-, , timeout (, ,
). 0.
- , readfds, , read() (
read() );
, , , , accept() ; , recvfrom() .. ,
  , read() .
- , writefds, , , write(), send()
- , . , , ,
, , - writefds, .
writefds ,
write().
- , exceptfds,
. , OOB
(out-of-band), . exceptfds , NULL.
select() , .
, select(),
: , . , FD_ISSET
, ,
( , , ..)
, select() . ,
ls; , .
, , out-of-band data , ,
readfds.
138

for(;;) { /* */
int fd;
fd_set readfds;
int max_d = ls;
/* ,
*/
FD_ZERO(&readfds); /* */
FD_SET(ls, &readfds);
/*
*/
/* */
for(fd=/* */ ;
/* ?*/
;
fd=/* */)
{
/* fd - */
/* */
FD_SET(fd, &readfds);
/* , ,
*/
if(fd > max_d) max_d = fd;
}
timeout.tv_sec = /*

*/;
timeout.tv_usec = /*
-
*/;
int res = select(max_d+1, &readfds, NULL, NULL, NULL);
if(res < 1) {
if(errno != EINTR) {
/* , select()' */
} else {
/* " */
}
continue; /* */
}
if(res == 0) {
/* "-" */
continue; /* */
}
if(FD_ISSET(ls, &readfds)) {
139

/* */
/*
accept()
*/

/* */
for(fd=/* */ ;
/* ?*/
;
fd=/* */)
{
if(FD_ISSET(fd, &readfds)) {
/* fd */
/* read()
recv() */
}
}
/* */

,
, , ,
- (. event-driven
programming)

25
25.1

Unix

Unix .
, . ,
,
. , ,
, , xterm.
.
,
140

2573

2573

57

2599
2581

2617
2597

276

2582
2581

3077
2764
2617

. 24:
. , .
, , 2 , ,
, .
, . ,
, , ( ,
).
( , fork() sid
pgid ).
. , :
( , , , ,
; ,
, );
(
);
2 foreground group, -

;

141

.
: ,
( sid pid')
, ( pgid
pid').
Unix . ,
(, ,
xterm), , , SIGHUP.
, , , SIGINT Ctrl-C:
, .
, . SIGTTIN.
, , , ,
.
. ,
.

25.2

,
. sid
pgid

int getsid(int pid);


int getpgid(int pid);
pid . 0 . ,
(pid) , ;
pid' . , ,
.

142

int setsid();
,
. ,
fork() , ,
, pid:

if(fork()>0) exit(0);
setsid();
setsid() , pid' , setsid(), , .
,
,
. setsid() ,
.
, open()
, .

int setpgid(int pid, int pgid);


pid ,
; pgid .
, ,
exec.
, pgid pid,
, .
, , .

int tcsetpgrp(int fd, int pgrp);


fd , ( 0 1). ,
ioctl()
.
143

25.3

, .
-, WWW . :
, crond
, .
; .
, . ,
, ,
..
, ( , ). ,
, , 0, 1 2 ,
, , , - 0, 1 2 ( ,
), -
, / .
/dev/null. ( -,
) . , , ,
 .
, - ,
(,
SIGHUP - , ), .
, - :

close(0);
close(1);
close(2);
open("/dev/null", O_RDONLY); /* stdin */
open("/dev/null", O_WRONLY); /* stdout */
open("/dev/null", O_WRONLY); /* stderr */
144

if(fork()>0) exit(0); /* change pid */


setsid();
chdir("/");
- , , , .., , .
.

void openlog(const char *ident, int option, int facility);


void syslog(int priority, const char *format, ...);
void closelog(void);
,

openlog(),

( ),

options

( -

0) , ,

facility.

(, )

LOG_USER.

syslog()

printf(). LOG_ERR , LOG_INFO

(,
,

LOG_WARN

). ,
.

, printf(). ,

syslog(LOG_INFO, "Daemon started with pid == %d", getpid());

closelog()

..
- ; ,

SIGHUP

. .

26

UNIX

, .
( ), ( ) .
3 . , , -

, ,

145

( 512 ,
), , ,
. ,
( ).
. , FreeBSD ,
, , ,
.., .
Linux (LILO) :
, ,
.
.

4 .

. ,
, , , .
.
 .
,
0.
, fork()
1. , ..
, fork().
1 execve(),
init ( /sbin/init; , ). , C5 ()
. init ;
. init , /
4

, GRUB, ,
LILO. Linux LILO GRUB .
5 init - ,

146

.
init , ,
; -; , ,
getty,
. getty , ,
, , exec
. , init getty
.
, init . ,
,
( /etc/rc,
/etc/rc.d/rc ..) init ( ),
. ,
(Bourne Shell).
, init, ,
, .
( SIGTERM, , , SIGKILL),
, (
),  , ( ,
). .

147

12
27
27.1

(race condition)

,
.
.
, .
5. 1 (
7). ,
1 1 .
, mov ( 5),
,
,
. mov,
inc mov
. 6.
,
. mov ,
5. inc ( 6)
mov ( 6 ).
, ,
6, 7. , 7, . ,
, , , , .
, , .
(. race condition );
 .
1

,
, .. . , ,
: ,

148

. ,
. , 301,
515 768 , , $1000, $1500 $2000.
$100 301 768,
$200 768 515.
, , ,
$900, $1700 $1900.
, , ,
515 768. (, $1500 $2000)
, ,
, . 301 768 (,
, $1000 $2000), ($900
$2100) , .
, ;
515 768
($1500 $2000), $1700 $1800.
.
768 $100
( $1800 $1900). ,
301, 515 768 $100,
.
, , :
, , , ,
$200 ($2100 $1900). ,
, , .
,
.
. ,
-
( ,
) ( ,
),
( , ,
, ,
, ). , -

,
, .
149

27.2

, , ,
: , , , ,
, , .
,
. , 
,
,
,
. , ,
. , .
, , .
.

, .

, , ,
;
, , .
,
. ,
, - ,
.
x f, ..
, ( )

, (. mutual exception ).
, , .
, :
1.
150

,
.
2. 2 .
3. , ,
.
4.   ( ,

).
5. , , (
)3 .

27.3

, , . , ..
.

27.3.1

,
. ( s) , 1
, , 0

, .
s 1.
:
2

, , : , -
,  , (
)
3 , ;
, ,

151

while(s == 0) {} /* ,
*/
s = 0;
/* */
section(); /* ... ... */
s = 1;

/* */

, ,
, ,
( section())
, 0 s, ,
, ,
s 1.
, . ,  1 s
while, 0 .
 1,
0 ; ,
, .
, , .
. , while
s = 0 , , .

27.3.2

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

, , .,
, , ,
( ), - , , , .
-, .
.
, ( ) ,
(  )
,
.
( , , )
. ( ),
. , , , , ,  .
.
,
; , , .

27.3.3

,

.

for(;;) {
while(turn != 0) {}
section();
turn = 1;
noncritical_job();
}

for(;;) {
while(turn != 1) {}
section();
turn = 0;
noncritical_job();
}

. 25:
153

.25 , ( section()) ,
turn. 0 ,
, 1
. ,
 ,
( noncritical_job()).

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

27.3.4

.
4 .


interested[2], , (
) ; .
, ( ) who_waits,
, .
, ,
( interested),
, , who_waits .
, who_waits
( , ), .
. 26 :
enter_section()
(

; ,
(bakery algorithm)

154

void enter_section() {
interested[0] = TRUE;
who_waits = 0;
while(who_waits==0 &&
interested[1]) {}
}
void leave_section() {
interested[0] = FALSE;
}

void enter_section() {
interested[1] = TRUE;
who_waits = 1;
while(who_waits==1 &&
interested[0]) {}
}
void leave_section() {
interested[1] = FALSE;
}

. 26:

leave_section() ( )

, , (.
bakery algorithm), . ,
, .

27.4

27.4.1

, , , ,
, ,
, .
, ,
, , , ,
,
. ,
,
;
,

.
. ,
, , 155

sleep(), .
, .. .
, / ,
.
, , , . , , , ,
, 5 . , .

27.4.2

6 , (/) , , : lock() ()
unlock() ().
unlock() ( ) , .
lock() :
1. .  ().
 (). .
2. , . ;
, ,
.

lock() unlock()

5 ,

, ..
:
, / , ..
6 . mutex mutual exception

156

7 . , lock()

.
(27.3.1). ( , ,
s), s = 0 s = 1 lock()
unlock(). lock():

while(!lock(s)) {} /* , */
section();
/* ... ... */
unlock(s);
/* */
,
. lock() ,
( lock()) ,
(
), ,
.
lock() (
, ),
, :

lock(s);
section();
unlock(s);

/* , */
/* ... ... */
/* */

, : ,
, , 8 . ,
.

27.4.3

.
7

, lock()

8 , , lock() ,

157

, ,
, : up()
down(). up() , 1, . down() ,
, 1, ,
,
. , down() 1
.
, , .

, : , (
, ). , ,
,
.
,
, 0 , 1 , lock() unlock() down() up().
, up()
, ,
up() ( ), ,
down(), .
, , .
, -

. up(sem, n) n,
down(sem, n) , n, n. ,
down(), lock() , . ,
, , .
, .
System V IPC (. [5]) , , ,
, (!) . , ,
.

158

27.4.4

TSL


. TSL (test and set lock,
). : . , ()
, , ,
1. , ( )
, -
.  ,
. , .
lock() unlock()
:

mutex: DB 0
lock:
TSL RX, mutex
CMP RX, 0
JE ok
JMP lock
ok:
RET
unlock: MOV mutex, 0
RET
, .
, . , (), 9
(
).

lock()

(.. ) -

, .

spinlock.

159

13
.

28
28.1

, ,
. , , ,
- ; ,
; ,
, , . .
, , ( ) , -.
.
, , . , ,
. , N .
, . , 1 ,

.

. , () , :
,
( );
, ( ).
1

, , , , ,

160

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

void producer() {
/* ...
...
*/
down(empty);
lock(m);
put_data();
unlock(m);
up(full);
}

void consumer() {
down(full);
lock(m);
get_data();
unlock(m);
up(empty);
/* ...
...
*/
}

. 27:
. :
( , ), , ( -,
). , , empty
full; ( ) up(empty) , .
, , m,

161

. 28:

put_data() get_data(). . 27.

,
, , .

28.2


,
, ,
,
, .. .

28.2.1

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

2 . ,
, , ;
, , . ,
, . , ,
,
(, ,
, ,
).
, ,
,
.
, ( ,
).
, ,
. ,
, , .
, . !
3 .

28.2.2

, ,
. m1 m2.
,

lock(m1);
lock(m2);
,
:
2

, , ,
: ,
, , . ,
(, ).
3 deadlock.

163

lock(m2);
lock(m1);

, ,
, .

28.2.3


. .
ls
.
, :

char buf[100];
int rc;
int fd[2];
pipe(fd);
if(fork()==0) {
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
execlp("ls", "ls", NULL);
perror("ls");
exit(1);
}
close(fd[1]);
wait(NULL);
while((rc = read(fd[0], buf, sizeof(buf)))>0) {
/* ... */
}
, , , ,
. , , , ,

, .
, .
, .
ls 164

, , ,
.
, -,
wait(), ,
.
.
, ( ls) . 19.1,
( 4096 ), ,
, write(), ls,
. , ,
wait(), read() , .
, : ,
, , ,
, , ,
.
.
, , , , ls 4096 .
, :
wait() , read().

28.2.4

, . , (
forks4 ). , 0 4. ,
:

int left(int n) { return (n - 1 + 5) % 5; }


int right(int n) { return (n + 1) % 5; }
4

fork

.

165

, , ,
.

:

void philosopher(int n) {
for(;;) {
think();
lock(forks[n]);
/* ! */
lock(forks[right(n)]);
eat();
unlock(forks[n]);
unlock(forks[right(n)]);
}
}
, n 0 4 , ,
.  () ,
.

, .
sem. :

void philosopher(int n) {
for(;;) {
think();
down(sem);
lock(forks[n]); lock(forks[right(n)]);
eat();
unlock(forks[n]); unlock(forks[right(n)]);
up(sem);
}
}
, . , 1,
. ,
166

, , . , ,
,  
, .
, , . ,
,
, , .
[7] . 5 ,
. , : hungry, thinking eating;
state. , , ,
, .
, ( , ) . ,
, ,
, , unlock(); ,
.  , ,
. (
) , , , . , ,
, , , ,
, .
, , , : ,
.
state.
.
test(). , , , . ,
test() (
), ,
, ,
( , ,
).
5

, , .

167

enum possible_states { hungry, eating, thinking };


int state[5] = { thinking, thinking, thinking, thinking, thinking };
mutex mut[5]; //
mutex state_mut; //
void philosopher(int n) {
for(;;) {
think();
take_forks(n);
eat();
put_forks(n);
}
}
void take_forks(int i) {
lock(state_mut);
state[i] = hungry;
test(i);
unlock(state_mut);
lock(mut[i]);
/* ,
, */
}
void put_forks(int i) {
lock(state_mut);
state[i] = thinking;
/* , */
test(left(i));
test(right(i));
unlock(state_mut);
}
void test(int i) {
if(state[i] == hungry &&
state[left(i)] != eating && state[right(i)] != eating)
{ /* i- */
state[i] = eating;
unlock(mut[i]);
}
}

28.2.5

; .
,
( ) (
).   168

. 29:
. , 
 .

. 29 . ;
. ( . 163).
,
() .

28.3

, () , () .
, ( ). , ,
,
, ,
(. . 149).
,
( ),
.
, , ,
, ,
169

, .
, ( rc readers count).
, , ,
, , . db_mutex,
rc rc_mutex.
()
:

void writer(...) {
lock(db_mutex);
/* ... ... */
unlock(db_mutex);
}
 , .. rc.  ,
. , , , rc; ,
,
(, , )
db_mutex.
, , rc (rc_mutex ,
).

void reader(...) {
lock(rc_mutex);
rc++;
if(rc == 1) lock(db_mutex); /* ! */
unlock(rc_mutex);
/* ... ... */
lock(rc_mutex);
rc--;
if(rc == 0) unlock(db_mutex); /* , */
unlock(rc_mutex);
}
170

14
29
29.1

Unix
Unix

Unix : System V IPC POSIX.

29.1.1

System V IPC

System V IPC , :
;
1 ;
.
, , .
IPC ,
( , ). ,
System V IPC , .
System V ,
. (, ,
,
).
. , , , System V IPC 
, . ,
System V IPC: ,
 .
, System V IPC
, .
1

, ,
mmap()

171

System V IPC , .
System V IPC, , [5].

29.1.2

POSIX

, Unix, . : 1 (
), ,
 .
, . , , ;
.
POSIX threads (pthreads) , .
( , Linux) POSIX ().

29.2

Pthreads: Unix


(
, ..
). , thread.
1995 , , pthreads.
Unix, Windows.
pthreads, (
, main()) :

void* my_thread_main(void *arg) {


/* ... */
}
, (void*); , -,
172

. , ,
.
,
pthreads_t.
( )

int pthread_create(pthread_t* thr, pthread_attr_t* attr,


void*(*start_routine)(void*), void* arg);
thr , . attr
;
, . start_routine .
,
, pthread_create
arg.
pthread_create 0 , , (
EAGAIN, ,
,
).
: ( ,
main())

void pthread_exit(void *retval);


,
(, void*),
retval. , exit().

int pthread_join(pthread_t th, void **result);


th ,
. result void*, .
waitpid() .
173

- ; pthread_join(),
, ().
, 
(. detached mode ).

int pthread_detach(pthread_t th);


 ,
pthread_join() , ,
.
pthread_detach() pthread_join() ,
pthread_create(), 0 , , .

pthread_t pthread_self();
,

,

pthread_detach(pthread_self());.
,

int pthread_cancel(pthread_t th);


th
PTHREAD_CANCELED.
,

pthread_cancel() ,

, ,

, : , ,
pthread, (. cancellation points ). , , .
, ,

29.3

pthread_cancel().

pthreads

pthreads
pthreads_mutex_t. ,  ,
PTHREAD_MUTEX_INITIALIZER, :

pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;


174

, ,
, . ,
PTHREAD_MUTEX_INITIALIZER ,
.., , , , (
).
, ,
( ), : (unlock()) (lock()),
,
, , , ,
, ( ),
(, , ), ,
- ,
( ).
pthreads

int pthread_mutex_unlock(pthread_mutex_t *mutex);


int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
, , (unlock),
(lock) (trylock). 0 ,
, , pthread_mutex_trylock() , EAGAIN.

int pthread_mutex_destroy(pthread_mutex_t *mutex);


, (, Linux
pthread_mutex_t, ).
, .

29.4

POSIX-

, POSIX
, , , 175

. POSIX- (, , Linux) POSIX .


sem_t (POSIX , ,
pthreads Linux ,
, ).

int sem_init(sem_t *sem, int pshared, unsigned int value);


sem . pshared
, ; ,
,
. , value .
, , , , , : up() down().
1 . , ,
, - ( , ),
1 .
POSIX

int sem_post(sem_t *sem);


int sem_wait(sem_t *sem);

/* up()
*/
/* down() */

down():

int sem_trywait(sem_t *sem);


, 0, ,
, ,
EAGAIN.

int sem_getvalue(sem_t *sem, int *sval);


sval.
,
176

int sem_destroy(sem_t *sem);


,
,
sem_wait() , sem_destroy(). , Linux , , - .

29.5

, -.
, . , , FIFO -
, ,
.. .
: ,
, ( ,
, ).
, . , , . , , , , , .
(
), .
, .
,
N ( ).
-.
,
. ,
,
177

: , , ,
.
.
, ,
,
.
, ( )
-. ,  . , .
,
; , .
. 179181.
,
mkfifo. xterm, ,
. cat . ,
.
cat; .

178

#include
#include
#include
#include
#include

<stdio.h>
<unistd.h>
<pthread.h>
<semaphore.h>
<math.h>

#define BUFFER_SIZE 4096


/* */
struct buf_str {
int count;
double values[BUFFER_SIZE];
} buffer;
void init_buffer()
{
buffer.count = 0;
}
void put_buffer_item(double v)
{
buffer.values[buffer.count] = v;
buffer.count++;
}
double get_buffer_item()
{
buffer.count--;
return buffer.values[buffer.count];
}
/* */
sem_t buf_empty;
sem_t buf_full;
pthread_mutex_t buf_mutex = PTHREAD_MUTEX_INITIALIZER;
/* */
double grand_total = 0;
long grand_count = 0;
pthread_mutex_t grand_mutex = PTHREAD_MUTEX_INITIALIZER;
/* "" */
sem_t producers_count;
179

/* ("") */
void *producer_thread(void *v)
{
/* v */
double val;
FILE *f = fopen((char*)v, "r");
if(!f) return NULL;
sem_post(&producers_count);
while(!feof(f)) {
if(1 != fscanf(f, "%lf", &val)) continue;
sem_wait(&buf_empty);
/* */
pthread_mutex_lock(&buf_mutex);
put_buffer_item(val);
pthread_mutex_unlock(&buf_mutex);
sem_post(&buf_full);
/*
------*/
}
sem_trywait(&producers_count);
return NULL;
}
/* -. */
void *consumer_thread(void *ignored)
{
double local_total = 0; /* */
long local_count = 0;
for(;;) {
double val;
sem_wait(&buf_full);
/* */
pthread_mutex_lock(&buf_mutex);
val = get_buffer_item();
pthread_mutex_unlock(&buf_mutex);
sem_post(&buf_empty);
/*
------*/
/* */
local_total += log(val); local_count++;
/* , */
if(0==pthread_mutex_trylock(&grand_mutex)) {
grand_total += local_total;
grand_count += local_count;
local_total = 0; local_count = 0;
pthread_mutex_unlock(&grand_mutex);
}
}
}
180

int main(int argc, char **argv)


{
pthread_t thr;
int i;
/* */
init_buffer();
sem_init(&buf_empty, 0, BUFFER_SIZE);
sem_init(&buf_full, 0, 0);
sem_init(&producers_count, 0, 0);
/* "" */
for(i = 1; i<argc; i++)
pthread_create(&thr, NULL, producer_thread, (void*)argv[i]);
/* "" */
for(i = 0; i<10; i++)
pthread_create(&thr, NULL, consumer_thread, NULL);

/* 5 */
for(;;) {
int p_c;
sleep(5);
pthread_mutex_lock(&grand_mutex);
/* 0 */
if(grand_count>0) {
printf("total average: %f (sum = %f; count = %ld)\n",
grand_total/((double)grand_count),
grand_total, grand_count);
} else {
printf("No data yet...\n");
}
grand_total = 0; grand_count = 0;
pthread_mutex_unlock(&grand_mutex);
sem_getvalue(&producers_count, &p_c);
if(p_c == 0) {
printf("No more producers\n");
break;
}
}
return 0;

181

15
30

Unix. X Window

, Unix ,
. ,
.

30.1

X Window

, Unix () , X Window System.


XWindows. , X Window . window () .
, , , Unix. , Unix-, ,
, (stdin, stdout, stderr).
, X Window , X-.
  , X.
.
; - ,
- .
,
(),
X-. (), ,
.
, , , X- . , , ; , , 182

, ,
,
, ..
X- (
SOCK_STREAM), X-
AF_UNIX, AF_INET,
X- . , X Window ,
.
, X- ,
, , . , ,
X. X- .
, X Window
System XFree86 X.org ( ).
Linux, FreeBSD; Linux XFree86, X.org, (, , Openwall/*/Linux) , .. , .
X Window. ,
X-, Win32 (MS Windows)
MS Windows ( Unix-) .

30.2

X- (
X), ,
X, , ,
X-,
.
, ,
X-. - ,
, X-
(

X :1

X :1.0).

,
.
, .
183

, Ctrl-Alt-F11 , , X-, Ctrl-Z bg


X . - , xterm.
 X-,
, X-
. ( Bourne Shell)
:

DISPLAY=:0.0 xterm
X- X-, 

:1.0.

:0.0

X- ( ,
Alt-F7, Alt-F8 ..).
, xterm ,
, , .
. ,
-
. , ,
, ,
! , , ,
.
, , X Window X-,
.
.
X-, ( ) . ,
, .
, ,
.
, .
X Window , .
1 ,

Linux FreeBSD,

184

, - , twm, X Window.
, X-, xterm,
. ,
twm. .
, , ..
-
, ,

xeyes,

twm.

X Window ,
.
X- X-,
.
, twm. , .
twm , .

,  

fvwm95,

fvwm2

MS Windows-95. -

- ,
.

KDE Gnome
, , , , .

30.3

X-

,
, X-, ,
X- . X-, ,
X-. X.
X- - , ,
. . ,
, .
X- 185

, , , ( , ..),
X- , ( X-), . ,
,
, , , , X- . X Window
. , xdm (X Display Manager).
X- xdm XDMCP (X Display Manager Control Protocol). xdm getty:
, ( ) .

, X-
, ()
, , .
X-, , xdm (X-) , ,
. , X , () .
, xdm,
XDMCP. , , X- ( !),
X-, , (!), X-.
, X Window System , , X, (
) , , . , ,
. X Window System X- , , .
X- 186

. X-
, , . , .
X- ,
. ,
.
, , , ,
. X-
,
.

187


,
.
, Unix- Solar
Designer, .


[1] . . Unix. .:, 1986.
[2] . . . Unix. - BHV, -, 1997.
[3] . . Unix. .: -
, 2005.
[4] , . Just For Fun ( ). .: - -, 2002.
[5] . UNIX: . .: , 2002.
[6] . . . UNIX: . .: ,
2004.
[7] . . . 2- . .:
, 2002.
[8] . . . 4- . .: ,
2003.
[9] Dennis M. Ritchie. The Evolution of the Unix Time-sharing System.
In: Lecture Notes in Computer Science 79: Language Design and
Programming Methodology, Springer-Verlag, 1980. Online version:

http://cm.bell-labs.com/cm/cs/who/dmr/hist.html
188

2.1

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

2.2

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

2.3

()

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

2.4

( ) . . . . . . . . . . . . . . .

2.5

( ) . . . . . . . . . . . . . . . . . .

10

2.6

( )

11

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

11

2
4

13

13

4.1

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

13

4.2

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

14

4.3

. .

15

4.4

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

17

4.5

. . . . .

17

20

5.1

() . . . . . . . . . . . . . . . . . . . . . . . .

21

5.2

() . . . . . . . . . . . . . . . . . . . . . . . .

23

5.3

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

24

. .

25

6.1

27

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

28

28

29

8.1

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

30

8.2

: . . . . . . . . . . . . . . . . . . . . . . .

31

8.3

33

4
9

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

42

Unix

42

189

10 Unix

45

10.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

45

10.2 .

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

47

10.3 . . . . . . . . . . . . . . . . . . . . . . . . . . .

49

10.4 -

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

49

10.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50

10.6

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

51

10.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

52

10.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

54

56

11 -

56

11.1 . . . . . . . . . . . . . . . . . . . . . . . . . .

56

11.2 - . . . . . . . . . . . . . . . . . . . . . . . . . .

57

11.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

58

11.4 -

59

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

11.5 -

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

61

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

62

11.7 - . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

11.6

71

12 -

71

12.1 . . . . . . . . . . . . . . . . . . . . . . . . .

71

12.2 Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

72

12.3 . . . . . . . . . . . . . . . . . . . .

78

12.4 . . . . . . . . . . . . . . . . . .

82

86

13 :

86

13.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

86

13.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

87

14 Unix

88

14.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

88

14.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

90

14.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

95

15 (race condition)

96

98
190

16

98

16.1 . . . . . . . . . . . . . . . . . . . . . . . . . . .

98

16.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

98

16.3

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

99

16.4 . . . . . . . . . . . . . . . . . . . . . . .

99

umask

16.5 . . . . . . . . . . . . . . . . . . . . 101

17 Unix

101

18

103

18.1 . . . . . . . . . . . . . . . . . . . . . . . 103
18.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
18.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
18.4

alarm()

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

18.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

19

108

19.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
19.2

. . . . . 111

19.3 (FIFO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

114

20 ;

114
21

116

22

118

10

120

23 .

120

23.1 . ISO OSI . . . . . . . . . . . . . . . . . . . . . . . 120


23.2 . . . . . . . . . . . . . . 121
23.3 .

bind()

. . . . . . . . . . . . . . . . . . . . 123

23.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
23.5 . -

. . . . . . . . . . . . . . . . . . 125

23.6

. . . . . . . . . . . 131

11

132

24

132

24.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
24.2 . . . . . . . . . . . . . . . . . . 134
24.3 -. -

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

191

25 Unix
25.1

140

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

25.2 . . . . . . . . . . . . . . . . . . . . . . . . . 142
25.3 -

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

26 UNIX

145

12

148

27

148

27.1 (race condition)

. . . . . . . . . . . . . . . . . . . . . . . . . . 148

27.2 . . . . . . . . . . . . . . . . . . . . . . 150
27.3 . . . . . . . . . . 151
27.4

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

13

160

28

160

28.1 . . . . . . . . . . . . . . . . . . . . . . 160
28.2
28.3

. . . . . . . . . . . . . . . . . 162

. . . . . . . . . . . . . . . . . . . . . . . . . 169

14

171

29 Unix
29.1 Unix

171

. . . . . . . . . . . . . . . . . . . . . . . . . . 171

29.2 Pthreads: Unix . . . . . . . . . . . . . . . . . . . . 172


29.3 pthreads
29.4 POSIX-

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

29.5

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

15

182

30 Unix. X Window
30.1 X Window
30.2

182

. . . . . . . . . . . . . . . . . . . . 182

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

30.3 X- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

188

188

192

You might also like