消息队列。 msgsend msgrcv。 C 语言中的 System V IPC 系统调用 (Linux)

发布于 2024-08-10 06:51:16 字数 1391 浏览 6 评论 0原文

您好,我正在使用一些共享内存,其中不同的进程读取和写入数据。我使用消息队列来存储数据在读取和写入操作之间发生更改时的消息。

/* 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝颜夕 2024-08-17 06:51:16

返回值是一个 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 see 4 * sizeof(int) come back for a full message. If rc comes back as -1, you have an error. If rc came back as a positive number, at least some of the fields of msg will have the received message data.

Check out the man page for more details.

枕花眠 2024-08-17 06:51:16
rc = msgrcv(msqid, &msg, sizeof(msg), BUFFER_CHANGED, IPC_NOWAIT)

msg 包含您想要在屏幕上显示的数据。由于使用了IPC_NOWAIT,所以函数立即返回,不会阻塞。如果没有读取任何消息,则 rc 值为 -1,否则它将是从 msgq 中读取的字节数。

rc = msgrcv(msqid, &msg, sizeof(msg), BUFFER_CHANGED, IPC_NOWAIT)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文