:





        , 
   ,        -
 .   ,      -
   ;     ,  
  .   ,   -     -
,             .
, ,       8 ,  
   1         -
.            (  
),    ,    ,   -
     ,  
 .            
        ,
   ,     
.           -
 .
        UNIX       -
       ,    
,         -
.        ().
         PDP-11,  -
    64 .     -
         ,  
.  BSD ( 4.0)       
 ,  "  " (demand paging),  -
           , 
 ;        -
 V  UNIX.          
,          -
  ,   .   -
      ,     
        
:       
           -
.          -
 ""  .       -
   .




           :  -
      ,      
    .




         ,    -
      .     
        ,      

                                    253

       . , 
 ,    ;      
          , 
        , -
,      . 
   , ,  ,  
     . , -
   ,       , -
    .     -
    ,  -      
  ,     ,   
   ,      
.
              
 ,    ,  , 
 ,   . ,    
  ,        ,
       ,  -
            ,
 "  ".  ,   -
,      (,   -
 ),       ( -
  )    .
              
     ;     
   .            
,        .   
    ,       
    ,   -      -
   .       -
,   10000    ,  1,   -
 9.1.    ,
   ,   ,     -
       .
      9.2          
   (malloc).       , -
   ,     .
         ,   , 
     (        
).          
       ,     .  
  9.3 ,        
  100, 50   100  .      
    , ,   250   
 ,      9750 ,    
251.



                           
              +------------------------------------+
              |   1                  10000         |
              +------------------------------------+

     9.1.       





                                    254
    +------------------------------------------------------------+
    |  malloc    /*     -|
    |                          */         |
    |  :  (1)     /*    -  |
    |                                        */   |
    |                      (2)        |
    |  :  -     |
    |                      0 -                   |
    | {                                                          |
    |     (  )                               |
    |    {                                                       |
    |        (      |
    |         )                                       |
    |       {                                                    |
    |           (  ==    )   |
    |                 ;                      |
    |                                            |
    |                  ;      |
    |           (  );            |
    |       }                                                    |
    |    }                                                       |
    |     (0);                                            |
    | }                                                          |
    +------------------------------------------------------------+

     9.2.       


      ,          
.     :

                  
+------------------------------+  +------------------------------+
|   1            10000         |  |  101            9900         |
+------------------------------+  +------------------------------+

              ()                               ()

                  
+------------------------------+  +------------------------------+
|  151            9850         |  |  251            9750         |
+------------------------------+  +------------------------------+

              ()                               ()

     9.3.     


 1.        . -
     ,          ,  -
           .    
             
       .
 2.          .  
        ,      , 
          (   
      ),       
            .  
         .
 3.          ,  

                                    255

         -   . -
               .

       , ,     50
 ,    101,       ,
    ,    
    .      100  ,
   1,     ,    -
   ,     .  -
         9.4.
    ,        200  ()
  .      -
     150 ,     
    (.  9.5). , ,  -
  350


                  
+------------------------------+  +------------------------------+
|  251            9750         |  |  101              50         |
+------------------------------+  +------------------------------+
                                  |  251            9750         |
              ()                 +------------------------------+

                                                ()

                        
                 +------------------------------+
                 |   1              150         |
                 +------------------------------+
                 |  251            9750         |
                 +------------------------------+

                               ()

     9.4.     


                  
+------------------------------+  +------------------------------+
|   1              150         |  |   1              150         |
+------------------------------+  +------------------------------+
|  251            9750         |  |  451            9550         |
+------------------------------+  +------------------------------+

              ()                               ()

     9.5.       ,
                       


 ,    151.   ,   350  -
         ,   ,  
       .    ,   -
          -
,        ,     -
 .
        UNIX    -
,      V    -

                                    256

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




       ,      ,
     :
 1.      fork,    
        -.
 2.      brk,   -
    .
 3.          
    .
 4.           -
    .
        fork    , 
  ,   ,    
(),  .
         ,      -
  ,     ,   -
  ,    ,     
 0.            
  (   1-3),    (.  9.12)  
,      .     -
       .
       -,      
           ,
  -  .    
           ,
        
        .  , -
       ,   ,
      .  -
,       ,  -
,         .
          ,
    ,   
  -.    ,     ,
  ()      --
.
               
       .  
        , -
  ,     .  
    ,       -
       .    
       ,   ,  -
      .
        9.6        
    (*).    -
: ,   .     


                                    257

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

                
      ,  

         +----------------------+            +-----------------+
  |   0         278 ----+-------684--+--->             |
   +----------------------+            +-----------------+
         |  1         432 ----+------------+--->             |
         +----------------------+            +-----------------+
         |                 |   +--------+--->             |
         +----------------------+   -        +-----------------+
         |          -           |   -+-------+--->             |
         |          -           |   --       +-----------------+
         |          -           |   --+------+--->             |
         +----------------------+   ---      +-----------------+
  | 64         573 ----+---+-- +----+--->             |
   +----------------------+    -- -    +-----------------+
         | 65         647 ----+----+- 690  |                 |
         +----------------------+     - -    +-----------------+
         | 66         595 ----+-----+ -
         +----------------------+       -
         |                 |       -
         +----------------------+       -
         |          -           |       -
         |          -           |       -
         +----------------------+       -
  |128         401 ----+-------+
    +----------------------+
         |                 |
         +----------------------+

     9.6.      

 2,       64,     -
          62 .  
 ,        0, 1,
