:





              -
  ()   ,  ,  -
       ,   -
    .       
UNIX      .    -
      ;      
 .        
     ,     -
    ,   .
      ,   ,      
  :  , ,  time -
  ,       , 
date      .     -
          -
   ,      -
.        ,  -
        , - , 
50-100   .      ()  -
  .      -
     ,      UNIX,
     ,   , -
     .




        UNIX      -
,         "      
".         
   ,         -
         ,  . 
  ,         
    .       -
  ,       -
.



            
  ( 8.1),       -
   ,    "" 
"    ,    ".  -
,    ,   ,     -
,    .      -
  , ,     (), -
    ,       "  
"  .         
  ,        ,
         ;    
      .


                                    232

    +------------------------------------------------------------+
    |  schedule_process                                  |
    |  :                             |
    |  :                            |
    | {                                                          |
    |      (       -|
    |     )                                                |
    |    {                                                       |
    |        (      )|
    |                 -|
    |              ;                                |
    |        (          |
    |        )                                         |
    |            ;                            |
    |           /*       - |
    |           /*                                          |
    |            */                                              |
    |    }                                                       |
    |           - |
    |     ;                                                   |
    |        , - |
    |       ;                                   |
    | }                                                          |
    +------------------------------------------------------------+

        8.1.    






               , 
 .        ,
       .    -
   ( 8.2):      
    .       -
,         -
.        , -
        ,   
          sleep.  -
       ,  -
       .     -
              :
       ,  -
          (.
 7.2.1).
             
   8.2   ,    -
   (  )        
  .   ,  -, -
  ,      ,   
    ,       
1, 3, 2  1 , ,          
  ,        ,
   ,     
  4, 0  2 , .    
      (*).
          :

                                    233

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

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

            
      
     |                 +----------------------+
     |                 |               | +--+
     |                 |              |-+  |
     |    +----------------------+ +--+
     |                 | -,| +--+ +--+ +--+
     |       |     |-+  +-+  +-+  |
     |                 +----------------------+ +--+ +--+ +--+
     |                 |              | +--+ +--+
     |                 |                |-+  +-+  |
     |                 +----------------------+ +--+ +--+
     |                 |              | +--+
     |                 |               |-+  |
     |                 +----------------------+ +--+
     |                 +----------------------+
     |                 |    -| +--+ +--+ +--+ +--+
     |                 |                |-+  +-+  +-+  +-+  |
     |      +----------------------+ +--+ +--+ +--+ +--+
     |                 |      |
     |       |              |
     |                 +----------------------+
     |                 |    | +--+ +--+
     |                 |               |-+  +-+  |
     v                 +----------------------+ +--+ +--+
     +----------------------+
     ^                 |     0   |
     |                 +----------------------+ +--+ +--+ +--+ +--+
     |                 |     1   |-+  +-+  +-+  +-+  |
     |                 +----------------------+ +--+ +--+ +--+ +--+
     |                 |          -           |
     |                 |          -           |
     |                 +----------------------+ +--+
   |     n   |-+  |
          +----------------------+ +--+

              8.2.   

                                    234


    ,         
     . -, ,   -
     ,    ,       -
     -.   -   
    ,           .
         ,    -
    ,           ,   
     ; ,     .
  *             -
        .         
    ,         -
    ,            -
           .  , -
     ""      ,  -
         .
  *             1  (
     V)       ,  -
            ,   -
          .

              -
;          -
 ,      ,    
        (). 
 V          
  ,    (decay):

    decay() = /2;

      , 
  ",    ",  

     = (/2) + (   )

    "   "   ,
        . -
      . -
         -
    :       , 
         ;  
      "    "   
   .
             
,    ,     ,  -
      8.3.      8.2    
,   1,  ,   -
.     ,      
,      .    
       ,    -
     ,   
,   ,          -
   .
            
,        -
.          ,   
   ( ,   ,   
     , , ,  ,  


                                    235

  ),    ,    -
       .    -
 ,      ,    
    ,     -
 .         
  ,       -
,    .       
      ,     -
  : ,  ,    
  (       ) 
      ,      -
  (.  [Thompson  78],  .1937).    
 ,       ,  -
    0  1 .      -
