:





        -    
   ,       -
   , ,   ,   ,    -
,       ,   -
.          -
 :         -
  ,       -
          -
.      ,    -
 - ,    , -    
         
      ,     
            -
.          .
,       , -
  .   ,   :  -
  ,    ,    
 .
      " ",        
       . ,  
  ,       -
,       .  ps -
       ,   
  .   :     -
     ,         
   . ,  ,   -
 8,   :     , -
    ,    .
            -
  -,       .
         -
        .
          -
.




       UNIX     -   -
       -.  
    2,  - ,    
,            
 ;      -  
  ,       . -
  -        -
 -.
           -
    (.  2.1).    ,   
,        .     -
          .
          ,   
,  "",  " ",    -

                                    290

,    .     ,
   ,    :   -
 -         
-.       ,   open, close,
read  write,       ,     .
  ioctl       -
    -,       
  (*).   ,      -
      . ,  
        ,    
,     .




              
 ,      . -
      ,    -
,     ,      
 .        -
,      ,   -
          "". ,  
  ,       -
   .
        ,       -
. -,      
 ,        .  -
          ,  
    ,   . -, -
       ,  
 ;      .
,      , -
   .       -
.          -
,          -
    ,    .
      "  - "      -
-           -
(  10.1).         , 
         .
  open    close,    , "" 
          .    mount  
umount         , 
  - .  read  write,  -
  -         ,
     ,   -
    .      
       .   -
       .
      " - "   - -
    -     -
 :     ,   -
 ,  ,     -

----------------------------------------
(*)  ,   fcntl     -
    ,     ,     -
    .     ioctl     .


                                    291

 . ,  " ",  
       ( 8)    -
,           
"    " . , 
         
   .
          mknod,
      (  ),   
 .  mknod        
  ,   . ,   

    mknod /dev/tty13 c 2 13

"/dev/tty13"  -     , "c" ,    - "-
 " ("b", , ), "2" -   -
, "13" -   .     -
  ,        ,
     -      
.         "/dev/dsk1" 
 0,    gdopen   0    
  -  ( 10.2);     -
    "/dev/mem"   3,


                      
    +------------------------------------------------------------+
    |                                    open  close             |
    | open close read write ioctl                     read write |
    |                                    mount umount            |
    +--+-----+----+-----+-----+------------+-----+-----+-----+---+
       |     |    |     |     |            |     |   +-+-----+---+
       |     |    |     |     |            |     |   |    |
       |     |    |     |     |            |     |   |    |
       |     |    |     |     |            |     |   |   |
       |     |    |     |     |            |     |   |      |
       |     |    |     |     |            |     |   +-----+-----+
    +--+-----+----+-----+-----+-+         ++-----+---------+-----+
    |     |         |   -  |
    |  -|         |  -  |
    |                           |         |               |
    +--+-----+----+-----+-----+-+         +--+------+--------+---+
    +--+-----+----+-----+-----+-+         +--+------+--------+---+
    |open close read write ioctl|    | open  close  strategy|
    |                           |    |                      |
    |                    |      |               |
    |                           |  -  |                      |
    |  - |    |    |
    |          |         |  |
    +------------+--------------+         +-----------+----------+
        +--------+----------+               +---------+---------+
        |   |               |   |
        +--------+----------+               +---------+---------+
                 +-------------------+----------------+
                                     |
                           

                10.1.    



                                    292

   mmread   3       -
 -.  nulldev -  "" , -
      ,       
.            
  ;       
 .           -
  ;    ,   
, ,       -
.




             -
.        ,   
, ,   ,    -
 ,   

        +-----------------------------------------------+
        |    -  |
        +-------+--------+---------+--------------------+
        |   |  open  |  close  |     strategy       |
        +-------+--------+---------+--------------------+
        |   0   | gdopen | gdclose |    gdstrategy      |
        +-------+--------+---------+--------------------+
        |   1   | gtopen | gtclose |    gtstrategy      |
        +-------+--------+---------+--------------------+
