一、共用記憶體:
系統調用:shmget();當shmget()建立了一塊新的共用記憶體後,返回一個可以用於引用該共用記憶體的shmid_ds資料結構的標識符。
原型:int shmget(key_t key,int size,int shmflg);
傳回值:如果成功,返回共用記憶體段標識符。
如果失敗,則返回-1
系統調用:shmat();將共用記憶體區域對應到自己進程中去。
原型:int shmat(int shmid,char *shmadddr,int shmflg);
傳回值:如果成功,則返回共用記憶體段串連到進程中的地址。
如果失敗,則返回-1。
系統調用:shmdt();當一個進程不再需要共用的記憶體段時,它將會把記憶體段從其他地址空間中脫離。
原型:int shmdt(char *shmaddr);
傳回值:如果失敗,則返回-1
執行個體代碼:shmadd.c
#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <stdio.h>#include <stdlib.h>#define BUFSZ 2048int main(){ int shmid; char *shmadd; if((shmid=shmget(IPC_PRIVATE,BUFSZ,0666))<0){ perror("shmget"); exit(1);}else printf("created shared-memory:%d\n",shmid);system("ipcs -m");/*ipcs 命令往標準輸出寫入一些關於活動處理序間通訊設施的資訊。*/if((shmadd=shmat(shmid,0,0))<(char *)0){ perror("shmat"); exit(1); }else printf("attached shared-memory\n");system("ipcs -m");if((shmdt(shmadd))<0){ perror("shmdt"); exit(1); }else printf("deleted shared-memory\n");system("ipcs -m");exit(0);}
二、訊息佇列:
訊息佇列就是訊息的一個鏈表,它允許一個或多個進程向它寫訊息,一個或多個進程從中讀訊息。具有一定的FIFO的特性,但是可實現訊息的隨即查詢。這些訊息存在核心中,由“隊列ID”來標識。
訊息佇列的實現包括建立和開啟隊列、添加訊息、讀取訊息和控制訊息隊列這四種操作。
int msgget(key_t key,int flag):建立和開啟隊列,其訊息數量受系統限制。
int msgsnd(int msqid,struct msgbuf *msgp,size_t msgsz,int flag):添加訊息,將訊息添加到訊息佇列尾部。
int msgrcv(int msqid,struct msgbuf *msgp,size_t msgsz,long msgtyp,int flag):讀取訊息,從訊息佇列中取走訊息。
int msgctl(int msqid,int cmd,struct msqid_ds *buf):控制訊息隊列。
執行個體代碼msg.c
#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#define BUFSZ 512struct message{ long msg_type; char msg_text[BUFSZ];};int main(){ int qid; key_t key; int len; struct message sndmsg,rcvmsg; if((key=ftok(".","a"))==-1) { perroe("ftok"); exit(1); } if((qid=msgget(key,IPC_CREAT|0666))==-1) { perror("msgget"); exit(1); } printf("opened queue %d\n",qid); puts("Please enter the message to queue:"); if((fgets((&sndmsg)->msg_text,BUFSZ,stdin))==NULL) { puts("no message"); exit(1); } sndmsg.msg_type=getpid(); len=strlen(sndmsg.msg_text); if((msgsnd(qid,&sndmsg,len,0))<0) { perror("message posted"); exit(1); } if((msgrcv(qid,&rcvmsg,BUFSZ,0,0)<0) { perror("msgrcv"); exit(1); } printf("message is :%s\n",(&rcvmsg)->msg_text); if((msgctl(qid,IPC_RMID,NULL))<0) { perror("msgctl"); exit(1); } exit(0); }
三、管道
建立一個簡單的管道,可以使用系統調用pipe()。他接受一個參數,也就是一個包括兩個整數的數組。如果系統調用成功,此數組將包括管道使用的兩個檔案描述符。建立一個管道之後,一般情況下進程將產生一個新的進程。
系統調用:pipe();
原型:int pipe(int fd[2]);
注意:fd[0]用於讀取管道,fd[1]用於寫入管道。
一個管道是半雙工的。
管道執行個體代碼:
#include <unistd.h>#include <sys/types.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>int main(){ int pipe_fd[2];/*管道描述符*/ pid_t pid; /*定義進程ID*/ char buf_r[100];/*讀的字串*/ char *p_wbuf; /*寫的字串的指標*/ int r_num; memset(buf_r,0,sizeof(buf_r));/*將buf_r字元數組中的資料全都清為0*/ if(pipe(pipe_fd)<0) { printf("pipe create error\n"); return -1; } /*父進程寫,子進程讀*/ if((pid=fork())==0) /*在fork()建立的子進程當中*/ { printf("\n"); close(pipe_fd[1]);/*寫管道描述符關閉*/ sleep(2); /*休眠2秒*/ if((r_num=read(pipe_fd[0],buf_r,100))>0) /*在pipe_fd[0]的管道描述符中讀100個位元組放入到buf_r中*/ { printf("%d numbers read from the pipe is %s\n",r_num,buf_r); } close(pipe_fd[0]);/*寫管道描述符關閉*/ exit(0); /*退出*/ } else if(pid>0) { close(pipe_fd[0]);/*讀管道描述符關閉*/ if(write(pipe_fd[1],"Hello",5)!=-1) printf("parent write1 success!\n"); if(write(pipe_fd[1],"Pipe",5)!=-1) printf("parent write2 success!\n"); close(pipe_fd[1]; sleep(3); waitpid(pid,NULL,0); exit(0); } }