posixプロセス間通信

12646 ワード

************************************************************************************************** posix    

                                      。

    3       。

posix      :  posix IPC    ,             。

posix        :        ,             。

system v   :      ,             。

  sem_open                          。       

          ,          。

#include<semaphore.h>

sem_t *sem_open (const char *name,int flag,.../*mode_t mode,unsigned int value*/);

  :             ,     SEM_FAILED

oflag     0,O_CREAT O_CREAT|O_EXCL,     O_CREAT  ,               :  

mode       ,value           。        ZSEM_VALUE_MAX(         32767)。

            1,              1.

     O_CREAT(   O_EXCL,                     。                

O_EXCL       。                     ,         。           

     O_CREAT|O_EXCL      。

sem_open         sem_t       。     sem_close,sem_wait,sem_trywait,sem_post,  sem_getvalue

   。

  sem_open       ,  sem_close    。

#include<semaphore.h>

int sem_close(sem_t *sem_t);

  :     0,     -1;

       ,                                。          (  

  exit _exit)         ,          。

                  。    posix              :             

   ,       。

       sem_unlink      。

#include<semaphore.h>

#sem_unlink(const char *name);

                     (      ),sem_unlink     I/O unlink  :     

    0 ,name          ,         (                )        

sem_close    。

sem_wait sem_trywait  

sem_wait             ,      0,      1     。      0,        

   ,        0,      1,      。

#include<semaphore.h>

int sem_wait(sem_t *sem)

int sem_trywait(sem_t *sem)

   :     0,     -1sem_wait sen_trywait     :           0 ,             ,  ,       ;

sem_post sem_getvalue  

               ,     sem_post。            1,               

       。

#include<semaphore.h>

int sem_post(sem_t *sem);

int sem_getvalue(sem_t *sem,int *valp);

   :     0,     -1

sem_getvalue  valp                  。            ,      0,      

,                  。



         posix     。         name     ,               。  posix

           ,                 (       sem_t         ),     

       。

#include<semaphore.h>

int sem_init(sem_t *sem,int shared,unsigned int value);

  :     -1int sem_destory(sem_t  *sem);

  :     0,     -1;

          sem_init    。sem              sem_t  。  shared 0,                         ,              。 shared  0 ,            

                  。 sem_open  ,value           。

              ,    sem_destory   。

sem_open      shared   ,                     。             

     O_CREAT     ,    ,sem_init          。  ,          ,            sem_init  。               sem_init        。

      sem_open sem_init       。          sem_t     ,    sem_open      

    。            sem_t     ,         ,   sem_init   。

                ,          。                    ,    

     。                   ,                        。    

                      ,         。

1>                          (sem_init shared    0),              

,           。

2>                       (sem_init shared   1),                ,

             ,          。posix      system v               。    

            ,           posix        ,    。     ,       

         ,               。

  ,               。

sem_t mysem;

sem_init(&mysem,1,0)

if(fork()==0){//child 

.....

sem_post(&mysem);

}

sem_wait(&mysem);//parent;wait for child 

       mysem        ,fork                   。            

       ,            。

        

                      :     (     semz_init      sem_t      )

                        ,  sem_init         1。

       ,    (           )               ,

       sem_open ,          。           sem_open                 

,           (sem_post sem_wait)                。

     sem_open    sem_t              fork,        ?posix   fork

       “                       。”             :

sem_t *mutex;/*global pointer that is copied across the fork*/

//parent creates named semaphore

mutex=sem_open(name,O_CREAT|O_EXCL,FILE_MODE,0)

if((child==fork())==0){

 //child

......

 sem_wait(mutex);

}

//parent

sem_post(mutex);

.....

                                    :

                 sem_t     ,            

(       )。  system v          semget

         。                     。system v   

             ,                       。



*************************************************************************************** posix     

      (memory-mmaped file):

 open    , mmap                          )

       (shared——mmaped file):

 shm_open    posix.1 IPC  (               ),

        mmap              。

         mmap  ,      mmap              :  open   shm_pen。

posix      

fd=open(pathname,....);

ptr=mmap(...,fd,.....);

posix       

fd=shm_open(name,...);

ptr=mmap(....,fd,.....);

posix                。1)          shm_open,                               。

(2)  mmap                    。   shm_open                

       。

#include<sys/mman.h>

int shm_open(const char *name,int oflag,mode_t mode);

  :           ,     -1int shm_unlink(const char *name);

  :     0,     -1;

name  :        ,          ;

oflag         O_RDONLY(  )  ,    O_RDWR(  )  ,         :O_CREAT,O_EXCL,

 O_TRUNC.   O_RDWR  O_TRUNC  ,             ,        0  。

mode       ,     O_CREAT        。   mq_open sem_open    ,shm_open mode  

      。      O_CREAT  ,          0;

shm_open            ,     mmap  5   。

shm_unlink                。      unlink  (             unlink,  

  posix     mq_unlink,      posix      sem_unlink)  ,               

         ,             。             open sem_open      。



ftruncate fstat  

  mmap   ,                       ftruncate  

#include<unistd.h>

posix                          。

1>        :         length  ,          。         length,  

                      。          ,        length         :

 lseek    length-1 ,  write 1      。         unix       ftruncate      。

2>           :ftruncate          length  。

    ftruncate                 ,             。             

    ,      fstat           。

#include<sys/type.h>

#include<sys/stat.h>

int fstat(int fd,struct stat *buf);

  :     0,     -1
, , 。