消息發送/接收API
msgsnd函數
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
參數
msgid: 由msgget函數返回的消息隊列標識碼, 也可以是通過ipcs命令查詢出來的一個已經存在的消息隊列的ID號
msgp:是一個指針,指針指向準備發送的消息,
msgsz:是msgp指向的消息長度, 注意:這個長度不含保存消息類型的那個long int長整型
msgflg:控制着當前消息隊列滿或到達系統上限時將要發生的事情,如果msgflg = IPC_NOWAIT表示隊列滿不等待,返回EAGAIN錯誤。
消息結構在兩方面受到制約: (1)它必須小於系統規定的上限值(MSGMAX); (2)它必須以一個long int長整數開始,接收者函數將利用這個長整數確定消息的類型;
//消息結構參考形式如下:
struct msgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data, 可以設定爲更多的字節數 */
};
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.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> <type>\n", argv[0]);
exit(EXIT_FAILURE);
}
int len = atoi(argv[1]);
int type = atoi(argv[2]);
int msgid;
msgid = msgget(1234, 0);
if (msgid == -1)
ERR_EXIT("msgget");
struct msgbuf *ptr;
ptr = (struct msgbuf*)malloc(sizeof(long) + len);
ptr->mtype = type;
/*
if (msgsnd(msgid, ptr, len, IPC_NOWAIT) < 0)//非阻塞
ERR_EXIT("msgsnd");
*/
if (msgsnd(msgid, ptr, len, 0) < 0)//阻塞
ERR_EXIT("msgsnd");
return 0;
}
msgrcv函數
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
參數
msgid: 由msgget函數返回的消息隊列標識碼
msgp:是一個指針,指針指向準備接收的消息;
msgsz:是msgp指向的消息長度,這個長度不含保存消息類型的那個long int長整型
msgtype:它可以實現接收優先級的簡單形式(見下圖)
msgflg:控制着隊列中沒有相應類型的消息可供接收時將要發生的事(見下圖)
返回值:
成功->返回實際放到接收緩衝區裏去的字節數(注意: 此處並不包含msgbuf中的mtype的長度);
失敗->返回-1;
msgtyp參數 |
|
msgtyp=0 |
返回隊列第一條信息 |
msgtyp>0 |
返回隊列第一條類型等於msgtype的消息 |
msgtyp<0 |
返回隊列第一條類型小於等於(<=)msgtype絕對值的消息,並且是滿足條件的消息類型最小的消息(按照類型進行排序的順序進行接收消息) |
msgflg參數 |
|
msgflg=IPC_NOWAIT |
隊列沒有可讀消息不等待,返回ENOMSG錯誤。 |
msgflg=MSG_NOERROR |
消息大小超過msgsz(msgrcv 函數的第三個參數)時被截斷, 並且不會報錯 |
msgtyp>0且msgflg=MSG_EXCEPT |
接收類型不等於msgtype的第一條消息 |
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.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 */
};
#define MSGMAX 8192
int main(int argc, char *argv[])
{
int flag = 0;
int type = 0;
int opt;
while (1)
{
opt = getopt(argc, argv, "nt:");
if (opt == '?')
exit(EXIT_FAILURE);
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;
}
}
int msgid;
msgid = msgget(1234, 0);
if (msgid == -1)
ERR_EXIT("msgget");
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;
}
getopt用法是解析命令行參數
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
//示例: 解析 ./main -n -t 3 中的參數選項
//t後面的:表示t後面可以跟參數 參數保存在optarg中
int main(int argc, char *argv[])
{
while (true)
{
int opt = getopt(argc, argv, "nt:");
if (opt == '?')
exit(EXIT_FAILURE);
else if (opt == -1)
break;
switch (opt)
{
case 'n':
cout << "-n" << endl;
break;
case 't':
int n = atoi(optarg);
cout << "-t " << n << endl;
break;
}
}
}