手册页mq_notify:
1)消息通知,只有当新的消息到达和队列以前为空发生。如果在调用mq_notify()时该队列不是空的,则只有在队列被清空并且有新消息到达之后才会发生通知。
2)通知发生一次:通知发送后,通知注册被删除,另一个进程可以注册消息通知。如果通知的进程希望收到下一个通知,它可以使用mq_notify()来请求进一步的通知。
3)只有一个进程可以注册接收来自消息队列的通知。
所以:
1)清空MSGQ与mq_notify注册后立即RECV在读取器进程新消息()。
2)重新注册通知功能,接收下一条消息。
3)寄存器只有1用于从Q.
接收消息
这里是在C++中的简单消息队列读取的代码:
#include <iostream>
#include <mqueue.h>
#include <string.h>
#include <sstream>
#include <unistd.h>
#include <errno.h>
using namespace std;
#define MSG_Q_NAME "/MY_MSGQ_3"
static void /* Thread start function */
tfunc(union sigval sv)
{
mqd_t msq_id = *(static_cast<mqd_t*>(sv.sival_ptr));
struct mq_attr attr;
if(mq_getattr(msq_id, &attr) < 0)
{
cout << "Error in mq_getattr " << strerror(errno) << endl;
return;
}
// Reregister for new messages on Q
struct sigevent sev;
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = tfunc;
sev.sigev_notify_attributes = NULL;
sev.sigev_value.sival_ptr = sv.sival_ptr;
if (mq_notify(msq_id, &sev) < 0)
{
cout << "Error during Reregister in msq_notify : "
<< strerror(errno) << endl;
exit(EXIT_FAILURE);
}
// Read new message on the Q
char* arr = new char[attr.mq_msgsize];
memset(arr, 0, attr.mq_msgsize);
if(mq_receive(msq_id, arr, attr.mq_msgsize, 0) < 0)
{
if(errno != EAGAIN)
{
cout << "Error in mq_receive " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
}
else
{
cout << "Msg rcvd " << arr << endl;
}
}
int main()
{
mqd_t msq_id = mq_open(MSG_Q_NAME, O_RDONLY | O_NONBLOCK | O_CREAT, 0666, 0);
if(msq_id == (mqd_t) -1)
{
cout << "Error on msg Q creation: " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
// The process is registered for notification for new message on the Q
struct sigevent sev;
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = tfunc;
sev.sigev_notify_attributes = NULL;
sev.sigev_value.sival_ptr = &msq_id;
if (mq_notify(msq_id, &sev) < 0)
{
cout << "Error on msg Q notify : " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
else
{
cout << "Notify for msg Q reception " << MSG_Q_NAME << endl;
}
// Man Page mq_notify: Message notification occurs only when a new
// message arrives and the queue was previously empty. If the queue was
// not empty at the time mq_notify() was called, then a notification will
// occur only after the queue is emptied and a new message arrives.
//
// So emptying the Q to recv new messages
ssize_t n = 0;
struct mq_attr attr;
if(mq_getattr(msq_id, &attr) < 0)
{
cout << "Error in mq_getattr " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
char* arr = new char[attr.mq_msgsize];
memset(arr, 0, attr.mq_msgsize);
while((n = mq_receive(msq_id, arr, attr.mq_msgsize, 0) >= 0))
{
cout << "Empty the Q. Msg rcvd " << arr << endl;
}
while(1)
;
mq_close(msq_id);
}
下面是一个简单的消息Q作家代码:
#include <iostream>
#include <mqueue.h>
#include <string.h>
#include <sstream>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
using namespace std;
#define MSG_Q_NAME "/MY_MSGQ_3"
int main()
{
struct mq_attr attr;
memset(&attr, 0, sizeof attr);
attr.mq_msgsize = 8192;
attr.mq_flags = 0;
attr.mq_maxmsg = 10;
mqd_t msq_id = mq_open(MSG_Q_NAME, O_RDWR | O_CREAT | O_NONBLOCK,
0777, &attr);
if(msq_id == (mqd_t) -1)
{
cout << "Error on msg Q creation: " << strerror(errno) << endl;
exit(1);
}
// Write 5 msgs on message Q
for(int i = 0; i < 5; ++i)
{
stringstream s;
s << "My Msg " << i;
if(mq_send(msq_id, s.str().c_str(), strlen(s.str().c_str()), 0) < 0)
{
if(errno != EAGAIN)
{
cout << "Error on sending msg on MsgQ " << strerror(errno);
mq_close(msq_id);
exit(1);
}
}
else
{
cout << "Sent msg " << s.str() << endl;
}
sleep(1); // Easily see the received message in reader
}
mq_close(msq_id);
}
`struct mq_attr attr = {0};和struct sigevent sev = {0}; `这有什么区别吗?你不是在这些结构中的每个领域创新,你可能会传递垃圾值。 – nos 2010-12-30 22:26:31