.        

            
      
     |                 +----------------------+
     |                 |               | +--+
     |                 |              |-+  |
     |    +----------------------+ +--+
     |                 | -,| +--+ +--+ +--+
     |       |     |-+  +-+  +-+  |
     |                 +----------------------+ +--+ +--+ +--+
     |                 |              | +--+ +--+
     |                 |                |-+  +-+  |
     |                 +----------------------+ +--+ +--+
     |                 |              | +--+
     |                 |               |-+  |
     |                 +----------------------+ +--+
     |                 +----------------------+
     |                 |    -| +--+ +--+ +--+ +--+
     |                 |                |-+  +-+  +-+  +-+  |
     |      +----------------------+ +--+ +--+ +--+ +--+
     |                 |      |
     |       |              |
     |                 +----------------------+
     |                 |    | +--+ +--+
     |                 |               |-+  +-+  |
     v                 +----------------------+ +--+ +--+
     +----------------------+ +--+
     ^                 |   0     |-+  |<- - - - - -+
     |                 +----------------------+ +--+            -
     |                 |                      | +--+ +--+ +--+ ++-+
     |                 |     1   |-+  +-+  +-+  + +  |
     |                 +----------------------+ +--+ +--+ +--+ +--+
     |                 |          -           |
     |                 |          -           |
     |                 +----------------------+ +--+
   |     n   |-+  |
          +----------------------+ +--+

     8.2.        


   ,          
      ; ,   ,  
    .

                                    236




      8.4      A, B  
C    V   :       -
  60,      -
    ,     60   , -
     ,     ,
  .       :


          A          B          C
      |        -     -    
  0 --+--                  -                 -
      |    60           0  - 60           0  - 60           0
      |                 1  -                 -
      |                 2  -                 -
      |                   -                 -
      |                   -                 -
  1 --+--               60 -                 -
      |    75           30 - 60           0  - 60           0
      |                    -              1  -
      |                    -              2  -
      |                    -                -
      |                    -                -
  2 --+--                  -              60 -
      |    67           15 - 75           30 - 60           0
      |                    -                 -              1
      |                    -                 -              2
      |                    -                 -              
      |                    -                 -              
  3 --+--                  -                 -              60
      |    63           7  - 67           15 - 75           30
      |                 8  -                 -
      |                 9  -                 -
      |                   -                 -
      |                   -                 -
  4 --+--               67 -                 -
      |    76           33 - 63           7  - 67           15
      |                    -              8  -
      |                    -              9  -
      |                    -                -
      |                    -                -
  5 --+--                  -              67 -
      |    68           16 - 76           33 - 63           7
      |                    -                 -
      |                    -                 -

            8.4.   


     = decay() = /2;
    :
     = (/2) + 60;

 ,     A      
,        1 :     
 60         -
   A  ,    ( 0  60).

                                    237

      ,    -
      ,      B.  
       60   
        B,     
        .  
 ,      -
.
         ,    8.5,
 ,       .   
 A,     "   ",    ,
             -
         ( 8.5). 
     A   "   -
"     B,       
  A ( 8.5).       -
      ( ,       ),
   (A  B)       
    ,    B      
  - ,       (-
  8.5  8.5).   ,    A   B,
  A    "    "  
   ( 8.5) -   ,   -
     .
      6.4.3    ,       -
   :      -
           ,
             
      .   ,   -
   ,          -
  .    ,     
   ,     ,
          
    .        -
      ,      
    .       
     ,      
,            ,
   .




            
nice:

    nice(value);

 value - ,      -
 :

     = (/) + ( ) + ( nice)

  nice      nice  -
     ,    -
   ,   .  -
,      ,   -
 . ,     nice    ,
         -
, ", " (nice)    -

                                    238

          +---------+      +---------+      +---------+
     ^ 60 +---------+      +---------+      +----B----+
     |    +---------+      +---------+      +---------+
     |    +---------+      +----B----+      +----A----+
     +---------+      +---------+      +---------+
   +---------+      +----A----+      +---------+
  - +---------+      +---------+      +---------+
       +----A----+      +---------+      +---------+
     |    +---------+      +---------+      +---------+
     |        ()              ()              ()

          +----B----+      +-A-----B-+      +----B----+
       60 +----A----+      +---------+      +---------+(
          +---------+      +---------+      +---------+
          +---------+      +---------+      +---------+
          +---------+      +---------+      +---------+
          +---------+      +---------+      +---------+
          +---------+      +---------+      +---------+
          +---------+      +---------+      +---------+
          +---------+      +---------+      +---------+
              ()              ()              ()


     8.5.       -
                  


,   .    nice   -
     fork.  nice   
   ;      nice  -
 .      ,    -
      , -
          ,     
   ,        (kill)  
  .




            -
    ().  ,  -
   , ,    
.   ,          
    ,     -
          
   .        ,  
"    " (Fair Share Scheduler)  -
            Indian  Hill    AT&T  Bell
Laboratories [Henry 84].
     "    "   
   ,   , -
           .
         ,  -
  ,     .  ,  ,  
    ,      
25%    ,  , 1, 2, 3  4 ,  
,        .  ,   -
     ,      
        10%   (

                                    239

  10       ).  -
         -
               -
    ,  3        
    4          -
.             , -
  ,    ,    
.
         ,     .
         -  "-
    ".     
 ,       -
,      .       
          
          .
         
      .      
      ,      
 .
           ( 8.6),   -
        (A),   -  (B  C). , 
      A,     
     ,   
 .         -
 B  C    . ,    
  B.         -
 B   60,       
    B  C.  ,     
C  ,  75 (   8.4),    
  A   74.     
 :      A, B, A, C, A, B  ..



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


                                    240

          A          B          C
      |    - -  -- - -  -- - -  -
      |     - - -  - - -  - -
      |          .  -       .  -       . 
      |              -           -          
  0 --+--                  -                 -
      |    60     0     0  - 60     0     0  - 60     0     0
      |           1     1  -                 -
      |           2     2  -                 -
      |                  -                 -
      |                  -                 -
  1 --+--         60    60 -                 -
      |    90     30    30 - 60     0     0  - 60     0     0
      |                    -        1     1  -              1
      |                    -        2     2  -              2
      |                    -               -              
      |                    -               -              
  2 --+--                  -        60    60 -              60
      |    74     15    15 - 90     30    30 - 75     0     30
      |           16    16 -                 -
      |           17    17 -                 -
      |                  -                 -
      |                  -                 -
  3 --+--         75    75 -                 -
      |    96     37    37 - 74     15    15 - 67     0     15
      |                    -              16 -        1     16
      |                    -              17 -        2     17
      |                    -                -             
      |                    -                -             
  4 --+--                  -              75 -        60    75
      |    78     18    18 - 81     7     37 - 93     30    37
      |           19    19 -                 -
      |           20    20 -                 -
      |                  -                 -
      |                  -                 -
  5 --+--         78    78 -                 -
      |    98     39    39 - 70     3     18 - 76     15    18
      |                    -                 -
      |                    -                 -

      8.6.      ,  -
                       



  ,            -
,        .  -
     UNIX   .




       ,     -
 : stime, time, times  alarm.        -
   ,   -    
.
     stime        
  .           
 time:

                                    241

    time(tloc);

  tloc -   ,  ,   -
   .        
  , ,  date,    ,  -
  .
     times         
,  ,     .  -

    +------------------------------------------------------------+
    | #include                                      |
    | #include                                      |
    | extern long times();                                       |
    |                                                            |
    | main()                                                     |
    | {                                                          |
    |    int i;                                                  |
    |    /* tms -   ,   4 -   |
    |        */                                               |
    |    struct tms pb1,pb2;                                     |
    |    long pt1,pt2;                                           |
    |                                                            |
    |    pt1 = times(&pb1);                                      |
    |    for (i = 0; i < 10; i++)                                |
    |         if (fork() == 0)                                   |
    |             child(i);                                      |
    |                                                            |
    |    for (i = 0; i < 10; i++)                                |
    |         wait((int*) 0);                                    |
    |    pt2 = times(&pb2);                                      |
    |    printf("-:   %u             |
    |               %u    %u             |
    |            :    %u    %u\n",|
    |            pt2 - pt1,pb2.tms_utime - pb1.tms_utime,        |
    |            pb2.tms_stime - pb1.tms_stime,                  |
    |            pb2.tms_cutime - pb1.tms_cutime,                |
    |            pb2.tms_cstime - pb1.tms_cstime);               |
    | }                                                          |
    |                                                            |
    | child(n);                                                  |
    |    int n;                                                  |
    | {                                                          |
    |    int i;                                                  |
    |    struct tms cb1,cb2;                                     |
    |    long t1,t2;                                             |
    |                                                            |
    |    t1 = times(&cb1);                                       |
    |    for (i = 0; i < 10000; i++)                             |
    |         ;                                                  |
    |    t2 = times(&cb2);                                       |
    |    printf(" %d:   %u    %u|
    |               %u\n",n,t2 - t1,                  |
    |            cb2.tms_utime - cb1.tms_utime,                  |
    |            cb2.tms_stime - cb1.tms_stime);                 |
    |    exit();                                                 |
    | }                                                          |
    +------------------------------------------------------------+

       8.7.  ,   times

                                    242

 :

    times(tbuffer)
    struct tms *tbuffer;
  tms -  ,       -
   :
    struct tms {
       /* time_t -   ,           */
       time_t tms_utime;  /*        */
       time_t tms_stime;  /*          */
       time_t tms_cutime;  /*       */
       time_t tms_cstime;  /*         */
    };
 times  ,  "   -
  ",  ,    .
      8.7  ,    -  
10  ,    10000    . --
    times       -
,               
  . -    ,   -
             
  ,    -   -
        . ,    
   ,        fork  
exit,             -
 .
        alarm    -
     ("")      
.  ,      8.8    
   ,      ,  -
 .    ,    stat,  -
        ,      
 ,   .          
signal          ,   
alarm      60       pause
       .  60 -
  ,         -
  wakeup,     , 
   pause,     .
             -
 ,        (). -
   ,       
  .


    8.3

: * , * , , * ; * , * , * "" , * (. ), * . , - - . - 243 , ( ). - , - , . - 8.9. +------------------------------------------------------------+ | #include | | #include | | #include | | | | main(argc,argv) | | int argc; | | char *argv[]; | | { | | extern unsigned alarm(); | | extern wakeup(); | | struct stat statbuf; | | time_t axtime; | | | | if (argc != 2) | | { | | printf(" 1 \n"); | | exit(); | | } | | | | axtime = (time_t) 0; | | for (;;) | | { | | /* */ | | if (stat(argv[1],&statbuf) == -1) | | { | | printf(" %s \n",argv[1]); | | exit(); | | } | | if (axtime != statbuf.st_atime) | | { | | printf(" %s \n",argv[1]); | | axtime = statbuf.st_atime; | | } | | signal(SIGALRM,wakeup); /* | | */ | | alarm(60); | | pause(); /* */| | } | | } | | | | wakeup() | | { | | } | +------------------------------------------------------------+ 8.8. , alarm 244 +------------------------------------------------------------+ | clock | | : | | : | | { | | ; /* -| | */ | | ( ) | | { | | ; | | callout, ; | | } | | ( ) | | -| | ; | | ( ) | | -| | ; | | ; | | ; | | ; | | ( 1 ,| | ) | | { | | ( ) | | { | | "", ; | | ; | | ( )| | ; | | } | | - | | ; | | } | | } | +------------------------------------------------------------+ 8.9.

    8.3.1

, . - .

    8.3.2 -

, , . - , , , , " " (. 10.3.3). ( 8.9), - , , , , ( ) 245 . - ; - . - . , - . - , . +----------------------------+ +----------------------------+ | a() -2 | | a() -2 | +----------------------------+ +----------------------------+ | b() 3 | | b() 3 | +----------------------------+ +----------------------------+ | c() 10 | | f() 2 | +----------------------------+ +----------------------------+ | c() 8 | +----------------------------+ 8.10. 8.10 - . ( " " a ). , " " , . , f 5 : b " " , 2 ( b f - 5), " " c 8 ( c 13 ). , - . , . - " " . - , , , " " - , - , . , . , - . , , - . , - , . , " ", - . , , - 246 . , , " ", - , . - , " " - . , . , , a " ", -2 ( 8.10), , a , 2 . , b 2 , , a, " " b.

    8.3.3

- , - . , . - ( , ); . - , - , , - , - - - . , , , , , . - , . +--------------------------------+ | | | | | bread 100 5 | | breada 150 0 | | bwrite 200 0 | | brelse 300 2 | | getblk 400 1 | | user - 2 | +--------------------------------+ 8.11. 8.11 . 10 , - , : 110, 330, 145, , 125, 440, 130, 320, 104. , - . , 20% (user) 50% bread . 247 , . , , , - , - , , - . , - . , . - [Weinberger 84] , "if-then" "else", - . , 50-200%, - . profil: profil(buff,bufsize,offset,scale); buff - , bufsize - , offset - (, - ), scale - . "scale" . , , - , Oxffff, , , Ox7fff, buff , Ox3fff - - .. , , . , , , offset , - bufsize scale. , 8.12, f g. , - signal, theend, , - ( main theend), , - , profil, , - . 10 AT&T 3B20 , - 8.13. f 204 ; f 12 , - AT&T 3B20 4 , f buf 51, 52 53. g buf c 54, 55 56. buf 46, 48 49 , - main. , , - , . profil - , - ; , , . 248 +------------------------------------------------------------+ | #include | | int buffer[4096]; | | main() | | { | | int offset,endof,scale,eff,gee,text; | | extern theend(),f(),g(); | | signal(SIGINT,theend); | | endof = (int) theend; | | offset = (int) main; | | /* */ | | text = (endof - offset + sizeof(int) - 1)/sizeof(int); | | scale = Oxffff; | | printf | | (" %d %d %d\n",| | offset,endof,text); | | eff = (int) f; | | gee = (int) g; | | printf("f %d g %d fdiff %d gdiff %d\n",eff,gee, | | eff-offset,gee-offset); | | profil(buffer,sizeof(int)*text,offset,scale); | | for (;;) | | { | | f(); | | g(); | | } | | } | | f() | | { | | } | | g() | | { | | } | | theend() | | { | | int i; | | for (i = 0; i < 4096; i++) | | if (buffer[i]) | | printf("buf[%d] = %d\n",i,buffer[i]); | | exit(); | | } | +------------------------------------------------------------+ 8.12. , profil +------------------------------------------------------+ | 212 440 57 | | f 416 g 428 fdiff 204 gdiff 216 | | buf[46] = 50 | | buf[48] = 8585216 | | buf[49] = 151 | | buf[51] = 12189799 | | buf[53] = 65 | | buf[54] = 10682455 | | buf[56] = 67 | +------------------------------------------------------+ 8.13. , - profil 249

    8.3.4

, (- ). , . , . - . - , . - , - . - - wait, - , . - . - , , . , , 25 40 , , - 50 , 75 (50/5 + 25 + 40). - . , , , , - , . - .

    8.3.5

- , . - time . fork, , - . , - stime , .

    8.4

UNIX. - , . , , , , - . , - ( , ), , : - , , - . - nice - , . 250 , : , "". , - , , , , - , "" . - "" .

    8.5

1. , , , . , , , , . . *2. - 1 . , - . - ? 3. UNIX - : decay() = max ( , -10); : decay() = .8 * ; : = /16 + ( ); 8.4, . 4. 8.4 , 60 100 - . . 5. , - , - . , ? , - shell' ? 6. wait - , . "" - ? 7. nice , : nice 6 nroff -mm big_memo > output , nice. 8. 8.4, , , nice A, 5 -5. 9. renice x y, x - (), y - nice - . 10. , 8.6. , - , A, 33% , , B, - 66% . - ? , - . 251 11. date. : , : date mmddhhmmyy () (, , , , ). , date 0911205084 11 1984 8:50 . 12. sleep: sleep(seconds); . , alarm pause. , alarm sleep ? : 1) alarm , - , 2) alarm sleep. *13. . - sleep alarm pause. , alarm , pause. ? ? 252

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