:





             -
         -
 .         ,
      ,      
.  ()  ,     ,  
    ,    pipe: 
         
    .    ,   
    ,      
,        (.  13),   -
       -
:       ,  -
           -
.         -
         kill,   ""
     .
           .   -
        ,  ,    
      ,      
IPC:  ,     .   
   ,     ,
,  ,     "",    BSD.
  ,     ,    
,      .,   ,    
  .




      UNIX       ,  -
    , -  . -, -

     +-------------------------------------------------------+
     | if ((pid = fork()) == 0)                              |
     | {                                                     |
     |     /*  -   */              |
     |     ptrace(0,0,0,0);                                  |
     |     exec("  ");               |
     | }                                                     |
     | /*   - */       |
     | for (;;)                                              |
     | {                                                     |
     |     wait((int *) 0);                                  |
     |     read(    )   |
     |     ptrace(cmd,pid,...);                              |
     |     if (  )               |
     |          break;                                       |
     | }                                                     |
     +-------------------------------------------------------+

              11.1.   

                                    330



 sdb,          -
      ptrace,     ,
        . -
 ,  ,         
-       -
.
    ,    11.1,   -
   .    ,  -
  ptrace,       -
      . -
   (exec)  . ,  -
    a.out, -    
  .    exec  ,    -
,    ,    -  
.     exec,        -
,    ,     -
          
 .    , -  -
     ,    -
     wait,     -
,    (      
, .  6.1),    .
         - ()  
 ,      -
.       -  , 
    (wait),  (read)   -
            ptrace, 
 -.     ptrace:

    ptrace(cmd,pid,addr,data);

   cmd   , ,    ,
  ,    .., pid -  -
 , addr -        ,
     , data -  , -
   .      ptrace  -
,        pid      
    ,     -
 ,       . 
  ,  ,     
,   ,      cmd,  addr
  data,  -,     "
 "       .    -
-    (  ),   -
  () ,      
