Unix,进程,线程,同步,互斥,通信

在POSIX线程(pthread)情况下

   1. 互斥量:pthread_mutex_t

          相关函数:    PTHREAD_MUTEX_INITIALIZER
                             phread_mutex_init
                             phread_mutex_destroy
                             phread_mutex_lock
                             phread_mutex_trylock
                             phread_mutex_unlock


   2. 条件变量:pthread_cond_t

 

          相关函数:    PTHREAD_COND_INITIALIZER
                             pthread_cond_init
                             pthread_cond_destroy
                             pthread_cond_wait
                             pthread_cond_timedwait
                             pthread_cond_signal
                             pthread_cond_broadcast


   3. 读写锁:pthread_rwlock_t      

 

          相关函数:    pthread_rwlock_init
                             pthread_rwlock_destroy
                             pthread_rwlock_wrlock
                             pthread_rwlock_rdlock
                             pthread_rwlock_trywrlock
                             pthread_rwlock_tryrdlock
                             pthread_rwlock_unlock

 

Unix进程/线程同步:

      1. 信号量/信号灯semaphore
              特点: 信号量主要作为进程间以及同进程不同线程之间的同步手段。
              相关函数: semget
                               semctl
                               semop

 

Unix进程间通信:

       1. 管道

               特点:  a. 历史上是半双工的;
                              b. 只能在具有公共祖先的进程之间使用;
                              c. 没有名字;
                              d. 只能承载无格式字节流;
                              e. 缓冲区大小受限.

               相关函数:   int pipe(int filedes[2] );
                                   FILE * popen(const char * cmdstring, const char * type);
                                   int pclose(FILE * fp);

         
      2. FIFO

                特点:a. 能在不同的进程之间使用;
                             b.有名字,在文件系统中有对应的文件名。

                  相关函数: mkfifo();

      3. socket
             
更为一般的进程间通信机制,可用于不同机器之间的进程间通信。

 

      4. XSI IPC
                * XSI IPC源自system V的IPC功能
                    * XSI IPC不使用文件系统名字空间,而是构造了它们自己的名字空间。
                    * XSI IPC不使用文件描述符,所以不能对它们使用多路转接I/O函数select和poll。

             (1)消息队列             
                           特点: a.消息队列克服了信号承载信息量少,管道只能承载无格式字节流以及缓冲区大小受限等缺点。
                b.用户可以自定义消息结构

                           相关函数:  msgget
                                            msgsnd
                                            msgctl
                                            msgrcv          

 

 

             (2)信号量semaphore

                           相关函数:  semget
                                            semop
                                            semctl

             (3)共享存储(共享内存)
                    
    特点: a. 最快的IPC形式;
                                        b. 针对其他通信机制运行效率较低而设计的;
                                        c. 往往与其他通信机制(如信号量)结合使用,以达到进程间的同步及互斥;

                         相关函数:   shmget                                          
                                           shmat                          
                                           shmdt
                                           shmctl

 

       5. STREAMS

       6. 信号signal
              a. 信号是软件中断            
                  b.
承载信息量少
                  c. 信号提供了一种处理异步事件的方法

 

你可能感兴趣的:(socket,unix,Semaphore,File,System,Signal)