:





              
   ;           
  ,    .   fork
  ,  exit    ,    wait
        
  .     -
   .     
exit  wait   ,      -
     exit  wait.   exec  
  "" ,    
   .   brk    -
  ;       -
    ,      -
.         
   shell    init.
      7.1     , -
   ,   ,  ,    -
 ,  .       sleep
 wakeup,   .  exec,  , -
      ,       4 
5.

+-----------------------------+---------------------+------------+
|  ,   |  ,  |     |
|     - |   - |  |
|                        |             |        |
+-------+-------+-------+-----+--+----+------+----+-+-----+------+
| fork  | exec  |  brk  |  exit  |wait|signal|kill|setrgr|setuid|
+-------+-------+-------+--------+----+------+----+-------+------+
|dupreg |detach-|growreg| detach-|                               |
|attach-|  reg  |       |  reg   |                               |
| reg   |alloc- |       |        |                               |
|       |  reg  |       |        |                               |
|       |attach-|       |        |                               |
|       |  reg  |       |        |                               |
|       |growreg|       |        |                               |
|       |loadreg|       |        |                               |
|       |mapreg |       |        |                               |
+-------+-------+-------+--------+-------------------------------+

     7.1.           
                     





            -
  UNIX     fork. ,  -
    fork,     (-), 
    (-).   -
  fork:

                                    179

    pid = fork();
        fork    , 
    ,    
pid.     pid   -
 ,   - pid   .  ,
     ,    -
,      fork.
             
:
 1.        .
 2.      .
 3.      .    
         ,    ,  -
      ,       
                
      .
 4.     ,   ,  
     ,     .
 5.       -
    ,    -    .

        fork, ,   ,
      ,        
.          -
           ;
             
     ,    
   ,        
 .  , , ,    -
   ,        
.   9    ,   
   ,        -
  fork     .
       7.2    .   
  ,      fork    
 .        -
      ,   ;    -
        ( -
,   ).    ,  fork -
 .          -
      ,    , -
 fork,   -  
 .          
,        -
 .      -
  ,   ,   .   -
  - ,   
  0.          ,
        
.
          -
 (),         
     ,     
  .  ,      
 ,       ,
          .  ,  
    ,     ,  
         ,  


                                    180

    +------------------------------------------------------------+
    |  fork                                              |
    |  :                             |
    |  :    - -|
    |                        (PID)      |
    |                         - 0         |
    | {                                                          |
    |       ;                    |
    |           - |
    |        (PID);                           |
    |    ,          |
    |     ;                                             |
    |       ,    |
    |       "";                                |
    |          ,   |
    |       ,  , -|
    |       ;                     |
    |           |
    |        (   );             |
    |              |
    |     ;                                                |
    |         (|
    |     , , , )  ;         |
    |           |
    |        ,  -  |
    |      ;                                   |
    |             , |
    |             ,   |
    |                  |
    |            ;                         |
    |     (     ) |
    |    {                                                       |
    |            "|
    |         ";                                      |
    |        (  );    |
    |                  /*    */             |
    |    }                                                       |
    |         /*           |
    |                                         */          |
    |    {                                                       |
    |             - |
    |          ;                      |
    |        (0);   /*  */                 |
    |    }                                                       |
    | }                                                          |
    +------------------------------------------------------------+

                     7.2.  fork


   .     ,    
      ,   , -
,    ,   ,   -
,         . -
,          -
 ,     ,   -
    (.  7.2.3,     
kill).
            

                                    181

,   ,     -
    . ,    "-
"         ( 
,    ),  ,    -
 ,   ,     -
 nice      .  
        .    
     ,  -
    ,     -
  ,    ,  -
          -
.      "" (.  -
 6.1).
             ,  -
    . -, 
      .  -
,      ,   1 , -
,         .  -,
            -
     chroot,      -
          
. ,      
     ,  ,  -
   ,         
,    .     
     ,        -
 ,             
    .  fork      
 dup:          
        ,  
.  dup, ,      
   ;  fork -   .
              -
   .     -
  ,     ,     
 dupreg        
  attachreg     .  -
          ,  
  ,     .    
 6.2.4  ,        -
      .    ,  
         
      ,   -
    fork.  , , -
  fork    ,     
    .
    ,         
;       .  
      ,     
           
fork.            -
,          -
      .       
       ,   -
 ,   .         -
          .  
       (2),  -
            
.     ( PC)   , -

                                    182

   ,   ,      -
   ""   ,  -
          ,      
   ,   . ,     -
 ,    0,  ,  , 
      ,    -
       ,   
 .     ,     -
  (.  ).

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

     7.3.       -
                    fork

                                    183


        ,   -
          fork,    
 "  ,   "   
 . ,    ,   -
       ""    -
  fork.       -
;     ,        
        .    
 fork    ,      -
,        2  -
,        .
      7.3     -
             
     fork. ,    
,           
  fork,            
    ,    .  
  ,    ,    , -
            
  ,    .   , 
  ()    ;       -
   ,      
   .
           7.4 ,  
         fork. -
    
  -         .  
    ,     -   
 -   .      -
    ,      
     ,   -  . 
          -
 fdrd, fdwt  c,        -
  argc  argv,           -
 .   ,    fork     
      ,   ,
 ,      (   , 
   )       -
.
            ,  ,
  rdwrt          -
        .  rdwrt  -
,      .      
          
  ,    ,    ,  
      .  ,  fdrd     
         ,  -
 ,  ,    fdwt,  -    ,
     ( ).   
              ,
     ,    -
        .   ,