+----------------------------------------------------------------+
|          -       |
+------+-----------+-----------+---------+-----------+-----------+
|  |   open    |   close   |  read   |  write    |   ioctl   |
+------+-----------+-----------+---------+-----------+-----------+
|   0  |  conopen  |  conclose | conread | conwrite  |  conioctl |
+------+-----------+-----------+---------+-----------+-----------+
|   1  |  dzbopen  |  dzbclose | dzbread | dzbwrite  |  dzbioctl |
+------+-----------+-----------+---------+-----------+-----------+
|   2  |  syopen   |  nulldev  | syread  | sywrite   |  syioctl  |
+------+-----------+-----------+---------+-----------+-----------+
|   3  |  nulldev  |  nulldev  | mmread  | mmwrite   |   nodev   |
+------+-----------+-----------+---------+-----------+-----------+
|   4  |  gdopen   |  gdclose  | gdread  | gdwrite   |   nodev   |
+------+-----------+-----------+---------+-----------+-----------+
|   5  |  gtopen   |  gtclose  | gtread  | gtwrite   |   nodev   |
+------+-----------+-----------+---------+-----------+-----------+

   10.2.      -
                   


    ,     ,     
    -   .    -
     ,     -
          
       ,  
     .     
          ,  -
         ,      
 .     ,  -
      ,     -

                                    293

  ,         .  
,  ,    (, 
  stat),     ,  
   .
             
 .       ,  
          ; 
          -
      ,       (-
,    ).      -
 .


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

             10.3.   



            ,    -

                                    294



    (.  5.1),    ,  -
            
    . ,    -
     ,   -
     ,      . -
,  ,     ,  
    open ( 10.3).     -
-     open,    -
  - ,    -
-  open,    .   
   ,    ,    open, -
   ,  :   
   ,     .
            open   
       -
  . ,  open     -
    ,       () 
,         .  
       -
   (,      ).
  " ",    ,  open 
    .
              
-   ,   ,  ,  -
      ,     -
. ,         
 ,  getty, ""  ( 7.9), -
    ,       -
,        . 
           
  open      :    , 
          ,  
      ,    . 
  ,   setjmp ( 6.4.4), -
    open;     , -
      ,     
   ,   longjmp ( 6.4.4),  -
     open  .     
         , 
  .       -
,      ,  ,  
    ,    -
.     open  .
          , 
   .     -
 "no delay" ( ), ,     
    open,       .  
  open       
,      .      
  "no delay",   ,    -
 read,     ( 10.3.4).
       ,   -
  ,       ,    -
      5,      open 
    open.  ,    -
 ,     "",   
 open,      . -

                                    295

,      ""   -
    ,       .  
      ""    
   ,        -
.       ,     -
:        -
   ;     
     (**).



    +------------------------------------------------------------+
    |  close           /*   */               |
    |  :                         |
    |  :                            |
    | {                                                          |
    |        ( 5);  |
    |     (       0)   |
    |         finish;                                   |
    |     (    ,   -|
    |             |
    |     )                                            |
    |         finish;     /*    */   |
    |     (  )                      |
    |    {                                                       |
    |                 |
    |            -- |
    |        ;                                                 |
    |         ,   - |
    |                - |
    |         ;                                     |
    |    }                                                       |
    |     (  )                         |
    |    {                                                       |
    |        ( )                        |
    |            finish;                                |
    |              -|
    |        ;                                            |
    |                 |
    |           - ;     |
    |         ,   - |
    |                - |
    |         ;                                     |
    |          , -|
    |           ;                                 |
    |    }                                                       |
    |  finish:                                                   |
    |     ;                                      |
    | }                                                          |
    +------------------------------------------------------------+

              10.4.   

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



                                    296



         ,    .  ,
        close   -
   close   ,       -
  ,    ,  
     ;   -
,     ,      , -
  .      -
      open,       
 ,   ,    -
   .      ,    -
    :     close 
    -     
,     ,    
 ,      open  close   -
 .

              
 ( 10.4). ,  ,    ,   -
 ,    .
 1.     ,    ,    
          ,    . 
    ,    close      ,
               ,
                -
    ,       .   -
              , 
              -
    . ,  ls -l ,      -
         ("c"   )    -
    ,       (9  1) .  
         ,  1,   ,  
     .

       crw--w--w-     1 root   vis   9, 1  Aug 6 1984  /dev/tty01
       crw--w--w-     1 root   unix  9, 1  May 3 15:02 /dev/tty01

            ,   -
            .
 2.    ,     -
          .   
    ,         ,
           .  
      ,       -
    ,        close  -
    .          ,  -
           ,    -
               , -
       " ".    -
                
         .    
             ,
           ,    -
            .
 3.     .
     ,       -
          
   ,             

                                    297

.




               -
    .       
    -,    read 