"" .          -
              
        .    -
      ,  -
 ,        -
 .
        -    
     (wait),     ,  
     wait,        -
    .



                                    331
    +------------------------------------------------------+
    | int data[32];                                        |
    | main()                                               |
    | {                                                    |
    |     int i;                                           |
    |     for (i = 0; i < 32; i++)                         |
    |         printf("data[%d] = %d\n@,i,data[i]);         |
    |     printf("ptrace data addr Ox%x\n",data);          |
    | }                                                    |
    +------------------------------------------------------+

       11.2.  trace ( )


      ,    11.2  11.3   -
  trace   debug, .    trace  
 data    ;     
    .     debug     
 ,      trace,    :
          addr,  
,    ptrace    ,  -
  trace.      exec      -
- (   trace)  SIGTRAP ( ), -

    +------------------------------------------------------------+
    | #define TR_SETUP 0                                         |
    | #define TR_WRITE 5                                         |
    | #define TR_RESUME 7                                        |
    | int addr;                                                  |
    |                                                            |
    | main(argc,argv)                                            |
    |      int argc;                                             |
    |      char *argv[];                                         |
    | {                                                          |
    |      int i,pid;                                            |
    |                                                            |
    |      sscanf(argv[1],"%x",&addr);                           |
    |                                                            |
    |      if ((pid = fork() == 0)                               |
    |      {                                                     |
    |          ptrace(TR_SETUP,0,0,0);                           |
    |          execl("trace","trace",0);                         |
    |          exit();                                           |
    |      }                                                     |
    |      for (i = 0; i < 32, i++)                              |
    |      {                                                     |
    |          wait((int *) 0);                                  |
    |          /*   i      |
    |           *  pid  ,   |
    |           *  addr */                             |
    |          if (ptrace(TR_WRITE,pid,addr,i) == -1)            |
    |               exit();                                      |
    |          addr += sizeof(int);                              |
    |      }                                                     |
    |      /*     */    |
    |      ptrace(TR_RESUME,pid,1,0);                            |
    | }                                                          |
    +------------------------------------------------------------+

         11.3.  debug ( )

                                    332



  trace    ,    
 debug.  ,   debug,   -
 ,     wait,    "-
",          
 wait.   debug   ptrace,  -
   i     trace   ,  -
      addr,      addr; 
 trace  addr       data. -
   debug   ptrace     
trace,      data    0  31. -
,    sdb,        
,        , 
    ptrace.
      ptrace      
,     .
  *    ,         
    -   ,    -
      :         
       ptrace,     
          -    
       .   , -
              -
        ,   
     .
  * -        -
    -,          .
       ,  ,     -
         :   --
       fork,       -
    ,   , ,     
      .     -
     exec,          
            ptrace,    
       ,       
    .
  *           , 
           ptrace,    
         .  ,    -
               -
       .
  *    setuid-,      -
             (    
    ptrace         )    
            .   ,   ,   
    setuid-     "privatefile".  -
        ptrace          "/bin/sh",
           shell (  , -
     shell'),       .  
    exec     setuid,    , 
       setuid-    -
     .

     [Killian 84]     , -
     (.  5).  -
     "/proc";   
        ,  -
  "/proc".      ,    -

                                    333

      .   -
        (read)  
     (write)  .  stat -
      ,  .  
   ,    ptrace.  -,  
   ,  -     -
      ,   
 ptrace. -,        -
  ,     . ,  -
         
;        .  -
   setuid-,   -
,        .




      IPC (interprocess communication)   V  UNIX 
   .      
    ,    
        -
 ,   -     -
  .   ,        
 ,    .
  *       ,     
     .
  *       (key),   -
      ,  .
  *          "get",  
            ;  
          (flag).   
          .    
      IPC_PRIVATE     -
    .    IPC_CREAT     ,  -
           ,       
     IPC_EXCL,         ,  
        .    -
       ,    
       ,  ,    -
      creat  open.
  *           -
           :

     =         

    , ,      100 , -
    ,     1,  ,  1, 101, 201
     ..    ,    -
            :    
         ,      -
          "get".  ,    
            ,  -
    .     .    1  -
    ,   201,        -
      ,   301. ,   
     201,  ,     .
          ,   
    ,     .
  *      ,     

                                    334

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




         : msgget,  -
 (    )  ,  -
       , msgctl, 
         
  ,  msgsnd,   ,  msgrcv, 
 .
        msgget:

    msgqid = msgget(key,flag);

 msgqid -   ,  key  flag     -
,       "get".     -
   (),   ,   -
 msgqid       .  -
 ,       ,  -
    :
  *        ;
  *          ;
  *     ;
  *  ,     ;
  *  ,       msgsnd, msgrcv 
    msgctl.
        msgget  ,   

                                    335

,        -
    .    ,  -
  ,      -
.           -
  .
           msgsnd:

    msgsnd(msgqid,msg,count,flag);

 msgqid -     ,      
msgget,  msg -   ,      
     , count -  -
 , flag - ,         
  .
        ( 11.4),      -
      ,      
        ,      
   ,        -
    .     ,   
,    (.  9.1),        
     .    ,
           .  
      -

    +------------------------------------------------------------+
    |  msgsnd       /*   */              |
    |  :  (1)         |
    |                      (2)            |
    |                      (3)                    |
    |                      (4)                              |
    |  :                |
    | {                                                          |
    |            |
    |       ;                          |
    |      (     |
    |     )                                                 |
    |    {                                                       |
    |         (   )                     |
    |              ;                                    |
    |         (  ,    -  |
    |         );                                            |
    |    }                                                       |
    |      ;                           |
    |           - |
    |      ;                                          |
    |      :   |
    |     ,         |
    |     ,  ,  ,   |
    |          - |
    |     ;                                                    |
    |         , |
    |         ;               |
    | }                                                          |
    +------------------------------------------------------------+

               11.4.   


,          -

                                    336

     ,  -
  (           -
,        , -
 ).          ,
    .      -
  ,     ,   -
      . ,     
  ( IPC_NOWAIT),       -
  .   11.5   ,   -
  ,      ,    -
 .
     ,    11.6.    
  msgget  ,       -
 MSGKEY.     256 ,  
    ,       -
  ,     1,   -
  msgsnd   .       
.
       ,   msgrcv   -
:
    count = msgrcv(id,msg,maxcount,type,flag);

 id -  , msg -   , -
    , maxcount -     msg,
type  -   , flag - ,   
 ,    -

                                             
                                              
    +------+                    +->+------+
    |      |     +------+    +------+    +------+ |  |      |
    |    --+---->|      +--->|      +--->|      | |  |      |
    |      |     +---+--+    +---+--+    +---+--+ |  |      |
    +------+         |           |           +----+  |      |
    |      |         +-----------|------------------>+------+
    |      |                     |                   |      |
    |      |                     |                   |      |
    +------+                     |                   |      |
    |      |     +------+        |                   |      |
    |    --+---->|      |        |                   |      |
    |      |     +---+--+        |                   |      |
    +------+         |           |                   |      |
    |   -  |         |           |                   +------+
    |   -  |         +-----------|------------------>+------+
    |   -  |                     |                   |      |
    |   -  |                     |                   |      |
    |   -  |                     +------------------>+------+
    |   -  |                                         |      |
    |   -  |                                         +------+
    |   -  |                                         |   -  |
    |   -  |                                         |   -  |
    |   -  |                                         |   -  |
    +------+                                         +------+

     11.5.  ,    


 .   count     
 .

                                    337

        ( 11.7),     
   .        
,             .  
    ,    ,    
        -
     :      
    ,      
 -,     -
       ,    .  -
- ,    ,      
  -      ,    
 .      maxcount,  -
  ,         
   . ,   ,    -
      (  flag   MSG_NOERROR),  
,            
 .

    +------------------------------------------------------------+
    | #include                                      |
    | #include                                        |
    | #include                                        |
    |                                                            |
    | #define MSGKEY     75                                      |
    |                                                            |
    | struct msgform {                                           |
    |        long     mtype;                                     |
    |        char     mtext[256];                                |
    | };                                                         |
    |                                                            |
    | main()                                                     |
    | {                                                          |
    |        struct msgform msg;                                 |
    |        int msgid,pid,*pint;                                |
    |                                                            |
    |        msgid = msgget(MSGKEY,0777);                        |
    |                                                            |
    |        pid = getpid();                                     |
    |        pint = (int *) msg.mtext;                           |
    |        *pint = pid;         /*    |
    |                              *       |
    |                              *  */                |
    |        msg.mtype = 1;                                      |
    |                                                            |
    |        msgsnd(msgid,&msg,sizeof(int),0);                   |
    |        msgrcv(msgid,&msg,256,pid,0);   /*     |
    |                               *       |
    |                               *    */ |
    |        printf(":     pid %d\n",    |
    |               *pint);                                      |
    | }                                                          |
    +------------------------------------------------------------+

               11.6.  


         ,   -
  type    .      ,
     ,    ,  

                                    338

      ,     -
     type,    -
  . ,      ,  
  3,  1  2, ,      
-2,      1.     ,    
        ,   -
   ,      flag  -
  IPC_NOWAIT (     ).
      ,    11.6  11.8. 
  11.8        
  ().  , ,   , -
   ;    ()    
     ,    
         .
       IPC _CREAT 

    +------------------------------------------------------------+
    |  msgrcv     /*   */              |
    |  :  (1)                |
    |                      (2)  ,   |
    |                                                   |
    |                      (3)                      |
    |                      (4)               |
    |                      (5)                              |
    |  :     |
    | {                                                          |
    |        ;                              |
    |   loop:                                                    |
    |         ;         |
    |      /*  ,   */            |
    |       (    == 0)                   |
    |               ;          |
    |          (    > 0) |
    |               ,   |
    |             ;                                     |
    |           /*     < 0 */|
    |                  - |
    |                ,    |
    |                , -|
    |              ;                                   |
    |       ( )                              |
    |      {                                                     |
    |                -|
    |            ,  ,   |
    |            ;                                            |
    |                  -  |
    |                ;             |
    |               ;            |
    |            ;                              |
    |      }                                                     |
    |      /*   */                                   |
    |       (    )     |
    |              ;                    |
    |       (     -  |
    |       );                                                 |
    |        loop;                                      |
    | }                                                          |
    +------------------------------------------------------------+
              11.7.   

                                    339



  msgget     -
 1 -   -.       ,  
 -      -
     .     -
      -    ,  
     ,   .  -
,       ,  
-    .         
  ,      -
.

    +------------------------------------------------------------+
    | #include                                      |
    | #include                                        |
    | #include                                        |
    |                                                            |
    | #define MSGKEY     75                                      |
    | struct msgform                                             |
    | {                                                          |
    |        long     mtype;                                     |
    |        char     mtext[256];                                |
    | }msg;                                                      |
    | int msgid;                                                 |
    |                                                            |
    | main()                                                     |
    | {                                                          |
    |       int i,pid,*pint;                                     |
    |       extern cleanup();                                    |
    |                                                            |
    |       for (i = 0; i < 20; i++)                             |
    |            signal(i,cleanup);                              |
    |       msgid = msgget(MSGKEY,0777|IPC_CREAT);               |
    |                                                            |
    |       for (;;)                                             |
    |       {                                                    |
    |            msgrcv(msgid,&msg,256,1,0);                     |
    |            pint = (int *) msg.mtext;                       |
    |            pid = *pint;                                    |
    |            printf(":     pid %d\n",|
    |                   pid);                                    |
    |            msg.mtype = pid;                                |
    |            *pint = getpid();                               |
    |            msgsnd(msgid,&msg,sizeof(int),0);               |
    |       }                                                    |
    | }                                                          |
    |                                                            |
    | cleanup()                                                  |
    | {                                                          |
    |       msgctl(msgid,IPC_RMID,0);                            |
    |       exit();                                              |
    | }                                                          |
    +------------------------------------------------------------+

             11.8.   ()


       " - ",      

                                    340

.          
 ,        .  -
,            -
    ,     . 
 ,         -
    ,        
       .
         msgctl     
  ,      
  .   :

    msgctl(id,cmd,mstatbuf)

 id -  , cmd -  , mstatbuf -  -
 ,         -
  .       
 .
       ,    11.8.  -
       cleanup   
 .                
  SIGKILL,       ,     
    .       
      ()    -
   ,        .


    11.2.2

( ) - . . shmget - , shmat , shmdt , shmctl , . , - , . - , ; - - - . shmget: shmid = shmget(key,size,flag); size - . key : - , . IPC_CREAT, , - allocreg ( 6.5.2). , - ( 11.9) , - , . - ( ..) , - . 341 , , . , - , ( ). - - - - +----------+ +--------------+ +---------+ | ----+----+ | | +----+---- | +----------+ +|->+--------------+<----+ +---------+ | ----+---+| | | +---+---- | +----------+ | +--------------+<----+| +---------+ | ----+--+ | | | +|---+---- | +----------+ | | +--------------+ | +---------+ | - | | | | | | | | | - | | +->+--------------+ | +---------+ | - | | | | | | | | - | +--->+--------------+<-----+ +---------+ | - | | | ( | | | - | +--------------+ shmat) +---------+ | - | | - | | | | - | | - | +---------+ | - | +--------------+ | - | | - | | - | +----------+ +---------+ 11.9. , shmat: virtaddr = shmat(id,addr,flags); id, shmget, - , addr , - , (flags) , - - . , virtaddr, - , - , . shmat ( 11.10). ; 0, . ; , . , , brk, , ; - , - . - , - . , , , - . - 342 +------------------------------------------------------------+ | shmat /* */ | | : (1) | | | | (2) | | | | (3) | | : , | | | | { | | , - | | ; | | ( ) | | { | | - | | ; | | , | | ; | | } | | /* , | | * */ | | : | | ; | | | | ( attachreg); | | ( ) | | | | ( growreg); | | ( | | ); | | } | +------------------------------------------------------------+ 11.10. attachreg. , - , , - growreg, , - . - shmdt(addr) addr - , shmat. , , - , - - , . , detachreg ( 6.5.7). - , , , . , 11.11. 343 , 128 - . "" , "" - . 11.12 , ( 64 , , - ); , , . "" (pause), - ; , . shmctl: shmctl(id,cmd,shmstatbuf); id , cmd , shmstatbuf , . - , . - , : , , freereg ( 6.5.6). - - ( 0), , , - . , , , . - , . , - .

    11.2.3

- , - ( ). , , - creat . , creat , , . , , , , . , - , : P V (. [Dijkstra 68]). P , 0, V - ( , ). , - P V. , , , , 1. ; , 344 . - , ; , . V UNIX : * , * , , * , , * , , 0. semget, - semctl, - semop. +------------------------------------------------------------+ | #include | | #include | | #include | | #define SHMKEY 75 | | #define K 1024 | | int shmid; | | | | main() | | { | | int i, *pint; | | char *addr1, *addr2; | | extern char *shmat(); | | extern cleanup(); | | | | for (i = 0; i < 20; i++) | | signal(i,cleanup); | | shmid = shmget(SHMKEY,128*K,0777|IPC_CREAT); | | addr1 = shmat(shmid,0,0); | | addr2 = shmat(shmid,0,0); | | printf("addr1 Ox%x addr2 Ox%x\n",addr1,addr2); | | pint = (int *) addr1; | | | | for (i = 0; i < 256, i++) | | *pint++ = i; | | pint = (int *) addr1; | | *pint = 256; | | | | pint = (int *) addr2; | | for (i = 0; i < 256, i++) | | printf("index %d\tvalue %d\n",i,*pint++); | | | | pause(); | | } | | | | cleanup() | | { | | shmctl(shmid,IPC_RMID,0); | | exit(); | | } | +------------------------------------------------------------+ 11.11. 345 +-----------------------------------------------------+ | #include | | #include | | #include | | | | #define SHMKEY 75 | | #define K 1024 | | int shmid; | | | | main() | | { | | int i, *pint; | | char *addr; | | extern char *shmat(); | | | | shmid = shmget(SHMKEY,64*K,0777); | | | | addr = shmat(shmid,0,0); | | pint = (int *) addr; | | | | while (*pint == 0) | | ; | | for (i = 0; i < 256, i++) | | printf("%d\n",*pint++); | | } | +-----------------------------------------------------+ 11.12. +-------+ | | +---+---+---+---+---+---+---+ | +------->| 0 | 1 | 2 | 3 | 4 | 5 | 6 | | | +---+---+---+---+---+---+---+ +-------+ | | +---+---+---+ | +------->| 0 | 1 | 2 | | | +---+---+---+ +-------+ | | +---+ | +------->| 0 | | | +---+ +-------+ | | +---+---+---+ | +------->| 0 | 1 | 2 | | | +---+---+---+ +-------+ | - | | - | | - | | - | | - | +-------+ 11.13. , 346 semget: id = semget(key,count,flag); key, flag id , - ( ). - , - count ( 11.13). - , semop semctl. - semget 11.14, , . semop: oldval = semop(id,oplist,count); id - , semget, oplist - , count - . oldval , +------------------------------------------------------------+ | #include | | #include | | #include | | | | #define SEMKEY 75 | | int semid; | | unsigned int count; | | /* sembuf sys/sem.h | | * struct sembuf { | | * unsigned shortsem_num; | | * short sem_op; | | * short sem_flg; | | }; */ | | struct sembuf psembuf,vsembuf; /* P V */| | | | main(argc,argv) | | int argc; | | char *argv[]; | | { | | int i,first,second; | | short initarray[2],outarray[2]; | | extern cleanup(); | | | | if (argc == 1) | | { | | for (i = 0; i < 20; i++) | | signal(i,cleanup); | | semid = semget(SEMKEY,2,0777|IPC_CREAT); | | initarray[0] = initarray[1] = 1; | | semctl(semid,2,SETALL,initarray); | | semctl(semid,2,GETALL,outarray); | | printf(" %d %d\n", | | outarray[0],outarray[1]); | | pause(); /* */ | | } | | | | /* */ | +------------------------------------------------------------+ 11.14. 347 . - : * , , , * , * . +------------------------------------------------------------+ | else if (argv[1][0] == 'a') | | { | | first = 0; | | second = 1; | | } | | else | | { | | first = 1; | | second = 0; | | } | | | | semid = semget(SEMKEY,2,0777); | | psembuf.sem_op = -1; | | psembuf.sem_flg = SEM_UNDO; | | vsembuf.sem_op = 1; | | vsembuf.sem_flg = SEM_UNDO; | | | | for (count = 0; ; count++) | | { | | psembuf.sem_num = first; | | semop(semid,&psembuf,1); | | psembuf.sem_num = second; | | semop(semid,&psembuf,1); | | printf(" %d %d\n",getpid(),count); | | vsembuf.sem_num = second; | | semop(semid,&vsembuf,1); | | vsembuf.sem_num = first; | | semop(semid,&vsembuf,1); | | } | | } | | | | cleanup() | | { | | semctl(semid,2,IPC_RMID,0); | | exit(); | | } | +------------------------------------------------------------+ 11.14. () oplist , - ( 11.15). , . - , , - . - , 348 +------------------------------------------------------------+ | semop /* */ | | : (1) | | (2) | | (3) | | : | | { | | ; | | start: - | | ; | | - | | ; | | | | ( ) | | { | | ( ) | | { | | ; | | ( UNDO)| | | | ; | | , | | ; | | } | | ( -| | ) | | { | | ( + >= 0) | | { | | - | | ; | | ( UNDO ) | | - | | ; | | ( 0) | | /* - | | * */ | +------------------------------------------------------------+ 11.15. , . , - - . . , , . 0, : 0, ; , , , "". , ( ) . - 0, , - . , , - . - , , ; , - , . 349 +------------------------------------------------------------+ | | | , -| | ; | | ; | | } | | | | - | | ( - | | ); | | ( ) | | ; | | ( , - | | ); | | start; /* | | * * / | | } | | /* */| | { | | ( ) | | { | | | | - | | ( | | ); | | ( ) | | ; | | ( , | | ); | | start; /* */ | | } | | } | | } /* */ | | /* */ | | , -| | | | ; | | , | | semop; | | } | +------------------------------------------------------------+ 11.15. () , 11.14, , ( a.out) - : a.out & a.out a & a.out b & , - , 1. - pause - , (cleanup). - 'a' (A) : 0, 1, 0 1. , - 350 0, , , ( ). - 1 - , A , - 1 0. 'b' (B) 0 1 , - A. A B , , A 0 1, B 1 0. , . , . , - . : struct sembuf psembuf[2]; psembuf[0].sem_num = 0; psembuf[1].sem_num = 1; psembuf[0].sem_op = -1; psembuf[1].sem_op = -1; semop(semid,psembuf,2); Psembuf - , 0 1. - , - . , , 0 1, 1 0, , , . IPC_NOWAIT semop : , - , , 0, IPC_NOWAIT , . , , . , - , , . , , . , , kill, , - . , , - , , , . - , semop SEM_UNDO; , , . , - . - , ( 11.16). - - , . , - semop SEM_UNDO. - - 351 +------+ | - | | - | | - | | - | +----------+ +----------+ +----------+ +------+ || || || | +-->| +-->| +-->| | +------+ | | | | | | | | +----------+ +----------+ +----------+ | | +----------+ +------+ || | +-->| | +------+ | | | - | +----------+ | - | | - | | - | +------+ 11.16. , . , . , - , , SEM_UNDO. , , - . 0, . , - +---------------++-------+ +---------------++-------+-------+ | || | | || | | | || semid | | || semid | semid | +---------------++-------+ +---------------++-------+-------+ | || 0 | | || 0 | 1 | +---------------++-------+ +---------------++-------+-------+ | || | | || | | | || 1 | | || 1 | 1 | +---------------++-------+ +---------------++-------+-------+ () () +---------------++-------+ | || | | || semid | +---------------++-------+ | || 0 | +---------------++-------+ | || | | || 1 | +---------------++-------+ () () 11.17. 352 , - . , - , , - , 0. 11.17 'a'. - , semid, - , 0, , 1, , 1, - , 1. , - , - 0. - 1 , - , , - . , - . - , , , - . , - , - , . semctl: semctl(id,number,cmd,arg); arg : union semunion { int val; struct semid_ds *semstat; /* . - * */ unsigned short *array; } arg; arg cmd, , ioctl ( 10). - , cmd: ( .), , . - . - , IPC_RMID, , - , - . - , - : - , , - , .

    11.2.4

353 . "get" - creat open, "control" , unlink. , - , , close. , , IPC, , - , , , "get". - , , . , - , . , - - , - - . , , (keys). , - . - , - . (. 13). , " ", , , - , , . , , , . , (. ).

    11.3

, , , - , , - - . , , , , ( "mjb" "/usr/mail/mjb"). - , mail () - , . , mail . , , mail . , , , ; - , - . - () . UNIX fork, , , . - ( - ), . 354 , , init, , , - , . , , ; , - . uucp, - (. [Nowitz 80]). - - (, ), , - - . , - uucp; init getty- - , . - - : getty- , uucico, "/etc/passwd", - - , - . UNIX , , - . - . , . , - , , , - . - ioctl, -. , , , , - . UNIX, . V - , - - . - BSD, .

    11.4

, , , , - . , - , - , () , - open read . - BSD , - "sockets" () (. [Berkeley 83]). ( ). 355 - - | | +--+ +--+ +-------------------------+--+ +--+--------------------------+ | | | | +-------------------------+--+ +--+--------------------------+ | TCP | | TCP | | | | | | | | IP | | IP | +-------------------------+--+ +--+--------------------------+ | | | | | Ethernet| |Ethernet | +-------------------------+--+ +--+--------------------------+ +---+ +---+ | | 11.18. : , ( 11.18). - ( ) , , ( - TCP IP), , . - ( ); . - -: , , - - , , - . - . , , , - ( ), - ( ). BSD 4.2 : "UNIX system" - "Internet" () - DARPA ( - ) (. [Postel 80] [Postel 81]). : - ( , ) . - . , - , - ; , . - . , , "Internet" - (TCP), - (UDP). . socket . sd = socket(format,type,protocol); Format ("UNIX system" "Internet"), type - - ( ), protocol - , . sd, socket, . 356 close. bind : bind(sd,address,length); sd - , address - , - , ( socket). Length - address; , , . , "UNIX system" . - - -. connect - : connect(sd,address,length); (. bind), address , - . - , . - , connect - send ; . - , - . - listen: listen(sd,qlength) sd - , qlength - - - , . +--------------------+ +-------------------------+ | - | | - | | | | | | - | | | | | +----+ ------ | | | | | | - | | | | |listen addr accept addr| +---------+----------+ +-----+-------------------+ | | - +--------------------------+------------- 11.19. accept , -: nsd = accept(sd,address,addrlen); sd - , address - , , addrlen - . addrlen , . - (nsd), sd. - , ( 11.19). 357 send recv . send: count = send(sd,msg,length,flags); sd - , msg - , length - , count - . flags SOF_OOB ( out-of-band - " - "), . , - , out-of-band , "delete". recv: count = recv(sd,buf,length,flags); buf - , length - , count - , . (flags) , - , - out-of-band. - , sendto recvfrom, - . send recv read write. , , , read write, . shutdown : shutdown(sd,mode) mode , (, - ) . , - , , . close. getsockname , - bind: getsockname(sd,name,length); getsockopt setsockopt - - . , 11.20. "UNIX system" sockname. listen - . accept , ; , - . - , - -; - , . - - read. - - . 11.21 -, . , , sockname. 358 +------------------------------------------------------------+ | #include | | #include | | | | main() | | { | | int sd,ns; | | char buf[256]; | | struct sockaddr sockaddr; | | int fromlen; | | | | sd = socket(AF_UNIX,SOCK_STREAM,0); | | | | /* - */ | | bind(sd,"sockname",sizeof("sockname") - 1); | | listen(sd,1); | | | | for (;;) | | { | | | | ns = accept(sd,&sockaddr,&fromlen); | | if (fork() == 0) | | { | | /* */ | | close(sd); | | read(ns,buf,sizeof(buf)); | | printf(" '%s'\n",buf); | | exit(); | | } | | close(ns); | | } | | } | +------------------------------------------------------------+ 11.20. - "UNIX system" +------------------------------------------------------------+ | #include | | #include | | | | main() | | { | | int sd,ns; | | char buf[256]; | | struct sockaddr sockaddr; | | int fromlen; | | | | sd = socket(AF_UNIX,SOCK_STREAM,0); | | | | /* | | /* */ | | if (connect(sd,"sockname",sizeof("sockname") - 1) == -1)| | exit(); | | | | write(sd,"hi guy",6); | | } | +------------------------------------------------------------+ 11.21. - "UNIX system" 359 - . - . , , - "Internet", : socket(AF_INET,SOCK_STREAM,0); , . BSD , . connect , - ( - ), , - -. - , select , .

    11.5

. , , - , . ptrace , , , -, , - . V UNIX (IPC), , . - , , - - . , . UNIX . - ioctl, -- . BSD , - . V 10 , .

    11.6

1. , debug wait ( 11.3) ? (: .) 2. ptrace . - , - ? - ptrace ? 3. ptrace , - pid , . , - : 360 , ? - ? 4. - , - . , ; . - , , , - ( ). - . - . , - , , - . 5. , 11.22 ? *6. , - . ? 7. , 11.14, IPC_NOWAIT, . , . 8. , P V . P ? 9. , () , () creat unlink, () - . . 10. - , , read write, . 11. . , , - - . +------------------------------------------------------------+ | #include | | #include | | #include | | #define ALLTYPES 0 | | | | main() | | { | | struct msgform | | { | | long mtype; | | char mtext[1024]; | | } msg; | | register unsigned int id; | | | | for (id = 0; ; id++) | | while (msgrcv(id,&msg,1024,ALLTYPES,IPC_NOWAIT) > 0)| | ; | | } | +------------------------------------------------------------+ 361

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