,  , - ,       
,         ,  -
    ,       .
     ,      -
 (     read-write),   -


                                    184

    +------------------------------------------------------------+
    | #include                                          |
    | int fdrd, fdwt;                                            |
    | char c;                                                    |
    |                                                            |
    | main(argc, argv)                                           |
    |     int argc;                                              |
    |     char *argv[];                                          |
    | {                                                          |
    |     if (argc != 3)                                         |
    |          exit(1);                                          |
    |     if ((fdrd = open(argv[1],O_RDONLY)) == -1)             |
    |          exit(1);                                          |
    |     if ((fdwt = creat(argv[2],0666)) == -1)                |
    |          exit(1);                                          |
    |                                                            |
    |     fork();                                                |
    |     /*         */    |
    |     rdwrt();                                               |
    |     exit(0);                                               |
    | }                                                          |
    |                                                            |
    | rdwrt();                                                   |
    | {                                                          |
    |     for(;;)                                                |
    |     {                                                      |
    |          if (read(fdrd,&c,1) != 1)                         |
    |                return;                                     |
    |          write(fdwt,&c,1);                                 |
    |     }                                                      |
    | }                                                          |
    +------------------------------------------------------------+

     7.4.  ,     
                      


          . ,
, ,         -
      "ab". ,   
  "a",     ,       
    .      
"b"            
,    "ab"       "ba".  
    .
       ,     7.5,    
-       0  1 (-
     ).   -
    pipe     -
   to_par  to_chil.    fork    -
  :         -
  ,       .   -
    ( 1)     -
,    to_chil.      
        -
      (close)   ,   
            
     to_chil.      
      ,   -

                                    185

    to_par.          
    (  0)       
 to_chil.        -
      ,   -
     to_chil.     
    ,      
   to_par.  ,     , 

    +------------------------------------------------------------+
    | #include                                         |
    | char string[] = "hello world";                             |
    | main()                                                     |
    | {                                                          |
    |    int count,i;                                            |
    |    int to_par[2],to_chil[2];    /*      |
    |                                     */              |
    |    char buf[256];                                          |
    |    pipe(to_par);                                           |
    |    pipe(to_chil);                                          |
    |    if (fork() == 0)                                        |
    |    {                                                       |
    |       /*    */               |
    |       close(0); /*     */ |
    |       dup(to_chil[0]); /*     |
    |                                |
    |                            */                         |
    |       close(1); /*     */|
    |       dup(to_par[0]);  /*     |
    |                                  |
    |                            */                        |
    |       close(to_par[1]); /*     |
    |       close(to_chil[0]);    */                       |
    |       close(to_par[0]);                                    |
    |       close(to_chil[1]);                                   |
    |       for (;;)                                             |
    |       {                                                    |
    |          if ((count = read(0,buf,sizeof(buf))) == 0)       |
    |             exit();                                        |
    |          write(1,buf,count);                               |
    |       }                                                    |
    |    }                                                       |
    |    /*    */                 |
    |    close(1);  /*   - */|
    |    dup(to_chil[1]);                                        |
    |    close(0);                                               |
    |    dup(to_par[0]);                                         |
    |    close(to_chil[1]);                                      |
    |    close(to_par[0]);                                       |
    |    close(to_chil[0]);                                      |
    |    close(to_par[1]);                                       |
    |    for (i = 0; i < 15; i++)                                |
    |    {                                                       |
    |       write(1,string,strlen(string));                      |
    |       read(0,buf,sizeof(buf));                             |
    |    }                                                       |
    | }                                                          |
    +------------------------------------------------------------+

          7.5.   pipe, dup  fork

                                    186


    pipe -  ,     
.  ,         
  ,      to_chil     -
 ,       . 
       ,      
  to_par        ,  
   .        
.
            ,    -
   .  ,   , -
       fork    ,
   .     ,   
    ,      , 
     .    -  
 read ,     write,   
  ,           -
    .     
   ,        ,  
         ,    
           
    .       :
     read  write      -
   read   ,      -
 read-write. -
    15   ;   -
    ("end-of-file"),      
    ,    .  
         
,     ,         -
 .
          ,      
   .       .
-,       , -
     . -,   -
        -
  (     ).      
      ""  ,
    ,     
-  . ,  read      
      ,         -
  .        
 ,    ,      
     .      -
  ,         -
      .


       7.2 

          .  
   -  ,    kill, - 
.      V  ( )  UNIX  19 
,     :
  * ,        ,    
    ,       exit   signal  -
     death of child ( );
  * ,         
    ,     ,    -

                                    187

          ,     
    ,     (,  ),   -
       ,    
    ;
  *  ,        -
      ,         
          exec     
     (.  7.5);
  * ,         -
         ,     -
       (    ,
           ),   ,
           ,    -
        "reference"   lseek.  -
      ,         -
      ,          -
      ,     ,  -
         (*);
  *  ,  ,     , -
    ,   (alarm),     -
     ,   ,   ,
      kill;
  * ,    , ,   "-
    "  ( -    -
       )     "break"  "delete"  -
     ;
  * ,       .
                 
 .
        ,   ,   -
    ,     -
     .   ,  -
         , 
  .        ,
 ,    .     -
    (  ) .   
  ,       -
   . ,       "-
"      ,     -
         ,    -
,       .
       ,        
     ,        -
          -
 (.  7.6).     ,   -
          .  ,  
     ,   -
 .      ,     -
 ,      , -
         .    ,
      ,  -  
