Sytem V訊息佇列(二),sytemv訊息佇列
一.msgsnd 函數
功能:把一條訊息添加到訊息佇列中
int msgsnd( int msqid, // 由msgget返回的訊息佇列標誌嗎
const void *msgp, // 一個指標,只想準備發送的訊息
size_t msgsz, // 是msgp指向訊息的長度
int msgflg); // 控制這當前訊息佇列滿或到達系統上限時發生的事情
返回值:
成功返回0
失敗返回-1
注意:
msgflg=IPC_NOWAIT表示隊列滿不等待,返回EAGAIN錯誤。為0表示阻塞等待
訊息結構在兩方面受到制約。首先,它的具體資料必須小於系統規定的上限值MSGMAX;其次,它必須以一個long int長整數開始,接收者函數將利用這個長整數確定訊息的類型。
訊息結構參考形式:
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
};
二. msgrcv函數
功能:從一個訊息佇列接收訊息
ssize_t msgrcv(int msqid, //訊息佇列標識符
void *msgp, // 指標指向準備接收的訊息
size_t msgsz, // 訊息長度
long msgtyp, // 實現接收優先順序的簡單形式
int msgflg); //控制著隊列中沒有相應類型的訊息可供接收時將要發生的事msgtype = 0; 總是順序結束的
返回值:
成功:返回實際放到接收緩衝區裡去的字元個數
失敗: 返回-1
注意:
msgtype=0 返回隊列第一條資訊,按順序接收
msgtype>0 返回隊列第一條類型等於msgtype的訊息
msgtype<0 返回隊列第一條類型小於等於msgtype絕對值的訊息,並且是滿足條件的訊息類型最小的訊息
msgflg=IPC_NOWAIT,隊列沒有可讀訊息不等待(不阻塞),返回ENOMSG錯誤。
msgflg=MSG_NOERROR,訊息大小超過msgsz時被截斷
msgtype>0且msgflg=MSG_EXCEPT,接收類型不等於msgtype的第一條訊息。
發送函數msg_send.c
#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \}while(0)struct msgbuf { long mtype; // message type, must be > 0 char mtext[1]; // message data };int main(int argc,char* argv[]){if(argc != 3){fprintf(stderr,"Usage::%s <bytes> <types> \n",argv[0]);exit(EXIT_FAILURE);}int len= atoi(argv[1]); //訊息的長度int type = atoi(argv[2]); // 訊息的類型int msgid;msgid = msgget(1234,0); // 開啟一個1234的訊息佇列if(msgid == -1)ERR_EXIT("msgget err");struct msgbuf *ptr;ptr = (struct msgbuf*)malloc(sizeof(long) + len); // 分配空間ptr->mtype = type;if( msgsnd(msgid,ptr,len,0) < 0) // 向訊息佇列中發送訊息ERR_EXIT("msgsnd");return 0;}
接收函數msg_rcv.c
#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \}while(0)#define MSGMAX 8192struct msgbuf { long mtype; // message type, must be > 0 char mtext[1]; // message data };int main(int argc,char* argv[]){int flag =0; // 預設以阻塞int type =0;int opt;while(1){opt = getopt(argc,argv,"nt:"); // nt後面可以各跟一個參數if(opt == '?')ERR_EXIT("getopt err");if(opt == -1)break;switch(opt){case 'n'://printf("AAAA\n");flag |= IPC_NOWAIT; // 不阻塞的方式接收break;case 't'://printf("BBBB\n");//int n = atoi(optarg);//printf("n=%d\n",n);type = atoi(optarg); // 類型儲存,接收對應類型的訊息break;default:break;}}int msgid;msgid = msgget(1234,0);if(msgid == -1)ERR_EXIT("msgget err");struct msgbuf *ptr;ptr = (struct msgbuf*)malloc(sizeof(long) + MSGMAX);ptr->mtype = type;int n=0; // 接收到的位元組if( (n = msgrcv(msgid,ptr,MSGMAX,type,flag)) < 0)ERR_EXIT("msgsnd");printf("read %d bytes type=%ld \n",n,ptr->mtype);return 0;}
發送:
開始發送1 100 這個訊息長度為1,訊息類型為100的訊息佇列的時候,失敗,是因為沒有建立訊息佇列, 利用msg_get建立一個訊息佇列,
接收:
接收可以按照順序接收,也可以指定訊息類型接收,-n選項表示用不阻塞的方式接收訊息, -t選項 可以指定按類型接收訊息.