前言:Linux下的進程通信(IPC)
Linux下的進程通信(IPC)
POSIX無名信號量
System V信號量
System V消息隊列
System V共享內存
1。POSIX無名信號量 如果你學習過操作系統,那么肯定熟悉PV操作了.PV操作是原子
操作.也就是操作是不可以中斷的,在一定的時間內,只能夠有一個進程的代碼在CPU上面
執行.在系統當中,有時候為了順利的使用和保護共享資源,大家提出了信號的概念. 假設
我們要使用一臺打印機,如果在同一時刻有兩個進程在向打印機輸出,那么最終的結果會
是什么呢.為了處理這種情況,POSIX標準提出了有名信號量和無名信號量的概念,由于Li
nux只實現了無名信號量,我們在這里就只是介紹無名信號量了. 信號量的使用主要是用
來保護共享資源,使的資源在一個時刻只有一個進程所擁有.為此我們可以使用一個信號
燈.當信號燈的值為某個值的時候,就表明此時資源不可以使用.否則就表>示可以使用.
為了提供效率,系統提供了下面幾個函數
POSIX的無名信號量的函數有以下幾個:
- #include <semaphore.h>
- int sem_init(sem_t *sem,int pshared,unsigned int value);
- int sem_destroy(sem_t *sem);
- int sem_wait(sem_t *sem);
- int sem_trywait(sem_t *sem);
- int sem_post(sem_t *sem);
- int sem_getvalue(sem_t *sem);
sem_init創建一個信號燈,并初始化其值為value.pshared決定了信號量能否在幾個進程
間共享.由于目前Linux還沒有實現進程間共享信號燈,所以這個值只能夠取0. sem_dest
roy是用來刪除信號燈的.sem_wait調用將阻塞進程,直到信號燈的值大于0.這個函數返回
的時候自動的將信號燈的值的件一.sem_post和sem_wait相反,是將信號燈的內容加一同
時發出信號喚醒等待的進程..sem_trywait和sem_wait相同,不過不阻塞的,當信號燈的值
為0的時候返回EAGAIN,表示以后重試.sem_getvalue得到信號燈的值.
由于Linux不支持,我們沒有辦法用源程序解釋了.
這幾個函數的使用相當簡單的.比如我們有一個程序要向一個系統打印機打印兩頁.我們
首先創建一個信號燈,并使其初始值為1,表示我們有一個資源可用.然后一個進程調用se
m_wait由于這個時候信號燈的值為1,所以這個函數返回,打印機開始打印了,同時信號燈
的值為0 了. 如果第二個進程要打印,調用sem_wait時候,由于信號燈的值為0,資源不可
用,于是被阻塞了.當第一個進程打印完成以后,調用sem_post信號燈的值為1了,這個時候
系統通知第二個進程,于是第二個進程的sem_wait返回.第二個進程開始打印了.
不過我們可以使用線程來解決這個問題的.我們會在后面解釋什么是線程的.編譯包含上
面這幾個函數的程序要加上 -lrt選賢,以連接librt.so庫
2。System V信號量 為了解決上面哪個問題,我們也可以使用System V信號量.很幸運的
是Linux實現了System V信號量.這樣我們就可以用實例來解釋了. System V信號量的函
數主要有下面幾個.
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- key_t ftok(char *pathname,char proj);
- int semget(key_t key,int nsems,int semflg);
- int semctl(int semid,int semnum,int cmd,union semun arg);
- int semop(int semid,struct sembuf *spos,int nspos);
- struct sembuf {
- short sem_num;
- short sem_op;
- short sem_flg;
- };
ftok函數是根據pathname和proj來創建一個關鍵字.semget創建一個信號量.成功時返回
信號的ID,key是一個關鍵字,可以是用ftok創建的也可以是IPC_PRIVATE表明由系統選用
一個關鍵字. nsems表明我們創建的信號個數.semflg是創建的權限標志,和我們創建一個
文件的標志相同.
semctl對信號量進行一系列的控制.semid是要操作的信號標志,semnum是信號的個數,cm
d是操作的命令.經常用的兩個值是:SETVAL(設置信號量的值)和IPC_RMID(刪除信號燈).
arg是一個給cmd的參數.
semop是對信號進行操作的函數.semid是信號標志,spos是一個操作數組表明要進行什么
操作,nspos表明數組的個數. 如果sem_op大于0,那么操作將sem_op加入到信號量的值中
,并喚醒等待信號增加的進程. 如果為0,當信號量的值是0的時候,函數返回,否則阻塞直
到信號量的值為0. 如果小于0,函數判斷信號量的值加上這個負值.如果結果為0喚醒等待
信號量為0的進程,如果小與0函數阻塞.如果大于0,那么從信號量里面減去這個值并返回
..
下面我們一以一個實例來說明這幾個函數的使用方法.這個程序用標準錯誤輸出來代替我
們用的打印機.
- #include <stdio.h>
- #include <unistd.h>
- #include <limits.h>
- #include <errno.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #define PERMS S_IRUSR|S_IWUSR
- void init_semaphore_struct(struct sembuf *sem,int semnum,
- int semop,int semflg)
- {
-
- sem->sem_num=semnum;
- sem->sem_op=semop;
- sem->sem_flg=semflg;
- }
- int del_semaphore(int semid)
- {
-
-
-
- #if 1
- return semctl(semid,0,IPC_RMID);
- #endif
- }
- int main(int argc,char **argv)
- {
- char buffer[MAX_CANON],*c;
- int i,n;
- int semid,semop_ret,status;
- pid_t childpid;
- struct sembuf semwait,semsignal;
- if((argc!=2)||((n=atoi(argv[1]))<1))
- {
- fprintf(stderr,"Usage:%s number\n\a",argv[0]);
- exit(1);
- }
-
-
- if((semid=semget(IPC_PRIVATE,1,PERMS))==-1)
- {
- fprintf(stderr,"[%d]:Acess Semaphore Error:%s\n\a",
- getpid(),strerror(errno));
- exit(1);
- }
-
- init_semaphore_struct(&semwait,0,-1,0);
-
- init_semaphore_struct(&semsignal,0,1,0);
-
- if(semop(semid,&semsignal,1)==-1)
- {
- fprintf(stderr,"[%d]:Increment Semaphore Error:%s\n\a",
- getpid(),strerror(errno));
- if(del_semaphore(semid)==-1)
- fprintf(stderr,"[%d]:Destroy Semaphore Error:%s\n\a",
- getpid(),strerror(errno));
- exit(1);
- }
-
- for(i=0;i<n;i++)
- if(childpid=fork()) break;
- sprintf(buffer,"[i=%d]-->[Process=%d]-->[Parent=%d]-->[Child=%d]\n",
- i,getpid(),getppid(),childpid);
- c=buffer;
-
- while(((semop_ret=semop(semid,&semwait,1))==-1)&&(errno==EINTR));
- if(semop_ret==-1)
- {
- fprintf(stderr,"[%d]:Decrement Semaphore Error:%s\n\a",
- getpid(),strerror(errno));
- }
- else
- {
- while(*c!='\0')fputc(*c++,stderr);
-
- while(((semop_ret=semop(semid,&semsignal,1))==-1)&&(errno==EINTR));
- if(semop_ret==-1)
- fprintf(stderr,"[%d]:Increment Semaphore Error:%s\n\a",
- getpid(),strerror(errno));
- }
-
- while((wait(&status)==-1)&&(errno==EINTR));
-
- if(i==1)
- if(del_semaphore(semid)==-1)
- fprintf(stderr,"[%d]:Destroy Semaphore Error:%s\n\a",
- getpid(),strerror(errno));
- exit(0);
- }
信號燈的主要用途是保護臨界資源(在一個時刻只被一個進程所擁有).
3。SystemV消息隊列 為了便于進程之間通信,我們可以使用管道通信 SystemV也提供了
一些函數來實現進程的通信.這就是消息隊列.
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- int msgget(key_t key,int msgflg);
- int msgsnd(int msgid,struct msgbuf *msgp,int msgsz,int msgflg);
- int msgrcv(int msgid,struct msgbuf *msgp,int msgsz,
- long msgtype,int msgflg);
- int msgctl(Int msgid,int cmd,struct msqid_ds *buf);
-
- struct msgbuf {
- long msgtype;
- .......
- }
msgget函數和semget一樣,返回一個消息隊列的標志.msgctl和semctl是對消息進行控制
.. msgsnd和msgrcv函數是用來進行消息通訊的.msgid是接受或者發送的消息隊列標志.
msgp是接受或者發送的內容.msgsz是消息的大小. 結構msgbuf包含的內容是至少有一個
為msgtype.其他的成分是用戶定義的.對于發送函數msgflg指出緩沖區用完時候的操作.
接受函數指出無消息時候的處理.一般為0. 接收函數msgtype指出接收消息時候的操作.
如果msgtype=0,接收消息隊列的第一個消息.大于0接收隊列中消息類型等于這個值的第
一個消息.小于0接收消息隊列中小于或者等于msgtype絕對值的所有消息中的最小一個消
息. 我們以一個實例來解釋進程通信.下面這個程序有server和client組成.先運行服務
端后運行客戶端.
服務端 server.c
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/stat.h>
- #include <sys/msg.h>
- #define MSG_FILE "server.c"
- #define BUFFER 255
- #define PERM S_IRUSR|S_IWUSR
- struct msgtype {
- long mtype;
- char buffer[BUFFER+1];
- };
- int main()
- {
- struct msgtype msg;
- key_t key;
- int msgid;
- if((key=ftok(MSG_FILE,'a'))==-1)
- {
- fprintf(stderr,"Creat Key Error:%s\a\n",strerror(errno));
- exit(1);
- }
- if((msgid=msgget(key,PERM|IPC_CREAT|IPC_EXCL))==-1)
- {
- fprintf(stderr,"Creat Message Error:%s\a\n",strerror(errno));
- exit(1);
- }
- while(1)
- {
- msgrcv(msgid,&msg,sizeof(struct msgtype),1,0);
- fprintf(stderr,"Server Receive:%s\n",msg.buffer);
- msg.mtype=2;
- msgsnd(msgid,&msg,sizeof(struct msgtype),0);
- }
- exit(0);
- }
- ----------------------------------------------------------------------------
客戶端(client.c)
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/stat.h>
- #define MSG_FILE "server.c"
- #define BUFFER 255
- #define PERM S_IRUSR|S_IWUSR
- struct msgtype {
- long mtype;
- char buffer[BUFFER+1];
- };
- int main(int argc,char **argv)
- {
- struct msgtype msg;
- key_t key;
- int msgid;
- if(argc!=2)
- {
- fprintf(stderr,"Usage:%s string\n\a",argv[0]);
- exit(1);
- }
- if((key=ftok(MSG_FILE,'a'))==-1)
- {
- fprintf(stderr,"Creat Key Error:%s\a\n",strerror(errno));
- exit(1);
- }
- if((msgid=msgget(key,PERM))==-1)
- {
- fprintf(stderr,"Creat Message Error:%s\a\n",strerror(errno));
- exit(1);
- }
- msg.mtype=1;
- strncpy(msg.buffer,argv[1],BUFFER);
- msgsnd(msgid,&msg,sizeof(struct msgtype),0);
- memset(&msg,'\0',sizeof(struct msgtype));
- msgrcv(msgid,&msg,sizeof(struct msgtype),2,0);
- fprintf(stderr,"Client receive:%s\n",msg.buffer);
- exit(0);
- }
注意服務端創建的消息隊列最后沒有刪除,我們要使用ipcrm命令來刪除的.
4。SystemV共享內存 還有一個進程通信的方法是使用共享內存.SystemV提供了以下幾個
函數以實現共享內存.
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- int shmget(key_t key,int size,int shmflg);
- void *shmat(int shmid,const void *shmaddr,int shmflg);
- int shmdt(const void *shmaddr);
- int shmctl(int shmid,int cmd,struct shmid_ds *buf);
shmget和shmctl沒有什么好解釋的.size是共享內存的大小. shmat是用來連接共享內存
的.shmdt是用來斷開共享內存的.不要被共享內存詞語嚇倒,共享內存其實很容易實現和
使用的.shmaddr,shmflg我們只要用0代替就可以了.在使用一個共享內存之前我們調用s
hmat得到共享內存的開始地址,使用結束以后我們使用shmdt斷開這個內存.
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #define PERM S_IRUSR|S_IWUSR
- int main(int argc,char **argv)
- {
- int shmid;
- char *p_addr,*c_addr;
- if(argc!=2)
- {
- fprintf(stderr,"Usage:%s\n\a",argv[0]);
- exit(1);
- }
- if((shmid=shmget(IPC_PRIVATE,1024,PERM))==-1)
- {
- fprintf(stderr,"Create Share Memory Error:%s\n\a",strerror(errno));
- exit(1);
- }
- if(fork())
- {
- p_addr=shmat(shmid,0,0);
- memset(p_addr,'\0',1024);
- strncpy(p_addr,argv[1],1024);
- exit(0);
- }
- else
- {
- c_addr=shmat(shmid,0,0);
- printf("Client get %s",c_addr);
- exit(0);
- }
- }
這個程序是父進程將參數寫入到共享內存,然后子進程把內容讀出來.最后我們要使用ip
crm釋放資源的.先用ipcs找出ID然后用ipcrm shm ID刪除.
后記:
進程通信(IPC)是網絡程序的基礎,在很多的網絡程序當中會大量的使用進程通信的概念
和知識.其實進程通信是一件非常復雜的事情,我在這里只是簡單的介紹了一下.如果你想
學習進程通信的詳細知識,最好的辦法是自己不斷的寫程序和看聯機手冊.現在網絡上有
了很多的知識可以去參考.可惜我看到的很多都是英文編寫的.如果你找到了有中文的版
本請盡快告訴我.謝謝!