64, 65, 66  128;            
   62      ,      61
     ,     -
  .          ,
             ,     
    62,     -
   .        
 9.7.   9.6  9.7 ,   ,
     , 
  ; ,      -
    ,     -
    .
       ,  ,    
     ,   ,    
          -
 .  ,         



                                    258

                
      ,  
         +----------------------+            +-----------------+
  |   0         401 <---+-------684--+----             |
   +----------------------+            +-----------------+
         |  1         370 <---+------------+----             |
         +----------------------+            +-----------------+
         |                 |   +--------+----             |
         +----------------------+   -        +-----------------+
         |          -           |   -+-------+----             |
         |          -           |   --       +-----------------+
         |          -           |   --+------+----             |
         +----------------------+   ---      +-----------------+
  | 64         788 <---+---+-- +----+----             |
   +----------------------+    -- -    +-----------------+
         | 65         492 <---+----+- 690  |                 |
         +----------------------+     - -    +-----------------+
         | 66         647 <---+-----+ -
         +----------------------+       -
         |                 |       -
         +----------------------+       -
         |          -           |       -
         |          -           |       -
         +----------------------+       -
  |128         955 <---+-------+
    +----------------------+
         |                 |
         +----------------------+

               9.7.    


  ,        
( ) .        
,             
    (.  9.4).




        fork ( 7.1) ,   -
-       ,   
 .     ,     
  ,    ,   () -
.    ,  -    -
     ; -    
 "  " (.  6.1)    
.  -    "  -
",          ,    
    ;        -
   fork     .



            ,
    ,      brk, 
      ,   -
         . 
          

                                    259

                  
                       
        ,            ,           
                        
       +------------+          +------------+          +---------+
|   0  278  |          |   0  278 -+-----684--+--->     |
 +------------+          +------------+          +---------+
       |  1  432  |          |  1  432 -+----------+--->     |
       +------------+          +------------+          +---------+
       |       |          |       | +--------+--->     |
       +------------+          +------------+ -        +---------+
       |     -      |          |     -      | -+-------+--->     |
       |     -      |          |     -      | --       +---------+
       |     -      |          |     -      | --+------+--->     |
       +------------+          +------------+ ---      +---------+
| 64  573  |          | 64  573 -+-+-- +----+--->     |
 +------------+          +------------+  -- -    +---------+
       | 65  647  |          | 65  647 -+--+- 690  |         |
       +------------+          +------------+   - -    +---------+
       | 66  595  |          | 66  595 -+---+ 691 +|--->     |
       +------------+          +------------+     -   -+---------+
       |       |          |       |     -   -
       +------------+          +------------+     -   -
       |     -      |          |     -      |     -   -
       |     -      |          |     -      |     -   -
       +------------+          +------------+     -   -
|128  401  |          |128  401 -+-----+   -
  +------------+          +------------+         -
       |       |     |129  ...  -+---------+
       +------------+  +------------+
                               |       |
                               +------------+

     9.8.        



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




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

                                    260

 .          ,  
     ,     -
,           . 
      ,    
    ;      
.
           8,    
           
.           -
  ,    ,    "-
  ,  ",     , 
      (.  9.9).   
    ,     ,
   ,   .  
  ,      
     .
               ,  
  ,       
    ,      , 
   .     
   :
  *         ,   -
    .        ,  
                
    ,    . (    
    6.1).
  *    ,   ,    -
        .      -
               ,
       .
          ,      
   .       
,      ;      
 ,   , ,  
  .      , -
 ,   ,       -
   .        -
        .    
         ,   ,  
,   ,   ,      ,
   nice,     
  .
    ,    ,       -
    2   ,    ,  ,  -
  ,     2     -
.         ,  
,       ,   ,   
,     2  (**)   ,  
       ,     -
  ,        .    -
          .  

---------------------------------------
(**)      6    UNIX        
             ,  -
           3 .     -
            2 .   -
           ,     -
      .

                                    261

    +------------------------------------------------------------+
    |  swapper    /*   ,     |
    |                      *        |
    |                      *     */         |
    |  :                             |
    |  :                            |
    | {                                                          |
    |    loop:                                                   |
    |       (  ,   )|
    |           ,    -|
    |             ;                          |
    |       (  )                            |
    |      {                                                     |
    |           ( ,   -|
    |              );                 |
    |            loop;                                  |
    |      }                                                     |
    |       (      - |
    |        )                                        |
    |      {                                                     |
    |           ;                                |
    |            loop;                                  |
    |      }                                                     |
    |  /* loop2:   ,   - |
    |   *  */                                                |
    |       ( ,    ,   |
    |             |
    |       )                                              |
    |      {                                                     |
    |           (     ) |
    |               ,     |
    |                   -   |
    |               ;                                     |
    |              /*    -  |
    |                               *   */        |
    |               ,    -   |
    |                     nice|
    |               ;                                  |
    |      }                                                     |
    |       (      |
    |           )              |
    |           ( ,   -  |
    |             );                     |
    |                                            |
    |           ;                                |
    |        loop;   /*  loop2   -|
    |                          *  */                           |
    | }                                                          |
    +------------------------------------------------------------+

                     9.9.  


       ,    -
     ,     
         .
           
    ,       -
     (   ),   

                                    262

   .
      9.10        
        .   ,
         
      ; ,  -
            
    1 .       
  ,       -
,     .  ,    -
            
  .      A  B,  
.             -
   ,      -
           (   
 ),    2    -
  A  B       C  D.    
   E,   ,     -
    .  3-   E    
,     3      ,  
           ,  
    2 .  4-     
  C  D      E  A.
          ,   -
     .     -
         -
    ,    ,   -
    .  ,     
