一、概念
消息隊列就是一個消息的鏈表。可以把消息看作一個記錄,具有特定的格式以及特定的優先級。對消息隊列有寫權限的進程可以向中按照一定的規則添加新消息;有讀權限的進程則可以讀走消息。讀走就沒有了。消息隊列是隨內核持續的。 只有內核重啓或人工刪除時,該消息纔會被刪除。在系統範圍內,消息隊列與鍵值唯一對應。
二、步驟及思路
1、取得鍵值
2、打開、創建消息隊列
3、發送消息
4、接收消息
下面具體看看:
1、取得鍵值
key_t ftok(char *pathname, char proj)
頭文件爲<sys/ipc.h>。返回文件名對應的鍵值,失敗返回 -1。proj是項目名,隨便寫,不爲0就行。
fname就是你指定的文件名(已經存在的文件名)。需要有-t 的權限,或用root權限執行,通常設爲/tmp或設爲" . "。這裏我感覺不用這個函數也行,因爲key值可以自己指定,例如: #define KEY_MSG 0x101
2、打開、創建消息隊列
int msgget(key_t key, int msgflg)
頭文件爲<sys/msg.h>。key由ftok獲得。
msgflg有:
IPC_CREAT 創建新的消息隊列,應配有文件權限0666。
IPC_EXCL 與IPC_CREAT一同使用,表示如果要創建的消息隊列已經存在,則返回錯誤。
IPC_NOWAIT 讀寫消息不阻塞。
當沒有與key相對應的消息隊列並且msgflg中包含了IPC_CREAT標誌 或 key的參數爲IPC_PRIVATE 時,創建一個新的消息隊列。
3、發送消息
int msgsnd(int msqid, struct msgbuf *msgp, int msgsz, int msgflg)
向消息隊列發送一條消息。msqid爲消息隊列的id,msgp爲存放消息的結構體。msgsz是消息的長度,和數組的大小不一樣哦。msgflg爲消息標誌,通常爲0,也可以爲IPC_NOWAIT。出錯返回 -1。
消息格式
struct msgbuf {
long mtype;
char mtext[100];
};
4、接收消息
int msgrcv(int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg)
從msqid代表的消息隊列中讀取一個msgtyp類型的消息,並把消息存儲在msgp指定的msgbuf結構中。讀取後隊列中的消息將會刪除。size爲結構體中數據的大小,不要計算msgtyp。出錯返回 -1。
三、舉例
創建一消息隊列,子進程發數據,父進程收數據。實現向隊列中存放數據與讀取數據。
- #include<stdio.h>
- #include<sys/msg.h>
- #include<fcntl.h>
- #include<stdlib.h>
- #include<string.h>
- #define max 100
- struct haha{
- long mtype;
- char data[max];
- };
- int main(int argc,char *argv[]){
- int pid;
- if(argc!=2){
- printf("Usage: msg [Message]\n");
- return -1;
- }
- key_t key;
- if((key=ftok("/tmp",'g'))<0){ //這裏文件夾必須存在,有t屬性並且上級目錄也要有t屬性
- printf("Getting key error! \n");
- return -1;
- }
- int mgsid;
- if((mgsid=msgget(key,IPC_CREAT|0666))==-1){ //key值隨便寫一個數也能用
- printf("mgs queue create error\n");
- return -1;
- }
- pid=fork();
- if(pid <0){
- printf("fork create error!\n");
- _exit(1);
- }
- if(pid == 0){
- printf("welcome in child process\n Sending the message......\n");
- sleep(1);
- struct haha hehe;
- hehe.mtype=getppid();
- strcpy(hehe.data,argv[1]);
- if(msgsnd(mgsid,&hehe,sizeof(hehe.data),0)<0){ //此處注意長度
- printf("Sending error!!!\n");
- _exit(1);
- }else {
- printf("Sending complete!\n");
- _exit(0);
- }
- }else{
- wait(NULL);
- printf("welcome in parents process\n Receiving the message......\n");
- sleep(1);
- struct haha gaga;
- if(msgrcv(mgsid,&gaga,max,getpid(),0)<0){
- printf("Receiving error!!!\n");
- _exit(1);
- }else {
- printf("Receiving complete!\n");
- printf("The message is %s \n",gaga.data);
- }
- }
- return 0;
- }
#include<stdio.h> #include<sys/msg.h> #include<fcntl.h> #include<stdlib.h> #include<string.h> #define max 100 struct haha{ long mtype; char data[max]; }; int main(int argc,char *argv[]){ int pid; if(argc!=2){ printf("Usage: msg [Message]\n"); return -1; } key_t key; if((key=ftok("/tmp",'g'))<0){ //這裏文件夾必須存在,有t屬性並且上級目錄也要有t屬性 printf("Getting key error! \n"); return -1; } int mgsid; if((mgsid=msgget(key,IPC_CREAT|0666))==-1){ //key值隨便寫一個數也能用 printf("mgs queue create error\n"); return -1; } pid=fork(); if(pid <0){ printf("fork create error!\n"); _exit(1); } if(pid == 0){ printf("welcome in child process\n Sending the message......\n"); sleep(1); struct haha hehe; hehe.mtype=getppid(); strcpy(hehe.data,argv[1]); if(msgsnd(mgsid,&hehe,sizeof(hehe.data),0)<0){ //此處注意長度 printf("Sending error!!!\n"); _exit(1); }else { printf("Sending complete!\n"); _exit(0); } }else{ wait(NULL); printf("welcome in parents process\n Receiving the message......\n"); sleep(1); struct haha gaga; if(msgrcv(mgsid,&gaga,max,getpid(),0)<0){ printf("Receiving error!!!\n"); _exit(1); }else { printf("Receiving complete!\n"); printf("The message is %s \n",gaga.data); } } return 0; }