IPC System V - 消息队列创建
Posted
技术标签:
【中文标题】IPC System V - 消息队列创建【英文标题】:IPC System V - Message Queue creation 【发布时间】:2014-10-23 10:45:59 【问题描述】:我错过了在 IPC System V 中创建多个消息队列的理解问题。
我的问题是:主进程创建 NB_fils 子进程。每个进程(包括主进程)都拥有一个消息队列。子进程 i (0
这是我的作品:
#define SVID_SOURCE 1
#define NB_FILS 4
#define MSG_SIZE 128
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>
/* Message structure */
typedef struct msgbuf
long mtype;
int msg_val;
int mq_index;
message_buf;
int main(int argc, char **argv)
int mq[NB_FILS + 1]; /* There are totally NB_FILS + 1 message queues */
int i = 0;
int j = 0;
int proc_index = -1;
char path[14] = "File_msg";
key_t cle;
int max_msg_i = 0;
message_buf msg_send;
message_buf msg_rcv;
/* Creation of NB_FILS + 1 message queues */
for(i = 0; i < NB_FILS + 1; i++)
/* cle = ftok(".", i); */ <=======================================
cle = ftok(path, i); <=======================================
mq[i] = msgget(cle, 0666|IPC_CREAT);
/* Creation of NB_FILS child process */
for(i = 0; i < NB_FILS; i++)
if(fork() > 0)
break;
/* The child process */
if(i != NB_FILS)
int somme = 0;
proc_index = i;
printf("(Pid=%d) My index is %d\n", getpid(), proc_index);
/* Message creation */
srand(getpid());
max_msg_i = (int) (NB_FILS*(float)rand()/RAND_MAX);
msg_send.msg_val = max_msg_i;
msg_send.mtype = 1L;
msg_send.mq_index = proc_index;
printf("(Pid=%d) I'm waiting for %d messages from the main process\n", getpid(), msg_send.msg_val);
/* Send message to message queue associated with the main process */
msgsnd(mq[NB_FILS], &msg_send, 2*sizeof(int) + sizeof(long), 1L);
/* At the owned message queue, child process waits for max_msg_i message sent from the main process */
for(j = 0; j < max_msg_i; j++)
msgrcv(mq[proc_index], &msg_rcv, 2*sizeof(int) + sizeof(long), 1L, 0);
printf("(Pid=%d) I have received the message containing value %d of from the main process \n", getpid(), msg_rcv.msg_val);
somme += msg_rcv.msg_val;
printf("(Pid=%d) Sum of %d values received: %d\n", getpid(), max_msg_i, somme);
/* Drop the queue message */
msgctl(mq[proc_index], IPC_RMID, 0);
/* The main process */
else
srand(time(NULL));
/* At the owned message queue, the main process wait for values max_msg_i (1 <= max_msg_i <= NB_FILS) sent by
* the child process and send back to them max_msg_i messages. */
for(i = 0; i < NB_FILS; i++)
msgrcv(mq[NB_FILS], &msg_rcv,2*sizeof(int) + sizeof(long), 1L, 0);
printf("(P)J'ai reçu le message: msg_val = %d, mq_index = %d\n", msg_rcv.msg_val, msg_rcv.mq_index);
/* Creat max_msg_i messages and send to process i */
for(j = 0; j < msg_rcv.msg_val; j++)
msg_send.msg_val = (int) (100*(float)rand()/RAND_MAX);
msg_send.mtype = 1L;
msg_send.mq_index = msg_rcv.mq_index;
msgsnd(mq[msg_rcv.mq_index], &msg_send, 2*sizeof(int) + sizeof(long), 1L);
/* Drop the queue message */
msgctl(mq[NB_FILS], IPC_RMID, 0);
return EXIT_SUCCESS;
我的问题集中在两行:
cle = ftok(".", i); <=======================================
/* cle = ftok(path, i); */ <=======================================
当我尝试使用 cle = ftok(".", i) 时,它运行良好。但对于另一个,它总是显示 喜欢:
(Pid=3813) My index is 0
(Pid=3813) I'm waiting for 1 messages from the main process
(Pid=3813) I have received the message containing value 1 of from the main process
(Pid=3813) Sum of 1 values received: 1
(Pid=3814) My index is 1
(Pid=3814) I'm waiting for 3 messages from the main process
(Pid=3814) I have received the message containing value 3 of from the main process
(Pid=3815) My index is 2
(Pid=3815) I'm waiting for 2 messages from the main process
(Pid=3814) I have received the message containing value 2 of from the main process
(Pid=3816) My index is 3
(Pid=3816) I'm waiting for 0 messages from the main process
(Pid=3814) I have received the message containing value 0 of from the main process
(Pid=3814) Sum of 3 values received: 5
(Pid=3815) I have received the message containing value -1080789062 of from the main process
(Pid=3815) I have received the message containing value -1080789062 of from the main process
(Pid=3815) Sum of 2 values received: 2133389172
(P)I have received: msg_val = -1080789062, mq_index = 47
(P)I have received: msg_val = -1080789062, mq_index = 47
(P)I have received: msg_val = -1080789062, mq_index = 47
(P)I have received: msg_val = -1080789062, mq_index = 47
(Pid=3816) Sum of 0 values received: 0
就像消息没有准确发送一样。
谁能告诉我是什么问题。
非常感谢。
【问题讨论】:
你可以开始检查ftok
的返回值是否失败。也尝试给出File_msg
的完整路径。
【参考方案1】:
调用 ftok() 时需要为第一个参数指定一个现有文件。 作为 ”。”直接指向本地(存在),这在您的示例中有效。 但我怀疑当前目录中是否存在名为“File_msg”的文件,因此您会收到错误消息。 请改用现有文件的相对或绝对路径。
【讨论】:
以上是关于IPC System V - 消息队列创建的主要内容,如果未能解决你的问题,请参考以下文章