""         -
 (. [Peachey 84]).
                 
      , ,   . -,
      ,  -
      nice.   ,   
      -      -
 ,      ,      
  . ,     
    1 ,      ,
     ,   2  -
.         
  ,    ,   
 .    PDP 11/23 -
,         -
    10  (. [Peachey 84]).
    -,        , 
            ,   
      ,   ,     
      .       ,  
             
  ,         
  .        -
 ,      .    -
     ,      -
         -
,            
 ( 9.9).
    -,        , -
   "  ",     ,
              -
.      9.11,   ,   -

                                    263

  D  2-  ,     C,      
3-    D     E ( -
   nice),   ,   D       -
 . ,     .
             .    
        ,    
      ,  :    


   A       B           C           D           E
--+--------------+-----------+-----------+-----------+-----------
 0|       0      |     0     |   |   | 
  |       |           |     0     |     0     |     0
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      1      |     1     |     1     |     1     |     1
 1|              |    |           |           |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      2      |     2     |     2     |     2     |     2
 2|      |   |   |   |
  |       0      |     0     |     0     |     0     |
  |              |           |    |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      1      |     1     |     1     |     1     |     3
 3|              |           |           |    |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      2      |     2     |     2     |     2     |     4
 4|      |           |   |   | 
  |       0      |           |     0     |     0     |     0
  |              |           |           |           |  
  |              |           |           |           |
  |              |           |           |           |
--+-      1      |     3     |     1     |     1     |     1
 5|       |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      2      |     4     |     2     |     2     |     2
 6|      |   |   |           | 
  |       0      |     0     |     0     |           |     0
  |              |    |           |           |
  |              |           |           |           |
  v


     9.10.       ,   
                      



                                    264


   A       B           C           D           E
--+--------------+-----------+-----------+-----------+-----------
 0|       0      |     0     |   |  nice 25  | 
  |       |           |     0     |   |     0
  |              |           |           |     0     |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      1      |     1     |     1     |     1     |     1
 1|              |    |           |           |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      2      |     2     |     2     |     2     |     2
 2|      |   |   |   |
  |       0      |     0     |     0     |     0     |
  |              |           |    |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      1      |     1     |     1     |     1     |     3
 3|              |           |           |   | 
  |              |           |           |     0     |     0
  |              |           |           |           |  
  |              |           |           |           |
  |              |           |           |           |
--+-      2      |     2     |     2     |     1     |     1
 4|      |           |   |           |
  |       0      |           |     0     |           |
  |       |           |           |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      1      |     3     |     1     |     2     |     2
 5|              |   |           |           | 
  |              |     0     |           |           |     0
  |              |    |           |           |
  |              |           |           |           |
  |              |           |           |           |
--+-      2      |     1     |     2     |     3     |     1
 6|      |           |           |   |
  |       0      |           |           |     0     |
  |              |           |           |    |
  |              |           |           |           |
  v

     9.11.       
                     


    ,      
,             , 
     .      
9.5.    ,    ,   
        .





                                    265



            
    ,    (***).    
         ,  
   . ,      -
    1  2      4  5 .
    ,      
  ,     .     -
   ,        -
         ,    
.          -
,      .
         ,  -
        ,   
   ,    -
 .          "".
  [Denning  68]      -
   ,     n -
    ;  n     -
.           , 
           -
,         ,   -
      .  
  ,         ,  
,         ,
       .
      9.12     -
 ,         
     "" (  -
  ,       ).   -
         -
   ;      -
    ,   ,   -
     .     
,       -
     .   -
         
   (reference bit)     , 
    .     -
 ,      ;     
 ""      .
         -   , 
  ,         ,
           . -
   ,    ,   -
       - .  , 
      :   -
          -  
 .      , 
,        .    
         -
   V  UNIX.

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

                                    266




             ()  
       4  -
 :   ,     ,  
   (page frame data table - : pfdata) 
    .    pfdata 
      ,       
 .
      6  ,      
,        . 
    ( 9.13)     -
,  ,           
 ( ,   ),     ,
   :

-
 -              
         2         3           4               5
+------------+ +-------+----------+-------------+----------------+
|     24     | |  24   |    24    |     24      |       24       |
+------------+ |       |          |             |                |
|     15     | | 15 24 |  15 24   |   15 24     |     15 24      |
+------------+ |       |          |             |                |
|     18     | | 18 15 | 18 15 24 |  18 15 24   |    18 15 24    |
+------------+ |       |          |             |                |
|     23     | | 23 18 | 23 18 15 | 23 18 15 24 |   23 18 15 24  |
+------------+ |       |          |      -      |        -       |
|     24     | | 24 23 | 24 23 18 |      -      |        -       |
+------------+ |       |          |      -      |        -       |
|     17     | | 17 24 | 17 24 23 | 17 24 23 18 | 17 24 23 18 15 |
+------------+ |       |          |      -      |        -       |
|     18     | | 18 17 | 18 17 24 |      -      |        -       |
+------------+ |       |     -    |      -      |        -       |
|     24     | | 24 18 |     -    |      -      |        -       |
+------------+ |       |     -    |      -      |        -       |
|     18     | | 18 24 |     -    |      -      |        -       |
+------------+ |       |     -    |      -      |        -       |
|     17     | | 17 18 |     -    |      -      |        -       |
+------------+ |       |     -    |      -      |        -       |
|     17     | |       |     -    |      -      |        -       |
+------------+ |       |     -    |      -      |        -       |
|     15     | | 15 17 | 15 17 18 | 15 17 18 24 |        -       |
+------------+ |       |          |      -      |        -       |
|     24     | | 24 15 | 24 15 17 |      -      |        -       |
+------------+ |       |     -    |      -      |        -       |
|     17     | | 17 24 |     -    |      -      |        -       |
+------------+ |       |     -    |      -      |        -       |
|     24     | | 24 17 |     -    |      -      |        -       |
+------------+ |       |     -    |      -      |        -       |
|     18     | | 18 24 | 18 24 17 |      -      |        -       |
+------------+ +-------+----------+-------------+----------------+

             9.12.   


  *  
  *  

                                    267

  *  
  *    
  * "" 
             
 ,   ,    ,    
  ,        ,    
