在windows10環境下安裝了eclipse,cygwin,編寫第一個簡單的消息隊列程式。
1.建立工程并配置編譯器
在eclipse開發環境下建立一個C project,“Tool Chains”選擇“cygwin GCC”

在工程右鍵點選屬性,在“C/C++ build”下拉選項中“Tool Chain Editor”界面下的“current builder”下選擇“CDT Internal Builder”。
如果Cygwin安裝了make,則這裡不選“CDT Internal Builder”也可以(預設為Gnu Make Builder)。
2.建立源檔案并輸入代碼
建立一個c檔案,輸入源代碼(附後)。
3.編譯運作
運作結果,在控制台列印:
send pi is 3.141590.
msg size is 8192.
recv pi is 3.141590.
表示發送消息中的pi正确地賦給了接收消息結構體。
消息隊列中各函數的用法後續再仔細摸。
附.源代碼
#include <stdio.h>
#include <stdlib.h>
#include <mqueue.h>
#include <fcntl.h>
struct MsgType {
int len;
float pi;
};
int main() {
mqd_t msgq_id;
struct MsgType msg1;
struct MsgType msg2;
struct mq_attr msgq_attr;
unsigned int prio1 = ;
unsigned int prio2;
const char *file = "/myposix";
msg1.len = ;
msg1.pi = ;
msg2.len = ;
msg2.pi = ;
msgq_id = mq_open(file, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG, NULL);
if (msgq_id == (mqd_t) -) {
perror("mq_open error");
exit();
}
printf("send pi is %f.\n",msg1.pi);
if (mq_send(msgq_id, (char*) &msg1, sizeof(struct MsgType), prio1) == -) {
perror("mq_send");
exit();
}
if (mq_getattr(msgq_id, &msgq_attr) == -) {
perror("mq_getattr");
exit();
}
printf("msg size is %ld.\n",msgq_attr.mq_msgsize);
if (mq_receive(msgq_id, (char*) &msg2, msgq_attr.mq_msgsize, &prio2) == -) {
perror("mq_receive");
exit();
}
printf("recv pi is %f.\n",msg2.pi);
if (mq_close(msgq_id) == -) {
perror("mq_close");
exit();
}
if (mq_unlink(file) == -) {
perror("mq_unlink");
exit();
}
return ;
}
[20170510]