1

I have miss understood problem about multiple message queue creation in IPC System V.

My problem is: A main process creates NB_fils child process. Each process (include the main process) possesses a message queue. The child process i (0 <= i < NB_fils) possesses the message queue mq[i]. The main process possesses the message queue mq[NB_Fils]. The children generate a message and send to the message queue of the main process. The main process back to the child process exactly max_msg_i value.

Here is my work:

#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; 
 }

My problem is focus in 2 lines:

 cle = ftok(".", i); <=======================================
 /* cle = ftok(path, i); */ <=======================================

When I tried to use cle = ftok(".", i), it's worked fine. But for the other, it's always show
like:

(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

It like the message is not sent exactly.

Could any one can tell me what is the problem.

Thanks very much.

asked Oct 23, 2014 at 10:45
1
  • You may start checking the return value of ftok to see whether has failed. Try giving the full path of File_msg too. Commented Oct 23, 2014 at 10:54

1 Answer 1

1

You need to specify an existing file for the first parameter when calling ftok(). As "." points to the local directly (which exists), this is valid in your example. But I doubt that a file named "File_msg" exists in the current directory, therefore you get an error. Use a relative or absolute path to an existing file instead.

answered Dec 3, 2014 at 15:40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.