.        ,      
   ,    -   ,   -
  .     , -
      fork,   , 
  ,     ,   -
     . , ""    -
       .  ,
    ""   ,  -
   -  ;     9.2.4  
,       .

+--------+ +->+----------------------+---------------------------+
|        | |  |                      |                           |
|        | |  +----------------------+---------------------------+
|        | |  |                      |                           |
|        | |  +----------------------+---------------------------+
| | |  |                      |                           |
|        | |  +----------------------+---------------------------+
|        | |  |  |  |
|    ----+-+  +----------------------+---------------------------+
|        |    |                      |                           |
|        |    +----------------------+---------------------------+
|        |    |                      |                           |
+--------+    +----------------------+---------------------------+
              |                      |                           |
              +----------------------+---------------------------+
              |                      |                           |
              +----------------------+---------------------------+
              |                      |                           |
              +----------------------+---------------------------+
              |                      |                           |
              +----------------------+---------------------------+

                       
+-----------------------------+-------+-----+-----+----+-----+---+
|   () ||-|-|-|-  |-|
|                             |       |-|-|- |- |-|
|                             |       |  |  |- |-  | |
|                             |       |  |     | ||   |
|                             |       |-|     |    |     |   |
|                             |       |   |     |    |     |   |
+-----------------------------+-------+-----+-----+----+-----+---+

                     
+-----------------------+---------------+------------------------+
|     |     |  (  -  |
|                       |               |  ,    |
|                       |               | ,     |
|                       |               | , - |
|                       |               | )                    |
+-----------------------+---------------+------------------------+

     9.13.       

                                    268

               ,
     ( 9.13).  -
,   ,      
           .  -
         ,
   ,         .
     ,    -
          ,   
  .       ,
          
  ;         
.            -
   exec  :      -
 ("demand fill")    ("demand  zero").    
     9.2.1.2.
        pfdata     . 
         :
  *  ,   ,       -
         ,     -
           (    -
     ),   ,     .
  *  ,    .      
        ,     . 
         ,  -
        ,      ,  -
          fork.
  *    (    ) 
     ,    .
  *      pfdata     -
        - .
             pfdata  -
      -.    
 ,   ,   , 
,    ,      -
,          .   
        . 
         "-
".      -    -
  ()   .   ,   
   ,     .  
 ,         
,            
   -.
           -
,    .       ,
    ,     
 .
        9.14        ,
  ,   pfdata   -
  .   1493    
,      794;   -
  ,    ,   ,  -
          1    
     2743.   pfdata,  ,   
     , ,      
   ,  1.  ,     -
    pfdata,        9.2.4.1.  
     (     -
)   ,         

                                    269

     .




          7.1,     fork 
          -
-.     -

             +-----------------------+--------------------------+
  |   |  |
        +-----------------------+--------------------------+
   1493     |     794  |  1    2743 |
             +---+-+-----------------+--------------------+-----+
                 | |                                      |
                 | |   pfdata,               |
                 | |  -   +------------+
    +------------+ v    794      |
    | +------------+-----------------------+ |
    | |            |        1 | |   
    | |            +-----------------------+ |  
    | |            |      1 | |   
    | |            +-----------------------+ | +-----------------+
    | |            |        2743 | | |   1|
    | |            +-----------------+-----+ | +--------+--------+
    | |                              | +-----+          |
    | |                              | | +--------------+
    v v                              v v v
    +--------------------------+     +---------------------------+
    |    794 |     |            2743 |
    +--------------------------+     +---------------------------+

     9.14.    ,    -
                       

         -
-,         ,
      fork     
exec          .
  ,   V     
      (  ,   -
      pfdata).   ,   , 
    ,         -
  ,         -
-:   ,     
  pfdata,   ,   -
      (    ,   
   ).     -
 ,         -
  .
          ,   -
 ,       .    -
      ,    ,  ,   
     .       
fork     ,    -
    ,    "  ".  
   -   ,    -
 ,          -
.  ,       -

                                    270

,      .
         9.15.     
         T,  
     2,      ( 
pfdata).   -

           -           -

                          
                       
           +--------------+           +--------------+
           | -            |           | -            |
           +--------------+           +--------------+
           | -          - |           | -          - |
           +--------------+           +--------------+
             -   + ---------------------+          -
             -   -      -                          -
             v   v      v                          v
+-------------------+ +-------------------+ +-------------------+
|      T     | |      P1    | |      C1    |
|    2 | |    1 | |    1 |
|+-----------------+| |+-----------------+| |+-----------------+|
||    || ||    || ||    ||
||          || ||          || ||          ||
|+-----------------+| |+-----------------+| |+-----------------+|
||        -        || ||        -        || ||        -        ||
||        -        || ||        -        || ||        -        ||
|+-----------------+| ||        -        || ||        -        ||
||-   -|| ||        -        || ||        -        ||
||     || ||        -        || ||        -        ||
||  24        967 || ||        -        || ||        -        ||
|+-----------------+| |+-----------------+| |+-----------------+|
||        -     -  || ||-   -|| ||-   -||
||        -     -  || ||     || ||     ||
||        -     -  || ||  97        613 || ||  97        613 ||
||        -     -  || |+-----------------+| |+-----------------+|
||        -     -  || ||        -     -  || ||        -     -  ||
||        -     -  || ||        -     -  || ||        -     -  ||
|+-----------------+| |+-----------------+| |+-----------------+|
+-------------------+ +-------------------+ +-------------------+
                -                     -           -----------
                v                     v           v
+---------------------+         +---------------------+
|   967 |         |   613 |
|      1 |         |      2 |
+---------------------+         +---------------------+

     9.15.   ,     -
                     fork


-    C1,    P1  -
-.          , 
       97.    
pfdata     613,      2,
     .
         fork   BSD   
  . ,   ,  
     ,   BSD 

                                    271

  vfork,     ,     
    fork    exec.  vfork
   ,    ,    fork
    V  UNIX.  -     
   ,    ,       
       .    
vfork ,    ,      -
      .    
      UNIX  BSD   ,
         :     -
   ,        ,
          
   ?

    +------------------------------------------------------------+
    | int global;                                                |
    | main()                                                     |
    | {                                                          |
    |     int local;                                             |
    |                                                            |
    |     local = 1;                                             |
    |     if (vfork() == 0)                                      |
    |     {                                                      |
    |         /*  */                                      |
    |         global = 2;  /*      */|
    |         local = 3;   /*     */          |
    |         _exit();                                           |
    |     }                                                      |
    |     printf("global %d local %d\n",global,local);           |
    | }                                                          |
    +------------------------------------------------------------+

      9.16.  vfork    

        ,       9.16.
    vfork -    exec, 
   global  local     (****).
  ,  -    -
,     exec  exit.    -
  , - ,      -
         ,       
 vfork !       --
  ,   vfork (.  9.8).





         7,      -
 exec,          
.              ,

---------------------------------------
(****)  exit    _exit,     ""
        ,    - ( -
         ),    ,    printf,
        ,     -   -
             vfork.


                                    272

  ,        -
  .        ,  
     .          
    ,      
"    "  (   ,    bss) 
"  " (   bss).      
      read,    - 
() .    ,  
 "  " (      
           ) 
"  " (   ).    9.2.3  
,      .         ,
""     ,     -
  .
          . -,    -
       -   -
 ,               .
-,     ,  ""     
,    exec,         
          .
    exec,     
     ,     
 ,    .. " ".  -
,        (, bmap)
     ,     -
,  ,      -
    .  ,  bmap    -
,        .  
   read     
   -.        
          
, ,     ,   -
   .            
     .    -
         ,    
          -
   .
      ,       -
,      exec    
        .   
  ,      ,  
,       ;    -

                                                ,
                                 +->   
    +---------------------------------+ |    +----------------+
    |                -----------+-+  0 |                |
    |                                 |    - |                |
    |                                 |    - |                |
    |         |    - |                |
    |  +---------------------------+  |    - |                |
    |  |          84 |  |    - +----------------+
    |  +---------------------------+  |   84 |       279      |
    |                                 |      +----------------+
    +---------------------------------+      |                |
                                             |                |
                                             +----------------+

             9.17.    

                                    273

     .   9.17  ,  -
          84  -
 .      ,     -
      (279).





    ""   (page stealer)  ,  
         ,  
            .
           
  ,     .    -
        ""  
   (  ,  -
,   ,   ).     
,    ,  ,   ,
 ""       ,     
.
            :  "",
     ,         
      .   , 
            -
.          -
  ,      , ,  
          ( 9.2.4). 
    , ""    
,      ,    
        -
.   ,       
  ,   ""        
,        (.  9.18).  
    ,        
    -    .  -
         -
           
  .
      9.19    ,  
,   "" .     "-

                          
    +------------+----------+----------+-----------------+
    |            ^          ^          ^                 |
    v            |          |          |                 | -
+-------+        |          |          |                 | 
| - |      +-+-+      +-+-+      +-+-+             +-+-+ 
|  |----->| 1 +----->| 2 +----->| 3 +-------->| n | -
| |      +---+      +---+      +---+             +-+-+ -
+-------+      ""  ---       |   
    ^                                              |
    |                                                    |
    |                       +--------+                   |
    |                       | -|                   |
- +-----------------------|  - |<------------------+ -
                      | |                     
                            +--------+

             9.18.   

                                    274


"     ,  -
    . ,       
  "",   ""  0.   -
      ,    
""         -
           .
          ,   , 
                
.    ,       -
 ,   ""      .
          ,  
   ;       . , 
 ,  ,     ,      :
""            
 .
       "" ,      
   ,      ,
   ""      ,   -
     .     -
   ,        
    ,       
    ,      
         
    .      
    .      
  ,   ,  ,  -
        ,  "-
"           . 
          -
 .
      ""       ,
           -
.       :

              ( )
     +----------------------+-----------+
     |               |     0     |
     +----------------------+-----------+
     |                      |     1     |
     +----------------------+-----------+
     |                      |     2     |
     +----------------------+-----------+   
     |                      |     0     |
     +----------------------+-----------+
     |                      |     1     |
     +----------------------+-----------+   
     |                      |     0     |
     +----------------------+-----------+
     |                      |     1     |
     +----------------------+-----------+
     |                      |     2     |
     +----------------------+-----------+
     |                      |     3     |
     +----------------------+-----------+  
     |             |           |
     +----------------------+-----------+

           9.19.  "" 

                                    275


 1.         ,  "" -
     : ""       -
       ;    . -
           (  -
        ),      
    .
 2.             -
               (  
        ),        -
     (    ),     
     pfdata         
    .
 3.        ,    -
        ,      -
              .
    ""        ,  
  1  3.
          ,  -
,            -
   ,        
. ,         -
.   , ""      -
.            -
,           
        . -
,    -   ,      -
  ,       -
  ,   ,    -
 .          -
  ,         
  ,     -
 .
    ""     ,    -
      ,     
   .    ,    -
   :  ,   , -
,  ""  .       
  ,        -
 ,             
    ,        
 9.1.2.        -
,      (    -
),       .     -
     ,         
,         ,    
   .
          ,   
          -
       pfdata.    
   0,   pfdata    -
          . 
    0,  ,   (  -
  fork)    ,  -
    . ,     
,            
         -
.        ,       ,
   ,   ,   

                                    276

    ,     . , -
     ,       "-
" .
    ,   ,  ""   30, 40, 50  20
   A, B, C  D, ,         
        64 .   9.20
         -
,  ""        -
: A, B, C, D. ""       64
   30   A  34   B.  
        64     6 
 B, 50   C  8     D.    
              
. ""   12    D    
 ,       ,     -
    .          
          
  ( ),     .
      ,          
.    ""   ,   -
,          .    
     (    ),  -
             ,
        pfdata

              64 
+--------------------+ +-------------------+ +-------------------+
| A  30 - | | B  6 - | | D 12 - |
|                    | |                   | |         -         |
| B  34 -| | C  50 -| |         -         |
+--------------------+ |                   | |         -         |
 -                   - | D  8 - | +-------------------+
  -                   -+-------------------+
   -                   -  -                 -
    -                   -   -                 -
     -                   -    -                 -
      -                   -     -                 -
       -                   -      -                 -
+-------+-------------------+-------+-----------------+----------+
|       |    A 30    B 34   |       |  B 6  C 50  D 8 |          |
+-------+-------------------+-------+-----------------+----------+
                        

     9.20.      
                     

     0,       
.            ,
      .




              : 
-  ()      .  
         -
        ,    
   ,    -
  .   ,    

                                    277

       ,    
  ,     ; , 
   .



         ,    
 ,    -  ()    
          (-
 9.21).          ,  
       , 
 ,      ,     
    .      -
      ,    -
            
 .    ,   -
,  ""     ,   -
        .   -
      ,
           -
-   " " (.  7.25).  -
     ,   , 
    ,       -
,      ,      ""
        .     
,            -
         -
.
    ,  ,      :
 1.     .
 2.      .
 3.   .
 4.   "  ".
 5.   "  ".
        .
           ,   ( 1),
 ,   -   ,    -
 "" .       ,  
       ,   ,
 ,      .    -
  ,     ,   -
   ,     pfdata  -
(   )        
.        -
 -;         ,  
  .
        9.22       
,     66.      
   -  ,   -
         ,  
        847  (  ,
        ): , 
  .        ,  
          847.  , 
         -
     .        
  1776 ( 9.23),       -
         -
   1776.        -

                                    278



    +------------------------------------------------------------+
    |  vfault     /*   -    |
    |                        ()  */           |
    |  :  ,          |
    |  :                            |
    | {                                                          |
    |     ,    ,  -|
    |      ,   ,    |
    |     ,  ;                          |
    |     (     -|
    |      )                                      |
    |    {                                                       |
    |         (SIGSEGV:  ) - |
    |        ;                                              |
    |         out;                                      |
    |    }                                                       |
    |     (  )   /* ,  -|
    |                                        - |
    |                                      */            |
    |         out;                                      |
    |     (   )                          |
    |    {                                                       |
    |          ;                             |
    |           ;                  |
    |         (    -|
    |        )     /*      ,  |
    |                  *   */                            |
    |           ;                                 |
    |    }                                                       |
    |           /*     */|
    |    {                                                       |
    |          ;                    |
    |                                                            |
    |           ,  - |
    |           pfdata;                              |
    |        (        |
    |         "  ")                 |
    |             ;                    |
    |                                            |
    |       {                                                    |
    |                -|
    |                ;                   |
    |            (  -);    |
    |       }                                                    |
    |         (   |
    |        );                                          |
    |    }                                                       |
    |       ;                    |
    |        "" ;          |
    |      ;                         |
    |  out:    ;                          |
    | }                                                          |
    +------------------------------------------------------------+

     9.21.    -  (-
                  ) 


                                    279

,        ,   ,    
pfdata, ,         847 
  .
       -      -
    -,       
  ,     (  2).   , 
           
   -        -
     .     -
      ,     -
     .   
        ,   -
           
   . ,  ,   -


                        
                       

           -
                                                -
-  -  -    -              -     -
                             
          +-----+--------+++-------+---------+  +----+------+----+
    0     |     |        |||       |         |  |    |      |    |
          +-----+--------+++-------+---------+  |    |      |    |
   1     | 1648| -  ||| |    3    |  |    |      |    |
          |     |  |||       |         |  |    |      |    |
          +-----+--------+++-------+---------+  |    |      |    |
   2     |     |        |||       |         |  |    |      |    |
          +-----+--------+++-------+---------+  |    |      |    |
   3     |  | -  ||||    5    |  |    |      |    |
          |     |  |||   |         |  |    |      |    |
          |     |        ||| -|         |  |    |      |    |
          |     |        ||||         |  |    |      |    |
          +-----+--------+++-------+---------+  +----+------+----+
   4     |     |        |||       |         |  |1036|  387 |  0 |
          +-----+--------+++-------+---------+  +----+------+----+
    -     |     |        |||       |         |  |  - |      |    |
    -     |     |        |||       |         |  |  - |      |    |
    -     |     |        |||       |         |  +----+------+----+
    -     |     |        |||       |         |  |1648| 1618 |  1 |
          +-----+--------+++-------+---------+  +----+------+----+
  64     | 1917| -  ||| -|  1206   |  |  - |      |    |
          |     |  |||     |         |  |  - |      |    |
          +-----+--------+++-------+---------+  |  - |      |    |
  65     |  | -  |||-|         |  |  - |      |    |
          |     |  |||   |         |  |  - |      |    |
          |     |        ||| -|         |  |  - |      |    |
          |     |        ||||         |  |  - |      |    |
          +-----+--------+++-------+---------+  +----+------+----+
  66     | 1036| -  ||| -|   847   |  |1861| 1206 |  0 |
          |     |  |||     |         |  +----+------+----+
          +-----+--------+++-------+---------+  |    |      |    |
  67     |     |        |||       |         |  |    |      |    |
          +-----+--------+++-------+---------+  +----+------+----+

     9.22.    -  


                                    280

       64 ( 9.22). -
 ,  ,      1861    
    1206.       -
  64     1861,   
     .  ,   -
         pfdata,  
     .
      ,         -
,  -          ,
     .      


                        
                       

           -
                                                -
-  -  -    -              -     -
                             
          +-----+--------+++-------+---------+  +----+------+----+
  66     | 1776| -||| -|   847   |  |1776|  847 |  1 |
          |     |      |||     |         |  |    |      |    |
          +-----+--------+++-------+---------+  +----+------+----+

         9.23.     


                 A                       B
    +------------------------------------------------------------
    |      -  -              -
    |                           -              -
    |       -              -
    |                          -              -
    |         -              -
    |             -              -
    |               -               -      -
    |               -               -             
    |               -               -    
    |               -               -             
    |               -               -      
    |               -               -      
    |               -               -           
    |               -               -              -
    |       --    -              -
    |                -              -
    |          -              -
    |                      -              -
    |       -              -
    |                      -              -
    |                               -        
    |        -              -
    |               -               -              -
    |               -               -     
    |               -               -              -
    |               -               -              -
    |                          -
    v

             9.24.     

                                    281


,     .  ,   -
    ,   ,  -
   ,    .   
  9.24.

           ,   
 ( 3),     .    -
        ,   
   ,  ,   ,  -
        .     
        , -
       exec.       
      . , ,  -
 ,     1, ,  
    ,     -
 3 (.  9.22).
             ,    
"    "  "  " ( 4  5),
         -
  .   "  ",    
  .     "  -
"   "  " .    
,           -
 ,    .  ,    
      3  65 (.  9.22):   
        ,       
  exec.
             - 
()         
  .      ,   
         -
,          -
   . , ,    ,  -
 ,         -
.




        ,     , 
  ,  ,     
 ,    ,      ,
            . ( ,
        ;  .
  7.22).          ,  
   -  ,      -
     fork      .
     ,     
,      ,     -
 - .
            -
 ,    ,     -
      ( 9.25).   , 
""     ,        
 .     ,  
       ,    -
   ,        
     ;     

                                    282

   .       -
          
   pfdata,   .    -
      9.26,       
   828.  B  ,    -
   ,    .   -
         786,    
 828,        
        
786.
          ,      
  ,      -
  .        -
        (  ), -
    ,      -
.       pfdata   ,  -


    +------------------------------------------------------------+
    |  pfault    /*     */   |
    |  :  ,          |
    |  :                            |
    | {                                                          |
    |     ,    ,  -|
    |      ,   ,    |
    |     ,  ;                          |
    |     (   )                     |
    |          out;                                     |
    |     (     )         |
    |          out;    /*   -  */|
    |     (     > 1)            |
    |    {                                                       |
    |           ;                 |
    |             ;       |
    |              -  |
    |          ;                                       |
    |              - |
    |          ;                                   |
    |    }                                                       |
    |         /*  ,    |
    |                          *     */ |
    |    {                                                       |
    |         (     )|
    |                ,  |
    |               ;                                 |
    |         (   - )     |
    |                -;                |
    |    }                                                       |
    |          ,    |
    |         ;                   |
    |      ;                         |
    |    ,    ;                     |
    |  out:    ;                          |
    | }                                                          |
    +------------------------------------------------------------+

        9.25.     



                                    283

         .  
,           -
  ,       0,  -
    (.  9.11).
            ,   ,
      ,     -
 , ,         -
  -   (  -
     9.17).   ,    
       ,   -
      ,    ""
         .   -
 (  ),       
     -  .    -
,       .   ,  
       - -
   ,      , 
   ""    .  -
     9.27.

       -  A
+-----------------------------------------------+
|  828: ,    +-+
+-----------------------------------------------+ |
                                                  |
       -  B            |  +-----------+
+-----------------------------------------------+ +->| |
|  828: ,    +--->|   828 |
+-----------------------------------------------+ +->|    |
                                                  |  |   3 |
       -  C            |  +-----------+
+-----------------------------------------------+ |
|  828: ,    +-+
+-----------------------------------------------+

    ()  ,   B    


       -  A
+-----------------------------------------------+    +-----------+
|  828: ,    +-+  | |
+-----------------------------------------------+ |  |   828 |
                                                  +->|    |
       -  B            +->|   2 |
+-----------------------------------------------+ |  +-----------+
|  828:                         ++|
+-----------------------------------------------+||  +-----------+
                                                 +|->| |
       -  C            |  |   786 |
+-----------------------------------------------+ |  |    |
|  828: ,    +-+  |   1 |
+-----------------------------------------------+    +-----------+

    ()       
          B

     9.26.    -   -
                    


                                    284

           -
    ,      .  
       ,      
 ,  ,    ,   -
     -  .

       ,   
                        "" 
    +------------------------------------------------------------
    |               -                              -
    |               -                       
    |               -                              -
    |                            -
    |      -                      -
    |                                 -
    |               -                              -
    |               -                     - 
    |               -                      
    |               -                              -
    |               -                              -
    |               -                      
    |               -                     , 
    |               -                         
    |               -                          
    |                            -
    |               -                              -
    |               -                              -
    |        -                   -
    |          -                       -
    |        -                 -
    |        -                 -
    |                                          -
    |               -                              -
    |               -                              -
    |       - -                   -
    |                                   -
    v
  

     9.27.        
                   -  





            (-
)    ,      -
           
   ,   "  ".   
,          ,  
      .   ,  -
 ,  - , -
,       ,    
-          
 . , ,   VAX-11   
(.  [Levy 82]).    -  -
        .    -
   ,   ,    , 

                                    285

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


 -   -  -   -   -  -
                            
 -    -    -    -    -    -
                                  
+---------+----------+---------+  +---------+----------+---------+
|      |        |      |  |       |        |       |
+---------+----------+---------+  +---------+----------+---------+

    ()             ()  
                                      

     9.28.   ""  -
                    

              
.      9.28.




      ,         
             
,   ,    ""    
    -     
 -  .       -
     ,     
,       -
  .  ""      -
   ,      . -
  ,       
 ,     .
       V       
  ,        . 
       ,      
        , -
  "  ,  ".    
     .  -
         ,     
    .     -
  ,      "  ,
 ".        
 ,         -
  .        -
          ,
      .   
         -
:      ,    -
 VAX/VMS ([Levy 82]).


                                    286



             -
     ,    V  UNIX. 
          
     .     ,  
         (    
  fork, exec  sbrk      ),
   ,          .
      ( 0), -
    ,      ,
  .            
,            
        .      
     -   ,   
        -
      (  -
  );        -
    .      -
          
   .
                ,
             -
;         
     .      
, ""          
,         ,   "" 
,         .    -
    ,     
  ,        -  .  
  ,      -
        .
           -
       . -,  
  fork,      ,  
        .
-,        
  ,      exec  -
    .     -
,        ,   
     ,       -
,          ,  ""   
    ,     .




  1.     mfree,   -
            .
  2.   9.1.2 ,       -
     ,               
      .   ,       ?
  3. ,       
         .       
         ?
  4.        , 
             ?      
        ,     ?
 *5. ,     ,   -

                                    287

          ,       .
                 ,
            ,   
     .   ,    ,  
     , ,         
        ?      ,  -
        ?
  6.      ,    9.10,  ,
            1 .
  7.   ,    9.11.    
     ,          
     .   -     -
      ?

               +----------------------------------+
               | main()                           |
               | {                                |
               |     f();                         |
               |     g();                         |
               | }                                |
               |                                  |
               | f()                              |
               | {                                |
               |     vfork();                     |
               | }                                |
               |                                  |
               | g()                              |
               | {                                |
               |     int blast[100],i;            |
               |     for (i = 0; i < 100; i++)    |
               |          blast[i] = i;           |
               | }                                |
               +----------------------------------+

                            9.29

  8.      ,   -
      9.29,   BSD 4.2 ?    - ?
  9.      fork - 
       -,     
         ?     
        ?
*10.    -  ,   -
     ,   .      ,
          ,  ,   -
        .      -
      .
 11.       ""     
     -   ,       
       .        ,
           ,     -
      ?
*12.      fork (),   -
          (    pfdata)  .
     ,    ""      
      ,     (, )  -
           .   
             . ,  -
     -         ,

                                    288

         ,   - -   . ,
      -    ,     
           ?
 13.          , 
          ?
*14.       .  
                ?
 15.  ,     
     ,    ,       ,
      .    .
 16.  ,      -  -
     ,    ,     , -
       ""  .    
     -  .
 17.   VAX-11        -  
             .
            ?
 18.   plock      -
          ( )      -
      .    ""     -
            . ,  
      ,      ,  
               .
                
     ?     ,     -
            ?
 19.    ,    9.30 ?   -
        ,        
              -
      .
    +------------------------------------------------------------+
    | struct fourmeg                                             |
    | {                                                          |
    |     int page[512];       /*  int  4  */  |
    | } fourmeg[2048];                                           |
    |                                                            |
    | main()                                                     |
    | {   for (;;)                                               |
    |     {                                                      |
    |         switch(fork())                                     |
    |         {                                                  |
    |         case -1:  /* -      |
    |                    * fork ---    */    |
    |         case 0:   /*  */                            |
    |              func();                                       |
    |         default:                                           |
    |              continue;                                     |
    | }   }   }                                                  |
    |                                                            |
    | func()                                                     |
    | {   int i;                                                 |
    |                                                            |
    |     for (;;)                                               |
    |     {                                                      |
    |         printf(" %d  \n",getpid());    |
    |         for (i = 0; i < 2048; i++)                         |
    |              fourmeg[i]290ge[0] = i;                       |
    |   }   }                                                    |
    +------------------------------------------------------------+

                                    289

Last-modified: Thu, 12 Feb 1998 07:20:20 GMT
: