一,建立訊息佇列:
int msgget(key_t key, int flags);
功能:若flags 中設定了IPC_CREAT 位,建立一個新隊列,對
應於key 的值。
key 值為IPC_PRIVATE 時表明key 值由系統產生。
若flags 中沒有設定IPC_CREAT 位,開啟對應於key 的已有隊
列。
傳回值為訊息佇列的id,系統關機以前全域有效。
/* * mkq.c - Create a SysV IPC message queue */#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){ int qid; /* The queue identifier */ key_t key; /* The queue key */ key = 123; /* Create the queue */ if((qid = msgget(key, IPC_CREAT | 0666)) < 0) { perror("msgget:create"); exit(EXIT_FAILURE); } printf("created queue id = %d\n", qid); /* Open the queue again */ if((qid == msgget(key, 0)) < 0) { perror("msgget:open"); exit(EXIT_FAILURE); } printf("opened queue id = %d\n", qid); exit(EXIT_SUCCESS);}
查看目前已有的訊息佇列命令:ipcs -q
二,寫入訊息:
int msgsnd(int msqid, const void *prt, size_t nbytes, int flags);
/* * qsnd.c - Send a message to previously opened queue */#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#define BUFSZ 512/* Message structure */struct msg { long msg_type; char msg_text[BUFSZ];};int main(int argc, char *argv[]){ int qid; /* The queue identifier */ int len; /* Length of data sent */ struct msg pmsg; /* Pointer to message structure */ /* Expect the queue ID passed on the command-line */ if(argc != 2) { puts("USAGE: qsnd <queue ID>"); exit(EXIT_FAILURE); } qid = atoi(argv[1]); /* Get the message to add to the queue */ puts("Enter message to post:"); if((fgets((&pmsg)->msg_text, BUFSZ, stdin)) == NULL) { puts("no message to post"); exit(EXIT_SUCCESS); } /* Associate the message with this process */ pmsg.msg_type = getpid(); /* Add the message to the queue */ len = strlen(pmsg.msg_text); if((msgsnd(qid, &pmsg, len, 0)) < 0) { perror("msgsnd"); exit(EXIT_FAILURE); } puts("message posted"); exit(EXIT_SUCCESS);}
該案例前段建立訊息佇列預設qid為0,所以運行寫入端程式命令:./xx 0
三 ,讀入訊息:
int msgrcv(int msqid, void *ptr, size_t nbytes, long type, int flags);
功能:從msqid 代表的訊息佇列中,取出第一個符合類型type
的訊息,存入ptr 指向的空間中。
type 等於0 時,取出隊列中第一條訊息。
type 大於0 時,取出隊列中類型值等於type 的第一條訊息。
/* * qrd.c - Read all message from a message queue */#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <stdlib.h>#define BUFSZ 512/* Message structure */struct msg { long msg_type; char msg_text[BUFSZ];};int main(int argc, char *argv[]){ int qid; /* The queue identifier */ int len; /* Length of message */ struct msg pmsg; /* A message structure */ /* Expect the queue ID passed on the command-line */ if(argc != 2) { puts("USAGE: qrd <queue ID>"); exit(EXIT_FAILURE); } qid = atoi(argv[1]); /* Retrieve and display a message from the queue */ len = msgrcv(qid, &pmsg, BUFSZ, 0, 0); if(len > 0) { (&pmsg)->msg_text[len] = '\0'; printf("reading queue id: %05d\n", qid); printf("message type: %05ld\n", (&pmsg)->msg_type);printf("message length: %d bytes\n", len); printf("message text: %s\n", (&pmsg)->msg_text); } else { perror("msgrcv"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS);}
運行讀入端命令:./xx 0