| | | | for (i = 0; i < 18; i++) | | { | | switch (fork()) | | { | | case -1: /* --- -| | */ | | exit(); | | | | default: /* */ | | break; | | | | case 0: /* */ | | /* output */ | | sprintf(output,"%%d\n%s%d\n",form,i,form,i); | | for (;;) | | write(1,output,sizeof(output)); | | } | | } | | } | +----------------------------------------------------------------+ 10.14. , , ( 64 ) V . , write, . , , AT&T 3B20: this is a sample output string from child 1 this is a sample outthis is a sample output string from child 0 . read , , , . , , , , , , . - , shell shell' . , , "date" "who", - , : pic file* | tbl | eqn | troff -mm -Taps | apsend shell, , , (, ), - 312 . - . 10.15 . , ; 10.3.3 . , , - +------------------------------------------------------------+ | terminal_read | | { | | ( - | | ) | | { | | ( | | ) | | { | | ( "no delay" | | ( )) | | ; | | ( -| | ) | | | | ( ); | | ( -| | ); | | } | | | | /* | | */ | | ( ) | | -| | ; | | /* - | | */ | | { | | ( | | ) | | { | | | | - | | : | | -| | ; | | ( - " " " | | ") | | ; /* */ | | } | | } | | } | | | | ( | | , | | read) | | | | ; | | } | +------------------------------------------------------------+ 10.15. 313 , . , " " , , , , . , - . , - , , - , - . , read, . , , , , : - , . , , . - , - . , - . . , +--------------------------------------------------------------+ | char input[256]; | | | | main() | | { | | register int i; | | | | for (i = 0; i < 18; i++) | | { | | switch (fork()) | | { | | case -1: /* */ | | printf(" fork - \n");| | exit(); | | | | default: /* */ | | break; | | | | case 0: /* */ | | for (;;) | | { | | read(0,input,256); /* */ | | printf("%d %s\n",i,input); | | } | | } | | } | | } | +--------------------------------------------------------------+ 10.16. , 314 , . - , - , , . - , - - . 10.16 , - , , , . - , , , terminal_read, . , , - ; , . , ; i . , , . ; , . - , . , , shell , , , shell - . , . " " (Ctrl-d ASCII), - read , . ( 0) read, " "; - , . shell', 7, , shell' , : read 0 - shell'. -, - , . - , - . -, , - . 10.3.3  , - , 315 ioctl. -- , , - . - (. [SVID 85], .281), ioctl . - , , . , . , , : . , , read, - . read , - . , - ( 8). ( ) ioctl. , - . - - , , . - - , vi, . , - dw . 10.17 , ioctl 0, - . ioctl - TCGETA savetty - . , , , - : , , . , ioctl , : - +----------------------------------------------------------------+ | #include | | #include | | struct termio savetty; | | main() | | { | | extern sigcatch(); | | struct termio newtty; | | int nrd; | | char buf[32]; | | signal(SIGINT,sigcatch); | | if (ioctl(0,TCGETA,&savetty) == -1) | | { | | printf("ioctl : \n"); | | exit(); | | } | | newtty = savetty; | 316 | newtty.c_lflag &= ~ICANON;/* */| | newtty.c_lflag &= ~ECHO; /* -*/ | | newtty.c_cc[VMIN] = 5; /* 5 */ | | newtty.c_cc[VTIME] = 100; /* 10 */ | | if (ioctl(0,TCSETAF,&newtty) == -1) | | { | | printf(" - \n");| | exit(); | | } | | for(;;) | | { | | nrd = read(0,buf,sizeof(buf)); | | buf[nrd] = 0; | | printf(" %d '%s'\n",nrd,buf); | | } | | } | | sigcatch() | | { | | ioctl(0,TCSETAF,&savetty); | | exit(); | | } | +----------------------------------------------------------------+ 10.17. - 5- +----------------------------------------------------------------+ | #include | | | | main() | | { | | register int i,n; | | int fd; | | char buf[256]; | | | | /* "no delay" */ | | if((fd = open("/dev/tty",O_RDONLY|O_NDELAY)) == -1) | | exit(); | | | | n = 1; | | for(;;) /* */ | | { | | for(i = 0; i < n; i++) | | ; | | | | if(read(fd,buf,sizeof(buf)) > 0) | | { | | printf(" %d\n",n); | | n--; | | } | | else | | /* ; "no delay" */ | | n++; | | } | | } | +----------------------------------------------------------------+ 10.18. 317 5 , , - 10 . , - . 10.3.4  , , , - . 10.18 : "no delay" ( ) , , , (. terminal_read, 10.15). - , : "no delay" , . , - . BSD select, - . : select(nfds,rfds,wfds,efds,timeout) nfds - , rfds, wfds efds , "" - . , 1 << fd ( 1 - ) , . timeout (-) , select, , ; - - , select , , - . , 0, 1 2, rfds 7; select , , , - . wfds - , efds , , - . 10.3.5  - , - , , - . , - . - setpgrp - , - . - , , , , - . , () , . . "delete" (), "break" (), , - , . 318 , "", "" , . - , , , ""; ; , . - "" - , - , . 10.3.6  - , - . , shell , , , , . UNIX "" - "/dev/tty", () . , , "/dev/tty", . "/dev/tty". -, - -. - , , - , - -. , - "/dev/tty", - open, . "/dev/tty", - , , - , open, - . , - "/dev/tty", . 10.3.7  7, , 1, - "/etc/inittab" , , "- " "". - - ( 10.19). , getty- ( "get tty" - ), , ; getty- , setpgrp, open , . open - , getty- login ( ), , 319 . - , login shell - . shell' "login shell" (- shell, ). , shell', , getty-, - login shell , . , - , - , - getty-. - . , shell' , getty-, , . +------------------------------------------------------------+ | login /* */ | | { | | getty-: | | ( setpgrp); | | ; /* | | */ | | ( ) | | { | | : | | ; | | -, ; | | ( ) | | /* /etc/passwd */ | | { | | (ioctl);| | shell; | | } | | | | , | | - | | ; | | } | | } | +------------------------------------------------------------+ 10.19. 10.4  , - , , . , - , . , , , . , - , . , , -- . 320 , - , . - , - , , - . , . , "" (streams), -. [Ritchie 84b], - V . - . - , () - . , ; , , . , . , , - , , - . , - : * , open * , close * "", * "", , - * * , * , - * , , , - . , ; , - . +----------+ | | +-----------------------+ | | || v +----------+ +------------+-----------+ | | | | | | +------+-----+-----------+ | ^ v | +------------+-----+-----+ | | |------- | | | +------------+-----------+ 10.20. - 321 -; - , , , , . open , " ", - -. , , - , -. , - . - : "- " "" - , read, write ioctl. - , - , . , , . , ( 10.20). - , . . , - ; - . - - , . ioctl , , write . 1 2 3 +---------+ +---------+ +---------+ | +--------->| +-------->| | +----+----+ +---------+ +----+----+ v v +---------+ +---------+ | | | | +----+----+ +---------+ v +---------+ | | +---------+ 10.21. , - , - . "" - , , - - . , ( 10.21). , ,