:


        THE DESIGN OF THE UNIX OPERATING SYSTEM by Maurice J. Bach

                (   ...  ..)


        Copyright c 1986   Bell Telephone Laboratories.

          Prentice-Hall.
         Simon & Schuster
         , - 07632

              Prentice Hall.
          . 

                   
     ,                    
     Prentice-Hall International.     
      ,   .

         UNIX -     AT&T.
         DEC, PDP  VAX -     Digital Equipment
     Corp. Series   32000   -          National
     Semiconductor Corp.  Ada -    -
       (Ada Joint Program Office). UNIVAC -  -
       Sperry Corp.
                  APS-5
      AUTOLOGIC      TROFF    
      UNIX  AT&T 3B20.

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

           .    
          .

             .

         10 9 8 7 6 5

         ISBN 0-13-201757-1  025

         Prentice-Hall International (UK) Limited, 
         Prentice-Hall of Australia Pty. Limited, 
         Prentice-Hall Canada Inc., 
         Prentice-Hall Hispanoamericana, S.A., 
         Prentice-Hall of India Private Limited, -
         Prentice-Hall of Japan, Inc., 
         Prentice-Hall of Southeast Asia Pte. Ltd., 
         Editora Prentice-Hall do Brasil, Ltda., --
         Prentice-Hall, Inc.,  , -




        UNIX    1974      
    "Communications of the ACM" [Thompson  74].    
           -
   ,         -
.      ,   -
      .
                ;
     "Bell System Technical  Journal"    1978
  [BSTJ 78]   1984  [BSTJ 84].     -
 ,     , -
 ,     Shell;     ,
  "The UNIX Programming Environment" [Kernighan 84]  "Advanced UNIX
Programming"  [Rochkind  85],     . 
      ,   -
   (.. ""),      -
  .  ,       -
  . -,       
    " "    -
  ,         .    
          -
  ,        . -, 
           -
,          
         ,  
UNIX,  ,        .  ,
,      UNIX,     -
           
       .
               
,              Bell
Laboratories,      AT&T,  1983  1984 . -
  ,              
  ,  ,     -
,       .  -
          ,    
        .  
,          -
    ;      
  ,    ,     -
  .      ,    
 ,        -
;     ,  
  .     -
     .    -
           
.            
   ,    
 .          V;
    ,  ,   V, 
       .
      ,     ,
    ,     . -
 ,   ,   
  .        , -
     ,      
    .  ,  ,    

                                     3

 ,       .
    .
            UNIX
 V  2,     AT&T,  
    3.   ,    -
  ,          -
  ,    ,    
"Berkeley  Software  Distribution" (BSD).    ,  
   ,      -
         
      .  
  , ,  ,    , -
           ,
       .   ,  
           
,    -.
             
          . -
        UNIX   -
     [Kernighan 78].   ,  -
       ,    
     .         
     ,   
 ,      ,    -
      .
           .  1  ,
 ,           
     .   2   -
        .   -
     ,     -
         .  
   :  ,    -
,   .     ,  
        . ,  3 -
       -
   (),    .  4 
   ,   .  
  ,    3,    -
  "",        .
 5       ,  
     ;     -
     4.
        6   .   -
          
    ,     .  ,
    ,    -
 .   7    , 
  .  8   , -
 9 -  ,      .
      10     ,  -
  ,      -
 .   ,       -
 ,          -
 ,      ,    
  .       , -
   ,     .  11 -
     ,     ,
     V,  ,    BSD. 
12       UNIX,

                                     4

  13     .
    ,     ,      
      " "    ,
            -
   .
             .   
     ,     -
 ,    .   -
      ,    -
,    ,  .  ,    
         -
 UNIX.  ,   ,    -
  AT&T   ,  
  ,       -
.
               
          , -
    .       -
   ,      , 
         .   -
             .  -
      ,        -
    .        
,      . 
           -
,          -
.     ,  ,  ,  , 
,  ,  ,  ,  ,   ,
  ,    ,  ,  ,  , 