write,   .       -
,         
   ,       
  . ,     -
       ( 10.3.1).    
  "",        -
    write     ""  . 
,  ,       (..
  ):     , 
   ,     -
   ,            
.      ,   -
,   

                   

                   |     |
                   +-----+
             160110| CSR |         +------+ +---------tty00
                   | RDB +---------+ dz00 +-+---------tty01
                   | TDB |         +------+ |          ...
                   +-----+                  +---------tty07
             160120| CSR |         +------+ +---------tty08
             160122| RDB +---------+ dz01 +-+---------tty09
             160126| TDB |         +------+ |          ...
                   +-----+                  +---------tty15
                   |     |

     10.5.    -  -
                    VAX DZ11


       ,    -
 .
           -
 .      --
  , ,      -
        ,     -
,    .   
         
,      . , 
-   VAX-11       -
     (   )   -
  ( ),      
  .  ,   VAX DZ11 
8     (. [Levy 80],   
   VAX).      (CSR)
     DZ11   160120,   -
 (TDB) -  120126,       (RDB)  -  
160122   (  10.5).    ,          
"/dev/tty09",     (1 = 9      8)  

                                    298

           
    .       
   .  DZ11   ""  -
   ,         .
       "  " 
   ,    DZ11   -
  ,       .  
 DZ11  .
          -,  ,
        .  -
 ,   . ,    
IBM  370   "Start I/O" ( -),  -
  -,   .    
    .
            -
    -,     
 .          ,
      -     -
         
   ()  .    -
             
  ,      -
    .     
  ,        -
  .
           -
    ,    .    -
    ,    -
     , ,  ,  , -
    ,     . ,
        "",    
           ,   
   .




              -
   , ,    ,  
         -
 strategy (   )     -
         .  strategy
      -,    
,        
  .        
    .      -
  ;       (-
  )   ,     . 
      ,       9.
            ;  
   ,     (
).         , -
    (   ,  -
)      .
    ,       
         .   -
      .      
,   ,       -
    ,       3.   -

                                    299

,          (,  bread  
bwrite),     ,  -
  .       
            
   ,    -    
  .   ,     -
 -   (,  
        ),      
,       .




      ioctl        
  stty (  )  gtty (  -
),      UNIX.     -
               
      ,   , 
 ,    .    ,
   ioctl       -
 . ,   ioctl,     ,  
     ,     -.
      ,     
     .     ioctl 
    10.3.3.
      ,    :

    ioctl(fd,command,arg);

 fd -  ,       
open,  command  -  (),    ,
arg -   (    ).   -
    ; ,   
      ,   ,  
  ,    ,   -
.      arg    -
         -
        . , -
 ,   ioctl,    -
           ,
  , , ,   ,  -
      .




          ,  stat  chmod, 
,    ,    ;     
,    .    lseek  
. ,       -
              lseek, 
             ,
     .     -
 (read)   (write),          
    ,  ,     
     ,      
 ,    .   -
     10.3.


                                    300

                           
                                   
                                              +------------------+
                                              |         -        |
         tty00 -------------+                 |         -        |
         tty01 ....         |                 |         -        |
          ...               | +--+            +------------------+
         tty07 -------------+-+  |------------|     ttyintr 0    |
         tty08 -------------+ +--+            +------------------+
         tty09 ....         +-+  |------------|     ttyintr 1    |
          ...      +--------+ +--+            +------------------+
         tty15 ----+ +--------+  |------------|     consintr     |
        ------+        +--+            +------------------+
     00 -------------+-+  |------------|    printintr 0   |
               ....         | +--+            +------------------+
     03 -------------+ |  |            |         -        |
                              |  |            |         -        |
                              +--+            +------------------+

                 10.6.   





        ( 6.4.1),    -
     ,    -
    ,  ,   
    .     
   ,        -
   ,    ,  -
. ,       10.6   
          ("ttyintr"),  
    ,   8  .
    tty09    ,   
 ,       -
.           
   ,       
,   .       -
,    ,   0  1, 
          ,
         .
           ,
  ,  ,  ,  
 tty09,   tty12,   .      -
   ,      ,   
          
 ,     .
      ,  ,     ,  
   ,   ,  -
        .  -
        -
  .




      ,      UNIX  -
    ,      ,  

                                    301

