меня мисс поняла проблема о создании многократной очереди сообщений в IPC System V.IPC System V - Message Queue создание
Моей проблемы заключается в: Основной процесс создает дочерний процесс 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;
}
Моя проблема фокусировки в 2-х направлениях: ("", я)
cle = ftok(".", i); <=======================================
/* cle = ftok(path, i); */ <=======================================
Когда я пытался использовать CLE = ftok, он работал отлично , Но с другой стороны, это всегда показывать
как:
(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. – whoan