我很想念在IPC System V中创建多个消息队列的问题。
我的问题是:主进程创建NB_fils子进程。每个进程(包括主进程)都拥有一个消息队列。子进程i(0< = i< NB_fils)拥有消息队列mq [i]。主进程拥有消息队列mq [NB_Fils]。子节点生成消息并发送到主进程的消息队列。返回子进程的主要进程正好是max_msg_i值。
这是我的工作:
#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(&#34;。&#34;,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
就像消息没有准确发送一样。
任何人都可以告诉我这是什么问题。
非常感谢。
答案 0 :(得分:1)
调用ftok()时,您需要为第一个参数指定现有文件。 作为“。”直接指向本地(存在),这在您的示例中有效。 但我怀疑当前目录中是否存在名为“File_msg”的文件,因此会出错。 请改用现有文件的相对路径或绝对路径。