" []     - "  (.
[System  V 84b]). ,       -
,       ,   -
    ,      .   ,
        ,  -
           ,
  ,    4  5,       
 ,    .  ,   -
    (,  ,  )    
  ,     ,      -
,     ,     .
          ,   -
     ,      .
       :   -
     ,     
    ,           
()     ;     -
    ,    ,   
.         -
        ,    
    ,     .
,          ,  -
   ,    ,  
 -.

         +---------------------------------------------+
         |              |
         |                                             |
         |             = 512            |
         |                                             |
         |    0                0                 64000 |
         |    1            64000                944000 |
         |    2           168000                840000 |
         |    3           336000                672000 |
         |    4           504000                504000 |
         |    5           672000                336000 |
         |    6           840000                168000 |
         |    7                0               1008000 |
         +---------------------------------------------+

                10.7.    RP07


      ,     
     . ,  DEC RP07   , -
        10.7.  ,    
"/dev/dsk0",  "/dev/dsk1",  "/dev/dsk2"  "/dev/dsk3"  
 RP07,    0  3,        .
           -
 .         940  
,   "/dev/dsk3",        
    336940 ( 3   ,   336000;
336000 + 940 = 336940)  .
             -
      :   
      . .      -
. ,  0  1   RP07  ,   
       0  1008000,    .  7 
   .     ,  -

                                    302

 ,   ,   ,    
    .      ,     
, ,      .
              
   .      
     ,       -
  . ,        -
          
        .   
 V ,        -
      ,    ,    ,
      .     -
          -
    ,     -
  .
       ,    UNIX       
  ,         
,        .  
     , 
   ,       -
     ;     -
 .
                
      ,    4 
5,    ,        
.             - mkfs 
fsck.  mkfs           UNIX,
   ,  ,    -
         .  fsck
       , 
   5.
     ,    10.8,    
"/dev/dsk15"  "/dev/rdsk15",  ,   ls  
:
    ls -1 /dev/dsk15 /dev/rdsk15

    br--------  2 root   root   0,21 Feb 12 15:40   /dev/dsk15
    crw-rw----  2 root   root   7,21 Mar  7 09:29   /dev/rdsk15

     ,   "/dev/dsk15"      
,        "root",  
 "root"     .    -
0,  - 21.  "/dev/rdsk15"     
-,     "root",  
           ,    .
   - 7,  - 21. ,    ,  
     -
   -      
-, ,         21  
  ,      , , -
 2,  1.      ,  -
       ,  ,    
(***).    , ,  ,    

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


                                    303

   (  ),    
    8192      .  -
          ,    
   .

    +------------------------------------------------------------+
    | #include "fcntl.h"                                         |
    | main()                                                     |
    | {                                                          |
    |      char buf1[4096], buf2[4096]                           |
    |      int fd1, fd2, i;                                      |
    |                                                            |
    |      if (((fd1 = open("/dev/dsk5/", O_RDONLY)) == -1) ||   |
    |               ((fd2 = open("/dev/rdsk5", O_RDONLY)) == -1))|
    |      {                                                     |
    |            printf("  \n");                |
    |            exit();                                         |
    |      }                                                     |
    |                                                            |
    |      lseek(fd1, 8192L, 0);                                 |
    |      lseek(fd2, 8192L, 0);                                 |
    |                                                            |
    |      if ((read(fd1, buf1, sizeof(buf1)) == -1) ||          |
    |          (read(fd2, buf2, sizeof(buf2)) == -1))            |
    |      {                                                     |
    |            printf("  \n");                  |
    |            exit();                                         |
    |      }                                                     |
    |                                                            |
    |      for (i = 0; i < sizeof(buf1); i++)                    |
    |            if (buf1[i] != buf2[i])                         |
    |            {                                               |
    |                 printf("   %d\n", i);     |
    |                 exit();                                    |
    |            }                                               |
    |      printf(" \n");                         |
    | }                                                          |
    +------------------------------------------------------------+

     10.8.       
                      


    ,       , -
  ,        ,
   .    -
 -           .
,      "/dev/dsk15"  "/dev/rdsk15"  
   "root",          
  ,    "root"   ,   -
   ,     .
    ,       , 
        .    
,      3, 4  5,   
-,   ,     -
   ,           
         .
,    ,   . 
      ,   

                                    304

   ,        -
 .     fsck      -
   .
              
 .           -
,       ,    ,
          -
    (.  bmap   4)    -
          .  -
,    ,    , ,   ,
   . ,       
  (  ),    
      ,     ,  
   .    ,   
 ,          -
      ,   .
     ,        ,
              
,      ,     -
,             ,   
.   ,        -
,      ,    .
             
.            -
-   , ,  , ,   
  . ,       1 ,
        .
             ,
          .
,      - ,  -
 ,       
    . ,       
   1 ,    -   
  1  .   ,     -
  ,          
   ,      -
.    ,        -
,       .  -
  ,    10.8,  ,   -
  4096 ,       
  1 ,     ,   -
    ,      -
 ,      ,  -
         .  , 
        -
       ,     -
.




         ,    :
      . ,    
,    ,      
.     UNIX, 
      ,   
   .       -
  ,   ,  -
  (   ,  ,    

                                    305

   ) ,         ;
     
  ,  ,  ,  . 
          
  - .
    ,  ,      -
,   .        ("erase";
     ),      -
        .  -
    ,    
(*** *).          
 ( ,     (*****)) 
     ,     -
  .
        :
  *    ;
  *   ;
  *   "",    , -
         ;
  *  ,  ;
  *   , ,    
      ;
  *         
          ;
  *         , 
      ,    .

          
,           ,  
  ,  ,  ,    -
    .
     ,    ,    -
    70- ,      
    ,     (. [Ritchie 84], .1580). ,
      ,     -
.   ,      ,   -
          
 ,      ,     -
  .      10.9   ,  
    ,     -
  ,     .  
,       
 ioctl,   ,     
    ,    
,   ,          
,  .



          . 
  (clist)       
         . -

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


                                    306

                                
                                             
      +-----------------------+       +-----------------------+
      |  / |       |  / |
      +-----------------------+       +-----------------------+

         -  |          ^   -                 |         ^
       -    v          |     -               v         |
     - +---------------------+ -      +-----------------------+
  |   |    |    |
     - +---------------------+ -      +-----------------------+
       -    |          ^     -               |         ^
         -  v          |   -                 v         |
      +-----------------------+        +---------------------+
      |    |        |   |
      +-----------------------+        +---------------------+
                                             |         ^
                                             v         |
                                      +-----------------------+
                                      |  -  |
                                      +-----------------------+
                                             |         ^
                                             v         |
                                     +-------------------------+
                                     |  - |
                                     +-------------------------+

     10.9.      -
                    

     
                     
                    
                            0 1 2 3 4 5 6 7 8 9        14
----------+---------+---------++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+---
          |    7    |    14   ||g|a|r|b|a|g|e||| |e|q|n| ||| |...
----+-----+---------+---------++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+---
    |
    v

                 10.10.  


  (cblock)       , -
         ,  -
              (
10.10).        
  ,       -
  .
            
       .
 1.           
    .
 2.           -
    .
 3.          :    
              -
                  ,
             .    -

                                    307

          ,    
             
      .        , 
      .
 4.           -
        ,      -
      .    ,   -
      ,         
       .
 5.              
     ,           
    .
 6.            .

          ,  -
     ,    -