,  ,  ,  ,  ,     -
.       .      
       ,    
  ,   ,    ,
   ,   AT&T  Bell  Laboratories.
      Prentice-Hall   
      .   ,    
         , ,   
,       .






















                                     5


     1




      ,          1969 ,  UNIX
          
  ,     ,   
   .     .   
     , ,    
UNIX  ;     ,   
 ,   ,  ,  -
       .   -
      ,    
.         -
.       UNIX    V,
    AT&T,   -
      .   -
   ,        
       
.
          UNIX.     -
         .    
        .




      1965    Bell Telephone Laboratories,    
 General Electric   MAC    
,         , 
 Multics [Organick 72].   Multics   -
 -             
,        
         -
  .  ,     
   UNIX,        Multics  
  Bell Laboratories.     Multics   
 1969    GE 645,        -
 ,     ,     ,
        .    Bell Laboratories
    .
         Multics    
    Bell Laboratories   " -
    " [Ritchie 84a].  -
  ,  ,       
      ,      
    UNIX.    , -
         
,            
GE  645.                 "Space
Travel" (" ")   GECOS  (Honeywell  635),  
    ,   "-
  "  ,  ,    -
  .       PDP-7,
           .
  " "  PDP-7,   -
  ,       -

                                     6

  -        
GECOS       PDP-7.  ,  
 ,      PDP-7     ,
       UNIX,   -
    .   ,       
      GECOS    
       .    
UNIX,    Multics        -
     .
      ,      UNIX   -
,            ,   
   . ,  ,    -
                    Bell
Laboratories,  1971   UNIX     PDP-11. -
   : 16   , 8    
,    512      
    64 .        -
      ,     
 (B),     BCPL [Richards 69].   -
    ,   , -
      ,    (C)  
      ,     
 .  1973        ,    
,      ,      -
 .    Bell Laboratories,    
   ,   25,     
    UNIX  .
        AT&T         
      ,    -
   1956 ,    UNIX   -
,          .   ,
 AT&T  ,         .
  ,    .  1974   
  ,   UNIX,   Communications
of the ACM [Thompson 74],        -
.    1977   ,     UNIX,
  500,   125        .  
UNIX     ,  -
      ,      -
        (      MERT
[Lycklama 78a]).  ,    UNIX  -
    .    1977   Interactive Systems
  ,        UNIX  
      (*),    -
         -
 . 1977      ""  
UNIX  ,   PDP (     
           ),   
Interdata 8/32.
            
  UNIX    ,       
    ,       

---------------------------
(*) ,            -
       ,     -
     ,    ,   -
         .        
     ,   ,        
      .
                                     7

       .    1977  1982
  Bell Laboratories   ,  
  AT&T,  ,    UNIX  III.
     Bell Laboratories    III  
,    UNIX  V (**),       
      AT&T    1983 .   
         
 UNIX,   BSD 4.3    VAX  
 ,  .     
      UNIX  V,    
      BSD.
      1984   UNIX      
  100000     ,        -
  -       -    
.          
  .     UNIX   -
:

*      ,     ,
  ,      .    ,  
  ,        20-40 %    
         ,   -
             (.
  [Ritchie 78b], . 1965).
*     ,   
       .
*   ,       
   .
*    ,     -
    .
*     ,    -
   ,      .
*  ,     -
  .
*   , ;  
      .
*     ,      -
     ,     
  .
            UNIX  
      .
             ,  
UNIX        ,   , , ,
, ,   .  UNIX      -
,      ,  -
    ,    -
       ,   UNIX.




      1.1      UNIX. -
 ,    ,  , -
           
1.5.           

---------------------------
(**)      IV ?     
      " V".
                                     8

(***),         -
 .       ,    
   ,   

            +---------+----------------------------------+
            |         |         |
            |    +----+-------+--------+-------+----+    |
            |    | | nroff |   sh   |  who  |    |    |
            |    |    +-------+--------+-------+ a. |    |
            |    +----+                    |out |    |
            |    |    |    +--------------+    |    |    |
            |    |co|    |              |    +----+    |
            |    |    |    |   |    |    |    |
            | cc +----+    |              |    |date|    |
            |    |    |    |      |    |    |    |
            |    | as |    |              |    +----+    |
            |    |    |    +--------------+    |    |    |
            |    +----+                        |    |    |
            |    |    +-------+--------+-------+ wc |    |
            |    | ld |  vi   |   ed   |  grep |    |    |
            |    +----+-------+--------+-------+----+    |
            |         |         |
            +---------+----------------------------------+

                 1.1.   UNIX