.
      7.7  ,     ,

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


                                    188


                                      
                                      
                                       +-------+
                                       |       |       
                                       |   1   |       
                            - |       |  + -  
                           , - ++------+  -+ - 
                                   | ^   ^- -+-
                - +-----+  +-------+ |- -|- - +
                , |     |  | +-------+   +---+  
                |     |  | |        |  
                 -|     |  | |               |
                  -|     v  v | -    |
      +-------+     |   +------++  -   ++------+
      |       |        +-->|       |       |       |
      |   9   |<-----------+   2   +------------>|   7   |
      |       |       |       | -|       |
      +-------+            ++------+           +-------+
                 |     ^                  - -
               |- - -|- - - - - - - - + - 
                            |     |- - - - - - - + + -- -+
            +---------------+     +------+    --------   
            |            |    -  + - - - 
            v                            |    -
  -+-------+                        +-+-----+  
  - |       |           |       | 
  - |   4   +----------------------->|   3   |  
  -|       |                        |       |
   +---+---+                        ++------+
   -   |                             |  ^  ^
      |                             |  |  |  
          |                             |  |  |  
          |                             |  |  +---+
          | -                  -    |  |      |
          |                 |  |      |  
          |                             |  |-  ++------+
          |                             |  |-|       | fork
          |                             |  |   |   8   |<-----
          |                             |  |     |       |
          |                             |  |     ++------+
          |                             |  |      |
          |                             |  |      | -
          |                             |  |  +---+ 
          |                             |  |  |     
          |                             |  |  |  ( 
          |                             |  |  |   )
          v                             v  |  v
      +-------+                        +---+---+
      |       |           |       |
      |   6   +----------------------->|   5   |
      |       |                        |       |
      +-------+                        +-------+
      ,                     ,
                               

     7.6.            
                      


                                    189

        . ,    
 " ",   .   ,  
   ,    signal.  
 issig      ,   
    ,       -
 .
    +------------------------------------------------------------+
    |  issig     /*    */       |
    |  :                             |
    |  : "",    - |
    |                      ,               |
    |                      "" -              |
    | {                                                          |
    |      (    , - |
    |         ,    |
    |     )                                              |
    |    {                                                       |
    |         ,  ;            |
    |        (  " ")                  |
    |       {                                                    |
    |           (   )          |
    |                ,    |
    |               ,  -|
    |              ;                                        |
    |              (   -|
    |           )                                        |
    |              ();                           |
    |       }                                                    |
    |           (  )     |
    |           ();                              |
    |        ()  ,  |
    |            ,   |
    |          ;                        |
    |    }                                                       |
    |     ();                                      |
    | }                                                          |
    +------------------------------------------------------------+

               7.7.   



    7.2.1

, , , . : , () . , , exit, signal , . signal: oldfunction = signal(signum,function); signum - , , , function - , oldfunction - . signal 1 0: function = 1, 190 signum ( , " ", 7.4), = 0 ( ), . - , - . , - , , . - . +------------------------------------------------------------+ | psig /* | | */ | | : | | : | | { | | ; | | ; | | ( signal, - | | - | | ) | | ; | | ( , - | | ) | | { | | | | ; | | /* | | */ | | , | | ; | | : | | , - | | ; | | : | | | | , - | | ; | | ; | | } | | ( - | | ) | | { | | "core"; | | "core" -| | ; | | } | | exit; | | } | +------------------------------------------------------------+ 7.8. ( 7.8), () , - . - , () (. 7.7). - 191 , . - , - - , , , - . , - . , , "delete" "break" , , - , , "" - . . (quit), , , , . , , - - , , (). , - , , . - , , , . - , , , , : 1. , - . 2. - . 3. , , - , - , - . , - ( ) - , - ( ). 4. : - , . , ; - , - , . ( 7.9), - (SIGINT) ( - kill). 7.10 - , - VAX 11/780. kill () ee; - , kill, chmk (- ) 10a. - 10c. . , - , - 10c . - 192 , 104, . 7.11 . - . - , - . , - signal. - +-------------------------------------------+ | #include | | main() | | { | | extern catcher(); | | signal(SIGINT,catcher); | | kill(0,SIGINT); | | } | | | | catcher() | | { | | } | +-------------------------------------------+ 7.9. +--------------------------------------------------------+ | **** VAX DISASSEMBLER **** | | | | _main() | | e4: | | e6: pushab Ox18(pc) | | ec: pushl $Ox2 | | # signal | | ee: calls $Ox2,Ox23(pc) | | f5: pushl $Ox2 | | f7: clrl -(sp) | | # -| | kill | | f9: calls $Ox2,Ox8(pc) | | 100: ret | | 101: halt | | 102: halt | | 103: halt | | _catcher() | | 104: | | 106: ret | | 107: halt | | _kill() | | 108: | | # | | | | 10a: chmk $Ox25 | | 10c: bgequ Ox6 | | 10e: jmp Ox14(pc) | | 114: clrl r0 | | 116: ret | +--------------------------------------------------------+ 7.10. 193 | | | | | | +-->+--------------------+ | | | | - | | | +-- --+ | | | | | | | | | | ---->| (10c)| +--------------------+<--+ - +--------------------+ | | - | | | | - | | | | - | | +--------------------+ - +--------------------+ - - +--------------------+ - +--------------------+ | | - | | | (10c) -|---------------- | (104) | +--------------------+ +--------------------+ | -| | -| | - | | - | | | | | +--------------------+ +--------------------+ 1 1 7.11. : , , , . , , - . , - 7.12. signal , sigcatcher. , nice, - (. 8), . - 5 , - nice . , ( kill). , , - , - , kill , - . , - . - signal , 194 +------------------------------------------------------------+ | #include | | sigcatcher() | | { | | printf("PID %d \n",getpid()); /* | | PID */ | | signal(SIGINT,sigcatcher); | | } | | | | main() | | { | | int ppid; | | | | signal(SIGINT,sigcatcher); | | | | if (fork() == 0) | | { | | /* */ | | sleep(5); /* | | 5 */ | | ppid = getppid(); /* - | | */ | | for (;;) | | if (kill(ppid,SIGINT) == -1) | | exit(); | | } | | | | /* , -| | */ | | nice(10); | | for (;;) | | ; | | } | +------------------------------------------------------------+ 7.12. , . , : 1. . 2. - , , , signal . 3. , . 4. , . , . , - nice , . ( ), , , - , , , , - . , , . , 195 . , : , - , . , , , , . , , . , , , - , - . BSD, , - - ; , , () . , - , . , - : - . , , - . ( longjump), . , - , , . , , , - , BSD. , . , - , - , longjump. , , . . - , , , . - , , - . , ( sleep), , - . , , sleep (. 2), , - . , " " , . , " ", , . " - " , - . , , , . - , , - , 7.4. , signal " " (death of child), , , . 7.4 . 196

    7.2.2

, UNIX (PID), - "", . , , - shell', , "delete" "break", "", . , - . ; . , , - , setpgrp. : grp = setpgrp(); grp - . fork - - . setpgrp , (. 10.3.5).

    7.2.3

kill. - : kill(pid,signum) pid ( - ), signum - . pid - : * pid - , pid. * pid 0, , , kill. * pid -1, , , - , kill ( . 7.6). , , - , , 0 1. * pid - , -1, , , pid. , , , , , ( ) , , kill - . , 7.13, - 10 . - - , , , , . getpid getpgrp , , pause - . kill - , . 197 +------------------------------------------------------------+ | #include | | main() | | { | | register int i; | | | | setpgrp(); | | for (i = 0; i < 10; i++) | | { | | if (fork() == 0) | | { | | /* */ | | if (i & 1) | | setpgrp(); | | printf("pid = %d pgrp = %d\n",getpid(),getpgrp());| | pause(); /* - | | */ | | } | | } | | kill(0,SIGINT); | | } | +------------------------------------------------------------+ 7.13. setpgrp "" , - , "" - .

    7.3

UNIX , exit. " - " (. 6.1), . : exit(status); status - , . - exit , ( - : (startup), - , exit main, ). , exit , ( ). status . - , - . ( ) 1 (init), - , . getty-, , , , - . 7.14 exit. , , - . , exit, 198 +------------------------------------------------------------+ | exit | | : , - | | | | : | | { | | ; | | ( , - | | ) | | { | | , , | | ""; | | ; | | } | | ( -| | close); | | ( iput); | | , | | ( freereg); | | ; | | ( -| | ); | | - | | init (1); | | - , | | init " "; | | " " -| | ; | | ; | | } | +------------------------------------------------------------+ 7.14. exit , (. 10.3.5), , - , "". , shell' - , " " (Ctrl-d), - , , exit, "". , , , , (, exit) - . , , . , close iput - ( ). , - ( detachreg) . exit (status), - . 7.4 wait , - . - , - , , - , -, - 199 . - , - . , - 1 (init). , 1 . - , - init " " , (. 7.9); , . wait. , ; , - . , 7.15, , pause, . - PID , - status. exit , main. - , .

    7.4

, wait. : +------------------------------------------------------------+ | main() | | { | | int child; | | | | if ((child = fork()) == 0) | | { | | printf("PID %d\n",getpid()); | | pause(); /* | | */ | | } | | /* */ | | printf("PID %d\n",child); | | exit(child); | | } | +------------------------------------------------------------+ 7.15. exit pid = wait(stat_addr); pid - (PID) , stat_addr - , exit , . wait 7.16. - , , - . , , , - , exit, , wait. , - 200 exit (status) , , . , - , - - , , , . . , wait, , - , . , - wait: - . , " ", . " " - - : * ( - ) , wait, issig . issig ( 7.7) - " " "". longjump sleep, wait. wait, , - ( , ), - , , wait, +------------------------------------------------------------+ | wait | | : | | status, | | | | : | | exit | | { | | (, wait, - | | ) | | (); | | | | (;;) /* */ | | { | | (, wait, -| | , ) | | { | | ; | | -| | ;| | , - | | ; | | ( , | | exit, ); | | } | | ( ) | | ; | | , -| | ( ); | | } | | } | +------------------------------------------------------------+ 7.16. wait 201 , . * , - , . * , wait, , , - , . , , 7.17, , . , ( - , argc 1). 15 , - i, - . , wait , , - , - exit. , . , exit, , , exit 8-15 ret_code wait -. , ret_code - , 256*i, i - , ret_val . ( argc > 1), signal - " ". , , wait, , exit: - - exit, " "; , , - . +------------------------------------------------------------+ | #include | | main(argc,argv) | | int argc; | | char *argv[]; | | { | | int i,ret_val,ret_code; | | | | if (argc >= 1) | | signal(SIGCLD,SIG_IGN); /* | | */ | | for (i = 0; i < 15; i++) | | if (fork() == 0) | | { | | /* - */ | | printf("- %x\n",getpid()); | | exit(i); | | } | | ret_val = wait(&ret_code); | | printf("wait ret_val %x ret_code %x\n",ret_val,ret_code);| | } | +------------------------------------------------------------+ 7.17. wait - " " 202 , , "" ; , , , , wait , . - , " ", , wait , . wait , -1. - , - , , . UNIX exit wait " ". exit . - wait , , , , . ; - "" wait. - (init) , wait, - exit , - , . exit wait , , , , , wait. , wait, - - , - exit. , 7.18. - , , read - . , - - , , , , - - . , +------------------------------------------------------------+ | #include | | main(argc,argv) | | { | | char buf[256]; | | | | if (argc != 1) | | signal(SIGCLD,SIG_IGN); /* | | */ | | while (read(0,buf,256)) | | if (fork() == 0) | | { | | /* - | | - (buf) */ | | exit(0); | | } | | } | +------------------------------------------------------------+ 7.18. "- " 203 signal, " - ", , - , . - .

    7.5

exec - , - . - , - , . : execve(filename,argv,envp) filename - , argv - - , , envp - , . - exec , execl, execv, execle .. , - main(argc,argv) , +------------------------------------------------------------+ | exec | | : (1) | | (2) | | (3) | | : | | { | | ( namei); | | , - | | ; | | , | | ; | | , , | | ; | | ( , ) | | ( detachreg);| | ( , ) | | { | | ( allocreg); | | ( attachreg); | | ( | | loadreg); | | } | | , , - | | ; | | setuid-, ; | | | | ( ); | | ( iput); | | } | +------------------------------------------------------------+ 7.19. exec 204 argv , exec. , , "=" , . - , - environ, -. 7.19 exec. namei, , , - . , , - ( ). 7.20 - , . - : 1. , , - , - " ", . 2. , , : , , , . 3. , "" (, ), - . 4. , , - , . +---------------------------+ | | | | | -| | | +---------------------------+ | | 1- | | | | +---------------------------+ | | 2- | | - | | - +---------------------------+ - | - | - | - | - +---------------------------+ - | | n- | | | | +---------------------------+ 1 | (, ) | +---------------------------+ 2 | | - +---------------------------+ - | - | - | - | - +---------------------------+ n | | +---------------------------+ | | +---------------------------+ 7.20. 205 , - . ( ), , - . , PDP 11/70 , , , - 128 64 (**), - , - 9. . , - , . , . , , , - , - , . exec , - , . . ( , - ), ( ), , ( - ). . , , , , - exec , . - , . , , , ( ). exec - , , detachreg. , - . . , - , , , . , - ( allocreg, attachreg loadreg, ). : --------------------------------------- (**) PDP 11 " " , ; - - . - , . 206 , , , ("bss"). - . "bss" ( growreg) . exec. - , - . - . - , . . , - (sp) (pc): - . setuid- , 8 11, . , iput, - , namei exec. namei iput exec , , ; - exec , . . , , ; , - . - . +-------------------------------------------------------+ | main() | | { | | int status; | | if (fork() == 0) | | execl("/bin/date","date",0); | | wait(&status); | | } | +-------------------------------------------------------+ 7.21. exec ( 7.21), -, exec. fork - - - . - - exec , "/bin/date" "date", - , exec. "/bin/date" , , , - , , , . exec, argv, (- ). , , - (***). "/bin/date" "date" , , . , , "/bin/date", 207 --------------------------------------- (***) , V (mv), (cp) (ln), , . , . - . ( "date") . exec, - - "date"; "date" , -, , exit. , - , , . - : - . , - , , . , - . - , - , , . +------------------------------------------------------------+ | #include | | main() | | { | | int i,*ip; | | extern f(),sigcatch(); | | | | ip = (int *)f; /* ip -| | f */ | | for (i = 0; i < 20; i++) | | signal(i,sigcatch); | | *ip = 1; /* f */ | | printf(" ip\n"); | | f(); | | } | | | | f() | | { | | } | | | | sigcatch(n) | | int n; | | { | | printf(" %d\n",n); | | exit(1); | | } | +------------------------------------------------------------+ 7.22. , ( 7.22), ip f - 208 . , , , , - ip - , . AT&T 3B20 SIGBUS, . , main. , , - ( ), , - f. f 1. main, f, , . SIGILL . - . , UNIX - , PDP : , . - . - . - , , - . - , . , exec - , , - , "- " . , xalloc - (. 7.23). xalloc, , - . (- allocreg), ( attachreg), ( loadreg) (read-only). , - - , . - ( , ) . xalloc , , - detachreg exit exec, . , , . - , . , allocreg ( 6.5.2) , - , , exec ( namei). - - exec ( iput), , - , 1. ( unlink), ( ). 209 , , ; +------------------------------------------------------------+ | xalloc /* | | */ | | : | | : | | { | | ( )| | ; | | ( , - | | ) | | { | | /* ... | | */ | | ; | | ( ) | | { | | /* , | | | | */ | | ; | | ; | | ( | | ); | | ; | | ; | | } | | ( attachreg);| | ; | | ; | | } | | /* -- - | | */ | | ( allocreg); /* | | - | | */| | ( "") | | ; | | , | | ( attachreg); | | ( -| | ) | | /* 9 */ | | /* -| | */ | | ( | | loadreg); | | | | "read-only"; | | ; | | } | +------------------------------------------------------------+ 7.23. , . 0, 210 , : , exec, . - , allocreg , . exit exec , ( freereg), - "". +----------------+ - +----------------+ | - | , - | - | | - | | - | | - | /bin/date | - | | - | 0 +----------------+ | - | | | | - | -- - - - - -|- | | - | | | /bin/who | +----------------+ - +----------------+ | -|- - - - - -+ | - | | /bin/date| | - | | <+-----------+ | - | +----------------+ | +----------------+ | - | | | | | - | +-----------+- | | - | | /bin/date | | - | -+----------------+ | - | | - | | - | | - | +----------------+ +----------------+ 7.24. , 7.21, exec "/bin/date" - . "/bin/date" , ( 7.24) exec 1. "/bin/date" , - detachreg freereg, 0. , , - exec 0 . , , exec "/bin/who", , - "/ bin/date". - "/bin/who", "/bin/date". , "/bin/who", - . - , , , . , - exec. - 211 ( ) chmod - "sticky-bit", . , "sticky-bit", - , , exit exec, - 0. , 1, . ( ), - , . , - . - , ; , , (. 9). , - , "sticky-bit" ( , "" ), - : 1. , - , . 2. (chmod), "sticky-bit", . 3. (unlink), - , ; , , . , , . 4. , . - . 5. , , - , "sticky-bit", - . , , . "sticky-bit" , . - . , ( ); open, unlink umount ( 1, 3 4, ). exec , - . sh script shell ( ), shell' ( exec) "script". , , - . , , "" , "" , . "" - 212 , . exec fork; , fork - - , exec . , - ? , fork exec , UNIX fork (. [Ritchie 84a], .1584). , fork exec , - - , "" - . , , - 7.8.

    7.6

, - : () - setuid ( "set user ID" - - , - ). , . , kill. - , exec setuid setuid . setuid , - setuid. setuid , , - , . - , , - , - . , . setuid: setuid(uid) uid - . . , , - , uid , , - . , uid - , uid . . - ( fork) exec. 7.25 , - setuid. , , - , "maury" ( 8319) setuid; - . , "mjb" ( 5088) "maury" , . "mjb" - 213 : uid 5088 euid 8319 fdmjb -1 fdmaury 3 after setuid(5088): uid 5088 euid 5088 fdmjb 4 fdmaury -1 after setuid(8319): uid 5088 euid 8319 getuid geteuid - , +------------------------------------------------------------+ | #include | | main() | | { | | int uid,euid,fdmjb,fdmaury; | | | | uid = getuid(); /* UID */ | | euid = geteuid(); /* UID */| | printf("uid %d euid %d\n",uid,euid); | | | | fdmjb = open("mjb",O_RDONLY); | | fdmaury = open("maury",O_RDONLY); | | printf("fdmjb %d fdmaury %d\n",fdmjb,fdmaury); | | | | setuid(uid); | | printf("after setuid(%d): uid %d euid %d\n",uid, | | getuid(),geteuid()); | | | | fdmjb = open("mjb",O_RDONLY); | | fdmaury = open("maury",O_RDONLY); | | printf("fdmjb %d fdmaury %d\n",fdmjb,fdmaury); | | | | setuid(uid); | | printf("after setuid(%d): uid %d euid %d\n",euid, | | getuid(),geteuid()); | | } | +------------------------------------------------------------+ 7.25. setuid "mjb" , , 5088 8319. - "mjb" ( - (8319), ), "maury". setuid, ("mjb") - , , "mjb": 5088. - "mjb", - , , "maury". , - , setuid (8319), - 5088 8319. , , - setuid - , . "maury" : uid 8319 euid 8319 fdmjb -1 fdmaury 3 after setuid(8319): uid 8319 euid 8319 fdmjb -1 fdmaury 4 214 after setuid(8319): uid 8319 euid 8319 - 8319: "maury", "mjb". , - , setuid; . setuid ( ), .. , - . , setuid, (login). setuid , , login . , , , , setuid, , ( "/etc/passwd"). login shell, . setuid- , mkdir. 5.8 , - , . , - , mkdir setuid-, . mkdir , , mknod, - .

    7.7

brk . : brk(endds); endds - ( ). , - : oldendds = sbrk(increment); oldendds - , increment - , oldendds - . Sbrk - , brk. , - , ; , - . , - - , , , (- 7.26). , growreg, (, ) , . ; , ( 9). brk , - ; , 215 , . +------------------------------------------------------------+ | brk | | : | | | | : | | | | { | | ; | | ( ) | | ( -| | ) | | { | | ; | | (); | | } | | ( growreg); | | ; | | ; | | } | +------------------------------------------------------------+ 7.26. brk 7.27 , brk, , AT&T 3B20. signal - (segmentation violation), sbrk - - . , , , , , . , sbrk , 256 ; , .. , 3B20, - . , , , , , .. "" (, - ). - , ( 7.27): sbrk - 140924, , 388 , 3B20 2 . - , , , 141312. - 256, 141180 , , . , , 141312. - sbrk , , 2 - , 143360, . , sbrk, . - , , . 216 , - , , brk. , exec, - +-------------------------------------------------------+ | #include | | char *cp; | | int callno; | | | | main() | | { | | char *sbrk(); | | extern catcher(); | | | | signal(SIGSEGV,catcher); | | cp = sbrk(0); | | printf("original brk value %u\n",cp); | | for (;;) | | *cp++ = 1; | | } | | | | catcher(signo); | | int signo; | | { | | callno++; | | printf("caught sig %d %dth call at addr %u\n", | | signo,callno,cp); | | sbrk(256); | | signal(SIGSEGV,catcher); | | } | +-------------------------------------------------------+ +-------------------------------------------+ | original brk value 140924 | | caught sig 11 1th call at addr 141312 | | caught sig 11 2th call at addr 141312 | | caught sig 11 3th call at addr 143360 | | ...( 10- | | sbrk) | | caught sig 11 10th call at addr 143360 | | caught sig 11 11th call at addr 145408 | | ...( 18- | | sbrk) | | caught sig 11 18th call at addr 145408 | | caught sig 11 19th call at addr 145408 | | - | | - | +-------------------------------------------+ 7.27. , brk, . - , - . - , - . - , . 217 +------------------------------------------------------------+ | /* */ | | while (read(stdin,buffer,numchars)) | | { | | /* */ | | if (/* & */) | | amper = 1; | | else | | amper = 0; | | /* , | | shell */ | | if (fork() == 0) | | { | | /* - ? */ | | if (/* */) | | { | | fd = creat(newfile,fmask); | | close(stdout); | | dup(fd); | | close(fd); | | /* stdout */ | | } | | if (/* */) | | { | | pipe(fildes); | | | +------------------------------------------------------------+ 7.28. shell .

    7.8 SHELL

, - shell. , , , . 7.28 shell, , - . Shell - . , shell', , , - (. 10). shell (, cd, for, while ..), , ; - . - , : who grep -n include *.c ls -l 218 +------------------------------------------------------------+ | if (fork() == 0) | | { | | /* */| | close(stdout); | | dup(fildes[1]); | | close(fildes[1]); | | close(fildes[0]); | | /* - | | */ | | /* - | | */ | | execlp(command1,command1,0); | | } | | /* */ | | close(stdin); | | dup(fildes[0]); | | close(fildes[0]); | | close(fildes[1]); | | /* | | */ | | } | | execve(command2,command2,0); | | } | | /* | | * ... | | * - ,| | * | | * / | | if (amper == 0) | | retid = wait(&status); | | } | +------------------------------------------------------------+ 7.28. shell () Shell "" (fork) , - , . (shell) . ( ), nroff -mm bigdocument & shell (&) amper. shell , , - wait, . , - fork - , shell'. , - , nroff -mm bigdocument > output - ; 219 (, ), - - . - - dup . - . shell . +-----------+ | Shell | +-----+-----+ wait | ^ | | +-----+-----+ exit | wc | +-----+-----+ read | ^ | | +-----+-----+ write | ls - l | +-----------+ 7.29. , - ls -l|wc , shell , . , : ls -l|wc - - . - ; . "" - (ls): , - , , ( - ) . (wc) "" (ls) , shell' (. 7.29). (wc) , - . - . - , . (wc), : , - wc, . Shell .

    7.9

, , " ". , : . . - , , , , . , . UNIX 220 - ( ). , - , (, "/unix" , ). - ( start, 7.30). . , -- , , .. - , , . , - . "", fork , - +------------------------------------------------------------+ | start /* */| | : | | : | | { | | ; | | - ; | | 0; | | 1; | | { | | /* 1 */ | | ; | | | | init; | | - | | ; | | - | | , -| | ; | | : | | ; | | /* init -- | | * , | | * init "/etc/init" | | * "" , - | | * | | */ | | } | | /* */ | | ; | | /* , - | | * - | | * . | | * ; -| | * , -| | * . | | */ | | , ; | | } | +------------------------------------------------------------+ 7.30. 221 . , 1, - , - . - : 1. 1 , "- " . , - , , 1 - . , 1, - exec, "/etc/init". 1 init, - . , , - exec, 1 ? - , - , - , exec , . , init, - exec - . init ( 7.31) , - , - . , , - init "/etc/inittab". "id" ( , - . .), (. 7.43) , (. 7.32). init - , - , , , , . , - ( 2) init - getty-, , . , getty-, login, - shell (. 10). init - (wait), , "" , "" - . UNIX , - , . - , . , , , - , - .. init , , . , - . . (, vhand), . , , , . - , , . , 222 +------------------------------------------------------------+ | init /* init, | | " 1" */ | | : | | : | | { | | fd = open("/etc/inittab",O_RDONLY); | | while (line_read(fd,buffer)) | | { | | /* */ | | if (invoked state != buffer state) | | continue; /* while */ | | /* | | */ | | if (fork() == 0) | | { | | execl(" "); | | exit(); | | } | | /* init */ | | /* while */ | | } | | | | while ((id = wait((int*) 0)) != -1) | | { | | /* ; | | * , - | | * */ | | /* , - | | * */ | | } | | } | +------------------------------------------------------------+ 7.31. init +------------------------------------------------------------+ | : , , , | | | | | | '#' | | | | co::respawn:/etc/getty console console # | | 46:2:respawn:/etc/getty -t 60 tty46 4800H # | +------------------------------------------------------------+ 7.32. inittab , , , - , .

    7.10

, . - fork , - , . - fork , - 223 , , , . - exit, - " ". - -, wait. exec - , - . , , - . "sticky-bit" - , . - , , setuid setuid-. brk . signal - . - - - . , kill, , , setpgrp. shell init , , - . Shell - , - , , , - , . init , , - . - , init - , "/etc/inittab".

    7.11

1. , 7.33. - - . +------------------------------------+ | main() | | { | | printf("hello\n"); | | if (fork() == 0) | | printf("world\n"); | | } | +------------------------------------+ 7.33. , fork - 2. , 7.34, 7.4. 3. , 7.5 - , , - . , , - ? 4. , - 224 ? ( , - .) ? 5. kill. 6. 7.35 " - " . ? 7. , , . - "core" , , . . - , "core". , - ? , ? 8. ( 7.12), , , . , , : +------------------------------------------------------------+ | #include | | int fdrd,fdwt; | | char c; | | | | main(argc,argv) | | int argc; | | char *argv[]; | | { | | if (argc != 3) | | exit(1); | | fork(); | | | | if ((fdrd = open(argv[1],O_RDONLY)) == -1) | | exit(1); | | if (((fdwt = creat(argv[2],0666)) == -1) && | | ((fdwt = open(argv[2],O_WRONLY)) == -1)) | | exit(1); | | rdwrt(); | | } | | rdwrt() | | { | | for (;;) | | { | | if (read(fdrd,&c,1) != 1) | | return; | | write(fdwt,&c,1); | | } | | } | +------------------------------------------------------------+ 7.34. , - - * , - ; 225 * , - signal . 9. , , . , - ? BSD. *10. , , longjump. ; , V , - ( ). BSD - . ? +------------------------------------------------------------+ | #include | | main() | | { | | extern catcher(); | | | | signal(SIGCLD,catcher); | | if (fork() == 0) | | exit(); | | /* */ | | pause(); | | } | | | | catcher() | | { | | printf("- \n"); | | signal(SIGCLD,catcher); | | } | +------------------------------------------------------------+ 7.35. , " " 11. mkdir mknod, - link, - "." ".." . . , mkdir - ? SIGKILL, ? mkdir. 12. - ( - , ), - . ? *13. , . - 226 . (, "break".) , ? *14. , - , . - , - () . ? , ( - ) ( - ), ? 15. newpgrp(pid,ngrp); pid - ngrp ( ). , . 16. : wait - . 17. nowait(pid); pid - , , . , , - , . ? " ". 18. - (startup), main, - . - exit, startup main. , startup exit (- - ) ? 19. , wait, exit ? , - - exit() exit(n). exit , , wait ? . 20. , , , - 7.36 exec . , ? +----------------------------------+ | main(argc,argv) | | int argc; | | char *argv[]; | | { | | execl(argv[0],argv[0],0); | | } | +----------------------------------+ 7.36 21. exec ( - ) . , 7.37 ? , "a.out" - 227 , , - 7.36 ? 22. , "read-only" ( ), , , . . (: " - ".) ? - ? 23. open, chmod, unlink unmount , "sticky-bit" ? , , , ? 24. , "/etc/passwd", . passwd , . ? +-----------------------------------------------------+ | main() | | { | | if (fork() == 0) | | { | | execl("a.out",0); | | printf(" exec\n");| | } | | } | +-----------------------------------------------------+ 7.37 *25. , , setuid- . 26. , "a. out" - : +-----------------------------------------------------+ | main() | | { | | char *endpt; | | char *sbrk(); | | int brk(); | | | | endpt = sbrk(0); | | printf("endpt = %ud sbrk\n", (int) endpt); | | | | while (endpt--) | | { | | if (brk(endpt) == -1) | | { | | printf("brk %ud | | \n",endpt); | | exit(); | | } | | } | | } | +-----------------------------------------------------+ 7.38 228 chmod 4777 a.out chown root a.out chmod "" setuid (4 4777); "root" . - ? 27. , - 7.38 ? . 28. malloc brk, free , malloc. : ptr = malloc(size); free(ptr); size - , , ptr - - . free, ptr malloc. - . 29. , - 7.39 ? - , . +-----------------------------------------------------+ | main() | | { | | int i; | | char *cp; | | extern char *sbrk(); | | | | cp = sbrk(10); | | for (i = 0; i < 10; i++) | | *cp++ = 'a' + i; | | sbrk(-10); | | cp = sbrk(10); | | for (i = 0; i < 10; i++) | | printf("char %d = '%c'\n",i,*cp++); | | } | +-----------------------------------------------------+ 7.39. , sbrk 30. shell , - , ? , , - shell ? ? 31. shell ">>" - , , : run >> outfile "outfile" ( ) . - , . 32. shell , exit, "", - "" ( ). , , 7.40, "truth". , , shell - : while truth 229 +------------------+ | main() | | { | | exit(0); | | } | +------------------+ 7.40 do truth & done 33. 7.29: - - ? 34. shell . , , . 35. PATH , shell' . execlp execvp PATH , , "/". . *36. , shell - , PATH. , shell ? 37. shell cd ( ) ? shell : cd pathname & ? 38. "delete" "break", , shell', . - , shell', . shell ( 7.28) ? 39. nohup command_line "" - (quit) , (command_line). shell ? 40. shell: nroff -mm bigfile1 > big1out & nroff -mm bigfile2 > big2out shell ( 7.28). , nroff - ? shell ? 41. shell' - : "Bus error - core dumped" ( - ). , - ; shell , ? 42. 1 init. - , init, - . , , , . , init 230 2 ( ), init 2 . shell init. , - init ? 43. "/etc/inittab" , - . , getty- "respawn" (), , init getty-, - . , init getty-, . init ? 44. . , : , , , . , , , ( ). - ? - 231

Last-modified: Thu, 12 Feb 1998 07:19:53 GMT
: