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




    
 ! "#$ %&' ()$!' $*
«
    

 
  
   »

.. , .. !"

    

+
 
,- . 


    
  -  
   
  

     

  
        ,
 
 !  

   



230100.62 «"
 
  
 ! 
»

!
/0' ($1$"0' $$
2010

1
 681.3.066(076.5)
 32.973-018.273
-55

 .
.
-55 


.     : 



/ ..  , ..  ; !  "# "
 -

"# $ "
%
 

. – $: &'-
$* "
%
* 

 , 2010. – 122 .
ISBN 978-5-98298-599-6




'"
 "#  '
   
 
-
% 
 % 

 Unix (Linux), ' 
* % 
 
 "#' 
 
 "'  % ",  +
  "
4 
-
      
  % % ' .
<
 ' 
 " 
,  4=%   "
4 230100
«&>    "
"#  
% ».

681.3.066(076.5)
 32.973-018.273



 
%
%  

  > "#
 >   $?
.
.  
   
%
%  
  "# 
"    
 – ' .   "#
 "
 >  % 
%"*  «* ' »

.. 

ISBN 978-5-98298-599-6 © ? < «!  "# "


 
"#
$ "
%
 

», 2010
©   ..,   .., 2010
© > "

. &' 
"# $*
"
%
* 

 , 2010

2



 ..................................................................................................5
1.        1.   
 
     UNIX..........6
1.1. !@ A$@  <CAD&!!E &$CFC UNIX ......6
1.1.1. &
>
      
 % Unix ..................6
1.1.2.  
   "
bash...............16
1.1.3. G "  
 ................................................................22
1.2. <C$CH!$H @<!C!&J A$@.............33
1.3. $AC!&J  $KC$  A$C M 1..................................34
2.        2.   
 
     
GNU MAKE    

 UNIX.......35
2.1. !@ &<H!&J $&&$@ <$AC!&J
<AC$ MAKE.........................................................................35
2.1.1. 
"   " make .....................35
2.1.2. < 

*  "#'  " make .....41
2.2. <C$CH!$H @<!C!&J A$@.............52
2.3. $AC!&J  $KC$  A$C M 2..................................52
3.        3.   
 
      
 

 UNIX .......................................................................53
3.1. <AC!&C <$F&........................................................53
3.1.1. <
 ....................................................................53
3.1.2. <
=
  
   "#'   ......54
3.1.3. < *   
................................................55
3.1.4. % '   ......................................................59
3.1.5.  "  *  * ...........................68
3.1.6. 
 "  *  *  ...........69
3.1.7. < 
 

"' ...................................70
3.2. <C$CH!$H @<!C!&J A$@.............78
3.3. $AC!&J  $KC$  A$C M 3..................................78

3
4.        4.   
 
   ,   !
"       ..............79
4.1. <ADC@ & FCV<ADC!C &FCE$&C .....79
4.1.1. <

 .................................................................79
4.1.2. F
+ 

' 

........................................80
4.1.3.  
   
   "
bash.........82
4.1.4. F
% ' 
+ 
* ' 

  Unix ..............................................................................89
4.1.5. 

 =
.............................................................96
4.1.6. A   '
"
 #4 ...........................................99
4.1.7. < 
 

"' .................................100
4.2. <C$CH!$H @<!C!&J A$@...........119
4.3. $AC!&J  $KC$  A$C M 4................................120
 # ......................................................................................120
   !.....................................................................121

 
&>  -"
"#

   
"

 +

    +
" 

 ". W ' ,

+

*,  
,  "#' 
"  % *  ' 
4
 "
"# 
% 
 
 "#* (
*)
* * 


 – 

' 
4 
 (). ! 
-
*Y 
# =

 +
 '"% ,  
% 
'   (OC 

 Windows)    %
 % ( 

-
 Unix). <  \, " *  
 « » % 
, 

"

"

% " '


  '% 
 > -
  .
Unix- 
 "4    "  +
"


% 
"
,  "  – 
# 
#
'  . !
  
"#Y
" '
 
 Unix, 
% ]

  -
% 
,  %   ' *   , "+
  
 
 
,  "
    %

 
>
%

Y
 (POSIX, System V),  "#' 

, "
* "+
-
 
%
 > " 
, '"%  "
"#%

, "4  
 ,
 ' 

"  =
  
* % 
.



, "4   

 Unix, 
+ 4 -
*'  #  * #. <  *   

-


 +
  # 
"# '  ,   +  ' '   +

# '  '  ,  "

""
"#. " \*  -


% 

    % 
% ', '"4-
=%   \>>
 

# % ' 4 
 


   % 
+ ,  
 '+#
Y # '   
'     
' ,     

 
+ .
  %
" *
* 
*  
 
 "-

 '+# "# 



     '
'
  Unix – Linux  


Y
 
% ' -
 : '  * * 
  =#4 " "  -
'     * % 
 GNU make; ' 
* * "+
  '  * 
* "+
-
   "#' 
   % 
+ 
% 
 % -
'  

   % 
+ "
  

.

5
1.        1.   
 
     UNIX
$  !
' #  
 
 Unix, "# 
-

     "

  
   
-

 
bash, '#     * '  > " 
 Unix
  '%    "
 > " .
 
=
#  " "# 
  =#4 *  PuTTY 

'
" ssh    "
  #4
 (
%
 

"   '



  
"#)   "

  Linux  -
"#
+
. ' # 


 %  ,  "#-
'
% "#' 
"  Linux  
 

.

1.1. 
!  !
   UNIX

1.1.1. %&'%*+ /:;:* +&:/ < ++%= Unix


 Unix- %   '  
# =
  "#' 
"

' "4
  
 "       
 
-
 >     "

(    =


    ' 4
«
>
    »). < «"   »  « "

   "
 * "  
' 
"# '  4
"#-

   #4
,    
 "#' 
"#. ! -

, « "

» +
 #  * >
 
 "#' 
" "

>
 *   "
*  ,  "#'4=
 "
telnet " ssh. !  \ "  \* 
'

:  ' -

>
 "#' 
"
 " *
      
 -
>  . " *,  
# «  "##»  
   
 , % 

 ' 4 
 ".
Unix – * "#' 
"#  , "
 
"#,  +  #-
4
  "

 \  +
 
# +
 
 ",
 '"
 

  # * "#' 
". $
 -
" * # 


 "4
   #4
 " =
-
 # *
-"  
 (" "# " *" "#). 



 -
" 
 "4   #4
  
 ,  -
 % ' =
 *   "
*  ,     

6
* 
telnet,  4=
   "#' 
 

% " 

TCP.
  =


 
 telnet   "#'4 * ,
 4=
 ' ==
 Y>  
 " ssh.
&
>
     – 


   =
 
 Unix ( 
, =
4 * >


>
 "#' 
-
": KDE, GNOME, Xfse  . ., '"
> "
"  . .), 

  Unix 

 #    " 
#  +. 
* \ ""
 *      '%  , % 
'-
  *#4  '+#4 
*  "#'  "
 '     %.
%>+&?
 
% Unix- % ,  "
%  
  #-
4

, 

 
   '  % "#' 
"
, 
4=% 
 "#' 
  \*  #4
. <"#' 
"
, 
"4
-
%  \  , 
  

 .  '  % 


    #4
 
+ "  +* "#' 
"
"
4=4 >  4:
x
* 
;
x ' Y>   "#;
x 
>  "#' 
" (User ID – UID);
x   *  ,   
"4
 "#' 
"#;
x #    "
;
x #   Y
   "*;
x  *   "
"#  >  .
A
* 
  "# 
% " 
 
*-
 "#' 
". &
>  UID 
  

>  
  


  "#'

. F
% '
*  '"
 ]
# "#' 
"
  

"
 "-
    > "   *  . <#    "

+
 "

'  "



 
* . &,  
, -
 Y   "* – \  
  > " 

,
" -
 "
+ =

  "#' 
"4 , 
,   .
@C: &D:F
K   #   Unix, + "#   
 " 
'
*   #  

.  "
 "
  "4



 " + =
# '  *  
+ 4=

" ssh  


  
  #4
,   
 " -

7
*
 
 . *  #4
* '
*   # "#' -

",  \ 
 +
 *" Y

 
* 
:
login: _
 
  \ *" Y

+ 

* 
,
*"  
    " " 
"#
  #4
( -
,   
  #4

"#' 
"#    
 #

 "#' 
"
). & "#' 
"


  "# '
 % " %   > . !
 

 ( 
, root, ftp
 . .) * # '
'
   " 
%
"
  
*
#    "#' 
"4.    "   "#-
Y

 –  root,  
 "
+    "
" 
"#   #4
. W  
 
 
* 

-
   "
4 
. <"#' 
"    root    ' -
4 "
*  .
<"
 
   +  " Y Enter («Return», «Ret»,
«CR») 
 

  \  '    ". ! 
:
login: alex
password: _

#   "

  "*% 
% 4 "#' 
"

>  4 
 
"#  * Y >,  '" " #

 % 
 ".
 "  +
' 
Y
  + 
 " Y Enter. -
 "# 
 +
  \ 
. C" "#' 
"#   ' 


 =

 
* "# 

 "#, 
 
"
 -
 Y   "* "#' 
" 
=  '   4 "-
, ' 4    "#' 
"
. "   "


    "#    ,   +
 
 
-


=

,  ' #   "
" 
 , -
"#   "#     (
\ 
 '   
-

    
 ). &,  
,  \ 
 -
*" Y

   :
$_
< *" Y


' 
"# 

   ,    '  Y


,
 '   
   " 

>* .
:;F% :D:C:@/  Unix
 
 % Unix  "#'4 '"
 
"
(command shells),  '

 +
  
  " -

8


    . 
 %  "

'
    
-
:
x sh (Bourne shell) – "   (   


,  

"Y   


);
x csh (C-shell) – " (
"# "

   
-
4  sh,  

  
    ');
x ksh (Korn shell) – "   ("4
 =  -
 ',    '
sh,  '

 

);
x bash (Bourne-Again Shell) – « » " «  » (
" 
 , '   
sh   *  
 -

 ).
$ ",   ", + 

"#  "

 -
" *" Y
: '  "" («$»)  '
  sh-
4
" (sh, bash, ksh), '  
  («&») 

 "-

csh.    "
*  * "#' 
", 
'   -
"#'
*  * 
 , "
 " *" Y
-
  
 ' 
Y
 («#»).
 >   % "
 "4:
x  * '   "*  "#' 
"
 (  );
x  "


%  ;
x '  
Y% * ;
x  "

 % > ".
'+  % '  

Unix "4 * '
"

", 
  

MS-DOS,   "
* #  ' 
"
 '  *   .  
'  '%
" % '" 4,      #  
' " bash.
:;F Unix  HJL+/ J&:>&
=      Unix- %  *" "
4-
=  ':
_
[  ...] [
...]
<
 \"

 ' 
 
4  , *
 ("4-
  
 ) * = #  "
"#4 >  4. "4
   4  '  «». ! 
,  
ls -l -a /home
:

9
x ' 
   «ls», =
   > "  '  
  "*
;
x "4 (>  ) «l»,  ' 4=
*,  + 

  "*;
x "4 «a»,  ' 4=
*,  + # 
> ",
"4  "+

(«-> "»);
x 
 «/home», '  4=
* #    "*.
   %  Unix, % "4 %  
 %
* 
( 
" ' *" 
) '"
. " "#Y
     % -

 ' #    . "4  *% " % *
]
#   *  . ! 
,  
ls -la /home
"#4 \ "
 
 Y
.
  '
"4  

,  
 "4 -
  
 ,  
Y
. 
%    
-
*, %      * ' 
#   "#'
  
". <   "#'  bash "       

%   + "#,    "
*" Y
  -
 «help».

Y
 
 "4  '  * , 
' -
%  ". " '  *    ' 



-
 
%,  #  \ * 
"  '  




 PATH (  "* 



 
  MS-DOS). C"
*  
 
    "* %,

"
%  PATH,

 
-

 *  "+
 #   '  #,  +

" * 
 %  
=
   "*
(%  

% Unix 
 % \
+



). ! 
, '  *  hello ' 
=
*  -
 "* +
 *"
#  :
$ ./hello
 \ 

'  ""    "
 
 "
 
*" Y

 , >  


,     – -
>  4, 

4 "#' 
"
.
< 


 
 PATH 
"4  *   * '  

 
'  "4=% 
". C" 
  \  "*

%   "*, %=%  PATH, +    ' "# "-
   
 
Y%   
,    =
"

 . "

 
#,  ' 

"4


 
 +-
 "#,  '   

 


 
Y
4=
'  "" . ! 
,   

10
$ echo $PATH
 +

$PATH 
 ' 

   

    -

+



 PATH.  ,  

  echo ' -
"4
  
      
* *
 , 
 \   
 
 
+



 
 PATH.
H%%% J&:C
<
    ,  4 "

  "# 
 


  

–   '

 
* ": passwd.
W   '
 
' 
 . <"

'   \ 

 *" Y


   "# (
" " 
", \
Y * +
 #  =
). <"
"#*   * "


"+
 
  "#, ' 
 

*
=
' "
"4
 "  Y. < "  
 + # 
-
. !+

 "
 
 "    passwd:
$ passwd
Changing password for alex
Old password:
New password:
Re-type new password:
$_
"  "
 =
4 

"

" . 


 
  ,  "# 
"+
 # * 
. !

   # 
     
, 
"

  "# "#
'  % "  
%  "  
  . . < "#
"+
 
+ #  Y
#-
# "  "4 # 
-


  , > , 
>  
 .  

%

 %
 
"#  "#' 
"% "
 ' 12- 
"

". F*

"'    passwd  4 

-
"# *# * "  

+ 4=
=
-
,
" "# 
 
. $  "
, * passwd +

# «
"#» –  " "# '  % .
!
"

  #,  Y
 ' 
" 
'
.  +

"  Y  #4


 %
 
# Internet 

' 





+
  #   * "4,  
*  -
 # 
# "# " ' # "# 

   
.
<\  "# +
 =

  # '   '"-
Y"
  # «
» "#' 
"4 
#  *.

11
:CL@%% +J&<:@:* ':&?

 Unix,   ",  "4  *  "

  >    \"
  
.    >  
'  '
"  
 
. !
 '
"  '% 
 %
+
 # '. <+ ",       "#'
  >  

+  '
"
1, *
   4    " 

* ,  
 "#' 
" 
. 

" %
'
"  
 
 "  * '      '

«  » (manual page).  +       
+ 
-
 4  
  
, > , 
>
, "  . .
 
"# +
 # *  
. " "
-
   >   +  "#' #  
man [  ] [ ]
" "
     "#' 4   " * 
*
 «"4» "+
 # 

 
4=
   "
* . < 
 « '
"» +

 "#  >  ("
) 
'
"  % ,     %
+    
 . 
,  
'
"  ' #

' 
"#, . . 
*  
  

 %-
= '
", *


 +  
 . K "#    -
"#'      man, =

* 

$ man man
=
4   *
"
'
  "    -
  .   ,  
apropos [ ]
'"
    



# 
%   ,  


+    
 * 
 '  
"4

" "4.
   >   man   "# " 
Y%  . "
"
  '  
    " 
%
 "#' #   help,  

$ help cd
&:+%*O% /:;F ;C &D:F + '*C:<:* ++%:*
  '

 
=
*   "* :
cd [  ]
C"   cd '  
' *
, 
=   "*  -

  Y   "* "#' 
". K 
  \  "

12
 
=
*   "* , +  "#' #   pwd 
' *
-
.  
ls [_ ]
'"
 "# "*  ' *   "* . C"   "* 

 ' ,  


 

 "* 
=
*   "* .    ls
#

"# "
'% "4
:
l – 
 "4 >  4   + > "
;
a – 
 "* 
% > ", "4   
, 
  %
  4  " .
  mkdir  rmdir '"4, 

, ' # "
 "#  '    "*:
mkdir [  ]
rmdir [  ]
   > " less '"
    # > "
'"#* '



= #  % 
+  =#4
" Y  "
    (" %  "#'
 " Y «q»):
less [  ]
      > "
cp [ ] []
 


=
 "


  > "
mv [ ] []
   "
 > "
rm [  ]
    cp  rm +
  "#' # "4 «r», '"4-
=    #,


= # "  "#   "*  
 % 
+-

 .
" " >   

"
%   %, % *
-
 %     % %  "#'  +  #     -
 "#' 
" (  man).
;&F% J::/ <<:;-<F<:;
  + * , ' 
 '     Unix, -
'      %   %:
x      (stdin);
x      (stdout);
x     Y (stderr).

13
< * , 
4=
%%  %,   4 > -
 4 '   *   . ! 
,   wc -

 "
  , "  "  %%  %. C" ' -
# \   
' *
,  wc 
 + # %%  -
%  
 " ( ' #   %, +  + # -
 4 " Y Ctrl-D):
$ wc
two words
<Ctrl-D>
1 2 10
   

*  wc  " 

 "#' 
-
"
 
 '   *   ( "#' 
"# 
" 

«two words»). < " 4 \  

  
 " ( " -
  ) "#' 
",   

*

 "

. K
' #  
  * %*   '"# > ",
+  "#' # 
4

 "
 «<»,  
:
$ wc < /etc/passwd
28 37 1052
   "
  wc +



   "   ,
. .  +
"" %
 
' > " /etc/passwd.  
,
      +
 
# 





 
> ' 
 "
    > "
/etc/passwd,  


"
 "#' 
"
, '
*   %  

.
     – \ ,  *  ' -
 4 %
 
. 
=
 

  wc "

'"#  (  " ) 
  \ . $  +
 4  "#Y-
  *% 

%   ("4  echo, pwd  ls, -


Y
). <      % 
'  "# '   
 "   +
 


 "

.
" '    *    > "  "#'


 «>»,  
:
$ ls > filelist.txt
 \ 

  ls, 
 *,  
  
> "  \ , '  "
*  > "  

 «filelist.txt». <  \,

" > "    



 
=
 ",  
 ' ,  -
 "

*  

+

 
. =

   * 
'+#

 "
  , * 
%
 

     
=
4=
* > " . " \*  "#'


14

 «>>».  "
4=
 


=
   
  -
   
> "  

 «dates.txt»:
$ date >> dates.txt
=
  Y %       Y.
! 
, #  "
   "#   > "    -
"*

' 
4=%   :
$ ls -l /home/ftp/bin/
ls: /home/ftp/bin/: Access denied
   "
  ls 
" =

    
Y. K

 #
*   '  > ", +  "#'-
 # 
 «2>»  «2>>» (   "*  «>»  «>>», "# > 2
*   ,  +

 #  Y),  
:
$ ls -l /home/ftp/bin/ 2> last-error.txt



 "
  - +   #,
 
:
$ wc < /etc/passwd 2>> errors.txt > result.txt
=

  * "
'  

 "
  -
 – 

  . 
 «|» ('  
 "# 
)
'"


 #         
   %   *  :
$ ls -l /etc | less
 \ 

  ls  "   > "   -
 "*
/etc, \  
  4  % *  less,   -
'"
 " # 
  =#4 " Y  "
   .
$  =
"
 «]


» % 
' %    
«

».
A   "

"+ 
>    

-
 . <#   

 "#  > "
«bash-users.txt»   -
    "#' 
"
  

, "#'4=%   -
" bash. W* + "  #  "#' 
 
"#-
%  , %  
+
 
 

% > " %
(
      " 


"
'  #)
$ grep 'bash' /etc/passwd > list1.tmp
#       «bash»    /etc/passwd
$ sort < list1.tmp > list2.tmp
#     
    list1.tmp    list2.tmp

15
$ cut -f1 -d: < list2.tmp > bash-users.txt
#
  
        :
#     bash-users.txt
$ rm list1.tmp list2.tmp
# !   
  


'    '"
 #    -
  
'  "#'  
+% > "
$ grep 'bash' /etc/passwd | sort | cut -f1 -d: > bash-users.txt
 
,     sort " cut    ' 4 >"# .
G"#  " 4  
'   * %*  ,
 -
' 4 %         .
<%&O%% &D:F + Unix
 + 
    Unix "+
 '   #  -
  logout. $ +
+  "#' #  4 " Y Ctrl-D,
  '"
  "#   ' 
Y
    
", "

* 


% 
+ + 
* -
 "
4=
* "#' 
". C" 
  '"   -
"
  Y   "#' 
 " telnet " ssh,  ' 
-
Y

   " '
 '  

.

1.1.2. +:<F %&/<:* &D:F < :D:C:@/% bash


" (shell) "   

   Unix-
 %



    > :
x 

   * '   "

 , 
-

% "#' 
"
 " *"
% ' 

 
 > "
;
x 

' 

 "#' 
"
, .
.
-
 "

"#' 
"4 '+
       .
!+

 
    *   '  
> , .
. 

'+   " bash,
  "
    " 
 GNU/Linux,  +
 # -
 "
   *% Unix- % 
 %.
" bash
 "
 "#' 
"4 '

 -

 .   ,  
+

   

   ,  ",   , «  » (>


),  -
+
 4   (.
. '+#  # 



4 -
 )    

 

 "

.
"

 
#,  

"#' # 
 '-
+ " ' 
"# Y
 \>>
#  

16
Unix-

(
  
   % Y ' 
  *
' ). "

*,  


    "
 
 '
 ' 
"# "

   
4   "#'-
 
 > "% 

+
,  %   Norton Commander, Far
Manager " Windows Explorer.     
=
 
 "
Unix ' "4
  "
"#   "#

'
-
.
 "

  "Y# 
 
 "

 "#'




 . " "

"*    '+


 -
" "

 "#' #    "#' 4 bash (-
  man bash).
%;/&:<% /:;:* +&:/
" 

" Unix '"" #     -
"
 
"## ", ' 
Y    + 
 " Y Enter.




  % "
,  
  bash, "4 4
'


   .
" *% > 
     "#'4  
" Y  >   CTRL  META. F>  CTRL 


 "  
IBM-
%  #4
,   

META  =


*  "#'
 " Y ALT. A  # > 
META '      
 " , * >
 
 " -
*   "
*  . C"  =#4 " Y ALT 


# +
"
*
'"#  , +  "#' # "#
 
   META- . " \*

 " +  -
+ # (  #) " Y Esc. $   ',  
,  -
4 " Y META-d + ' 
# "
 
"##4  + 
Esc, d. "     
META-_ ('  
 )
" META-> ('  «"#Y
») 
%  + #  
+ # " -
Y Shift.
  ". 1.1–1.3 

 
  "   -
   
.  
4 

,   ", 
-
"# '%   " Y, . . %  # +

' 
#   
 " . <\,
" 

   -" '
" Y ( 
, Home), 
 

+
 #  "#'  "#
-
     ( 
, CTRL-a). $ +
"

 
#, 
*
'   " Y 
4  "*
" %+

 ' -


   *% *  %,   
%  Unix.

17
$ " 1.1
      
  " Y   


  CTRL-f <


=

  "  
"
 CTRL-b <


=

  " "

META-  META-f <


=

  "  
META-"
 META-b <


=

  " "

Home CTRL-a <


=

   "  
End CTRL-e <


=

 
 

$ " 1.2
    ! 
  " Y   


Backspase CTRL-h  "# " "
   
Del CTRL-d  "# "  '   
CTRL-u 
' #  #   "
   
CTRL-k 
' #  #       
META-Backspae 
' # " "
   
CTRL-w
META-d 
' # "     
CTRL-y  # "
 
'  
 
' 4  
CTRL-/ CTRL-_ 
# "
44 
4
 -
 

$ " 1.3

" % '
  " Y   


Enter  "# 
=4   ( "+


  


 ' 
)
CTRL-L # \   
# 
=4 -
   
%
  
\ 
CTRL-d  ' " bash,  "* -
   logout ("#  ",
       )

18
+J:CPH:<% +:& /:;
" bash 
+
  4  , .
. ' 
 
-




 . W '"


 
#  "4 



-
  
,  +
 "#' # 
"#
> *
   ' -
  " 
  %  . &  % 
  -
%
' "  > "
 

 .bash_history   Y
   "*

"#' 
"  ' * +
 #  "
4=
 ' 
bash. $ 
 ',     
 
 

 % 
+ 
  -
.  
, =

 * 

 "
 ' 
%
  ( 
, 1000),  
Y
 \* * 
  


    
  "#.
K 
#  4  , +  "#' #  
history. C" "

 \    '  " *
, 

 

 
4=

" "
% 

%  .
! 
:
$ history 5
4995 mkdir tmp/work
4996 cd tmp/work
4997 cp ~/work/log.txt.
4998 joe log.txt
4999 history 5
   '    history,  +  
 "
 



  
  . K  "#  '
   , + 
     
'   
,

 
* " 
"# ' . ! 
:
$ !4996
cd tmp/work

,  '     "#' 
 % 

-

. 

 "#' # %+  ,  '  


 -




"# "  .  \ "

 '-


      4= 
 " ,    
  , .
.  
  %  . ! 
:
$ !cd
cd tmp/work
       +


 
   

 "#'  '-' '+ "
* Y#   "# 

-
4  . 
 \*  =
 "#'4 



 *      . ! "

 

"#
 
" Y, ' 
  
  , 

   ". 1.4.

19
$ " 1.4
(  % '  !   
    


" Y

% <

 
=
  

CTRL-p
' <

  "
4=
  

CTRL-n
META-< <

    "    
META-> <

  
    (.
.  
=
 -
 
)
CTRL-r =
#   

 "#   -
    (.   
+
)
META-.  # "


"
=
    
-
=4 ' 4  
CTRL-o  "* Enter,  "
 "
   -
 ' # "
4=4    
      "#'    ' "4
 

-
%
  , 4 ,  

 
,


   -
   +  " Y Enter. C" +
 \ 
 Enter  + #
 4 CTRL-o,  \ '"  #  
 "
 -

"#%  , % 
%   .

"#*   ' "+
 '+# 

 "#*
    (  " Y CTRL-r). W, + ",  "

=    "#'     . <"


 +  -
  " Y CTRL-r 
*" Y

    
'
 
"
  
+ 

 "#* 
(reverse-i-search)`': _
 \
+
+ # " ' " "44  #
  '     

  
#  "

'44 '   4=%  . ! 


,
" % 
 , 
+ =
   «web»,  "
 +  CTRL-r -
   "  «w»
(reverse-i-search) 'w': cd tmp/work
,    
 " +*
'"#  ,  
 ,
 "
4=4 , «e»
(reverse-i-search) 'we': ./update-web.sh

20
$

# ,   
    +

+ > *

«web»  "

 %+
 "   
 "Y#  " .
C" +
 
     ' " # 
,   ", + -
"#' # CTRL-r "

%  "


 ,  +
-

+ =
   . < "+ 
= 
,  
 +
 CTRL-r. <  \ 
  
  * , "

  -
 ,  

(reverse-i-search) 'we': cd work/web/homepage/
$

# +  '
+  
"#   .
K

   
4     ,    + #
Esc "  4 " Y CTRL-j. K 
#   
#-
  %

, +  + # CTRL-g. &  
,  + 

Enter 

  

"
  "
4  
  .
+J:CPH:<% <:@%+/:>: ;:J:C% < /:;:* +&:/%
 

 "

(completion) '"
 ' 
"#
 #   , 
 > ", 



%  
  Y 
   
. ! 
, #  

 "
 * 
bunzip2  
    * *  "  ,   4=

  «bun».    "
 bash     #    "
-
    \      + # " Y Tab. <  \  "#-

", >  4=
  ,   "
  -

.  "
bash 
+
 
"#    "
 
+
   " Y " % ' . A   "Y#

 "

"
'
'+  "#  

 "-


( ". 1.5).
$ " 1.5
)* " !     
    


" Y
Tab  "

 "

%=  

META-Tab  "

 
> ' '     ( -
"# "# >  META    "

" Y ALT,   ALT-Tab  -
"#'
 * >
 
 " ' \ -
 


  "#' # "
 
"#-
#  +  Esc, Tab)

21
 "

 =#4 Tab +
  # - '  ' -
   "#'  
 . $ ". 1.6   =
 
 -
'
 "    "
.
$ " 1.6
)* " !     

 $  "

!  "    "


   (  
 

 
%   "  *  

-

 
 PATH)
<"
"  "





 (  
 

$  "
%


% 
)
<"
"  "


  Y (  
 
  -
@ Y  > "
/etc/hosts)
<"
"  "


 "#' 
" (  
 

~ '
% 

"#' 
"
)
<"
Y "  
 Y " , "#
"  
 "Y# 

 > " %= > " (   "
' 

 "

, ' 
 Y "  %=


> " )
  "#%  "


 > " (  
 
 > "
" %    "*)

 "

 =#4 META-Tab 
* =
  "
  -
  ,   > ',   4=
  ", =%



=
 ' 
   . C" ' *    
 
, 
-
'     "
  
 "#  #,   4= 
 
%    %, ,  '   >*  ", +

# 

   %=%  "
. C"     

  
,
*  + 
  
 + 
 Tab " META-Tab.

1.1.3. Q*C:< ++%


+:D%:+ ':&&:< '*C:<:>: J&:+&+<
G "
  Unix-

 "
  
%4
> ",   


 = 
# –    '
  
-
   "*, ' 
 '    
 «/». K -
'  
>   # "4 > ", +  ' # #  \
> "   
* " 
=
*   "* . 
\"

  
"-
4  *   * "   
. C"
 "
   +
  
 ,  # 

   "   
   "*
, 

22
 "
–  
=
. <# 

 

 '  -

 > "  
=
   "*
. < 
:
x docs.ps – > "  

 docs.ps  
=
   "*
;
x /usr/doc/FAQ/README – > "  

 README    "*

/usr/doc/FAQ;
x work/thesis.tex – > " thesis.tex    "*
work 
=
*  -
 "* .
<

=
*   "* 
"# "
   * 


MS-DOS " Windows.  Unix   +* 
 


=   "*.  
   "* > "* 

 Unix  -

+ "
4=
  "* ( '% 
 % \   -
+
 " #):
x /bin –  "#    "
% > ", 
%
"   
;
x /etc – > " >*  
;
x /dev – > "  ;
x /home –  Y
  "* "#' 
"
;
x /lib – 


"
  ";
x /root –   "*   
 root;
x /proc – > "- '  "4=% 
;
x /sbin –  "#   "   ;
x /tmp –   "* " 

% > ";
x /usr –  ]
 > " 
:  "

-
* , "
, %

  , > "  %  

;
x /var –   "* " '
4=
 >   (
%  -
%, % =, 


 
, >    %  -
 
 , "*   .).
"

 
#,  "  
 
"
  #4

   "*, "Y#  '
,   
\"

  % 
 
   "*
.   +   "*
 +
=

  %
«   "* »  
  «
»  « ». <
 ' % "+
 ' 
"
  '  

"
 
"#   "* (Y
-
=),   –    
=   "*. ! 
, #
«../readme»  '
  > " «readme»,    %  -

"#   "*
( 
# Y
), # «./readme.now»  +
 
> " «readme.now»,    %  
=
   "*
.
"#Y   # > "* 

 Unix  

   -
 "*
/usr.   ",   +   "
4=
  "*:
x /usr/bin –  "

> ";
x /usr/doc – 
   '"% >   %;

23
x /usr/etc – > " >*  * * 


, -
 "
*  "
"#;
x /usr/include – "4

> " " * ,  

'
C;
x /usr/info – 
  "#' 
"  *

 >  -

info;
x /usr/lib – '
"

"
;
x /usr/local – " "#
* 




, > "  %
 "
 (\   "*  
 % 
 % +
 
 "#'-
 #);
x /usr/man –  "#' 
" (manual pages);
x /usr/sbin – "   ;
x /usr/share –  
, 
  "#'

'" -
"  *  ;
x /usr/src – %

 '"%  
 
,
"4   .
       "
    "*   
Red Hat Enterprise Linux 5.2.
Q:&&:<% % '*C:<
 '  
,  '  4 " * ' =

 
"#-
   ( 
, " * ' =
4 "
4=

 : CP866, CP1251, KOI-8R  . ., %  "





 
  

 UTF8   

 "Y
), 

-

,   > " "   "*  ""# ' "
4=% -
":
x  
  
" 
;
x > ;
x " 
 ;
x " ;
x '   (
"+
 #
 " 
);
x '  "4 ( "#' # 



).
  + 
   
 % > " * #  
  *
",  %  "#' 
+
 
  

-
  
 % *  ,  
*, +
 '  #

-
 > " 
+ ' . !



  "#' #  ' -
 > " ' " "#%  "   ( 
, 
 > "
  '
), . . 
#   " * ' =

 
-
"#  .

24
F  "#  " 
 > "  # 
  '% 
 %
 '  

  "#'
 > " 
, 
    .
 +  "#' #   "

 > " ( 255
"). F  "# '
> "  > " 

 +
' -
 

 . " 

% > "% 
 '
> " -
"

4 ?  
"
 "
.
  
 "# Y
,  
  
  

Unix
'" 4, .
. 
 «filename», «FILENAME»  «FileName» "4-
 '. <  \ > ", " 4=
 "#
*  ,
*  %#     "*
.
 "
 
 MS-DOS, '   "
  -
",    "4 


 > " ,  * ,  
Y


 > " ,  * * ,  

Unix 
. $
 

-


, "

  
 > ", 
"

  "#   
-
  ,    ' 4   > " .   



> " «my-photo.tiff.gz» +
 '  #,  > "
 "
 
' +

 >  
TIFF, + 
*  +  gzip.
$ , "4= 
 " 
 > " "   "* ,


 
' 

:  

  " 4 
  "-
*

+*   "* (%   +   = #),
" "
 "*   > " 
 ls, + 
 ls -a.
 * " ,  
" # > " « », +   #
*
  . W   "#'4 " 
  "+
% > -
",   



 "  = # 
 
.
&:+:&  %&J&%? J&< ;:+LJ / '*C
 

 Unix –    * "#' 
"#

-
. K   #  #, "#' 
"# "+
 «»  
,

  * 
 " 

* 
  "#. K
-
"
, '
*     
% > " % 
 , "
 
"#-
, 
4= 

,  '
 '
*    "#' 
-
"
 
. A
* 4 % "#' 
"
   "
 -
  
.   "#  , 

-
 "
*  "#' 
"  

, "4:
x  "#' 
";
x  ' 
*  ,     "#' 
"#;
x "#.
 Unix  '
   > "  "4 4    "4-
=
:
x '
Y


 (' 
  «r»,  " Read);
x '
Y

'  ( «w»,  " Write);

25
x '
Y

 "
 ( «x»,  " eXecute).
A '
Y

 

'"
 "#' 
"4  # 
+

> ",  "


  "* –    #


# 
 > " 
  "*
( "#',  
,   ls).
A '
Y

 ' # '"
 "#' 
"4  #  > ", .
.
'
#
* 
+
. "   "* \ 
  '  #   -
 "*

> "    "* "  "# > "  \   "*
.
! 
, '
Y

  "

'"
 "#' 
"4 ' -
 # > "   "

(  *    Y 
,   
 
> "). C"  > "
    "
,  
' -
 
* 
  
 * ,  4 + ' -
# ( "
 DOS " Windows,  Unix '+#  "

> " 
'   « Y
» 
 > " ,  *   .exe). A '-

Y

  "



"#    "*  ' 
 '+-
#

%  \   "* ( 
,   cd). <\ "  -
 "*   "
    ' 4   . 
,
 "   "*  
   "
 (r  x)  =

*  "#-
'4 
, .
. " 4  , " 4.
   %   > " ,

"

   
* #
 "
 " 
% "  "#' 
"
:
x " 
"# (  +* > "  Unix
#  " 
"
);
x *   (  + > " '  *  "#' 
"
 \*
> " );
x 
%  "#% "#' 
"
.
!    " 
% > " + 
# 
=#4   ls -l. ! 
:
$ ls -l tmp/
drwxrwxr-x 10 john users 1024 Aug 30 2002 newdir
-rw-r----- 1 john users 173727 Jan 13 23:48 archive-0113.zip
 \ 

,  " 
"#
 > " "
 "#' -

"# john, *   " 
"#
 "
 *  users. !   

  "
   

"
  > " (
 ")  
  > " ( "#

# ").  

 


  ' #     "* (
   d)  
  

  rwxrwxr-x.   ' #    > "
( 
  


* " )   '
  rw-r-----. 
#
"   

"4 '+# 
 (r), '  (w)
  "
 (x) " " 
"# > " (

  " ), *  
" 
"# ("
4=
  " )  
%  "#% ( "

 

26
" ). < 
 '  4 

4=% . "
-
 
"#,  

 

x john  
"#' 
" *   users *    # 
'
# 
+
  "* newdir,  +


%#  
*, -
 "#
"#' 
" *  # 

%#  \   "*,  

* '  # "  "#  


 
> ";
x john +
  #  '
# > " archive-0113.zip, "#' -

" *   users * "#  # 
+
\* > " , 

 "#


4  
  %   .
 
"#*
 "
      "#'
-
 >   >  .  > 
 "
    -
"4 ' 
% #
% > ,  +  '  % 

"
  -
 ' 
%  " rwx. K


   '
"#*
 "
  "
, "

:
x
 #      
( 
, 110100000
"    rw-r-----);
x


 "


"  #
4 

"
 ( 
, #

 "

 * "
110100000 
 640).
<    +
+  " > 
'  # 
 -
   #
% ' 
 
"#%    :
x 400 – " 
"


   

;
x 200 – " 
"


   ' #;
x 100 – " 
"


    "

;
x 040 – *  

   

;
x 020 – *  

   ' #;
x 010 – *  

    "

;
x 004 –  "#

4   

;
x 002 –  "#

4   ' #;
x 001 –  "#

4    "

.
F+ ' 
#,  "   rw-r----- ":
400 + 200 + 040 = 640.
JF '*C:<
  Unix 
4 "
4=

  > ":
x 
> " (regular files);
x   "* (directories);
x "#
" (symbolic links);
x > " >'
%   (device files);
x 
 
  " (named pipes);
x 

*
' (sockets).

27

> "  "#'4  "

Y  
 "4
 
 
    %  '+#4 '"#*
 .
  "* – 
 "#  > ", '"4= *    #


 *
> "    "*. 
+
  "*
 "

    %=%  
 > ".
G "    Unix "4 
 =
 " %
*    
     #4
. " * 
-

 #  
 
  *-"   , "   * 
"+ '
 ' #  
4= 
 "# > ". <
 "*, 
 
 ' > "   ' 
 "


 % 
*  
.    "*  > "  

  «/dev».
"#
"  « " »  Windows.  '"-
4 '  # "#
 

 > ", 
 *  ' # 
> "   *%   "* %. <    *  "#
" > 
  
 > ",      '
. -
"#
" *  ' #    
> ",       -
"*  > "  *%  .
&
 
  "
=
 ' 4 >
 FIFO (First In First
Out). K

' > "  *   
' % 
 * 
-
 #  : 
,  '    > "  
, +

#    *.
?
' – \  


 

* 

.  -
   
 \ > ", 
 "
 %  
#. <  \ -

,  Y
'# 

'  *
', * # ' =
  
 '%  #4
%,     .
:&:<% +:&:= '*C:<F= ++%
  "
 "
 Y
,  > " 

Unix * #
"4
  
> "

,  
> "

-
  *%  " > "

,  "+

 
Y% -

"% (>" - %, CD-ROM   .). K   > " 

 "    Unix, 
% =
# 
4

  . G 


,   
– \  ' 
*, 


"

 
 #   =
  ]
   

> " 
. " 
 \  ' 
 '
  -
  .
$


 +  ' # 

'"#

 
  ,   
"     % > "%

 =

   "* /mnt.  
 
% ' #   "*,

28
  
 "+#    . ! 
> "4 -

 Windows, >'
  "+
4 
 "*
 '
-
"
* +


( 
C), + 
" #   " Unix,
  

 =#4   mount (


 
"# +
' #   "* /mnt/windows)
mount -t vfat /dev/hda1 /mnt/windows (  Linux),
mount -t msdos /dev/ad0s1 /mnt/windows (  FreeBSD),
*
"4 t  *
 vfat (msdos) '  4   
  -

 > " 
 (   "
FAT); /dev/hda1 –
 '-

"
* +
*  ,    
  =
  =#4
> "   ; /mnt/windows –   "*,
 "4=  
  .
+%F* '*C &%>+&? J:CPH:<%C%*
 Unix-
 %
*  "#' 
"
 

  > "

/etc/passwd. 
+
\* > "
 "
  "
 -

"## 
%  .  +    

  '
-
*     

"#' 
"4  
+ 
# "
, '-

"
% "  
. W "  :
x
* 
 "#' 
";
x ' Y>   "#;
x ' 

UID;
x ' 

GID  *  ;
x 
  (+
 
+ # Y
4 >  4 
"#' 
"
,  
, "+#, 
"
>  . .);
x  Y   "*;
x    " "#' 
".
G " /etc/passwd "+
 # 
 " 
 
 "#' -

", . .  
 "+  = # *
* , ' 


 
 * "#' 
" ( 
,  ' # 


UID
*  
). !  # " 
 
% ' Y>-
 % "
 
#
' 
#Y
 
' # 
, 
 


"
"#
= '"4  
"#
   # " ( 
, 
   

-
 "#' 
"). <\    "#'
 %
 

% -
"
 (shadow passwords),    "
"  /etc/passwd *-
 
,
"# "# 

 '  ** > " ( 
,
/etc/shadow),  * " 
 "# "
*   "#-
' 
"4. G " 

% "
   
+   *4  +4 -
>  4:  ,  


 *  
  "#' 

'-

29

* ",   "

* '

 "  . . <   "#-
'  

% "
  
"
 /etc/passwd  
+
" '
' " "4  * '"# ". < -
"
"  /etc/passwd  "# 
"#',      \ "


+
  #,    "#' 
"4 "# 


. < -

  ' > " /etc/passwd (' 
#
,  "

    -
 "

):
john:*:1004:101::/home/john:/bin/sh
 "
"#4 >  4  >  
> " /etc/passwd +
"#,      man  

passwd  '
"
5 (>  
> "). W   
 *"
#  :
man 5 passwd
Q*C &%>+&? >&LJJ J:CPH:<%C%*
&>    *  %, '
% 

, 
+  > "

/etc/group. < > "


*  "#' 
"
, >   
/etc/group
 "
     ,   "  + '
*-
    *   "#' 
"
.  +    
+ 


", '
"
% 
:
x
* 
 *  ;
x "# *   ( \ "

,     *   -
 
 '  4 ");
x ' 

GID, 
4=

  * 
;
x '
"
 '    "#' 
"
, %=% 
*   (+
 # ).
 
,     "#' 
"
  '  /etc/group 

' 
,   \ * 

  * "#' 
",     GID -
 *   "#' 
" 

"
  > "
/etc/passwd.
J&%;%C%% ;%'/:&:< J:CPH:<%C%*  >&LJJ
K 

"# UID "#' 
", GID  
* 
*  ,  +
  % *  ,    "4
 "#' 
"#,
+  "#' #   id.  "

 "#'  
' *-

,   

 >  4  
=
 "#' 
"
. C"
+
 ' #   

*
  '
*   * "#' 
",
   
 
 #  '  "#' 
"4.
K  "
   id "
   groups.  

  
 
% *  ,   
"4
 
= "  ' 
"#' 
"#.

30
   who 
' *
 '"
 "#  
"#' 
"
,  4=%    
  

. C" +
 -
 # whoami, 
 

 >  4  
=
 "#' 
"
.
  ,  "
"#4 >  4  
%

"
%
  % + "#  =#4   man,  

$ man who
H%%% <C;%CP?%< '*C:<
" 
"#
 > "   "#' 
"#, ' Y \ > ".
?   " 
"#  " 4     * 
* -
 "#' 
". " '

 " 
"#

 ' 
    
  chown (change owner).    

% 
 % " 
"#-
> " +
 '
# "# "
*   "#' 
"#
(root).  * "#' 
" =

 '+# '


"# *   " 
"#
,   "Y# 

" % 
% *  ,   

%   "#' 
"#. " '

 *   " 
"#
  -
"#' #   chgrp (change group). ! 
,  
" #
*   " 
"#
   "* newdir *   student, + 

chgrp student newdir
=

 '+#
 * '

 " 
"#
 "

% > "    "* '  *   "* . " \* "

 -
"#' # "4 R,  

chgrp -R student newdir
 
,  
 * 
 > " "   "*  

-
%   % +  "#' # +
 
, '
"
% -

" , " Y " 
 > " (\    "#Y
  
 *%   Unix,  4=% 
 > "   

"
%
*
).
H%%% J&< ;:+LJ / '*C
&'
#    > " +
 "
* " 
"
, "
"
*   "#' 
"# (root). 
"
 \   chmod
(change mode). =

  >    "#'  \  : 
 "#' 
 "#*  "*
 "
   .
& "#' 
"*
 "
 '"
    '-

# "     ,  

chmod 770 newdir
          "
' 

770,
.
. rwxrwx---,    "
 " 
"#   * 
" 
"# 
 %  
  "#.

31
& "#' 
"#*
 "
     

chmod +
  ' # 
"# "+

,  '"
   "-
 # 
"#     . ! 
,  # 
'  " *   " 
"#   "* newdir,   

chmod g-w newdir
"   \    :
chmod {u,g,o,a}{+,-,=}{r,w,x} 
...
  

*
   
  ' 
"  "#-
' 
"
:
x «u» – " 
"
- "#' 
"# (user),
x «g» – " 
"
-*  (group),
x «o» –  "#
"#' 
" (others),
x «a» – 
Y


"

*   

(all).

,  
+ '
     , "
4=
:
x «+» –  #,
x «–» –  #,
x «=» – #.
<   («r», «w», «x»)  ' 
   "*   > " .
     
chgrp,  chmod +
  "#' # "4 R, -
'"4=
     # 
+
  "*.
&< ;:+LJ J: L:C@R, /:; umask

,   '  % > "    "*  +
-
 "  # 

"
      . W   ,
  "

 " 4, 

"4 ' 

   
 ,     "
   umask. <  
 
umask 
' *
  

 
=

' 

 ,   "#-
'  #
* "   

*
 
  "


' 

.
F    

"
,  
 "+ #  "

' "*   , .
.     "
  
 

  ' 

    " 4. ! 
,  
022 

     '  " *   " 
"#   "#%
"#' 
"
.  
,  " % > " (
  "*) 
-
  "
 (x)   %  " 4   Y
 
' 
 
=
  .
< 
, 
  4= \>>
   umask:
$ umask
002

32
$ mkdir dir1
$ ls -l
drwxrwxr-x 2 john users 1024 Apr 21 07:29 dir1
$ umask 072
$ umask
072
$ mkdir dir2
$ ls -l
drwxrwxr-x 2 john users 1024 Apr 21 07:29 dir1
drwx---r-x 2 john users 1024 Apr 21 07:30 dir2

1.2.  
$$
!  !
1. ' #  


  
 ".
2. 
*   #  

 

,  
 -
 
"
.
3. ' #  "
4=    " "#' 
"#-
    Unix: man, apropos, ls, cd, pwd, mkdir, rmdir, cp, mv,
rm, cat, echo, less, touch, grep, date, history. 

"# 
 , -

"

  #    "#'   %  .
4. 

"# "4 # 
*  Y
*   "* .
5. 

"# ' 
 "
4=%


%  +
:
PATH, MANPATH, PAGER.
6. 

"# *   > "*   , *

 -
'"
 '  # 

> "    "* ('+  "#'-
 
 
*    ).
7. < 
#, '+ " 
Y 
"#  "
> "

   ** "#' 


".
8. ' #     

"
    > -
"   % '

 (  id, groups, ls, stat, chmod, chown, chgrp,
umask).
9. !  ' #  > "
/etc/passwd, 
4=4  Y


*  
.
10. 

"#  UID, ' #,    *     Y


* 
, ]#    id, groups.
11. 

"#   *  ,   
% "#' 
"# root.
12. ' #,      "  4 # ' 


> "    "* (.


. ' #  > "     "*,  -

# " %   ).

33
13. 

"# ' 

umask,    ' 

> "
   "*  
  " 
, '    "
 ,
 
" 
"# .
14. 
" #   Y   "*  " 
% "#' -

"
 *   users.
15. ' #   Y
   "*
  "* tmp, > "  -
 +
 '  #,  "# 


 # "4, %= 
*   users,  \ 
+
\*   "* 
"+ #
 
  "#' 
".

1.3. 
 
   1

 "+
 
+ # "
4=
'
":
1. $"# ", > "
 *"  
+
  ' .
2. D
".
3.   
.
4.   

  "
 :
x  
  

;
x  ' 
   "  ;
x
  
     (
" ]
 %  -
%
Y
 
"#  , 4 >  4 "4 #  
 

"

).
5. .

34
2.        2.   
 
     
GNU MAKE    

 UNIX
$  !
' #  
%  "  *   '
-
*    C (C++)  

 

 Unix,  +
"#


   "#'  " GNU make "  

 .
 
&'# 
   " make  '  
-
  '
( ++)   Unix,  +
"# 

 
 "#'  " GNU make  '    

 .

2.1. 
! $
 ! 
 
MAKE
«  » "#Y *  "  

 Unix '-
   "#' 
 " make. W " 
 > "
( =  «makefile» " «makefile»;  "

,    
\* > " , 
  "#' # makefile),    
+  -
 ,   "
  
   
, 
%
"
  * .  *% " % makefile "#4 *

 


 "# * . ! 
, " '  
  
 "#'4 *  autoconf/automake.    
 % -
*  % +
 
 # 



' 
> "
makefile 
'  "#'  
  
 *

.
"

 
#,  =

,   ,   '"%
 "

  
%    " make: GNU make, System
V make  Berkeley make.

2.1.1. +:<F% J&<C &D:F + LC:* make


  "4= "4* make-> " "4 "
(rules).  =
 
" *"  :
<% _1> ... <% _n>: <_1> ... <_n>
< _1>
...
< _n>

35
9 (target) – \ 
  +
"

'"# ,   -
+
  *     "
. D
"# +

 "#  
> " .  \ "
"  
,    ' + "-
# 4 
4 \* > " .
 "
4=
 


"#4 "
 > " iEdit ( "

> " *  
 * * 

* 
 
*
-
   *"  > " 
 main.cpp   "
"# –
Editor.cpp, TextLine.cpp). < "  
,    ' + -
"# 4 
4  * > " iEdit (  # '

-
"
% ]
% > "):
iEdit: main.o Editor.o TextLine.o
gcc main.o Editor.o TextLine.o -o iEdit
C" 
%  "  # 
,     C++, 
+  "#' #  " g++. "

  +

#, 
"4 o  " gcc  '
  
  * > " .
D
"#  +
+
 # 

 
 * 
.    "-

"  
,    ' 
Y
  ' 


.
 "
4=
 


"#4 "
 

clean ( ,  "
-

):
clean:
rm *.o iEdit
<* 
"  ' 4 

" (pseudo targets) "
 
" (phony targets).
 # (dependency) – \ 

«%
 
», 
%-

" +
  '   "

", 
 
«
 -

"#
"
» " +

".  # +

 -
"#   > " . " *  
Y #  ' 
",
\ > " "+
 =
 #.
 "
4=
 "
> " main.o, Editor.o  TextLine.o "4
' . W > " "+ =
 # " *,   "
'+ +


" –  

> " iEdit:
iEdit: main.o Editor.o TextLine.o
gcc main.o Editor.o TextLine.o -o iEdit
 #  +
+
 # 

 
 * 
. W


"+ #
 
"#  "


 +



",  '   "
.  "
4=
 

' #
clean_obj "
 

 
 ( "# ]

> " -
* ):

36
clean_all: clean_obj
rm iEdit
clean_obj:
rm *.o
" *,  #
" clean_all, 
%   " -
"# 

(*#
") clean_obj.
  – \ 
,  

%  "# " -
"
 " +

".  "
4=
 

  "-

 '  " gcc. " make "


  , 
+ -
=
 ,  %   make-> "   "4 "  "-
 ("   9)    "
 :
iEdit: main.o Editor.o TextLine.o
gcc main.o Editor.o TextLine.o -o iEdit
 

 Y


  gcc main.o Editor.o TextLine.o
-o iEdit "+   #  "  " .
DT* C>:& &D:F make
$  make-> " 
 
+ 
"# ".  +

' " 

 
 4
"#  
 
' . "
 make "
 +


",  4   "   
-

*" 
" (default goal). C" *"  
"# "
 

 
-
 (.
.  
"#4),  "  make ' "4
 
 "
 
4=
* 
. C" +
*"  
"# "



 > " ,  *  make "+  #  4 «
+4»

4  ' * > " .

FD:& >C<:* ?%C. ?"  
"# +
 #   '  
   
 ' 
make.  "
4=
 

make 


# #
" iEdit ( "# 4 
4 > " iEdit)
make iEdit
 \ 

make "+ #
" clean (# 
-
 4  ]
% > " 
 )
make clean
C" 
 ' #  -"
"     
,  make

   

*" 
4 

4  make-> "

"#.
 "
4=
 

' 

%

"
%  make-> "

"

(iEdit, main.o, Editor.o, TextLine.o, clean)  " 4   

*" -
 
  
"# iEdit:

37
iEdit: main.o Editor.o TextLine.o
gcc main.o Editor.o TextLine.o -o iEdit
main.o: main.cpp
gcc -c main.cpp
Editor.o: Editor.cpp
gcc -c Editor.cpp
TextLine.o: TextLine.cpp
gcc -c TextLine.cpp
clean:
rm *.o
%
  «
%  
#» "*   make +

 #  :
make()
{
 _%  = 
&  ;  ()
? ;  ( _% )
}
:+V%% ?%C. <"
*   *"  
"#   , make ' -

 «  4» 
  +

".   "  make-
> "
 "
  " ,  
 
   +
-
 \
" (>  «! < "»).  
   
 "


  "*    " (>  « -
 #< "»):
? ;  (; )
{
  = @  (; )
G  ( )
}
D&D:/ J&<C.   " '
"
   -
% \ . !
 \
   4 
' ,

-
"

 "
(>  «  # »). !   \ -



Y

 , + "  "#  ' 
 -
"
  (>  «!+ "# »). <  
%-


"

 "
   "4 (>  «-
"# »):

38
G ( )
{
GI ( )
  @K
 M
( )
{

 M
( )
}
}
D&D:/ H<+:+%*. G  «  # » -


 

 


"

 "
' . !
-

' % *  ' #
"  %-# ". " \% ' -

  "
   
 +

" (> 
«#D
"»). $
' ,  

"4
",  -
4 
  > ". "  % > " 

 >  %  ".
<  %  make   ' 
Y
   =

 
Y
.
GI ( )
{
%  i=1   .  _
{
  QU;  ( .[ i ])
{
? ;  ( .[ i ])
}
 
{
@  V  ( .[ i ])
}
}
}
D&D:/ /:;. !      
Y
  
 , "

 "  "#   
 "
  " 
.

,  +  "#     % " %,  :
x
"# "
 

 
 (  
"#4);
x
"# "
 

 > "  \* > " 
=

;

39
x   -" ' ' 
 "
  
"#4;
x
"# "
 

 > "    -" ' ' 
, -
"4=% 

 > " , 

 "

'


 > , 


"#.
  "
(.
.   ' Y


% " 

 "
)   
 "
  
 "4. "* 

Y
   "
   %
  +
 -
#  :
@K
 M
( )
{
   .; .W X ()
return true
// %      
  V @Y ( .; )
return true
%  i=1   .Z _
{
   .I[ i ].W X ()
return true
 
//      
{
  \ %( .I[ i ]) >
\ % ( .; )
return true
}
}
return false
}
D+&/F% ?%C  % '*C:<. &
 
  

> " " make "
 "
4=  ':   "  "-

  > "   '  



, 
" > "  
,  


"# " ' # "4 

 > " ;   "


,   
 "
 " 

 
=
4=
*
> " , " 

 
 ( '" 
+ \     


"
, "#     4  ).

40
"

 
#,   % 

  
 .
-
%, " make 
 "# %
 
,  "
 
=
4=% 
 > ",  
   
"
"4

  
. - %,   %

 


"+   #  
   %-" > " " 
 ,
 
make-> " 
  "# Y.
!
 

 make
" * 4    
Y
 \
"
. $ ,  
,  "
GNU make 

 
% ' (
-
 "# 
"# .PHONY),  =#4  * +  ' #,   -

 "
 

 
.

2.1.2. &%& J&/@%+/:>: +J:CPH:< LCF make


&%& +:H; J&:+%*O%>: make-'*C
A  ,   " make 
    #   make-
> " (makefile):
iEdit: main.o Editor.o TextLine.o
gcc main.o Editor.o TextLine.o -o iEdit
main.o: main.cpp
gcc -c main.cpp
Editor.o: Editor.cpp
gcc -c Editor.cpp
TextLine.o: TextLine.cpp
gcc -c TextLine.cpp
clean:
rm *.o
<
 "+,   
   
  % "
4=

> ":
main.cpp
Editor.cpp
TextLine.cpp
<
 "+  +
,  *  make " '  "
4-
=  ':
make
D
"# 
 '      
, \ ' 
 "* 

" (>  « #?" 4D
"#»). ?" 
"#4  -
 > " iEdit (
 
"# '
* " ). D
"# iEdit




41
>  «#D
"». W >   "
  " , -

 
   
4
"#.    "
\

-
" make-> " . "  
* " ' 
 
  -
 (>  «  #< "»).
  " 

    4   
 "
' -
 (>  «  # »). <
  ' # – ]-

 > " main.o. <"#  make-> "



# "   
-
"#4 (>  «C#$  D
"#» ' =
 true),  "
" main.o ' -

 
 «#D
"».
G  «#D
"» =
 ", *
  
"# main.o.
W
"#       "
make-> " . " \* " ' -

 >  «  #< "»,   ' 
 

  ' 
 (>  «  # »).  -
 "
 ' 

  ' # – main.cpp. $ 
" 
make-> "

=

, \ 
,  ' #
main.cpp "
 

 > " .  "

, 

  "
\*
> "  
(>  «< 
#! "
G " ») –   > " -
=

. ! \ 
   ' 
 ' 
Y
.
<"
  ' 
 >  «  #< "»


Y

 , "

 "  "#  ' 
 "

  (>  «!+ "# »). D


" " (> "
main.o) 
=

, \    "# "

. G 
« "# » ' 
  ' 4  "
  ( -
" gcc), 
'"# 

* '
 > " main.o,    '

]
 > " (object file).
D
"# main.o * (]
 > " main.o  
). $

#
make ' =
   
 "#% ' 

* -
" .   Editor.o  TextLine.o    4  "*. "
%  "4 
+

,   " '  main.o.
<"
*,   
'  (main.o, Editor.o  TextLine.o) -
 ,
Y
    
%  "
  ' % 
"
  (>  «!+ "# »).
<"#
"# (iEdit) "
 

 > " ,     

 
=

,  

Y

 "#   4
 "
  (>  « "# »).

+ =   "
  ' 
  " gcc, 
-
'"# 

* '
  "
 ( ) > " iEdit. $  -
', *"  
"# (iEdit) * . ! \ *  make ' 
-
Y
 4 .

42
A  
=
 
 " make  "%, -
* "     * Y
make-> " 
  "
 -
 
make clean
D
"#   '      
, \ *" 
"#4
    
"# clean. D
"# clean


 > 
«#D
"». W >  =
 ",  
 
   -

4
"#. W 
 
" make-> " . "  
*
" ' 
 
   (>  «  #< -
"»).
<"#  "

 '   %-" ' 
, make
 '

%  \     ' %  "
 . D
"#
"
 

 
, \   +  "#.  ' -

 "
   "4, 
"# clean,    ', -

 *. ! \ *  make ' 
Y
 .
+J:CPH:<% J%&%%F=
'+#  "#' 


%   make-> " –

# 
    "#'

 make.    %

% ",




 
      ' .
" '   ' 



  "#'
 
   .
! 
,  +


obj_list := main.o Editor.o TextLine.o


 



 obj_list ' 

«main.o Editor.o TextLine.o»
(
'  
). < 
" 
+ " «=»    "
* "
*  4. "
4=
' "
 " 
"  +
.  
-




 +  "#' #  =#4   
$(_)
! 
,   
 * make-> "
dir_list := . .. src/include
all:
echo $(dir_list)
 \  
 

  
. .. src/include
<



* 
"# 
+ # 

 ,  
«" #»   *




. ! 
, 
'"# 
 
make-> "

43
optimize_flags := -O3
compile_flags := $(optimize_flags) -pipe
all:
echo $(compile_flags)
 \  
 


-O3 -pipe
 *% " %  "#' 



% '"
  -
# make-> "  #
*  *"#. " *  "
*#
> 4 make-> " , + '
# «"4

» 
   -
  
"#%


%  
# %    " make-> " :
program_name := iEdit
obj_list := main.o Editor.o TextLine.o
$(program_name): $(obj_list)
gcc $(obj_list) -o $(program_name)
...
    * make-> " "    * *  
-

  '

4 
"#%   "#%  .
+J:CPH:<% <:@%+/= J%&%%F=
 





– \



 
 "#

 ,  
«  
»  4 

"

' 



  "

   %  "
 .  


-



+  "#' # " « =
» '  ". $ 
,
 
, "
iEdit: main.o Editor.o TextLine.o
gcc main.o Editor.o TextLine.o -o iEdit
  "#' 
  
%


% + '  # "
-
4=  ':
iEdit: main.o Editor.o TextLine.o
gcc $^ -o $@

# $^  $@ "4  



. <


-
  $^ ' 
 «  ' 
».    "
 '

 " gcc  


 " #    «main.o Editor.o
TextLine.o». <


  $@ ' 
 «
"»  
  \ 
-

" #   «iEdit». C"   

"  "#'  "
-
4=   
 


  $<,    ' "  


 ' , .


.    "
 > " main.o.

44
&*  "#' 
 
%


% 
Y


%,  
 Y "% " %.
WDC:F% J&<C
q "
" (implicit rules " pattern rules) – \ " ,
 
* # 

 
" * 
> ".  \ % "-

 % ",   4=% Y
 
+ 

> " . $  

"'  make 
+ 4    '
-
4 «>>4» >  '  Y "% ":
.<_ _>.<_ _% >:
< _1>
< _2>
...
< _n>
! 
, "
4=

" *" ,  


> "  Y
-

 «o» '   


4=% > "  Y

 «cpp»:
.cpp.o:
gcc -c $^
" 


"'  make "


 
"#  "
-
4=  ' #  
":
%.o: %.cpp
gcc -c $^ -o $@
"

  #  
  "#' 
 

-


 $^ "

   "  
 > " -' . <-
"# Y "
" +
 
#  ' > " , -
"#' 
 
%


% – \

  
' # "  % > " ' 

 "    
. q -
"
" '"4  # make-> "  
" #
* "


 "#. A    
 > ":
iEdit: main.o Editor.o TextLine.o
gcc $^ -o $@
main.o: main.cpp
gcc -c $^
Editor.o: Editor.cpp
gcc -c $^
TextLine.o: TextLine.cpp
gcc -c $^

45

%

 *     4  : "
% '
  " gcc.   "#' 
 Y "% "
\ 
+

 #  :
iEdit: main.o Editor.o TextLine.o
gcc $^ -o $@
%.o: %.cpp
gcc -c $^
* make =
  > "

 ",   4=

 
+
 
" (. . «+


"», >  «! -
< "»),   
  4  Y "
" . "  +-
* ' % 

, 
"#' " ' 
 # \ " " -
+
 
".
&%& +:H; D:C%% +C:V:>: make-'*C
<
=
 
'  make-> " =

  -
= 4 ' 

. "

 
#,   

"
-
4 
% ]
% > ",  "4=% * , +
 #
 +
 '   . <  \    '   * > "

iEdit: *.o
gcc $< -o $@
+
 
  #, . .   '  "
 
 "# =
-
4=
   
 ]

> ". 
 \  "#
 "
 " "+% ' *"% > " (*.h), 

"4=% ' -
  
 
 . " *  '
+ # * '  -

, "

  "#' # "

"+  ,    



 "+
,  
> ", 
+ =
% 
, "+-
 #  "       
'"# 4=4 -
* . $     
    ' % Y *:
1. <"#   
% > "  % 
 * 
(
% > "  Y

 «cpp»). " \* "

  "#' #
>     ,    "
> 4 wildcard,  
"
   > "  '   Y "   '    "*
.
2. <
 ' #   %% > "    ]
%
> " (' 
# Y

«cpp»  Y

]
% > "
«o»). " \* "

  "#' # > 
 patsubst,   ' -


 '  4    '    
.
"
4= 

+ >   4 
4 make-
> "   "#' 
  ' % % Y *:

46
iEdit: $(patsubst %.cpp,%.o,$(wildcard *.cpp))
gcc $^ -o $@
%.o: %.cpp
gcc -c $<
main.o: main.h Editor.h TextLine.h
Editor.o: Editor.h TextLine.h
TextLine.o: TextLine.h
  ]
% > " *     

(
"# iEdit).   "  =#4 >  wildcard "
  

% > "  Y

 «cpp»,  %=%  
  
 .
 
  =#4 >  patsubst "
    '  
%% > "
 '
    ]
% > " ( Y
-

> " 

  «cpp»  «o»). Make-> " 

#  " "

-

 "#.
<:@%+/:% J:+&:%% H<+:+%* : H>:C:<:@F= '*C:<
 '  

"

]
% > ", 
 -
"


"
 ' 
 ]
% > "  ' *"%
> ". ! 
:
….
main.o: main.h Editor.h TextLine.h
Editor.o: Editor.h TextLine.h
TextLine.o: TextLine.h
<

"

' 
 « 4» +
 
 # =
-

* ]
 . !

*     # > " 
% 
 

"# 
 
% ' *"% > ",
"4
%  =#4 
 «#include»: ' *"
> "
* "4 #  
  *
' *"
> "    «

' 
» +
 #   ". < 

  ' -

 +
"' #   "#' 
 " make  -
" gcc.
" 
   make  " gcc 

 
"#
 :
x "4  "  M. "  +* > "  % 



 
   #     %   ' -

  
" " *  make.    ' 
  -

   % > ",  +

> ", "4

 =#4

 «#include <_>»  «#include "_"». <"
' -

47


  "   "
   *


]
% > " 
%.
x "4  "  MM.  "*
 "4 M,     ' -

  
 "#   % > "  > ", "4


 =#4 
 «#include "_"».
x "4  "  MD.  "*
 "4 M,    ' -

 
 
    %, ' 
  
"#-
 > " ' 
. & \* > " >  
 ' 
 %-
* > " 
 ' 

* Y
  Y

«d». ! -

, > " ' 
 " > " main.cpp 
  ' # main.d.
 "
 "4 M  "  %   ', 



 "
> ' ' 

 .
x "4  "  MMD.  "*
 "4 -MD,    
' 
  
 "#   % > ",  > ", "4
-

 =#4 
 «#include "_"».
  ,  " +
  #     –  
"
 
 "#   ' 
  '  
 
(  M  MM).   * "
 "  %   ,
"#   "
  ]
 > " *

 

=
 > " ' -

 (  MD  MMD). <
 
"#
  "#' # -
   , . .
1)  '

  *-" ' %% > " 
  

'  "Y#  
4=
 > " ' 
;
2)  

> " ' 
 % « ""
"#» 
   "  
 
 +
  

-
  " .
&' % '+%  , MD  MMD,
 
"#

 ,
. .  =#4 
 «#include <_>»   "4 4

"# «  
» ( 
«#include <iostream.h>»),   


' *"
> ",  
* * 
# ( -

, «#include «myclass.h»).
<"
*   > " ' 
 >   ,  
4
Y

«d». " *,  
" # %   "
make,
"

  "#' # 
 #include:
include $(wildcard *.d)
"

  #  
  "#' 
>  wildcard,
. .   
include *.d

 "#  # "#   "
,
"    "*


 %# %   > "  Y

 «d». C"  % > "

48

,  make   ' 
Y, . . 
 
    

«  #» \ > ". C" +


 "#' # > 4 wildcard, 
  % > " \ >   

 4
 .  "

, 
 include  *
  
  ,

 *    , 
'  Y. $

#    
makefile ' \* 
*" "
4=  ':
iEdit: $(patsubst %.cpp,%.o,$(wildcard *.cpp))
gcc $^ -o $@
%.o: %.cpp
gcc -c -MD $<
include $(wildcard *.d)
G "  Y

 «d» – \ *

  
 " 
gcc > " ' 
. ,  
,   *" > " Editor.d, 
 

"
 '  " > " Editor.cpp:
Editor.o: Editor.cpp Editor.h TextLine.h
$

#  '

 "4* ' > ", Editor.cpp, Editor.h "
TextLine.h, > " Editor.cpp 


 "   " "
 -
 
 > " Editor.o.
H%T%% '*C:< + +=:;F %/+ J: ;&%/:&
< 

 Y
make-> "  "
 
   
%
+
 #  "#'  "   
"#Y% * .   

"

 '
* ,   

#  % #

> "  % 
      "*
.    "


 
"# '
= # \ > "  ' 
 ,  -
+ 4= "*
4    
 . " \* + >  -
 # make-> ",  

"
%.o: %.cpp
gcc -c $<
 "#  . " \*  "#'4


4 VPATH,
  

"4 

 , *
*  " * # -
%

.  "
4=
 

> " Editor.cpp  Editor.h -
"+
    "*
Editor, > " TextLine.cpp  TextLine.h    "*

TextLine:
main.cpp
main.h
Editor /

49
Editor.cpp
Editor.h
TextLine /
TextLine.cpp
TextLine.h
makefile
   *" makefile " \* 
:
source_dirs := Editor TextLine
search_wildcard s := $(addsuffix /*.cpp,$(source_dirs))
iEdit: $(notdir $(patsubst %.cpp,%.o,$(wildcard $(search_wildcard s))))
gcc $^ -o $@
VPATH := $(source_dirs)
%.o: %.cpp
gcc -c -MD $(addprefix -I ,$(source_dirs)) $<
include $(wildcard *.d)
<  
4 
=    make-> " 


"
"
4=
'

:
x " % 
   
   % 
 , -
 + 
  ' #  
"#% 
 %, ' 

 
"# 



  source_dirs;
x Y "  " >  wildcard (


 
search_wildcard s)   « 
», % '   
-
 source_dirs;
x


  VPATH  "#'
 "  > "  %-
 
 ;
x  "  '
Y
  # ' *"
> "  
%

 %  % 
 ; " \*  "#'
 > 
addprefix,  "4= 
>->" * «I»   
 " gcc;
x  >      ]
% > " ' 
 %%
> " «
»    "* , *
  "+
 ( =#4
>  notdir);
x  >      %% > "  Y


«cpp» "  "#'  >  addsuffix,  "4=  >>
«/*.cpp»   '    "*  % > " ,  '  



 source_dirs.

50
D:&/ J&:>&F + &HF J&%& /:JC?
K  '
 
%#  "
 
"#%   
* ,  "  %   "#' 
 '" 
-
. $  
  "#'  % '"%    -
"
  "#' 
"   
 
 * .   %
" % "

  "#' # %,   
1) 
   *   4  =#4 *  *
+
make-> " ;
2) 
%
    " =
"4  make-
> "
  "#' 
 
 ,

 
% * 
make 
   
( 
, >" *  " )
make compile_flags="-O3 -funroll-loops -fomit-frame-pointer"
$   ',  "

   '   


 
 "  
 


 "
"#


 compile_flags
 makefile. C" 
   "  
"# (   
-
 
+ 
"),     ' 




 com-
pile_flags "+ # ' "4
   .   > " makefile
  "#' 
 
  +
 *"
# "
4=  ':
override compile_flags := –pipe
source_dirs := Editor TextLine
search_wildcard s := $(addsuffix /*.cpp,$(source_dirs))
iEdit: $(notdir $(patsubst %.cpp,%.o,$(wildcard $(search_wildcard s))))
gcc $^ $(compile_flags) -o $@
VPATH := $(source_dirs)
%.o: %.cpp
gcc -c -MD $(addprefix -I ,$(source_dirs)) $(compile_flags) $<
include $(wildcard *.d)
<


  compile_flags "
 
' 

'  
 ,
"  '  ,   "
 "#'
 ' 


" 4, .
. «pipe». " 
   "  
-
  "   "
 >" + pipe. "

  #  

 
%#  "#'   


 override, -
"#'  " '




 compile_flags   make-
> " .   "


 
>" *  "  '  -
   
 '
=
 


 compile_flags.

51
2.2.  
$$
!  !
1. ' #  


  
 ".
2. & "#' "4 

  , ' # 
Y4
*   '
C (C++)   "#' 
,   , % -
%% > " ( *  ).
3. "  '    
 " make ' #
make-> " (. . «< 
'  "

"+* make-> " »).


4.  "# *  ( "  #,  
%
" #).
5. < ' #,   '

 * %* > "  "
-
4=
 '
make   "
 "# 
%
 
 "  (
'


> "

 "   
)  '-


   /" '
]
% > " (> "  Y
-

 .o).
6. ' # make-> "    
  '   -
 %% > " *  *"  "
4= ":
x   "   *  ( "
 "  -
 > "), >" *  "   
   "*  % > " 
   > "  (  "* src, bin  . .) '  4  =#4



%  makefile;
x '  %% > "  '
C ( ++) 
"  make-
> "
"+ >   #  
;
x  "

" clean,  " 4=
 


> ";
x   "* 
 "+
 #      "
4= -
':
x src –   "*  % > " ;
x bin –   "*    > "  ( "  );
x makefile.

2.3. 
 
   2

 "+
 
+ # "
4=
'
":
1. $"# ", > "
 *"  
+
 -
' .
2. D
".
3.   
.
4. &%

 ' % * , 
+
' %
make-> ", ""4 4
'"#  .
5. .

52
3.        3.   
 
      
 

 UNIX
$  !
' #  
  "
    



Unix   *  
  " ' ,
 "
   "
 .
 
&'# 
* 

  "
   
Unix,  +
  % '  . A '  # "+

" * % "
   "#' 
 % '  -

 #4
, 
 >   "%


%.

3.1. 
  

3.1.1. :% J::/


< (thread) + 

"#    # 
 , "4 4= 
 "4=4 "
 
"##     "#'4=  



  \* 
 .
=

 
%  
* 
"'  .
<"#' 
"#
 – ,
"'



' 
-
"#
"
    4=
 "#' 
"# -
 
.
<    – ,
"'



' 

'-
   4=
  
 .
 + 
 

,   ,  ,  \  



 , ' 
  +
 * 
 ,   -
' #   "# " *"   \* 
 .

"

   ' "4
   
-
"#'  
%
 . 
 
 
 
' -
  
"#*, " 
* 
 
"
 
"#
-


 ,    %  %  
"# 

«  
» ' 
 

. <  \ + +# -

"# 
, 
'   
,   +
  "#
 "
' 
Y
 
"#* 
 .  "
 

*

 , ,  +
   
, "#4 ' 
 
  ' 
Y


 "

 +


% ' %

53
 , "# % +


%
 ,

"
% " \* 
 . C=
 "
 "
 
'-
+# '
#   "  , *   
 -

 
% " % ( 
,  

" " %  
-
) +
 "  #    , "   
"#*

 .
<
  
   Linux " #  1996 *   '-
 " # LinuxThreads,

  "
  ? (Xavier Leroy).
A '     "
 LinuxThreads "    * '-
 # 
+   Linux   
, *   

=



 ""  % 
 "#% 
% ' "    -
. <'

'   " Linux 


"  ' 
  4-
=
*   – NGPT (New Generation POSIX Threads)  NPTL (Native
POSIX Thread Library).  2002 * *  NGPT > 
 
-
" #  NPTL  

#
"'   NPTL "
   
Linux. <
  Linux 
 
 # 
 -
    POSIX, \ 
* 
"+
 Linux
"+ 
' "
  "  #  % POSIX-
%

 %.

3.1.2. &%LT%+<  %;:+/ +J:CPH:< J::/:<


<       * % Y -
*  . W Y ' 4   "#'    '
-
"
%
  
 ( 
, =
* 
*   ) 
"4   "
 "

Y * "  Y – Y


% ' . C" '   '
"
 
+ 
'  
 -
,     % =
    "
 ,  
#
Y '-' >"  +
.  
, '
"

' -
  
+ 
"# 
'  
     



' ==
   *% '
 Y % ' .  "#'
 +  ' # ,   " 
%  ' 
*
  *  "+
 +
, 
  " 
% 
' 
*  
"#* 
 .  
#  "  -
 
"#* 
    +
    * -
 "+
 Y
, 
  
#  " "+
   -

 
 .  
*, 
'"# 
 * %
* ,   ", 
 4 
' 
Y
 


-
, ' =
 
"#* ( 
-«'»),   +
 -
'   "#'  * 
* "+
.
<
=
  
   "#'  
"#%  

  "
 *  

   ". 3.1.

54
$ " 3.1

       
<
=
 !
 
<   "#' #  
- ' 
* * -
%  "
  

"+
 +
 
 #

"#% 
  ' ( 
: ' 
"#% "  \



     



   '-' 
%-
'  
"#% "#' 
"
)  % '  -



 "+
, - "  *  -
"#'4=% ,    - *  ' 
"# "+

,
  % ' 
 '+ 
 "    '-


"
 \% 
  - ' "+  "  "-

"#  . W 
 '+- 
 '"% 
# 

 = #  "


*   
 '+%

 '-' +   
-
/'   %

  ",

"4


+ < ""
"#
"
,
  % 

 
-
"' 
 
* -

 
#Y% '   
%
- * "+
,  -
 ,   
4 

"4
- #4

  
 

 
+ 
  
' 
"#   "-
# 

, 
  "*-
   >  "# -
   * 

3.1.3. &:>&&:<% J::/:<


 Linux  +     
"
"
 
,  " -
*,  ' #  , + ' #  
.  
" '   "
"#%   "#'4 
 *
 . W 

 "4  




*" * 
 ,   '
"4  *"  
 


 , > "



       * ". " -
' 
 
 \*  

 
 "# 
 – "
*-


 (lightweight processes). <"# "  


-
 '  # 
4  4 
*   (  *%
-
 ) 
* 
 - 
", ' 
* "
** 



 ' 
"# 
#Y% '  , 
 ' 
"
* -


* 
 .

55

>  POSIX 1003.1c 

,  
 * -
* "+
 
"  
>  ,    Linux   +-
* 
 ,   "
  
- ,
#  
>-
  .
+:<F% 'L/? ;C &D:F + J::/
"      "#'4 "
4=

>-
:
x pthread_create – ' 
 ;
x pthread_join – "  
 ' Y
* > 4 -

 "   +  ' 
Y
  ;
x pthread_cancel –  
' 
Y

 '  ** -
 " 
 ;
x pthread_exit – ' 
Y
 ,  ' 
Y




>  pthread_join.    >   >  exit,  
' exit  «» 

*  

  ' 
Y
4

 * .
JL+/  H<%&O%% J::/
< ' 4 > 
 pthread_create, 
4=
 "
4=4
*  :
int pthread_create (pthread_t* tid, pthread_attr_t* attr,
void*(*function)(void*), void* arg)
   >  

"
  ' *" > "
<pthread.h>.
<
 
 \ > 
 "
   ' 
"# 

-

4  pthread_t,   "+ 
>   ' 
*
 .   
 –  ' 
"# 


4  pthread_attr_t –
 "#'
 "      . $
# 
 
>  pthread_create "+
 # 
 >   . W >-
 *
 "   +
"#,  >  main " *"  -
* . K

 
 >  pthread_create 

  void*.
W 
 +
  "#' # "

  ' 
  > 4
 . 
"
' pthread_create >   
 ' -
=
   "

""
"#   *   * .
!  ' 
 
 ' "
' pthread_create, -
 

 
,   ' # 4 > 4  , + -
"# 
 
*
"#

, - 
"# 
\ "+
  "#. !
%  #  
-

"#  ' 
* * "+
,  
"
'+ 
#
'
Y   "
 * . C"

56
 '   '" Y ,  >  pthread_create '-
 =
 
"

' 

, 
4=


 Y.
G   "+ 
# *   
void* func_name(void* arg)
& >  +
 # "4.  *
 arg "
  ' 
-
"
,  


  "

 

> 
pthread_create. G   +
 
# ' 

,  
' 

+
 #     *  " 
.
G , '
  ' >   , "+ "  # -




"# (\ +
 "+ "  #
 -

> ). A



"#  >  – \ > ,   +

# '      
, *  +
'  . $ 
>-
  "#'4 " "#




( " "# 
"
4 -
#)  
% " %, * % 



"#
 "* * -
"#' # *" "#


.
 
Y

>   %  "
4=% " %:
x >   ' " > 4 pthread_exit;
x >   *"  % ;
x  "   ' 
Y
  *  " 
.
G  pthread_exit ]"
  ' *" > "
<pthread.h>


*  

 :
void pthread_exit(void *retval)
 *
 >  "
  ' 
"#  ' =
 ]
.
!
"#' ' = #  ' 
"#  
 (
 
) ]
,
]"
  
"
>   , "   
 ]
,
' 
   "
  
"
> , . . "
' 
Y
 -
 


]

* >   "4.  *
 ' 
"# -

 
+ # 
   


"
 
+,  +


  
#
' Y
.
 "
,
" 
% + # ' 
Y
   
"


"#* 
 , '
 >  pthread_join "
4=
*
 :
int pthread_join(pthread_t th, void** thread_return)
<
 *
 th 
% "  '  +
*  ,
' 

\* *
 

"
 
'"# 
 "
 >-
 pthread_create.   

 * *
 thread_return -

  ' 
"#  *
 >  pthread_exit " NULL,
" 

* 
' =
.

57
:+&:@:% H<%&O%% J::/
G   +    #    * 
"#
-
* ,  %=
   "

 >  main. $   +
,
    "
 
  * '
 
%# -
 ' 
Y# 
, *  * 
+
  -
#   ' 
Y#  ' . "  * ' 
Y
-
  +  "#' # > 
 pthread_cancel:
int pthread_cancel(pthread_t thread)
C
 *
 \ >  "
 
> 
 – thread. G  pthread_cancel ' =
 0  "

% 

"

' 

( Y)  "
Y.
!
   ,  pthread_cancel +
 ' 
Y#   -
,


"#'  ' # 
 
"#* ' 
Y
 .
 
"
>   + 
"#  
"#  #  -
 ' 
Y
  
  ' pthread_cancel,   
*  -
 # \ '. <\ ' >  pthread_cancel "

 -
   #   '    "

 * ' 
Y
  .
G  pthread_setcancelstate 

"
, 
 " 
*-
 #   =

 
  =#4 pthread_cancel " 

.
*  >  

 
int pthread_setcancelstate(int state, int* oldstate)
 *
 state +
  #  ' 
:
x PTHREAD_CANCEL_DISABLE – '
 ' 
Y
  ;
x PTHREAD_CANCEL_ENABLE – '
Y

 ' 
Y

 .
   *
 oldstate ' 
  ' 
"# 
=

' 

*
 state.  =#4 >  pthread_setcancelstate
+  ' #     ,  
  "
  %
 
"#' ' 
Y# ' >  pthread_cancel:
//...
//   %, 
 K   
//...
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
//...
//   %, 
     
//...
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
//...
//   %, 
 K   
//...

58
G  pthread_testcancel '
  '+*  *
' 
Y
  ( 
). $ 
 
% "  -

* ' 
Y
  , . .  +

"  
' 
Y

'-

Y
, , "Y '    
' 
Y

,   -
+
 ' 
Y#  
 '. C"   % 
+
"-
+
*  * ' 
Y
 (
 \
+  "
 
" 4),   " '    
' 
Y

, "# -
*  ' 
 
. *  >  pthread_testcancel
void pthread_testcancel()
 
     POSIX   
 "4
' *% «%» > ,  
open, pause  write.
$
 



,  "

 +
 #
  -

"#, 
+ # 
 
. " \* 
%



 
+ 

"
* ' 
Y
,  
"
  =#4 -
' > 
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)
' > 
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL)


  
+ "+
*  * ' 
Y
.

3.1.4. =&:H? J::/:<


<   "
 
"#%   *% " % 
%-
 % '  # % ' 

. =

 
"#  -
 % '  :
x ' 
"4
 – #4
 (Mutex – MUTial EXclusion);
x



;
x 
 > .
F
% '  "#' 


%   
 >  
* % "+
%  "*
 
% '  "#' 
\% 
 % '   * 
% "+
%.
F
% ' #4

 "
 = 
 % ' 
 "
 . F#4
 + 

"#   ]
 % -
' ,     "
  
* "#

, *

'   -" .  "4 
 #4
 +
 " -

# "#  .
& "#' 
#4
 *  
,  "#   

  
 

  "
  
4 
 4  .
F#4
 +  "#' #     
.

59
  "
4=

  #4
:   "' ,  -
"

, ' %  "  
,   ' %  .
]
 % '   "4


  

  "  4 +
#4 ]
 
 . <  '"-
% 
 % * ' #  *   * 

' ]
 % -
' , 
=

 '
"
4 # ,  +
 "
, *
  '"% 
 % 
  * "  * .
]
 % '  +  +
'
#  > " %, *

 =
 # 
'   ' Y
* % 
 .
!
%#  % '   '
  "
4=%
" %:
1. C" % '  – \

   *   #
"
 
"## '
"
% (=%)  %.
2. C"   % " "


 % *  "#' #


 ]
 % '  
. <  \ ]
 -
% '  "+
   "'  # "#  ' ' 
-
4=% 
,        "'  ]
 -
% '    "

*   

.
3. C" % '  +
 *   # 
# '
-
4=%  %.
4. C" 
 +
  '# > "  =

   \


,   "
  "#   ' .   "#
 "
 "  , "4  *   "4 

, -
 + 4=
 > ",   
  # " , " -

,  ' #  > " 



 ' 
 .
QL/? +=&:H? J::/:< + +J:CPH:<% PR%/+:<
" % '     "#' 
 #4
  "#-
'4 "
4=

> :
x pthread_mutex_init –   "' 
 ' "4 4=4
" ;
x pthread_mutex_destroy –  "
 ' "4 4=4 "-
;
x pthread_mutex_lock –   "
 " .  "
,
"
"  "  "
  * , 
=    -
"
   "   * 
;
x pthread_mutex_unlock – 
 " .

60
?CH?  L;C%% :DY%/ &DL:< PR%/+
  #4
 * # '    + . K
'
#   #4
  " 4, + ]#    -
"'  # ]
   #4
 , ' 
 '
# 

"


' 
. K    #4
   " 4   


  "
"+
,       '
# %. <"
*,  
>      #4
 , +
*   "'  #.
G 
int pthread_mutexattr_init (pthread_mutexattr_t* mattr)
 "#'
,    "'  # ]
   mattr ' 
-
  " 4. < # "  +* ]
   
"
-
 
 
+   
  "
.
< 
' >  pthread_mutexattr_init:
#include <pthread.h>
pthread_mutexattr_t mattr;
int ret;
/* % %       */
ret = pthread_mutexattr_init(&mattr);
" 
*  "
 ]
  , ' *  -
=#4 >  pthread_mutexattr_init, 
% ' # > 4
pthread_mutexattr_destroy.   "
'+ 
 ,
     pthread_mutexattr_t "
 '  .  ' =
 0
"

Y* ' 
Y
 "  *
' 

,
" 'Y"
Y . < 
' >  pthread_mutexattr_destroy:
#include <pthread.h>
pthread_mutexattr_t mattr;
int ret;
/*     */
ret = pthread_mutexattr_destroy(&mattr);
DC+P <;:+ PR%/+
" #4  #4
 +
 # " 
  -

, "  
 . 
 "

  # #4
 *
"# , ' 

,    '   #4
. 
  "
#4
 =

  '
"
   +
 #
'
"
 
  
"#% 
. < " 4 #4

'
  "   
  " 
 +
#4 -

 .

61
K  # " #    #4
  "#-
'
 > 
int pthread_mutexattr_setpshared(pthread_mutexattr_t* mattr, int)
<
 *
 mattr "
  ' 
"
  ]
  ,
"  *   "
 " # .   *
 -
"
  , '  4=
   "
4 " # :
PTHREAD_PROCESS_PRIVATE " "   
 
PTHREAD_PROCESS_SHARED " "   
. F#4-

, '   "   
, "+
 =
 # 
'
"
 .
< 
' >  pthread_mutexattr_setpshared:
#include <pthread.h>
pthread_mutexattr_t mattr;
int ret;
ret = pthread_mutexattr_init(&mattr);
/*        : private */
ret = pthread_mutexattr_setpshared
(&mattr, PTHREAD_PROCESS_PRIVATE);
G 
pthread_mutexattr_getpshared(pthread_mutexattr_t *mattr, int *pshared)
 "#'
 " "
 
=
 "   #4
 -
 :
#include <pthread.h>
pthread_mutexattr_t mattr;
int pshared, ret;
/*     pshared    */
ret = pthread_mutexattr_getpshared(&mattr, &pshared);
?CH? PR%/+
G  pthread_mutex_init
 ' 
 "   "' 
#4

int pthread_mutex_init(pthread_mutex_t *mp, const pthread_mutexattr_t
*mattr).
F#4
,     '

 *
 mp,   "'-

 ' 

  " 4,
"   *
 mattr 

NULL, " 

"
   ,  
+
 "
  -
=#4 pthread_mutexattr_init.

62
G  pthread_mutex_init ' =
 0 "

Y* ' 
-
Y
 "  *
' 

,
" 'Y" Y . < 
 "#'-
  >  pthread_mutexattr_init:
#include <pthread.h>
pthread_mutex_t mp = PTHREAD_MUTEX_INITIALIZER;
pthread_mutexattr_t mattr;
int ret;
/* % %        */
ret = pthread_mutex_init(&mp, NULL);
* #4
   "' 
,   %    ( '-
"  ) .  
 

"

#4
 *
  "'  # 


 ' 
  " 4  -
=#4   PTHREAD_MUTEX_INITIALIZER. < 
  "-
' :
/* % %       */
ret = pthread_mutexattr_init(&mattr);
/*    mattr  Y % */
ret = pthread_mutexattr_setpshared
(&mattr, PTHREAD_PROCESS_SHARED);
/* % %    
   */
ret = pthread_mutex_init(&mp, &mattr);
J&% (H=<) PR%/+
G  pthread_mute_lock  "#'
 " '   " ' % -
 #4
 .  *
 >  "
 
 ' 
* #4
-
 . C" #4
 +
'
, ' 4=  " 
  #4-

    

#  
. * % '  '
pthread_mute_lock, #4
 ' 
, ' 4=   -

* " 
"#
. G  pthread_mute_lock ' =
 0 "
-

Y* ' 
Y
, "  *
' 

,
" 'Y" Y .
< 
' :
#include <pthread.h>
pthread_mutex_t mp;
int ret;
ret = pthread_mutex_lock(&mp);

63
"   ( " ) #4
  "#'
 > 
pthread_mutex_unlock. <  \ #4
 "+
 # '  , '-
 4=  "+
 # " 
"#
 #4
 , 
# 
, 
*
'  ". < "4
 *
 +   #4
,
* -
-" 
"
,  %=    "


, 
"  . G 
pthread_mutex_unlock ' =
 0 "

Y* ' 
Y
 "
 *
' 

,
" 'Y" Y . < 
' :
#include <pthread.h>
pthread_mutex_t mp;
int ret;
ret = pthread_mutex_unlock(&mp);
=

   ' %  #4
 
' "    .
G  pthread_mutex_trylock 
 
 '  
#4
 .
 "
 
" 4=
 

 pthread_mutex_lock. C" #4-

 +
'  , ' ' =
 Y,   , ' Y
\ > 4, 
" 
.   "
#4
 '  
-
, ' 4= 
  
* " 
"#
. G 
pthread_mutex_trylock ' =
 0 "

Y* ' 
Y
 "
 *
' 

,
" 'Y" Y . < 
' :
#include <pthread.h>
pthread_mutex_t mp;
int ret;
ret = pthread_ mutex_trylock(&mp);
 %  

' #4
 
"+
     "'  # "
 "#,   *
 *
*  "#' #. C" #4

  "' 
   "  "
, "+

"+ 
-
#,    =


 \ #4
 
 "#'
.
;C%% PR%/+
G  pthread_mutex_destroy  "#'
 "  "
 #4-

  "4 . G  pthread_mutex_destroy ' =
 0
"

Y* ' 
Y
 "  *
' 

,
" 'Y"
Y . < 
' :
#include <pthread.h>
pthread_mutex_t mp;
int ret;
ret = pthread_mutex_destroy(&mp);

64
%&&= DC:/&:<:/
&* +
 '# 
%# 

* 
 
"#
  . <  \ '
 "
 , ' "4 4= 
 ,     4 ' % # 
  ,  '  4 -

4=
#4
  '"  
.
 

 +


  '  4 #4
 1  2
 '
     
'

#  ' #4
.
$ " 3.2

 «  »   J  
< 1 < 2
/*  "#'

  1 */ /*  "#'

  2 */
pthread_mutex_lock(&m1); pthread_mutex_lock(&m2);
/* 

# ' % 
 /* 

# ' % 


  2+1*/
  1+2*/
pthread_mutex_lock(&m2); pthread_mutex_lock(&m1);

! "Y   '


+ # "
 "
 '  

-
"#% #4
     +
 
 
%  %. W

%  '
 
%
 " : #4
    4

  ' 
  + 
* 
. <"
\* 
+ 4
" –
" #4
  
 n +
'
,  
"#' '  #
#4
  
 
#Y
n.
C" "  
*  "
   '   
,  

'
.   \ 
% +
  "#' # 

* , -
"# * 

 '  # #4
   
, " 
  % 
.
K
 #    \  , "Y
 "#' #
> 4 pthread_mutex_trylock.  '  "+
 #
 #4
,
"   +
,  +
 '#  .
!+
""4     "#' 
" " :
//  1:
pthread_mutex_lock(&m1);
pthread_mutex_lock(&m2);
/*  */
/*   */
pthread_mutex_unlock(&m2);
pthread_mutex_unlock(&m1);

65
//  2:
for (; ;) {
pthread_mutex_lock(&m2);
if (pthread_mutex_trylock(&m1)==0)
/* ! */
break;
/*   K  */
pthread_mutex_unlock(&m2);
}
/*   */
pthread_mutex_unlock(&m1);
pthread_mutex_unlock(&m2);
 

Y
 1 ' 
 #4
  +  
,
 2 
 '  # % -
. K 
#,    

'
,  2 "+
    = #  #4
 1;
"
 "  ", +  #4
,   
 +
, ,

, "#  ' "     1. K *   #,
 \ 
",  2 '
 pthread_mutex_trylock,  
' 
 #4
,
"  
. C" #4
 +
'
,  2
"
 =

 Y
.  \ "
 2 "+
 -
# #4
 2,   1 * '

#
*, ' 
 # 
#4
 .
=&:H? + +J:CPH:<% +%':&

 >
 ' 
 " % '    
 
 ,   =
 "
   "#'  
 > %
 
   "#'  #4
.

 > (S) – \ ' ==
 


 , ' 
   +-
  Y #  
# "#  = 
 "#% 

P(S)  V(S)  
   "' . 
 > +
  #
-
"

  
"#
' 

. <   "
  
 P
  
 >  S ' 


 > 
#Y
  1  S > 0 " -
 " 
, «+   
 >
»,  S = 0. <   "


 V(S) % +

* ' , + 4=%
 
 >
S,
"  % 
,  ' 


 > 
"

 1.   "
, * 
 > 
 
+
2-% 
(S > 0  S = 0),
* "*   "#4  
  "* 
#4
 .

66
  "

 ' Y
 ' *,  %
  
4 
 4
 "+
  "# 
4 P(S),  %
'  


  
4 V(S).
<   >  "   "   
 >    4
 > "
<semaphore.h>. !+
   >  




 %     "
%  
:
x int sem_init(sem_t* sem, int pshared, unsigned int value) – -
 "'  
 > sem ' 

 value.   

pshared 
*

%  ' # 0;
x int sem_wait(sem_t* sem) – «+ 
 
 >
».  "
-

 " 
  
%  ,  ' 


 > 
 

"+
"#. <  \ ' 


 > 
#Y
  1;
x int sem_post(sem_t* sem) – 
"
 ' 


 > sem
 1;
x int sem_destroy(sem_t* sem) – +
 
 > sem;
x int sem_trywait(sem_t* sem) – 
" 4=    > 
sem_wait. <  \ 
 "  ' Y
*  > 
' =
  "

  Y   


'"#  .
=&:H? + +J:CPH:<% L+C:<:* J%&%%:*
" 


  '"
   + #  "
 
-
 * " (), ' *  '
"
  . ! 
"


 

"
 


: > -
  
   "
   + 
. <   "
-
 
 «>   
»  ' , + 4=% ' 
-
 "


, '"
 4 .
" 


  
*  "#'
 
  #4
.
<

  "

 
 «+ 
»  "+
 ' "  #
#4
. <   "
 
 «+ 
»  '  #4
 -
 
 '" 
. <

 '"

 + 4=
* 
 "
  
  "  #4
 , '"4=  
   
4 
 4, "
 
 
 



'"  # #4
. <   
* "  *  

-

 > 4 «* "' »   +
' =# #4
.
<   >  "   "


 
-
+   > "
pthread.h. !+
   >  




 %     "
%  
:
x pthread_cond_init(pthread_cond_t*cond, const pthread_condattr_t*
attr) –   "' 
 "4


4 cond   '   -
  attr "      " 4 (   '  0   
-

attr);

67
x int pthread_cond_destroy(pthread_cond_t* cond) – +

"4


4 cond;
x int pthread_cond_signal(pthread_cond_t* cond) – >   -

   "
  , + 4=%  "


-
 cond;
x int pthread_cond_broadcast(pthread_cond_t* cond) – > -
 
   "
  , + 4=%  "
-


 cond. <  \ '"
  
+ 4=
;
x int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t*
mutex) – + 
  "


 cond.
A 
% 
   "
Y
 ' '% ' -
  % '  . 

 
  

 4 ' -
"4

 '  
 
 "
 
 
 ".
<  


 %  "#'  "
*  # Y '-
"*  : ' #  '  
 
 ,  "#' # -
 
  ' 
4,
"' # "+

 "#' 
-
  . . <  \ 
  #4
 , 
 >   "



  ' 4 '    
 *  -
 "+
,  Y
 
# "
 Y  "+-



   

.

3.1.5. :JC? :>:J::@:* J&:>&F


"  "     *  *  
%

# "
4=

:
x     " (cc, gcc, g++. .);
x > " ' *": <thread.h>, <pthread.h>, <errno.h>, <limits.h>,
<signal.h>, <unistd.h>;
x "

"'   (libpthread);
x  *
"
, 

 *  "+
-
 (libc, libm, libw, libintl, libnsl, libsocket, libmalloc, libmapmalloc   .).
G " ' *" <pthread.h>,  "#'
  "
 lpthread,
 " 
 ,   "
 
  
>
  -
* , 

"
    POSIX 1003.1c.
"  "  * ,  "#'4=
  


-

"#


> , 
%  "
"#  ' # 
 
'  " "
4=
*
:
-D_REENTERANT –lpthread.
   "  D "4
    _REENTERANT. W
    '
,  
 % >     "
-
  * 
"+ # "4
 %



"#
 "*.

68
A



"#    "
 glibc       ',

"' 
 




"#
>    +

#Y
" "#  % %  "*. $ +
  
'
 " *  "
"#  ' #  "  ' *"-
% > " ("4 «I»)  # "  "
 ("4 L). "
 =  '
 «l»,  *  "+ # '  
"
 libpthread,   
+ 

 "#
> ,

%
"    .

3.1.6. +:D%:+ :C;/ :>:J::@:* J&:>&F


"  *  *  "+

, 
 "   -
. !+


  "

 
Y %*  , -
 
* ' # Y  "
  * % *  %.
1.   *" "#  
'  "#'  
% '
% ' .
2. ' 
 , ' %   , 4=
"#      +
 *" "#%
   '"
 
(   "
 
 ,    "

 *
     
+
 "+ #  "

 -
+
 +* 
  ).
3. <    "#   +
' % 

 
(
   ).
4. ' 
 * 
+  % ' . W -
%, * 
*
  , ' ==
 
% ' % ' ,

+ ' > ,   +
  ' 
   ' -

 
% ' % ' 
+
, 
  "

' =
 
' 4=
 . 
'"# 
' 4=
 « +
»,  *"-
 "#
 
" ' ==
, % \ 
 .
5. !
 
  > ,    " 4 ' 4
   PTHREAD_CREATE_JOINABLE 
   %  "+-
 # "'   (' =
 
"# 
) 
-
 ' >  pthread_join.  
 
, 
pthread_exit 
+
 
"
4 #.
6. ' 
*" "+
%,
 %  =
  -
"#' 
"#Y%  
%   +
 ' # "
-
,   * 
*  
4 "

* 

'

 , 
  
.
7. 

"



 * '

 "  "#' 


 
 " 4.

69
3.1.7. &%&F J&/@%+/:* &%CH?
< 
*    "#' 
  :
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
int i = 0;
void* thread_func(void *arg) {
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
for (i=0; i < 4; i++) {
printf("I'm still running!\n");
sleep(1);
}
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_testcancel();
printf("YOU WILL NOT STOP ME!!!\n");
}
int main(int argc, char * argv[]) {
pthread_t thread;
pthread_create(&thread, NULL, thread_func, NULL);
while (i < 1) sleep(1);
pthread_cancel(thread);
printf("Requested to cancel the thread\n");
pthread_join(thread, NULL);
printf("The thread is stopped.\n");
return EXIT_SUCCESS;
}
< 

"'  *  * :
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

70
#define NUM_THREADS 6
void *thread_function(void *arg);
int main() {
int res;
int lots_of_threads;
pthread_t a_thread[NUM_THREADS];
void *thread_result;
srand ( (insigned)time(NULL) );
for (lots_of_threads = 0; lots_of_threads < NUM_THREADS;
lots_of_threads++)
{
res = pthread_create (&(a_thread[lots_of_threads]),NULL,
thread_function, (void *)&lots_of_threads);
if (res != 0) {
perror(“Thread creation failed”);
exit(EXIT_FAILURE);
}
sleep(1);
}
printf(“Waiting for threads to finish...\n”);
for (lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0;
lots_of_threads--)
{
res = pthread_join (a_thread[lots_of_threads],&thread_result);
if (res == 0) printf(“Picked up a thread\n”);
else perror(“pthread_join failed”);
}
printf(“All done\n”);
exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {

71
int my_number = *(int *)arg;
int rand_num;
printf (“thread_function is running. Argument was %d\n”,
my_number);
rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0));
sleep(rand_num);
printf (“Bye from %d\n”, my_number);
pthread_exit(NULL);
}
< 
 "#'  #4
 "  "  


-
.  

 +

>  increment_count  "#'

#4
,  *   #  # (
"#) > 
'
"



 count. G  get_count()  "#'
 #4-

,  *   #, 


  count   
.
#include <pthread.h>
pthread_mutex_t count_mutex;
long count;
void increment_count() {
pthread_mutex_lock(&count_mutex);
count = count + 1;
pthread_mutex_unlock(&count_mutex);
}

long get_count() {
long c;
pthread_mutex_lock(&count_mutex);
c = count;
pthread_mutex_unlock(&count_mutex);
return (c);
}
< 
*  *   % ' 
   "#'-
 
 #4
:
#include <pthread.h>

72
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#define SIZE_I 2
#define SIZE_J 2
float X[SIZE_I][SIZE_J];
float S[SIZE_I][SIZE_J];
int count = 0; //   
   
struct DATA_ {
double x;
int i;
int z;
};
typedef struct DATA_ DATA;
pthread_mutex_t lock; //‘  Y  
// V%  
 
double f(double x) { return x*x; }
//  %  
 
void *calc_thr (void *arg) {
DATA* a = (DATA*) arg;
X[a->i][a->z] = f(a->x);
//   
pthread_mutex_lock(&lock);
//     
count ++;
//   
pthread_mutex_unlock(&lock);
delete a;
return NULL;
}
//  %   
void *input_thr(void *arg) {

73
DATA* a = (DATA*) arg;
printf("S[%d][%d]:", a->i, a->z);
scanf("%f", &S[a->i][a->z]);
delete a;
return NULL;
}
int main() {
//   
pthread_t thr[ SIZE_I * SIZE_J ];
// % %  
pthread_mutex_init(&lock, NULL);
DATA *arg;
//  
  
for (int i=0;i<SIZE_I; i++) {
for (int z=0; z<SIZE_J; z++) {
arg = new DATA;
arg->i = i;
arg->z = z;
//       ’  %

pthread_create (&thr[ i* SIZE_J + z ], NULL, input_thr, (void *)arg);


} // for (int z=0; z<SIZE_J; P ++z)
} // for (int i=0;i<SIZE_I; P ++i)
// GK       

//  
    thr
for(int i = 0; i < SIZE_I*SIZE_J; i++) pthread_join (thr[i], NULL);
// 
  ’  %

pthread_t thread;
printf("Start calculation\n");
for (int i=0;i<SIZE_I; i++) {
for (int z=0; z<SIZE_J; z++) {
arg = new DATA;
arg->i = i;

74
arg->z = z;
arg->x = S[i][z];
//     
 
pthread_create (&thread, NULL, calc_thr, (void *)arg);
//     
 K
pthread_detach(thread);
// for (int z=0; z<SIZE_J; z++)
} // for (int i=0;i<SIZE_I; i++)
do {
// G % "
"  1
sleep(1);
//   
 
printf("finished %d threads.\n", count);
} while ( count < SIZE_I*SIZE_J);
// 
  
for (int i=0;i<SIZE_I; i++) {
for (int z=0; z<SIZE_J; z++) {
printf("X[%d][%d] = %f\t", i, z, X[i][z]);
}
printf("\n");
}
//     
pthread_mutex_destroy(&lock);
return 0;
}
< 
*  *   % ' 
 
 > :
#include "main.h"
#include <iostream.h>
#include <semaphore.h>
#include <fstream.h>
#include <stdio.h>
#include <error.h>

75
void* WriteToFile(void*);
int errno;
sem_t psem;
ofstream qfwrite;

int main(int argc, char **argv) {


pthread_t tidA,tidB;
int n;
char filename[] = "./rezult.txt";
qfwrite.open(&filename[0]);
sem_init(&psem,0,0);
sem_post(&psem))
pthread_create(&tidA,NULL,&WriteToFile,(void*)100));
pthread_create(&tidB,NULL,&WriteToFile,(void*)100));
pthread_join(tidA,NULL));
pthread_join(tidB,NULL));
sem_destroy(&psem));
qfwrite.close();
}
void* WriteToFile(void *f){
int max = (int)f;
for (int i=0; i<=max; i++)
{
sem_wait(&psem);
qfwrite<<pthread_self()<<"-writetofilecounter i="<<i<<endl;
qfwrite<<flush;
sem_post(&psem);
}
return NULL;
}
< 
*  *   % ' 
   "#'-
 
 "%


%. !+


 > *
 * ,
 "#'4=
 
 >  " % '  '  (writer)  

(reader)  %  >
data  ' 
*,
# >
–  ' #.
#include "main.h"
#include <iostream.h>
#include <semaphore.h>
#include <fstream.h>
#include <stdio.h>
#include <error.h>

76

int full;
pthread_mutex_t mx;
pthead_cond_t cond;
int data;
void *writer(void *)
{
while(1)
{
int t= write_to_buffer ();
pthread_mutex_lock(&mx)
while(full) {
pthread_cond_wait(&cond, &mx);
}
data=t;
full=1;
pthread_cond_signal(&mx);
pthread_mutex_unlock(&mx);
}
return NULL;
}

void * reader(void *)
{
while (1)
{
int t;
pthread_mutex_lock(&mx);
while (!full)
{
pthread_cond_wait(&cond, &mx);
}
t=data;
full=0;
pthread_cond_signal(&mx);
pthread_mutex_unlock(&mx);
read_from _buffer();
}
return NULL;
}

77
3.2.  
$$
!  !
1. ' #  


  
 ".
2. A '  #   * 
*    "#' 

 %   '"% 
 % ' . ! 
:
  '  4   4 >  4 ' * > " ;  -
 
" 4 ' 

=



;    '"
   4  '  4  
 = >
.
3. 

# % '  4     
-
 
    "#' 
:
x #4
;
x 
 > ;
x "%


%.
4. 
# 
'"#  

 
 % -
'  ,  
'"#   *    "#' 

 
'  "#'  
 % ' .

3.3. 
 
   3

 "+
 
+ # "
4=
'
":
1. $"# ", > "
 *"  
+
 -
' .
2. D
".
3.   
.
4. &%

 ' % * .
5. A
'"#   *    "#' 
 
 -
% ' . A
'"#   *  
'  "#'  

% ' .
6.  ( 

 '"
'"#   * 
  "#'   
'  "#'  
 % ' ).

78
4.        4.   
 
   ,  
! "    
 
$  !
< 

'   ]
 
,  
% -
' 

   % 
+ 
 ,  +
% ' 

' 
4=% 
   Unix.
 
&'#  '
'+
 " bash  Unix   -
"
4 
  ('  ). A '  # "+

"'4=

%
 «"
» – «

»   "#' 
 
 
+ 
*
' 
: 
 > , '
"
 , * %   "
  

 =
.

4.1. !  "


  


4.1.1. :% J&:?%++


<     =%    
  =
 

"#-
 "
  
4 " « *  »  «'  
»:
«…"
"#  
  "
  " 
"# * ,
 "  
 '  , *  * 
 #   
. .». <  \   
+
" '  "  ]
   

, 
   4=
 "
"# 
 ( 
,
 # > "  
),  ]
   
 ,
 %=
  

 "
. $
 « *  »  «'  -

»
 ' 
 "     
%, 
% ]
.
< *  +
 

 "
 "
  
, -
 ]
.
" "


 "#* ' "
  
 > -
  

%  #4
% 
 
% 
# "

4  ' 4 


"*4.
!
=

 ' -' * 
 
+ 
-
   *  :
x  
 %  "  *  +
  * ' #
"

* 
 ;
x    +

 +
  "# "
 
"# 
-
"# '"% * .

79
& , 

 % 
'
 
 4  #  -
  "4=%  ,    %  
  (
-
"
  "  "
 # " 

 , 
, -
"#'

> ",    -  . .)  
=
* 
-

*  "
 (' 

* , * * 
 , -


  ' 



%).
&'


  
 ' 
 , 
Y  
-
   .


   
   ]
#    
x ' 

 – ' 
Y


 ( '
);
x   
 (

 '  « "

» 

«*#») – '  
 (

 '  «*-
#»  
« "

»);
x "  

 (

 '  « "

» 

«+ 
») – '"  

 (

 ' -
 «+ 
»  
«*#»).
!
% #,  =


=
 (
 ) 
-
: '


 
 
 .
 
 
"4 "
4=

:
1)
*  (
+

%
*  
 );
2) 
 (' #   "

,  "4=  > -
   

 .);
3) "#' 
"# (   
).
 # 
% Y
 ' % 
  ' 4 

 
 ,  "4 
 "#4 % 
'4=

.

4.1.2. %VJ&:?%++:% <H:;%*+<%


" Y
 \>>
 >    "
"#-
 
 

 4   ' 
 
:
x 
 ""
"#
( "

  "
"#


);
x ""
"#
( "

 '% "
"#% 
-
 %).
=
4 '"
 
 
:
x Y

   ( 
  + ,  *
 "
 "
'4 ,  "
4 
Y

=
 '  );
x 

 "#' 
 % ( "#' 
'"

     +
 
  '  % " '
"
-
* > " );

80
x "#      -" 
 ( 
,  -

    
 , * ' 


 -
=
"
 


  =
 

'   );
x "   "#' 
" ( 
,  

-

     " 
*  

   " -
 "+ ' 
 #).
A '" 4   
:
x 

("4  ' 




 

>  
);
x 
' 
(

"## 
 
 
'
 
"4  >  ).
< ]


 
 >    

 '+* '-

  


 ** 
 

  * 

+ '
"#     
* :
1) * "#
;
2)   "#
;
3) '
"
  #.
 "
* "#* 



  "#
"
 -
>  ,  
" '
=
 
    "
 .
<    "# 

>  
 «=

» 
 -
% 

' " ',
 "

. ]


 

>    \ "

  

 * 
  
 #4 " '.
<   "#'  
  '
"
  
 -
"#'
 
  " # 
*   , >  
 
. W   
 >  

 "
   "

-
    ' 
 
   "
"# -


,  

   "#'  Y
  +.
A '" 4    
   

>  
 
+

 :
x  – 
 =
"4 
 
  ,
  '   " 
\% 
;
x 
 –  

= 4

 4= 
  
-
  
+ ]
 " % 
  %  
, 
 ' 
 * # ']  -"  * 
.
<   
  +
 # %  :
x 
   – 
,  4=


 4=
 -

,  ' 4 
 % 
  ' 
4,  \
   * 
 
+
 
Y #  
  
 -
* * =
 % 
,

% # " 
" -

# +

 
;

81
x 
   – "#  ' ' 
4=% 
-
  '
  
* 
 
,   
 
 

'+* 
  
 "4 
  

.
"

 
"# 
'"

"

   %    -
"  ':
x   "#' 
   - . !
 +  
 %, 
=
"
 % 

 ; 
,  Y
100   ' " ', 
'
, " " 

  

,
Y"  * 
 "  '%. < 
   
" -
"
 «pipe» (  "   ");
x 
 =
. !

 

 
 " *
 
-
    , 
#  >   '
"
  
"#
-
=
,  
+  ,   
 

, *   =
.
! "

     (  " ) – 

    "
'
 " Unix ( 
, bash) 
+ *  , ' 
-
 '    , '
"
 " «|». ! 
,
    
dmesg | less
'
   "  *  dmesg  less, =
 " 
-
=
  ,  * 
 *  less.

4.1.3. +:<F :J%&&:< J&:?%++ < :D:C:@/% bash


;  J&:?%++F
F*
 
" ("4  bash  tcsh) 
4 > 
 "
 '   (job control).  "

'   '"

'  # 

 
"#   " '   (jobs)  =
-
"#  "

. <
+

 * #  \ "

 ,
"

 
# 

 (process).
 + '  ' 
*   
 
  
.

   
 4=%   =


 
 + -
  ps,  
, "
4=  ':
/home/larry# ps
PID TT STAT TIME COMMAND
24 3 S 0:03 (bash)
161 3 R 0:00 ps
/home/larry#
!

 (process ID, " PID),  ' 

 -
"
, "4  "# 
,  

  


82
 +  4=
 
. <"
 " , ' *" "
 
COMMAND,  '
   4=
  .    "

 
 '  
,  
' "   "#' 
"# larry. 




=
*  *% 
, % "   +-
  #   ps -aux.   
  , ' =
% "#'-
 
"
 larry,
# "# bash (   " " "#' 
"
larry)      ps. ,  " bash 
 
-

    ps. * "#' 
"# 
"   ps, "
bash   "

 "#. <"
*,     ps ' " 4
 ( " 
 

  \ ),  "

' =
-
 
 bash.
A  4= 
  +
 ' 4 '  
 (job). 
#   "



 
" # '" 
+ \ . "

 
#,
  
  ' 4 «'  
», * 
4   "
-

'  
 (job control) – \ >    ",  

 "
 "#' 
"4 '+#

"4 # 
+ 
-
"# '  .
 "#Y
"
 "#' 
" '  4 "#  '  -

– \    ,  4  
" "

    -
"
.  ,  "#'   "
 '  , + ' -
#  ' 
"# '   ,  

 ,

"4 #

+ .
 "

'   +
 # "
',
",  
, 

  

"#Y 
 > "  %



 #
-
   
,  
" #  4-#  *4 
4.
 =#4 >   "
 '   + 

 #

  , 
#  *" Y
4   "   "#
 
-"  *

. *   
" , + 
#-
    

     +#
*   +
,
    " .  >   "
 '  
#
=

* "
'% 

.
%&%;* JC  '::<F* &%V
   * # " 


 " 
(foreground), " >-
 (background). !


 " 
 "4 
 

 -
+
 # "#  '  
.   



 " 
– \  '  -

,    % ' 

"#' 
";  "

  "     "
   \  (
"  "  



 "  -"
=
). ! , >
'   
" -
4   
 " ;   ",  
'   
+ 4 
' 
  "#' 
"
.

83
!
 
'    "4 
# "*   
 % 

% 
* 

*. < 
 % '   –  "-
 
* ,  +
+ 
"#Y% > ". !
  % -
 
#  \   + #, * \ '    ". $ 
' -
  "

  #  >
+
.  \ 
 +  # 
 * *  .
    +
+ (

)   " # (suspend).  -

  "
 '  4 +  #  ' 
"+ # -
 


 " 
"  >
+
. <  '"
 -
"
  "
* '  
* 

'

 – ' -
 
"+
  "#  * 
 , *

*  ".
<
 
'   – 

"
   '  .
< 
  (interrupt) '   
 *
. <
 
' -
   =
"
  + 
 
4=
  
" Y,  \ Ctrl-C.  #
 
'  
 
 ' 
'+. "

  +
' #,  
 
* 


%  4  
 ,     + 
  " -
Y Ctrl-C +
 

 # 
 

"
. W 
"  " -
*,  *  *" +# "
 
 
+
,

  
 ' 
Y
 . ! 

 
* 
 #
    
"#'.
%&%<:; H;* < '::<F* &%V  L@:V%% H;*
! 
  * 
. A     yes,   

 '*"  +
 
 "
'. W   "
 

-
   , =% ' " «y»,     :
/home/larry# yes
y
y
y
y
y
<"
 
"##  %   
 

 "+ #.
+# \ 
 +  + 
 " Y
 , -
  "
  
 Ctrl-C. <  

#  
. K-
  \  
" # \ 

  "
 
"##,

-
        yes  /dev/null.   
 "#
Y
,   /dev/null 

   «
   »: 
 
,
" 
 \  ,   4.  =#4 \*  

84

#  ' "#  "Y "#*  
 %
* :
/home/larry# yes > /dev/null
$

#  \  
* 
.    *" Y

-
  "  +

' =
. W % , 
  yes 

=

  "
  =
, =
'
 y  /dev/null. +# \ '  
 +
+  + 
 " -
Y
 .
  

#,   %
,    yes "+ "
 #,   \  *" Y

  " "+ 
-
#  \ . " \* +   yes


  >
-
+,   
    #, 
= #   .
  


 
  >
+ –   #
" «&»     . < 
:
/home/larry# yes > /dev/null &
\verb+[1] 164+
/home/larry#
  , *" Y

  "  # "#.
 ,  ' 
 «[1] 164»? & 

"# "   yes -
"+
  #?
=

«[1]»
 "
  
'   (job number)
" 
 yes.    "  
 
'    +-
  "
 '  4. <"# yes "



 "
 '  
,
  
 
1. K" «164» "-

 
>  
, 
4=   
-
 (PID),  \ 
 +
  
 
.    
 "#Y
,  
 +  = #,  '   \% 
.
& , 

#   
# 
   yes,  4=  >



 " 4=  '  y    /dev/null. "
*,  ' #   \* 
 , +  "#  
jobs,   "
 

   "
/home/larry# jobs
[1]+ Running yes >/dev/null &-
/home/larry#
F ,  \ *  

"# 
. " *,
 ' #   '  , +  +
 "#' #  
ps,   \ "  '  Y
.

85
" *, 
 #  '  ,  "#'
  
kill.   

*
 \  

 " 
'  , "
PID.  
 Y
"

'   " 1,    -
 
/home/larry# kill %1


  '  . *  '  4  = 4 
* 
,

PID, *

 \ 
     
+  -
# " 
 .
$

# 

   jobs  ,  
#
'"# 
-
=
* 
:
/home/larry# jobs
[1]+ Terminated yes >/dev/null
/home/larry#
G 
 '  
+
,   
  jobs
 "
4= '  \ 
 
 

   >  .
+# '  
+  +
,  "#' 
> 


 (PID). W 
,    
>  
-
 '  ,  '
  
   '  .   Y
 

' 

PID " 164,     
/home/larry# kill 164
"  \ "
  

/home/larry# kill %1
<   "#'  PID   

*
   kill #
" «%» 


.
&:+:</  J&:;:CV%% &D:F H;*
<
"+
=
 
,  =#4  * 
 +



  >
+. < 
 ' 
   ' (



 " 
), ' 
   "
   stop,  ' -

    >
+
.
    " 
   yes 


 " 
,   \

" "# #Y

/home/larry# yes > /dev/null


   

, "# 
 
 


 " 
, -
*" Y

  "  \  
' =
.
$

# 
 *, 
 # '  
 
 " Y
Ctrl-C, '  
+  # (suspend,  "# – « 
-
#»). «<
Y

» '  


 +
,
*  "


86

 

  "
  
%  ,   
 '"
-
. "   '      + # 
4=4  -
4 " Y,  \ Ctrl-Z
/home/larry# yes > /dev/null
{ctrl-Z}
[1]+ Stopped yes >/dev/null
/home/larry#
<  "
 
   
 "
. ! 
* 

  "
"#

  
 . <  "

'  -

+ ' #   +
,       
" -
 "
.
" '"
  "
 '   


 " 
+
 "#' #   fg ( " «foreground» –

 " ):
/home/larry# fg
yes >/dev/null
   "
=
' 

  \   ' 
 ,
   "#' 
"# 
 ' #,  

 '  
   

 ' " 


 " 
. <   \ '  

=
'
 + 
 " Y Ctrl-Z,   \ ' ' 
*  >
+
  bg ( " «background» – >). W 

  , 
  
 
  #  ,  
"  
* ' 
 "#-
' " #    " «&»  
(  \ 
" "# 
-
=
 '
"
)
/home/larry# bg
[1]+ yes &>/dev/null &
/home/larry#
<  \ *" Y

  " ' =
. 
 
  jobs "+  ' #,  
 yes 

"#   -
 
 
; \ 
 + +#   kill,
  \ 
" "# #Y
.
" *   # '  
,  4=

 >
-
+
, 
"#' "#' #  
 " Y Ctrl-Z. <
+


  " # '  
,
* +


 

 "  -
  fg  "Y#   #. $   ',   fg
+ 
# "   "
 '  , "  '  4,
 4=
  >
+
.
F
+ '    >
+
  "
 '  -

# "#Y  ' . <  "

'  


 

87
 
* 
  "
"#
= 
 . W '  


 "
  % 
. <  "

'  
' 


  ]
 
   #4
, %  +

#



  « ». ! , '  
 >
+
-
"
,  "#'
 #  
Y
 
 

, -

, '+, 
4 "#' 
"4,    \ 
 +
 -
 #   * *  .
  ,  4=
 >
+
, *  # #

  
  \ . W 
 
Y #  #    * ' -
  . ! 
,
" 
  
/home/larry# yes &
(    
"

 "
    /dev/null), 
 \  
 # 

  " y. W 

'+ 
  #, "#   " Y Ctrl-C 

'

  '    >
+
. K  # \ -
 ,    "#' #   fg, ' 
 +# '  
-
 
 " Y Ctrl-C.

"

=
 ' 
 
.    fg    bg
'
4  
'  ,  
"  "
 "

(\ '    

 " «+»   
 '  ,

" 
   jobs). C"     +

 
  "

"# '  , '   + 
= # 

 "  " 
>
+, '      

*
   fg "  -
 bg % 
>  
(job ID). ! 
,  
/home/larry# fg %2

=
 '  

2 

 " ,  
/home/larry# bg %3

=
 '  

3  >
+. & "#' # PID   
-

*
   fg  bg 
"#'. "

*, "

 '  -
 

 "  +   ' #
* 
. $ ,  
/home/larry# %2

 \ "
  

/home/larry# fg %2
 + #,  >   "
 '  
  "
+
"
.   fg, bg  jobs "4 
    -
".

88
4.1.4. %=HF %VJ&:?%++:>: <H:;%*+< <  Unix
< 
Y
 '   % '  
  % ' 
-
 
 '"% 
% ',
 "
% , +


 #  "#' 
"
4=% 
% ':
x ' 
, ' 
Y


 , "

>    -


: fork, exit, getpid, getppid  . .;
x % '  
: signal, kill, sleep, alarm, wait, paus),
semop, semctl, semcreate  . .;
x ' 
>  *   " , '
"
 , 
-

 =
    : pipe, mkfifo, read, write, msgget, shmget,
msgctl, shmctl  . .
F
% ' 
+ 
* ' 
 (Inter-Process Commu-
nication Facilities – IPC) "4

x 
 , 

 4=
'+# % '  -

  
 
  "#'

  , – 
(semaphores);
x 
 , 

 4=
'+# " 

=
  * '"# 
, – 

 =

(message queries);
x 
 , 

 4=
'+#  " =
 "

 , – 
*
 '
"
  (shared memory
segments);
x 
 , 

 4=
'+# «=
» 
,
  
%,    
, 

'   "   " (pipes).
! "

= 
 IPC "
 "4, %   =
-

  "
 '  4= ]
 
+ 
* ' -

,   
"# 
 . ' 
 "4
]
 +
 # 

#4 =
,    
 >  " 
*-

 '
"
 . "4 

  key_t,    * ' -


 
"'   

"
  > "
<sys/types.h>. "4 -
"#'
 " '  ]
 
+ 
* ' 
 "
"
   =
4=
 ]
.
%':&F
"   
 >  
+ 4   
% ' :
x semget – " '   "
      
 > ;
x semop – "   "   ' 
 
 >  (
-
 ',   '"
 
  % ' #  

 "#'  
 > );
x semctl – "  "
 ' '%  "4=% 
-
      
 > .

89
<  

"
% 
% '     > " %
#inlude <sys/ipc.h>
#inlude <sys/sem.h>

 ' semget 

  
int semid = semget (key_t key, int count, int flag)

   * "
 "4 "  "#
 
*

(key),   
 >    "
"#
>" * (flag), 

"


<sys/ipc.h>, " 
 >    

 >  (count), "  4=%
  
 +
"4. 
 ' ' =
 
> 
  
 >  semid. V
#  * 
 > 

"
 +-

#4  , .
. ]
 
 > 
 +
 *  "# -
* , * '


' * '  "
* 
"#  -
". <"
' semget  "# 
 > 
>  


>     
 >   
 
 >  \  -

. G" * 
* ' semget 

 +
  "
.
$ " 4.1
}! !  semge
G" *   

' semget '


  
 > "  *
"4 . C" >" * IPC_CREAT 
'  ,   
-
IPC_CREAT  >    '  "4 +
=

, 
 = 4= 
 " 
> 
=
4=
*   
 > 
G" * IPC_EXLC 

 >" * IPC_CREAT
-
 ' 
 " '  ( "# " ' )  -
IPC_EXLC  
 > . C"   
 >  +
=
-

, semget '  -1, 
 


 
errno 
 
+ # ' 

EEXIST
F" Y
9  >" * '  4       
 > 
( ". 4.2).
$ " 4.2
 *     ! %  IPC
    

S_IRUSR " 
"
– 


S_IWUSR " 
"
– ' #
S_IRGRP ?  – 


S_IWGRP ?  – ' #


S_IROTH < 
– 


S_IWOTH < 


– ' #

90
$   ', >" * '  
 > +  ' #  :
int flag = S_IRUSR | S_IWUSR | S_IRGRP | IPC_CREAT;

 ' semctl 

 >  
int semctl (int semid, int sem_num, int command, union semun arg)
*
semid – \ 
>    
 > ; sem_num – 

-
 >  * 
; command –  
; arg –  ' 
"#   -
, 
+
  

 
 - ',  ' 
 
.
]




 
union semun
{
int val; /*         SETVAL */
struct semid_ds *buf;
/*     IPC_STAT  IPC_SET */
unsigned short *array; /*     SETALL  GETALL */
};
]


semun 
* "+
 #



"
  *" "#

  * .   semid_ds *" "
4=  ':
struct semid_ds {
struct ipc_perm sem_perm; /*   % */
struct sem *sem_base; /*        */
ushort sem_nsems; /*     */
time_t sem_otime; /*     
 semop() */
time_t sem_ctime; /*       IPC_SET */
};
' semctl '"
:
x +#   
 >  "  "# 
 > 
 '  * 
(IPCRMID);
x 
# ' 


"#* 
 > (GETVAL) " 
% 
-
 >  (GETALL);
x  # ' 


"#* 
 > (SETVAL) " 
%

 >  (SETALL);
x 
# " 
 >    

 >  (GETPID).

91
 
 ' "   "   
 > 
"

int semop (int semid, struct sembuf *op_array, int count)
*
semid – \ 

"
 
   *   
 > ;
op_array –     sembuf
struct sembuf {
short sem_num; /*  : 0,1,2..n */
short sem_op; /* %   */
short sem_flg; /*   %: 0, IPC_NOWAIT, SEM_UNDO */
};


"
%  > "
<sys/sem.h>  
+ =%    

  
 >  *  , a count – "
 \"

   .  -


, ' =


 ', "
 ' 

 "
-

*   * 
 > .
C"  ' 
  
op_array 

 >  
%
'

" =
* '
  
 > ,  
 ' -
"
 
"# 

 ' 


 > (
" \ '+)  -

  ' 

 " «
». '+   " :
1.   
"#
' 

sem_op.
2. <"+
"#
' 

sem_op.
3. !"

' 

sem_op.
C" ' 

" 
 sem_op   
"# 
* "4-

' 


#Y
"  ' 
4 
 > semval,   
 "
 \   
"#
' 

 ' 
4 
 > . C" 

'"# 
' 


 >  " "
,    ' 
 


, + 4=
"
* ' 
 \* 
 > . C" +
' -


" 
 sem_op  "4 
"
"#Y

 >
semval,    
"
 
  " 
, + 4=%

"
 ' 
 
 >   "
 
= 
    -
"
 \* .
C" ' 

" 
 sem_op "+
"#,   -
 "
  ' 
4 
 > semval, 

, + 4=


"
 ' 
 
 > , ' 4 ( + 4  
-
"* Unix).
C" ' 

" 
 sem_op  "4  ' 


 -
> semval  +
 "4, 
 "
4= \"

  
op_array. C" +
' 


 > semval "  ",   

"
 
  " 
, + 4=% "
* ' 
-
 
 > ,  Y 


  
+ -

92
. <   "#'  >" * IPCNOWAIT    Unix 
" 


= 
, "Y# =
  
% 
 %  '-

  , 
Y
  "  4 
  
>" * IPCNOWAIT.
%:<F%  %%:<F% /CF (J*JF)



 

 Unix 
* 
+ 4 
  "
%   ":
x 

 
  ";
x 
 
  " FIFO.
!

 
  " – \   
  >  IPC  Unix (1973),
*"  
   % "
 

, "



*  *  "#' # " ' 
 "# 


 .  Unix System 
#

   (1982) "  "
  -
 " FIFO,  
 ' 4 
    " . 
 
FIFO Y> 
   «first in, first out» – «
 Y
",

Y
"», 
# \   "  4   

. &
 
  "
 Unix >  4  

  – '"4

  #
 
"#    .    "
 * %   -
"  +   " FIFO   "
 "
  > " 
-

,  '"
  

 
   #   
 +
FIFO.    
    " ,   

   -
* '
  =#4 % >  read  write.
FIFO '
 ' mkfifo:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
/* Y 0   
 , -1   */

# pathname – 
" Unix "
 > " ,  


 

 FIFO.
 *
 mode  '
 4   '
Y
  
> " ( ". 4.2),  "*   *
   open.
G  mkfifo 

   open, '    *

mode = O_CREAT | O_EXCL. W ' 
,  '
    "
FIFO " ' =
 Y EEXIST  "
,
"   "  '  
" 

 +
=

. C" 


 '  #   -
 ", ' 
open 
 mkfif. "   =
4=
*   -
" " '  *,   "
,
"
*
=

=

, -
'
mkfifo, 
#
, 
' =
 " Y EEXIST, 
"  

", '
> 4 open.

93
  mkfifo  +
'
   " FIFO. C4 + "#' #
 
 % 

  " '    .
V
#   " 

"
 +
#4 
, .
.   "

 =
 #  
%  ,   

 
"# '  
" 
 
  * 
  4=
*    ".
<"
'    " FIFO "+
 #    

" ' -
#  =#4 " >  open, "  '   % >-
   > " ' "
  - ( 
, fopen).
FIFO +
 #   " "#  

, " "#  ' #.
!
"#'   #   "  

 ' #, "# 
 
 -
 " * # "#   ( . 4.1).

. 4.1. )  J    FIFO


<  '   *    " "   " FIFO ' write
 

*  "4  +

4=, ' read 

 
, 
=

 *    " " FIFO
. <  -
'
>  lseek " * *   " " FIFO 
 ' =
-
 Y ESPIPE.
!

 
  " ' 4 ' pipe() 
 "4
'+# "#  "
 ( 
)

   %:
#include <unistd.h>
int fd[2];
pipe(fd);
/* Y 0      , -1 -     ;*/
G  ' =
  > "% 
   : fd[0]  fd[l], -


   " 
,   – " ' .
~   " '
  
 ( . 4.2), 
  "#-
'
 "# \ 
,   "   "#'4 " '

+  
  ( 
"#  
) "
4=  -
': 
 '
   ", ' 
 '
 fork, '   4  4 –

 
 ( . 4.3); ' 
 
"# 
 '  
 -

94
  " 
 
  " , 
 –    ' # -

  " ( . 4.4). W 


  44

   -
% 
+ 
  ( . 4.5)

. 4.2. }      " "! 

. 4.3. }      "! 


(  fork)

. 4.4. }    *   

95
. 4.5. }    *    
%% 
<  
  
who | sort | 1


    Unix  "
 Y
  

 "
'  
% 
     "  
+  ( . 4.5).
&

   +
"4
   " 
 
 +-
*   "       ,    ' # – 
     .



Y


 
  " "  -
"
 ( ), 
# '""

  #  

"#    . <  


%

   %  
-
 + '  #    "   "#' #  + ' % "


   %    . W  '   "
*


 *   " IPC "
4=
:
x ' 4   " 1 (fd1[0]  fd1[1])  2 (fd2[0]  fd2[0]);
x ' fork;
x 
"# 
 '  
   " 
 

  " 1 (fd1[0]);
x 
"# 
 '  
   " '  

  " 2 (fd2[1]);
x 
 
 '  
   " '  
  -
" 1 (fd1[1]);
x 
 
 '  
   " 
 
  -
" 2 (fd2[0]).

4.1.5. @%&%; +::DT%*


" 


 '+ 
 =
 
+ -

  
% ' 


 
+
 "
4= 

' :

96
x msgget "  '   

 =
 " "
-
 
   =
4=
 

;
x rnsgsnd "   =
   ' 4 

# -
=
;
x rnsgrcv "   =
 ' 

 =
;
x rnsgctl "  "
   "4=% 
.
<  

"
% 
% '     > " %
#include <sys/ipc.h>
#include <sys/msg.h>
< 
 ' msgget  
  "4 (key) 

"

 "#
 

    >" * ( "#4  "* >" * 
 
 '
semget). ' msgget   " '
 4


# =
  
 ' =
 "#' 
"4 
> 
'  

, "  % \"

  "  


 =

 , 
+ =  '  "4,  ' =
 
4=

>  

:
int msgqid = msgget(key_t key, int flag)
$   ', 

# =
 " 
 +
#4  . "

=
 =
  

# "+ 
 ' msgsnd:
int rnsgsnd (int msgqid, void *rnsg, size_t size, int flaq)
*
msg – \  ' 
"#     " size, 
+ =4 

-
"
 "#' 
"

""
  =
  "#
 -=

, 
 '
"#' 
"#%  % "-

 "
4=  ': size = sizeof(msg) – sizeof(long).
  msg 
* 

 
struct rnsq {
lonq rntype; /*  Y */
char mtext[SOMEVALUE]; /* Y */
};
<"
 long 
* "+ #
   
,  "

-
* "
 #  "4  
"#' 
"#
 
,  \ "

  
 " 
 * 

   %, "#  % "
(' =4 
"'  
). < 
 flag 

"
 

 " ' Y
*   
 

 " %
'

"
 % '
 

 >
 . C" flag = 0, 
  =
  

  " 
. C"
flag = IPCNOWAIT,   
" 
    =
-
 ' =
 Y ENOMSG.

97
" 
Y   =
  

# "4:
x  "
 
  '    4 

# =
;
x 

Y

" =
, '  * 
 
%
-
*

" ;
x "+
"#
' 

 =
.
C" +
 '
,  
=


'+ >
'-
 #  
 

Y
 
%
*

"   "
=
,  %=%    

 =
 (>" *
IPCNOWAIT  \ 
),   Y 
 " -

 ( "
)  
%  ,  

# =
 
'* '

 , + 4= "
 =
, " 

# 


 "
 , " ' Y  


 

% 
 *-
 ".
" 
 =
  "#'
 
 ' msgrcv
int rnsgrcv (int msgqid, void *msg, size_t size, long rnsg_type, int flag)
 *
 rnsg_type ' 
  =
,  
+  #
' 


x
" ' 

 0,  ' =


=




, .
.  
 
=

;
x
"  "#Y
0,  ' =


=

,  -
 * 
  '  ";
x
"  
#Y
", ' =


=

  -

#Y  , ' 

 * 
#Y
, "  "4  -
' * " .
 

size    "
 '
  ,  ' =


 

"+
Y # '
 ' *  size.

 ' msgctl '"
  "# 

 =
-

int msgctl (int msgqid, int command, struct msgid_ds *msg_stat)
  "#'
:
x "      
" 

 =
 (command
= IPCSTAT)  
=

*     msgstat;
x '


*  (command = IPCSET),  
'
-

    

;
x " +
  '  

 =
 (command =
IPCRMID).

98
4.1.6. D: + &H;%C%:* JPR
"   '
"
 #4  "#'4 

':
x shmget '
  
*
 '
"
  "  %
=
4= 
*
  
 +
"4;
x shmat "4
 
*
   '    
"
    "#-
   = 4=
* 
 ;
x shmdt "4
    "#  

"4
 

 
*
   '    "# 
   " ;
x shmctl "+ "  "
 ' ' 
 ,
'   =
4= 
*
.
<  

"
% 
% '     > " %
#include <sys/ipc.h>
#include <sys/shm.h>
<"
*   
*
 '
"
  "4
   -
"#  
 , \ 
 +
  = #  
-
4= \"

     "#' 
 %  Y% -
  
  ' .

 '
int shmid = shmget (key_t key, size_t size, int flag)
   
 size 

"
 +
"
 '

*
 
  %. C"   "
'
"
   % \"

, 
-
+ = '   "4,    

  
= % -

   = 4=
* 
 ,  ' 

 
* -
' "
 
>  =
4=
* 
*
 , 
 -

 size "+
 #  \ "

 0.   "
' -

  
*
  '
 

#Y
 "
*  


 "#* '



*
 '
"
   
"#Y
 -
"
*   "#* '
. V
# ]
 '
"

 

"
 +
#4  . ' 

*
 
' 



"
* 
"
  
*  ,  \ 

-
" 
   "

* 
* ' "4


*
    "#  
 * 
 . G" * IPCCREAT
 IPCEXCL  "* 
 Y
.
<"4


*
    "#   "
 

 =
  
 ' shmat:
void *virtaddr = shmat(int shmid, void *daddr, int flags).
< 
 shmid – \ 

"
 
>  
*
 , a
daddr – +
"
 
   "# 
,   "+
 -

99

 #   " 
*
    "# .  

 
-
* ' "
 > 
   "# 
   " 
*
 .
C" ' 

 daddr "
 NULL,   
  "


  "# 
   " 
*
 . G" * 
* ' shmat


 +
  "
.
$ " 4.3
}! !  shmat
G" *   

J  "4
    "# "
SHM_RDONLY




"
,
" '+,    
SHM_RND

"
* ' 
 daddr.

" "4
 
*
    "#   "#'


 ' shmdt:
int shmdt (*daddr)
*
daddr – \   "# 
   " 
*
    "# -
, 

"
  
* ' shmat.

 ' shmctl
int shmctl (int shmid, int command, struct shmid_ds *shrn_stat)
     ' 
4  "*
 msgctl.

4.1.7. &%&F J&/@%+/:* &%CH?


%':&F
< *  semsyn, %    

 +
, '


 >   
 , % ' 4=%  =#4 ' *

 > .  * 

 
 "
 *" ,  "-

  '" 
 
 > , 
"# 
 +
 +
-
 
 > .
#include <unistd.h>
#include <stdio.h>
#include <error.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <fcntl.h>

100
#include <time.h>
#include <iostream.h>
#define MAXLINE 128
#define SVSEM_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#define SKEY 1234L //   
union semun {
int val;
struct semid_ds *buf;
ushort *array;
};
int var;
int main(int argc, char **argv) {
char filename[] = "./rezult.txt";
pid_t pid; //     %
time_t ctime; //  
int oflag, c, semid;
struct tm *ctm;
union semun arg;
struct semid_ds seminfo;
struct sembuf psmb;
unsigned short *prt = NULL;
var = 0;
oflag = SVSEM_MODE | IPC_CREAT; //   
printf("Parent: Creating semaphore...\n");
semid = semget(SKEY, 1, oflag); //   
arg.buf = &seminfo;
printf("Parent: Getting info about semaphore (not required, for exam-
ple)...\n");
semctl(semid, 0, IPC_STAT, arg); //   .  
g.buf->sem_ctime;
ctm = localtime(&ctime);
printf("%s %d %s %d %s %d %s","Parent: Creating time - ",
ctm->tm_hour,":",ctm->tm_min,":",ctm->tm_sec,"\n");
arg.val = 5;

101
printf("%s %d %s","Parent: Setting value \"",arg.val, "\" to sema-
phores...\n");
semctl(semid, 0, SETVAL, arg); //    
printf("Parent: Creating child process...\n");
if ((pid = fork()) == 0) { // child process ;
printf(" Child: Child process was created...\n");
struct sembuf csmb;
unsigned short semval;
union semun carg;
int oflag = SVSEM_MODE | IPC_EXCL;
printf(" Child: Opening semaphore...\n");
int smd = semget(SKEY, 1, oflag); // 
 
csmb.sem_num = 0;
csmb.sem_flg = 0;
csmb.sem_op = -1;
printf(" Child: Locking semaphore...\n");
semop(smd,&csmb,1); //   
printf(" Child: Do something...\n");
//  %  YY K
sleep(2);
//  %  YY K  
printf(" Child: Done something...\n");
carg.buf = NULL;
carg.array = &semval;
semctl(smd,0,GETALL,carg); //      
semval = *carg.array;
printf("%s %d %s"," Child: Semaphore value = ",semval,"\n");
csmb.sem_num = csmb.sem_flg = 0;
csmb.sem_op = -semval;
printf(" Child: Unlocking semaphore...\n");
semop(smd,&csmb,1);
printf(" Child: Terminating child process...\n");
exit(0);
}

102
printf("Parent: Waiting for unlocking semaphore...\n");
psmb.sem_num = psmb.sem_flg = psmb.sem_op = 0;
semop(semid,&psmb,1);
printf("Parent: Semaphore is unlocked...\n");
printf("Parent: Waiting for SIGCHILD...\n");
waitpid(pid,NULL,0);
printf("Parent: Deleting semaphore...\n");
semctl(semid, 0, IPC_RMID);
exit(0);
}
  

 Y
*  % "
4= -
':
semsyn
Parent: Creating semaphore...
Parent: Getting info about semaphore (not required, for example)...
Parent: Creating time - 13 : 14 : 6
Parent: Setting value " 5 " to semaphore...
Parent: Creating child process...
Child: Child process was created...
Child: Opening semaphore...
Child: Locking semaphore...
Child: Do something...
Parent: Waiting for unlocking semaphore...
Child: Done something...
Child: Semaphore value = 4
Child: Unlocking semaphore...
Parent: Semaphore is unlocked...
Parent: Waiting for SIGCHILD...
Child: Terminating child process...
Parent: Deleting semaphore...
 
  *  '
 
 >  +
#4 
ipcs –s

103
------ Semaphore Arrays --------
key semid owner perms nsems
0x000004d2 425986 root 644 1
H;%C% JP
< *  shmget '
 
*
 '
"
 ,   '
    "
 '"#* > "  " 
*
 .
#include <stdio.h>
#include <error.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <stdlib.h>
#define SVSHM_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int main(int argc, char **argv)
{
int c, id, oflag;
har *ptr;
size_tlength;
oflag = SVSHM_MODE | IPC_CREAT; //     
while ( (c = getopt(argc, argv, "e")) != -1) {
switch (c) { //      
case 'e':
oflag |= IPC_EXCL;
break;
}
}
if (optind != argc - 2)
{
printf("usage: shmget [ -e ] <path_to_file> <length>");
return 0;
}
length = atoi(argv[optind + 1]);
id = shmget(ftok(argv[optind], 0), length, oflag);

104
ptr = (char*) shmat(id, NULL, 0);
return 0;
}
' shmget '
 
*
 '
"
   ' * '-

. <"
,

 

  

*
    -
,
 '
  "4 IPC System V ' >  ftok. C"  -
'  
        

=

 
*
  

+


, '  *  ' 
Y   Y
. C" '
,
 
*
 +
=

,      
"+ #  ' -
 "
  " 
*
 .
' shmat "4
 
*
  
   -

 , "

* *  ' 
Y
 .  '  
,  '
-
"
  # System V " 
 «+
#4  »,  
*
 '
-
"
   \ 
 "
.
< *  shmrmid '
 > 4 shmctl   
IPC_RMID "  "
 
*
 '
"
  ' 
.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#include <fcntl.h>
#define SVSHM_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int main(int argc, char **argv)
{
int id;
if (argc != 2)
{
printf("usage: shmrmid <path_to_file>");
return 0;
}
id = shmget(ftok(argv[1], 0), 0, SVSHM_MODE);
shmctl(id, IPC_RMID, NULL);
return 0;
}

105
< *  shmwrite ' "
 
*
 '
"
  "
-
 
"##4 ' 
 0, 1, 2, ... , 254, 255. 
*
 '
"
 -
  
 ' shmget  "4
 ' shmat. C*
'
+
 # "
 ' shmctl    IPC_STAT.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#include <fcntl.h>
#define SVSHM_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int main(int argc, char **argv)
{
int i, id;
struct shmid_ds buff;
unsigned char *ptr;
if (argc != 2)
{
printf("usage: shmwrite <path_to_file>");
return 0;
}
id = shmget(ftok(argv[1], 0), 0, SVSHM_MODE);
ptr = (unsigned char*) shmat(id, NULL, 0);
shmctl(id, IPC_STAT, &buff);
/* 4set: ptr[0] = 0, ptr[1] = 1, etc. */
for (i = 0; i < buff.shm_segsz; i++) *ptr++ = i % 256;
return 0;
}
< *  shmread 

 "
 
"## ' 
, ' -
 4  '
"
4 # *  shmwrite.
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>

106
#include <fcntl.h>
#define SVSHM_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int main(int argc, char **argv)
{
int i, id;
struct shmid_ds buff;
unsigned char c, *ptr;
if (argc != 2)
{
printf("usage: shmread <path_to_file>");
return 0;
}
id = shmget(ftok(argv[1], 0), 0, SVSHM_MODE);
ptr = (unsigned char*) shmat(id, NULL, 0);
shmctl(id, IPC_STAT, &buff);
/* check that ptr[0] = 0, ptr[1] = 1, and so on. */
for (i = 0; i < buff.shm_segsz; i++)
if ( (c = *ptr++) != (i % 256)) printf("ptr[%d] = %d", i, c);
return 0;
}
A  
'"#  '  

% Y
*  

 '
"
 #4.   "
'
 
*
 '
"

 " 1234   . " 
>  
*
  "#'

"
  "
* > " /tmp/test1. W  


 
>  ftok:
shmget /tmp/test1 1234
ipcs -bmo
IPC status from <running system> as of Thu Jan 8 13:17:06 1998
T ID KEY MODE OWNER GROUP NATTCH SEGSZ
Shared Memory:
m 1 0x0000fl2a --rw-r--r-- rstevens otherl 0 1234
< *  ipcs ' 
 " *,  
#,  
*

'
"
  

"# " '   
"  "
  ' -

Y
 *  shmcreate.

107
   *  shmwrite, + ' "# 
+
'
-
"
  "
 
"##4 ' 
.  
  =#4 -
*  shmread 

 
+

*
 '
"
 :
shmwrite shmget
shmread shmget
shmrmid shmget
ipcs -bmo
IPC status from <running system> as of Thu Jan 8 13:17:06 1998
T ID KEY MODE OWNER GROUP NATTCH SEGSZ
Shared Memory:
 "# '
"
4 # +, ' 
shmrmid /tmp/test1
&:>&F% /CF
< *  pipes '
  
  


  -
44 '# 
+  
 

 %   ".
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream.h>
#include <strings.h>
#include <fstream.h>
#define MAXLINE 128
void server(int,int), client(int,int);
int main(int argc, char **argv) {
int pipe1[2],pipe2[2]; //  
 
pid_t childpid = 0;
printf("Parent: Creating pipes...\n");
pipe(pipe1);
pipe(pipe2);
printf("Parent: Pipes created...\n");
printf("Parent: Creating child process...\n");
if ((childpid = fork()) == 0) { // child process starts
printf("Child: Child process created...\n");

108
close(pipe1[1]);
close(pipe2[0]);
printf("Child: Starting server...\n");
server(pipe1[0], pipe2[1]);
printf("Child: Terminating process...\n");
exit(0);
}
// parent process
close(pipe1[0]);
close(pipe2[1]);
sleep(2);
printf("Parent: Starting client...\n");
client(pipe2[0],pipe1[1]);
printf("Parent: Waiting for child porecess to terminate a zombie...\n");
waitpid(childpid, NULL, 0);
printf("Parent: Zombie terminated...\n");
return 0;

}
void server(int readfd, int writefd) {
char str[MAXLINE];
strcpy(str,"some string to transmit");
ssize_t n = 0;
printf("%s %s %s","Child: Server: Tranferting string
to client - \"",str,"\"\n");
write(writefd, str, strlen(str));
sleep(1);
printf("Child: Server: Waiting for replay from client...");
while ((n = read(readfd,str,MAXLINE)) > 0)
{
str[n] = 0;
printf("%s %s %s","Received OK from client - \"",str,"\"\n");
break;

109
}

printf("Child: Server was terminated...\n");


return;
}

void client(int readfd, int writefd) {


ssize_t n = 0;
char buff[MAXLINE];
while ((n = read(readfd, buff, MAXLINE)) > 0 )
{
buff[n] = 0;
printf("%s %s %s","Client: Recieved string from server: \"",buff,"\"\n");
break;
}
printf("Parent: Client: Sending OK to server\n");
sleep(1);
strcpy(buff,"sends OK from client");
write(writefd, buff, strlen(buff));
return;
}
 "



 * ,  * '4=

+ 

' -



 
 %   ".
&:>& +%&<%&
#include <unistd.h>
#include <stdio.h>
#include <error.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream.h>
#include <strings.h>
#include <fstream.h>
#include <sys/stat.h>
#include <errno.h>

110
#include <fcntl.h>
#define MAXLINE 128
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#define FIFO1 “/tmp/fifo.1”
#define FIFO2 “/tmp/fifo.2”

int main(int argc, char **argv) {


int readfd = -1,writefd = -1;
pid_t childpid = 0;
ssize_t n;
char str[MAXLINE];
strcpy(str," some string to transmit ");
cout<<"Creating pipes..."<<endl;
unlink(FIFO1);
unlink(FIFO2);
if (mkfifo(FIFO1, FILE_MODE) == EEXIST) cout<<"\n Pipes is ex-
ists"<<endl;
if (mkfifo(FIFO2, FILE_MODE) == EEXIST) cout<<"\n Pipes is ex-
ists"<<endl;
cout<<"Pipes created..."<<endl;
writefd = open(FIFO2, O_WRONLY);
if ((writefd != -1)) {
cout<<"Transmitting the string..."<<endl;
write(writefd,str,strlen(str));
readfd = open(FIFO1, O_RDONLY);
cout<<"Waiting for respond..."<<endl;
while ((n = read(readfd,str, MAXLINE)) > 0) {
str[n] = 0;
cout<<"Received string - \""<<str<<"\""<<endl;
break;
}
close(readfd);
close(writefd);
unlink(FIFO1);

111
unlink(FIFO2);
} else cout<<"Can't open pipes..."<<endl;
return 1;
}
&:>& /C%
#include <unistd.h>
#include <stdio.h>
#include <error.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream.h>
#include <strings.h>
#include <fstream.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define MAXLINE 128
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"

int main(int argc, char **argv) {


int readfd = -1,writefd = -1;
pid_t childpid = 0;
ssize_t n = 0;
char str[MAXLINE];
ofstream fsw("./rezult.txt");
fsw<<"Opening pipes..."<<endl;
while (1)
{
readfd = open(FIFO2, O_RDONLY, 0);
if (readfd != -1) {
fsw<<"Pipes opened..."<<endl;

112
fsw<<"Waiting for respond..."<<endl;
while ((n = read(readfd,str, MAXLINE)) > 0) {
str[n] = 0;
fsw<<"Received string - \""<<str<<"\""<<endl;
break;
}
strcpy(str,"Ok from other process");
writefd = open(FIFO1, O_WRONLY, 0);
fsw<<"Transmitting the string - \""<<str<<"\""<<endl;
write(writefd,str,strlen(str));
close(readfd);
close(writefd);
break;
}
sleep(1);
}
fsw.close();
return 1;
}
A  
'"#  '  

% Y
* , -
"#'4=% 

 
  ".
pipes
Parent: Creating pipes...
Parent: Pipes created...
Parent: Creating child process...
Child: Child process created...
Child: Starting server...
Child: Server: Tranferting string to client - " some string to transmit "
Child: Server: Waiting for replay from client...Received OK from client - "
sends OK from client "
Child: Server was terminated...
Child: Terminating process...
Parent: Creating pipes...
Parent: Pipes created...

113
Parent: Creating child process...
Parent: Starting client...
Client: Recieved string from server: " some string to transmit "
Parent: Client: Sending OK to server
Parent: Waiting for child porecess to terminate a zombie...
Parent: Zombie terminated...
< * , ' 
4=


'   " FIFO, '  4
"
4=  ':
client &
Opening pipes...
Pipes opened...
Waiting for respond...
Received string - " some string to transmit "
Transmitting the string - "Ok from other process"
server
Creating pipes...
Pipes created...
Transmitting the string...
Waiting for respond...
Received string - "Ok from other process"
[1]+ Exit 1 ./pn (wd: ~/makegnu/ipc/pipe_name/2/bin)
(wd now: ~/makegnu/ipc/pipe_name/1/bin)
@%&%; +::DT%*
< *  msgcreate '
 

# =
. < 
 -
     '"
  ' # >" * IPC_EXCL. <"
 > " ,
"4=

 ' 
"# *
    ,



>  ftok. <"
 "4
 '
  
>  >-

 msgget.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <error.h>
#include <unistd.h>

114
#include <fcntl.h>
#define SVMSG_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int main(int argc, char **argv)
{
int c, oflag, mqid;
oflag = SVMSG_MODE | IPC_CREAT;
while ( (c = getopt(argc, argv, "e")) != -1) {
switch (c) {
case 'e':
oflag |= IPC_EXCL;
break;
}
}
if (optind != argc - 1)
{
printf("usage: msgcreate [ -e ] <path_to_file>");
return 0;
}
mqid = msgget(ftok(argv[optind], 0), oflag);
return 0;
}
< *  msgsnd 
=
  

#  =

'  
"   .  * 
'
  ' 
"#     msgbuf =
-
*  , ' 
 
 ' call 
"
 
 
"#4  -
  (>
' ) 
4=
* '
.
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <error.h>
#include <fcntl.h>
#define MSG_W (S_IWUSR)
int main(int argc, char **argv)

115
{
int mqid;
size_t len;
long type;
struct msgbuf *ptr;
if (argc != 4)
{
printf("usage: msgsnd <path_to_file><#bytes><type>");
return 0;
}
len = atoi(argv[2]);
type = atoi(argv[3]);
mqid = msgget(ftok(argv[1], 0), MSG_W);
ptr = (msgbuf*) calloc(sizeof(long) + len, sizeof(char));
ptr->mtype = type;
msgsnd(mqid, ptr, len, 0);
return 0;
}
< *  msgrcv 
 =

' 

.   
 
+
 #  '  
 n, "4 4= " , -

 t +
 #  "#'  "  '   =
  >-
 msgrcv.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#define MSG_R (S_IRUSR | S_IRGRP | S_IROTH)
#define MAXMSG (8192 + sizeof(long))
int main(int argc, char **argv)
{
int c, flag, mqid;
long type;

116
ssize_t n;
struct msgbuf *buff;
type = flag = 0;
while ( (c = getopt(argc, argv, "nt:")) != -1) {
switch (c) {
case 'n':
flag |= IPC_NOWAIT;
break;
case 't':
type = atol(optarg);
break;
}
}
if (optind != argc - 1)
{
printf("usage: msgrcv [ -n ][ -t type ]<path_to_file>");
return 0;
}
mqid = msgget(ftok(argv[optind], 0), MSG_R);
buff = (msgbuf*) malloc(MAXMSG);
n = msgrcv(mqid, buff, MAXMSG, type, flag);
printf("read %d bytes, type = %ld\n", n, buff->mtype);
return 0;
}
< *  msgctl  "
 

#.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <fcntl.h>
#include <error.h>
int main(int argc, char **argv)
{
int mqid;

117
if (argc != 2)
{
printf("usage: msgrmid <path_to_file>");
return 0;
}
mqid = msgget(ftok(argv[1], 0), 0);
msgctl(mqid, IPC_RMID, NULL);
return 0;
}
A
'"#  '  

% Y
*  " "   

=
  

:
msgcreate /tmp/no/such/file
ftok error for pathname "tmp/no/such/file" and id 0: No such file or directory
touch /tmp/testl
msgcreate /tmp/testl
msgsnd /tmp/testl 1 100
msgsnd /tmp/testl 2 200
msgsnd /tmp/testl 3 300
ipcs -qo
IPC status from <running system> as of Sat Jan 10 11:25:45 1998
T ID KEY MODE OWNER GROUP CBYTES QNUM
Message Queues:
q 100 GGGGG‘I --rw-r–– rstevens otherl 6 3
  " %   '  

  =#4 
 
-
=
4=
* > " . < 
 '
,  > ",  '
   -


*
 >  ftok, ' 
"# "+
 =
 #.  -

 '
 > " /tmp/testl   "#'

*   '  

-
 =
. <"
\*  

# 
= 4   =
 "
1, 2  3    ' 
  100, 200  300. < *  ipcs  '-

,   

  % 3 =
 = ]
 6  .
$

# c =#4 *
 type  '
msgrcv  4
=
  '"#  
:
msgrcv -t 200 /tmp/testl
read 2 bytes, type – 200
msgrcv -t -300 /tmp/testl

118
read 1 bytes, type = 100
msgrcv /tmp/testl
read 3 bytes, type = 300
msgrcv -n /tmp/testl
msgrcv error: No message of desired type

 

' Y
 =

   200,  -
 

– =

  
#Y ' 

  , 

-
Y 4= 300,  
#
 –

=

 

. <"
 ' -
 msgrcv ""4  
 

>" * IPC_NOWAIT.
 "# 

# +, ' 
msgrmid /tmp/test1

4.2.  
$$
!  !
1. ' #  


  
 ".
2.  # 
"# '   ( 
,   
> " less), ' = #   4    
 " Y
Ctrl-Z  '# 

  ps, jobs, fg, bg, kill, killall.
3. 

# % ' 4 
 

   % 
+-
   

% "+
 «"
»  «

», '  

 (  "
% > " ) – 
 «"
» (
  "-

 > ")  
 «

» (   "
 > ").
 =#4 
% ' 
+ 
* ' 
 

#


  >    «"
 »  «

»    .   




 
 >   +  "#' #:  
, -

 "   ;  
, 

' > " ;  
, *

 
-

"   '  . .
4. 
   
+ 
  «"
»-«

» =
-
# "
4=  ':
x   "#' 
 * %   " (
 % "


 %,   ' 4
  
");
x   "#' 
 (   ' 4
  
") * '

-
"
%   :
€ '
"
  # (' 
"# % '  
,
 
 =#4 
 > );
€ 

# =
.

119
4.3. 
 
   4

 "+
 
+ # "
4=
'
":
1. $"# ", > "
 *"  
+
  ' .
2. D
".
3.   
.
4. &%

 ' % *   
, ""4 -
4= %  "
 .
5. .

 #



«


»
 ' 
 "

,  4=%   "
4 230100 «&>    -
"
"#  
% ». F 
 " 
 '+# "# -


   
  ' '
  Unix – Linux


% 
 % 
 '   % 


  -

 "   "
 
  "4=
   % 4-% " -
  % .

120
   !
1. A 
 .F. 
  
 UNIX. – <.: ~-
 -<

 *, 1999. – 528 .
2. 
 . 


 UNIX. – F.: « », 1995. – 600 .
3. 
 . UNIX: ' 


. – <.: <
, 2003. –
576 .
4. 
 .A., G

., A>> W.F. Unix: '  

%
"+
. – 3-
'. – <.: <
, 2007. – 1039 .
5. Free Software Foundation. GNU make Manual [W"
 
 ]. –
A
+  : http://www.gnu.org/software/make/manual/. –
12.03.2010.
6. Red Hat, Inc. Red Hat Linux 9, Red Hat Linux Customization Guide
[W"
 
 ]. – A
+  : https://www.redhat.com/
docs/manuals/linux/RHL-9-Manual/custom-guide/. – 01.03.2010.
7. 
 ""
"#* *    "  Linux [W"
-
 
 ]. – A
+  : http://www.opennet.ru/docs/
RUS/linux_parallel/. – 01.03.2010.
8. < *   
" Linux. <. [W"
 
 ]. – A
-
+  : http://www.citforum.ru/programming/unix/threads/. –
01.03.2010.
9.   ?.. 
   «
  
 Unix» [W"
-
 
 ]. – A
+  : http://www.intuit.ru/department
/os/osunix/. – 01.03.2010.
10.   ?.., F " .. 
   «
  -

 Linux» [W"
 
 ]. – A
+  :
http://www.intuit.ru/department/os/linux/. – 01.03.2010.
11. ">
.?. 





: 


/
.?. ">
, !.. ">
. – <.: <
, 2003. – 538 .
12. ? 

 .. 


 : 
 / .. ? 

. – 2-
'. –
<.: <
, 2004. – 416 .

121
" $$

 0 $/$$"


5

5/$$6 $/$$"

    
+
 
,- . 

" ($

"%6 0
 !  ! 
,
 #.$.  

0 %.#. &




/(!7* 0 '.#. &


 , %.#. 
5$6 #0$ (.'. )


.($ 0 ("$ 08.04.2010. / 60184/16. /' «'"0».


."! XEROX. . (". . 7,09. ".-$. . 6,42.
0 653-10. $# 100 C0.
D$!%6 $!0$6
/0$6 ($1$"0$6 $$
$/ /#/ 0"
/0' ($1$"0' $$ $)$D$
NATIONAL QUALITY ASSURANCE (  ISO 9001:2008

. 634050, '. /0, (. +$, 30


./)0 8(3822)56-35-35, www.tpu.ru

122

Вам также может понравиться