.      ,    
     UNIX  ,     -
 ,            
  .  , ,   
  ,        -
  ,    .
    ,    shell   (ed  vi) 
       ,     
       . -
           ,
   ,      -
  .   ,   ,  -
     ,      -
      ,     -
 a.out,     ,  
    .       -
,   ,     . ,  
    , cc,     :  
  ,      (),  ..  
  .      -
  ,     -
    ,  .   ,  -

---------------------------
(***)     UNIX   -
              , ,   ,
               -
        .      
              
         UNIX.    
        MERT [Lycklama 78a].       -
               IBM 370 [Felton 84]  UNIVAC
      1100 [Bodenstab 84].
                                     9

,    UNIX,       ,
    ,  .
          ,   
,     shell, , SCCS ( -
   )      ,
    " UNIX".    -
              
    .   V  64   -
   ,      
.      ,    , -
     .     -
        -
  "" ,        UNIX  
            -
  .  ,   ,    
       UNIX,    -
   .      " UNIX",
""  "",        -
 UNIX,      .




               UNIX,  
   ,      -
  (, ).       
      .




      UNIX :
    *  ,
    *    ,
    *     ,
    *   ,
    *    ,
    *    (     -
      )  .
               ,
   (: "/");     
  ,  ,   ,  -

                               /
    +-------------+------------+---------+---------+---------+
    |             |            |         |         |         |
   fsl           bin          etc       usr       unix      dev
  +-+-+       +---+---+        |       +-+-+               +-+-+
  |   |       |   |   |        |       |   |               |   |
 mjb maury   sh  date who    passwd   src bin          tty00 tty01
                                       |
                                       |
                                      cmd
                                   +---+---+
                                   |       |
                                 date.c  who.c

     1.2.     


                                     10


,    ,   ,  -
 ,   .     
,        
 .       ,    
   (/);  
   ,    (), 
     ( ),    -
.              
    (),       
        ,  -
   . ,    "/etc/passwd",  "/bin/who"  
"/usr/src/cmd/who.c"    ,   , -
   1.2,   "/bin/passwd"  "/usr/ src/date.c" 
 .        , 
        -
 ,      "  "      
.  ,  ,         "/dev",  
"tty01"  ,       "/dev
/tty01".
    ,     UNIX,   
   ,         
,           .
        ,    
           
   . ,  ,  -
      ,      
     ,     
. ,     troff      
           ,   
  acctcom         .  
          -
         ,     
       .     ,
   ,    .
           ;  
    ,       -
        ,   -
  ,   ls (         ),
  .
            -
 ,   .    , 
       ,    
  :   ,      
.    ,     .
          
.
       UNIX   ,      
 . ,      ,
     .    -
     ,      ; -
           -
            . 
       :  -
       ().   
    ,     ,    -
          , -
           
.

                                     11

    ,      ( 1.3),   -
     . ,   
   copy.     
 :

     copy oldfile newfile

 oldfile -   ,  newfile -     .
     main,   argc  -
    argv,       argv  
,   .    argc  -
 3,  argv[0]    "copy" (   -
     ), argv[1] -   "oldfile", 
argv[2] -   "newfile".    ,  
       .   , -
    open  ()     oldfile   "read-only"
(  ),        -
  creat  ()    newfile.     
   0666 (  ),   
         .    
          -1;   
  open  creat,        
  exit  ()      ,  1, 
      .
     open  creat   ,  
        .   
   copy,     read (-
),           -
 ,   write ()         .
  read       (0 - 
  ).  ,      -
      read (   -
      write).    -
 copy         exit
   0   ,     -
  .

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

    copy copy.c newcopy.c
    copy copy newcopy

  .       . -
