8.  - () , , - , - . UNIX . - ; . , - , . , , : , , time - , , date . - - , - . , - , - , 50-100 . () - . - , UNIX, , , - . 8.1  UNIX - , " ". , - , . , . - , - . 8.1.1  ( 8.1), - , "" " , ". - , , , - , . - , , (), - , " " . , , ; . 232 +------------------------------------------------------------+ | schedule_process | | : | | : | | { | | ( -| | ) | | { | | ( )| | -| | ; | | ( | | ) | | ; | | /* - | | /* | | */ | | } | | - | | ; | | , - | | ; | | } | +------------------------------------------------------------+ 8.1. 8.1.2  , . , . - ( 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.1.3  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 , - : - , . , - , - . , , , . - , . , , , . 8.1.4  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) . 8.1.5  - (). , - , , . , , - . , " " (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 .. 8.1.6  - , . , , - , . , , : , ( , , , , ). - , - , , . - - ; - , , . , . - - ( , ) - 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 . 8.2 ,  , - : 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