7.  ; , . 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. 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 | - -+-------+ +-+-----+ - | | | |