,  :

    copy . dircontents

   ,   ".",  -
  "dircontents";     ,   -
 ,   ,      (  -
      mknod). ,   
   . , ,   -
:


                                     12

    +------------------------------------------------------------+
    | #include                                          |
    | char buffer[2048];                                         |
    | int version = 1;        /*     2 */    |
    |                                                            |
    | main(argc,argv)                                            |
    |      int argc;                                             |
    |      char *argv[];                                         |
    | {                                                          |
    |      int fdold,fdnew;                                      |
    |                                                            |
    |      if (argc != 3)                                        |
    |      {                                                     |
    |             printf("need 2 arguments for copy program\n);  |
    |             exit(1);                                       |
    |      }                                                     |
    |      fdold = open(argv[1],O_RDONLY);  /*    |
    |                                               |
    |                                           */         |
    |      if (fdold == -1)                                      |
    |      {                                                     |
    |             printf("cannot open file %s\n",argv[1]);       |
    |             exit(1);                                       |
    |      }                                                     |
    |      fdnew = creat(argv[2],0666);  /*     |
    |                                          |
    |                                          -|
    |                                        */         |
    |      if (fdnew == -1)                                      |
    |      {                                                     |
    |             printf("cannot create file %s\n",argv[2]);     |
    |             exit(1);                                       |
    |      }                                                     |
    |      copy(fdold,fdnew);                                    |
    |      exit(0);                                              |
    | }                                                          |
    |                                                            |
    | copy(old,new)                                              |
    |      int old,new;                                          |
    | {                                                          |
    |      int count;                                            |
    |                                                            |
    |      while ((count = read(old,buffer,sizeof(buffer))) > 0) |
    |             write(new,buffer,count);                       |
    | }                                                          |
    +------------------------------------------------------------+

               1.3.   


    copy /dev/tty terminalread

 ,    ( /dev/tty   
),        terminalread,    
 ,   
.    :

    copy /dev/tty /dev/tty


                                     13

          .


    1.3.2

, - . - UNIX ( - ), , ( copy) . - , - , - . , . , , , 1.4, fork, . , , fork, 0, execl, copy ( 1.3). execl "copy", - , , . execl , ( 7). , , fork ( ), , wait, , copy, ( , , ). , run, : +------------------------------------------------------------+ | main(argc,argv) | | int argc; | | char *argv[]; | | { | | /* 2 : */| | if (fork() == 0) | | execl("copy","copy",argv[1],argv[2],0); | | wait((int *)0) | | printf("copy done\n"); | | } | +------------------------------------------------------------+ 1.4. , - run oldfile newfile "oldfile" "newfile" . "copy", , : fork, exec, wait exit. , , - , UNIX , - 14 "" . , - , UNIX . - shell, . Shell : , fork ( ) exec ( ), , . Shell . -, - , - (, ). -, , - , shell'. , shell ( - ). shell - ; shell - (for-in-do-done while-do-done), (if-then-else-fi), , (cd) . shell' . , - , , . shell - , , shell. Shell , - , . , , - , . - , , , - . , who , /bin/who (****) , - . who , shell . who & who shell - . UNIX . , - , . shell' cd ( ) . cd /usr/src/uts "/usr/src/uts". cd ../.. --------------------------- (****) "/bin" - , shell. 15 , "" ( ): ".." , . shell - , - . , ( , ), V , , - . shell , shell'. . , . , . shell' - - , , shell. shell' ( shell' . [Bourne 78]).

    1.3.3

, UNIX , - , - . - , shell, -. , : - , . , , , - "". , ls ( ) - , ls > output "output" - , creat. , mail mjb < letter ( open) "letter" "mjb". , , , , : 16 nroff -mm < doc1 > doc1.out 2> errors nroff doc1, - doc1.out errors ("2>" , 2, ). ls, mail nroff , , ; - shell "<", ">" "2>" , - . , , - , - . , - . , , . , - . , , , ; , - , . - - . , - , - . , grep (- ) : grep main a.c b.c c.c "main" - , a.c, b.c c.c , . - : a.c: main(argc,argv) c.c: /* here is the main loop in the program */ c.c: main() wc -l . grep main a.c b.c c.c | wc -l , - "main"; grep wc. : 3 .

    1.4

1.1 - . - , , . - 17 : * , . * (). : (*****) - , . - . * . - , - , , . , , , . , - UNIX (); - , . * - . . - , , - , , . * , - , , . . , , , . , , - , . , - , , . , , - shell' : shell - , , - , -- . shell , - , . , shell.

    1.5

UNIX : . , () : - , . - - , -------------------------- (*****) 12 ; . 18 , - , , , .. ( ) , , , , UNIX. : * - , ( ). . , , , , - . * - . , , - ; , A B C D +-------+-------+-------+-------+ | | | | | +-------+-------+-------+-------+ | | | | | +-------+-------+-------+-------+ 1.5. , . , , . - , . 1.5 - : A, B, C D, , - , . , , . - , - , . "", , "", , , , , - . , shell . - , shell, shell. Shell , , - , - .

    1.5.1

UNIX , - - , - 19 . ( ), . , - , , . - . - - - . , - , , - , .. , - , . - "" , , - , ; , , . - UNIX .

    1.5.2

- , . - , , .. , . , . , - . 1.6 . , , - . +------------------------------+ ^ | | | +------------------------------+ | | | +------------------------------+ | | | | +------------------------------+ | | | | +------------------------------+ | | | +------------------------------+ | | | | +------------------------------+ v 1.6.

    1.5.3

, - ( , ). - , - 20 , - . , . , , , , . , - , , .. , , , . , - UNIX, , -. , . 6 9 , , .

    1.6

UNIX, - , , - . - , - . , , , , - , - -. - , . - , , - , , , . UNIX , - , - . , , . 21 2

UNIX. - , - , - .

    2.1 UNI

(. [Christian 83], .239), UNIX , "" - "". , , UNIX. 2.1 - , , , . , - , , - . , - , - . 2.1 : , - . - , 1.1. , , - - , . 6. - , . - , -, . - . . , , , . - , , , . - , - , open ( , ),close, read, write, stat ( ), chown ( ) chmod ( ). - 5. , , , . , - , . , - . - 22 ^ | +----------------------+ - | | | ---------|------- +----------------------+ - | - ^ - | - | --------------------------|---------------------|----------------- v v +---------------------------------------------------+ | ^ ^ | +------+------------------------------------+-------+ | | +-----------------+---------------+ +----------------+---------+ | v | | v | | | | | | - | | ............| | | | . - .| | <---+-+ | . .| | | | | . .| | ^ ^ | | | ............| | | | | | | ............| +-------+--------------+----------+ | | . -.| | v +-+> . .| | +--------------+ | ............| | | - | | ............| | | | | . -.| | | () | | . .| | +--------------+ | . .| | ^ | ^ ............| | | | | | | v +-------+------------------+ +-------+----------------------+ | | v . | | | . | | | . | | +------------------------------+ | | | | | | | | ^ | | +--------------+---------------+ | | | +--------------+------------------------------+------------------+ | v v | +----------------------------------------------------------------+ ------------------------------------------------------------------ +----------------------------------------------------------------+ | () | +----------------------------------------------------------------+ 2.1. - ; , - . , - 23 . - "" -, - . -, - (), , - . , , . , (. 7): , . , , fork ( ), exec ( ), exit ( - ), wait ( - ), brk ( , ) signal ( ). 7 - . . - - , , . 9 : (- ) . , .. "" - . - " ", . "" . , , .-. , , , . - - ; () , . , - . , . , , . - . , , .

    2.2

, , , 2.1.

    2.2.1

, 24 , , . "" (inode) UNIX. , , - . . , , , . , : open("/fs2/mjb/rje/sourcefile", 1); "/fs2/mjb/rje/sourcefile". , - . ( ), . , . - , - . , , . - , - - . - , - read write, - +---------+ +-----+ +-----+ | | | | | | +---------+ | | | | | --+---+ | | +-----+ +---------+ | | | +-------->| | | --+-+ | +-----+ | +-----+ +---------+ | +-------->| --+---+ | | | | | +-----+ | | | | +--+ | ... | +-----+ | | | +-----+ +-------->| | | | +------->| --+---+ +-----+ | | +-----+ | | | | | | | | +---------+ +-----+ +-----+ 2.2. , . - . 2.2 . open (- ) creat () , - . read () write () - , - , . 4 5. , . UNIX - - , . 25 , - UNIX . - , , (. 13). , , . , . - . , , - , . ( ) () - . "" , . 512, 1024, 2048 , 512, - . - , - . , - , - . , 1 - , 512 . , - , , 5. "" - , - 1 , . +---------+---------+-------------------+-----------------+ | | - | | | | | | | | +---------+---------+-------------------+-----------------+ 2.3. ( 2.3). * , - , , - , - . - , ( ) - . * - , , - , , . * . - - . , . : , - mount (- ) ( 5.14). * . . 26

    2.2.2

- . - , . - - , . - , , (.. ""), - . , - , , , , . - , ; , . . UNIX , fork. , , fork. , fork, , - . - , . , (PID). , "" ; ( 1) . 1, - init, - , 7. , , , : * "", , * , * , , , , bss (*) ( 0 ), * , . , 1.3, main copy, - version ( , ), - buffer. V , - , . - exec, - , : , . - bss- , - . - , ; , , . - ------------------------------------------------ (*) bss IBM 7090 "block started by symbol" (", - "). 27 , , , - , . +--------------+ +------------------+ | | ^ | | | | | | ^ | | ( )| | | . | |--------------| | | . | | 2| | | . | |--------------| | | . | | | | | . | | | | | . | | write | | | . | |--------------| | | . | |, -| | | . | | | | | . | | write | | | . | |(new, buffer, | | | v | | count) | 3 | | +--------------+ call write() 3 +------------------+ | | | | | | | | | (count) | | | |--------------| |------------------| | 1| | 1 | |--------------| |------------------| | | | | | | | | | copy | | func2 | |--------------| |------------------| |, -| | , - | | | | | | copy | | func2 | | (old, new) | 2 2 | | +--------------+ call copy() call func2() +------------------+ | | | | | | | | |(fdold, fdnew)| | | |--------------| |------------------| | 0| | 0 | |--------------| |------------------| | | | | | | | | | main | | func1 | |--------------| |------------------| |, -| | , - | | | | | | main | | func1 | | (argc, argv) | 1 1 | | +--------------+ call main() call func1() +------------------+ 0 0 2.4. . 28 , , - , , . 1.3 argc argv, fdold fdnew, main, , main ( , ), old new count, copy, . UNIX , - , . , - , . 2.4 , write copy. ( ) main , - 1; 1 - main. main copy , old new, 2; 2 count. , write, . - - ; , , , "", . , , , , - . , - , . , . - , , , , - . , . 2.4 - write - - +---------------------+ +-------------+ +------------+ | -| | | | | | , | | | | | | | | | | | +---------------------+ +-------------+ +------------+ ^ +--+-> ---+-----+-> | | | +-------------+ +-----+------+ +----------+----------+ +--+-> ---+--+ | | | | | | | +-------------+ | | | | | | | | | | | +-----+------+ +----------+----------+ | | | +--+-> | | | v -----+--+ | | +-----+---+--+ +---------------------+ | | | | | | | | +-------------+ +-----+---+--+ | | | | +---------------------+ | | +-------------------------+---+--+ | v v | +--------------------------------+ 2.5. 29 copy. write - . , , , . - , - . - , , . , - , , - , . ( , , ) . - exec, , , , - . fork, - , , , , , - . exit, , - . 2.5 , . , , , , . , - , - . , 6. , , : * , * , , - ( UID), * , ( ""). , , , . : * , , * , , * , * -, * (. 5), * . - , , - . , , - , u, , - , - , u . - 30 .

    2.2.2.1

, , - , - , , , - , . - , . , - . , - , . , . , , , . , , - , , - . , - , . - 1.5, , , A B; - , , , A. , - . , . - , , , . - - .

    2.2.2.2

, , . - 6, : 1. . 2. . 3. , , - . , - , - . 4. (""). " ", , , - -. - , 1 2 . , . 31

    2.2.2.3

, , , . - , , , - , - . , . , , . 2.6 , . , , . , , . - , . , - " " " ". - , , ; , , , - , . - , , - " " - , - . ( 2.7) - , bp1, - , bp. , . , - +-------+ - | | - | 1 | | | +-+-----+ | ^ | | - | | | | v | +-----+-+ - | |<---------+ , - | 2 | | | |<---------+ +-+-----+ -| ^ - +-------+ | | +-------+ | |<----------+ +------------+ | ("")| 4 +--------------------------->| 3 | +-------+ +-------+ 2.6. 32 . - : bp1 . , bp1 , - , bp1 ( 2.8). - - , . - UNIX , . - "", , . , - , , . , , - 2.7, , - - +-----------------------------------------------------------+ | struct queue { | | | | | | | | } *bp, *bp1; | | bp1 - > forp = bp - > forp; | | bp1 - > backp = bp; | | bp - > forp = bp1; | | /* */| | bp1 - > forp - > backp = bp1; | +-----------------------------------------------------------+ 2.7. , - +-------+ | | | bp1 | | | +-------+ +-------+ +-------+ ----->| +------------------------------>| +----> | bp | | | <-----+ |<------------------------------+ |<---- +-------+ +-------+ bp1 +-------+ +-------+ +-------+ ----->| +---------->| +---------->| +----> | bp | | bp1 | | | <-----+ |<----------+ | +-----+ |<---- +-------+<----+ +-------+ | +-------+ +-------------------+ 2.8. , - - 33 , , . , , - , - . , . - , - , - . , , , , - . , - . - , . 12 , , . , , - , , "", . - , , . , - , , - .

    2.2.2.4 ""

, , - , . "" - , . , , , . , - (" "), - . , , , - " " . , - , , - - , , .. - , , , "" , " ". - ; , - , , , , , "". - , "" " - ", ; - , . . - , , .. - , . , , , 34 ; , - , , - . : while ( "") sleep (: ""); set condition true; : ( "") ( , ""); ""; "" , - - , : set condition false; wakeup (: ""); : ""; ( , ""); 2.9 , , A, B C - . "" - . , , , , , . - "", " - ". , , B, - . B, "while", , - , . B (, - -), - . A, , "while", , , ""; C. B , A C . , "while-sleep" , . "" 6. "". - "" , - "". , , .

    2.3

- , . , , , - . , 35 , . , , - , , , . - , , - . .

    2.4

, , , - . , - , , - . A B C +------------------------------------------------------------- | | | . | . | . . | . . | +----------------------------------------------------------+ | | "" | | +----------------------------------------------------------+ | | | . . | . . | . . | . . | . . . | . . | . . | . . . | . . | . . | . . | . . | . . | . . | . . | . . | . . | | "" | v 2.9. , 36 , . , - . , . , - - . , , - , . - , . - . , .

    2.5

; - - . - . , ; , , ( ). , , - , . , . , . , , - , "", - . , , - , , "" . - - , . , 2.1, , - . () - , 4, 5 7. 4 - , - , , . 5 - , , 4 - , , .. , open, close, read write. 6 - , 7 , - 6. 8 - , 9 . 10 , , . 11 . , , - , , - . 37

    2.6

1. : grep main a.c b.c c.c > grepout & wc -1 < grepout & rm grepout & ( "&") shell , , shell . ? grep main a.c b.c c.c | wc -1 2. , 2.7. , , , : remove(gp) struct queue *gp; { gp - > forp - > backp = gp - > backp; gp - > backp - > forp = gp - > forp; gp - > forp = gp - > backp = NULL; } : - bp1. - , bp1. - , bp1 , bp - . , , ? 3. , - , , ? 38

Last-modified: Tue, 23 Mar 2004 10:06:51 GMT
: