消息队列。 msgsend msgrcv。 C 语言中的 System V IPC 系统调用 (Linux)
您好,我正在使用一些共享内存,其中不同的进程读取和写入数据。我使用消息队列来存储数据在读取和写入操作之间发生更改时的消息。
/* struct that defines a message */
struct msgbuf{
long mtype; /* must be positive */
int childId; //ID of child sending message
int bufferChanged; //Buffer at which value was changed
int beforeValue; //Value before child sleeps
int afterValue; //Value after child sleeps
};
因此,在读写和检查更改时,进程按以下方式存储消息,
struct msgbuf msg = {BUFFER_CHANGED, id, position, read, bufferArr[position]};
if(msgsnd(msqid, &msg, sizeof(msg), 0)== -1){
perror("msgsnd in read.write");
}
这工作正常。哦,顺便说一下,这是我创建消息队列的方法。
#define BUFFER_CHANGED 1
qKey = ftok("./", 'A');
msqid = msgget(qKey, (IPC_CREAT | 0666));
/*Perform the following if the call is unsuccessful.*/
if(msqid == -1){
printf ("\nThe msgget call failed, error number = %d\n", errno);
}
/*Return the msqid upon successful completion.*/
else{
printf ("\nMessage queue successful. The msqid = %d\n", msqid);
//exit(0);
}
所以我的问题是我不太确定如何从队列中检索消息并将其显示在屏幕上。我一直在阅读 msgrcv()
系统调用,但我不太清楚。
rc = msgrcv(msqid, &msg, sizeof(msg), BUFFER_CHANGED, IPC_NOWAIT);
rc
是一个 int
,因为 msgrcv()
返回一个 int
。如何将此 int
指向实际消息?如何读取消息内容并显示它们?我假设这应该在某种循环中完成。
Hi I'm working with some shared memory where different processes read and write data. I'm using a message queue to store messages of when data has changed between read and write operations.
/* struct that defines a message */
struct msgbuf{
long mtype; /* must be positive */
int childId; //ID of child sending message
int bufferChanged; //Buffer at which value was changed
int beforeValue; //Value before child sleeps
int afterValue; //Value after child sleeps
};
So while reading and writing and checking for changes the processes store messages the following way
struct msgbuf msg = {BUFFER_CHANGED, id, position, read, bufferArr[position]};
if(msgsnd(msqid, &msg, sizeof(msg), 0)== -1){
perror("msgsnd in read.write");
}
This is working fine. Oh by the way here's how I create the message queue.
#define BUFFER_CHANGED 1
qKey = ftok("./", 'A');
msqid = msgget(qKey, (IPC_CREAT | 0666));
/*Perform the following if the call is unsuccessful.*/
if(msqid == -1){
printf ("\nThe msgget call failed, error number = %d\n", errno);
}
/*Return the msqid upon successful completion.*/
else{
printf ("\nMessage queue successful. The msqid = %d\n", msqid);
//exit(0);
}
So my problem is that I'm not quite sure how to retrieve messages from the queue and display them on the screen. I've been reading up on msgrcv()
system call, but it's not very clear to me.
rc = msgrcv(msqid, &msg, sizeof(msg), BUFFER_CHANGED, IPC_NOWAIT);
rc
is an int
since msgrcv()
returns an int
. How do I point this int
to the actual message? How to I read contents from the message so I can display them? I'm assuming this should be done in some sort of loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
返回值是一个
int
因为它告诉您它读入消息缓冲区的数据量 - 在您的情况下您希望看到4 * sizeof(int)
返回一条完整的消息。如果 rc 返回为 -1,则表示出现错误。如果rc
返回为正数,则至少msg
的某些字段将包含接收到的消息数据。查看手册页了解更多详细信息。
The return value is an
int
because it's telling you how much data it read into your message buffer - in your case you want to see4 * sizeof(int)
come back for a full message. Ifrc
comes back as -1, you have an error. Ifrc
came back as a positive number, at least some of the fields ofmsg
will have the received message data.Check out the man page for more details.
msg
包含您想要在屏幕上显示的数据。由于使用了IPC_NOWAIT,所以函数立即返回,不会阻塞。如果没有读取任何消息,则 rc 值为 -1,否则它将是从 msgq 中读取的字节数。The
msg
contains the data that you want to display on the screen. Since IPC_NOWAIT is used, the function returns immediately without blocking. The rc value will be -1 if no msg was read, else it will be the number of bytes read from the msgq.