,   .       
        . , -
 10.11      ;  
          ( 10.11-)   ,
        (

                                   
                                         

                       +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 8  | | p | i | c |   | f | i | l | e |
                |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
+----------+-+  |      |  0 8  | | * |   | | |   | t | b | l |   |
|    27    | +--+      ++------+ +---+---+---+---+---+---+---+---+
|  | +--+       v
+----------+-+  |      +-------+ +---+---+---+---+---+---+---+---+
                |      |  0 8  | | | |   | t | r | o | f | f |   |
                |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 3  | | - | m | m |   |   |   |   |   |
                       +-------+ +---+---+---+---+---+---+---+---+
                   ()

                       +-------+ +---+---+---+---+---+---+---+---+
                +----->|  1 8  | |   | i | c |   | f | i | l | e |
                |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
+----------+-+  |      |  0 8  | | * |   | | |   | t | b | l |   |
|    26    | +--+      ++------+ +---+---+---+---+---+---+---+---+
|  | +--+       v
+----------+-+  |      +-------+ +---+---+---+---+---+---+---+---+
                |      |  0 8  | | | |   | t | r | o | f | f |   |
                |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 3  | | - | m | m |   |   |   |   |   |
                       +-------+ +---+---+---+---+---+---+---+---+
                   ()


                                    308

                       +-------+ +---+---+---+---+---+---+---+---+
                +----->|  2 8  | |   |   | c |   | f | i | l | e |
                |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
+----------+-+  |      |  0 8  | | * |   | | |   | t | b | l |   |
|    25    | +--+      ++------+ +---+---+---+---+---+---+---+---+
|  | +--+       v
+----------+-+  |      +-------+ +---+---+---+---+---+---+---+---+
                |      |  0 8  | | | |   | t | r | o | f | f |   |
                |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 3  | | - | m | m |   |   |   |   |   |
                       +-------+ +---+---+---+---+---+---+---+---+
                   ()

                       +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 8  | | * |   | | |   | t | b | l |   |
                |      ++------+ +---+---+---+---+---+---+---+---+
+----------+-+  |       v
|    19    | +--+      +-------+ +---+---+---+---+---+---+---+---+
|  | +--+      |  0 8  | | | |   | t | r | o | f | f |   |
+----------+-+  |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 3  | | - | m | m |   |   |   |   |   |
                       +-------+ +---+---+---+---+---+---+---+---+
                   ()

        10.11.     


                                   
                                         

                       +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 8  | | p | i | c |   | f | i | l | e |
                |      ++------+ +---+---+---+---+---+---+---+---+
+----------+-+  |       v
|    22    | +--+      +-------+ +---+---+---+---+---+---+---+---+
|   | +--+      |  0 8  | | * |   | | |   | t | b | l |   |
+----------+-+  |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 6  | | | |   | t | r | o | f |   |   |
                       +-------+ +---+---+---+---+---+---+---+---+
                   ()
                       +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 8  | | p | i | c |   | f | i | l | e |
                |      ++------+ +---+---+---+---+---+---+---+---+
+----------+-+  |       v
|    23    | +--+      +-------+ +---+---+---+---+---+---+---+---+
|   | +--+      |  0 8  | | * |   | | |   | t | b | l |   |
+----------+-+  |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 7  | | | |   | t | r | o | f | f |   |
                       +-------+ +---+---+---+---+---+---+---+---+

                                    309

                   ()
                       +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 8  | | p | i | c |   | f | i | l | e |
                |      ++------+ +---+---+---+---+---+---+---+---+
+----------+-+  |       v
|    24    | +--+      +-------+ +---+---+---+---+---+---+---+---+
|   | +--+      |  0 8  | | * |   | | |   | t | b | l |   |
+----------+-+  |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 8  | | | |   | t | r | o | f | f |   |
                       +-------+ +---+---+---+---+---+---+---+---+
                   ()
                       +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 8  | | p | i | c |   | f | i | l | e |
                |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
+----------+-+  |      |  0 8  | | * |   | | |   | t | b | l |   |
|    25    | +--+      ++------+ +---+---+---+---+---+---+---+---+
|  | +--+       v
+----------+-+  |      +-------+ +---+---+---+---+---+---+---+---+
                |      |  0 8  | | | |   | t | r | o | f | f |   |
                |      ++------+ +---+---+---+---+---+---+---+---+
                |       v
                |      +-------+ +---+---+---+---+---+---+---+---+
                +----->|  0 1  | | - |   |   |   |   |   |   |   |
                       +-------+ +---+---+---+---+---+---+---+---+
                   ()

        10.12.     

10.11);        , 
    .     10.12  ,
       ;   , 
     8            
  ( 10.12).




      ,      ,  
  :    ,   -
,      ,   
      , -
       ,    
  ,     -
    (     )
  .
            (  10.13),  
   .     
          
        ,      -
.     ,  ,  
    .    
        ,  -
    ,        
        , -


                                    310

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

          10.13.     


  .         
   ,     -
  ,    ,    -
     .     -
,        -
    ,     -
,    ,      .
          ,     
     .     -
;     ,   ,    -
.     - ,      -
,      write.    -
  ,      ,  -
   write,       -
  ,     .    
      ,    -
      write,   -
         .   
 ,       ,   
  .   ,   ,    
  ,        write, -
      .
     ,    10.14.  
  18  ;     -
  (    sprintf)   output,  -
          i    
fork,              
.     ,   -
    .     64 


                                    311

+----------------------------------------------------------------+
| char form[]="      "|                                                            |
| main()                                                         |
| {                                                              |
|     char output[128];                                          |
|     int i;                                                     |
|                                                                |
|     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). , , , . , - ; () - 322 . , , (. 8); . +----------+ | | +-----------------------+ | | || v +----------+ +------------+-----------+ | | | | | | +------+-----+-----------+ | ^ | | v | +------------+-----------+ | | | | | | +------+-----+-----------+ | ^ | | v | +------------+-----+-----+ | | | | | | +------------+-----------+ 10.22. "" , ioctl. - , - . , , - , - : "" ; . , - ( 10.22); - , - , 10.3, . - - . , , - : fd = open("/dev/ttyxy",O_RDWR); ioctl(fd,PUSH,TTYLD); PUSH - , TTYLD - , . , . , " - ", - ioctl ioctl(fd,POP,0); , 323 , , - . , , . - .

    10.4.1

, - (. [Pike 84]). - , . , - ; - . +---------+ +---------+ +-----------------+ | shell 1 | | shell 2 | | mpx | +---------+ +---------+ +-----------------+ ---------------+---------------+-----------+---+-------+---- | ^ | ^ +--+ ^ | ^ | ^ | | | | | +--+ | | | | | | | | | | | | | | v | v | - v | v | - | | - +-+++ - +-+++ -+-+++ +-+++- | | | | | | | | -| | | | | |- | | +++-+ +++-+ +++-+ +++-+ | | | ^ +-----------+-^------+ ^ | ^ | | | | | +---------+-+--------+ | | | | | | | | | | +-----------+ | | | v | v | v | v +-----------+ v | +-+++-+++ +-+++-+++ +-+++ - | | | | | | | | | | - | | | - +++-+++-+ +++-+++-+ - +-+-+ | ^ | ^ | ^ | ^ - 1 | +-+ | | +-+ | 2 +-----+ +-----+ 10.23. - 10.23 . mpx, - . Mpx , , - , .. mpx , - , , , . - , - : , - , ; , . , - ( 10.24), mpx , ( , ). Mpx 324 +----------------------------------------------------------------+ | /* , 0 1 | | */ | | (;;) /* */ | | { | | (); /* - */ | | , ; | | ( ) | | { | | : /* - | | - | | */ | | ( ) /* , | | */ | | { | | ; | | : | | ( ) | | { | | mpx; | | ; /* "" */ | | } | | /* - */ | | ; | | , stdin, | | stdout, stderr; | | ; | | shell; /* */| | } | | /* "" , */ | | -| | , - | | ; | | ; /* "" */ | | | | : /* | | */ | | , | | ; | | ;| | ; /* "" */ | | } | | } | +----------------------------------------------------------------+ 10.24. . Mpx - , ( ), shell'. shell ; . mpx , - . Mpx - , select. - , mpx , 325 , , , - , - . , - , ; mpx . - , . , ; mpx , , . ioctl , - ; - . , - - , . - , ioctl, , mpx, - .

    10.4.2

, "" . , , , . "" , . - , - . write "" , , , "" - , . . , , - , - . , . , - . - - , - . , (, ). , , - . - : * , - , . , , - , - . * , read - (, 326 newtty.c_cc[VMIN] = 0 10.17). - . * - . - , , - . , . .te1 10.5 UNIX. , ; . - - , , . . - - -, , , , ioctl. ioctl , - . - - , , , , , . , - , . - . - , , . . , , - , . - ioctl , - , - . Getty- : shell', - ioctl . - , "" . . - , . , . - . .te1 10.6 *1. , , , - - 327 . - , , . , - ? *2. 5, mknod . - , - mknod ? 3. , , . : - , , , - . - - . ? 4. mkfs , , - . mkfs ? ? - ? 5. mkfs fsck ( 5) , . . 6. , , UNIX. , . ? : * - , * , * , , * : - , - ? 7. UNIX , - . , , , , "". ( ) . ? 8. , - mount. , . , , , . . 9. , 10.14, . , . , - ; - . , write - printf(output); 10. , : ed file & . 328 11. crw--w--w- 2 mjb lus 33,11 Oct 25 20:27 tty61 . , "mjb", . ? 12. , . - . , write ? 13. stty: , - . . 14. , . 15. , , - , . - ? *16. getty- - . , getty-, ? ? 17. shell , "- " - . , ( shell') ? *18. , , "". , - ? 19. getty- , login - . , - ? 20. ("/dev /tty"), 10.3.6. ? (: stat fstat). 21. , - , - , - . *22. () - ( ) . *23. , - , - . . : , - , ? 24. ps - . ps , . , - ps . , . 329

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