Message Queue Error

Thread Solved

Join Date: Apr 2008
Posts: 60
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

Message Queue Error

 
0
  #1
18 Days Ago
I am new to message queues and IPC in general. I am trying to have a process establish the queue and send a message. Then I fork and the child will read the message. The goal is to measure the execution time for different sized messages.

The msgsnd is returning a -1 and I have no idea as to why its not sending anything. Any help is appreciated. Thanks in advance

  1. int main() {
  2.  
  3.  
  4.  
  5. //establish the character array to be passed around
  6. int msgSize = 1024;
  7.  
  8. char charArray[msgSize];
  9.  
  10. clock_t timeStampBegin;
  11.  
  12. clock_t timeStampEnd;
  13.  
  14.  
  15.  
  16. key_t key = 1234; /* key to be passed to msgget() */
  17.  
  18. int msqid; /* return value from msgget() */
  19.  
  20.  
  21.  
  22. //establish the message queue and make sure that it properly established
  23.  
  24. if ((msqid = msgget(key, IPC_CREAT)) == 1) {
  25.  
  26. perror("msgget: msgget failed");
  27.  
  28. //exit(1);
  29.  
  30. } else {
  31.  
  32. (void) fprintf(stderr, "msgget succeeded\n");
  33.  
  34. }
  35.  
  36. //Take an initial timestamp
  37.  
  38. timeStampBegin = clock();
  39. //printf("\nTimeStampBegin = %d\n", timeStampBegin);
  40.  
  41. //int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
  42.  
  43. int amountSent = msgsnd(1234, &charArray, msgSize, 0);
  44. printf("%d bytes were sent", amountSent);
  45.  
  46.  
  47.  
  48. if(fork() == 0) {
  49.  
  50. //this is the child, this is the one that we want to recieve the info.
  51. char recievedArray[100000];
  52.  
  53. if((size_t)msgSize == msgrcv(1234, &recievedArray, msgSize, 0, 0))
  54. printf("Mesage was recieved");
  55. timeStampEnd = clock();
  56. //printf("Time for transit of size %d = %f", msgSize, (double)(timeStampEnd-timeStampBegin));
  57.  
  58. }
  59.  
  60. printf("\n");
  61.  
  62. return 0;
  63.  
  64.  
  65.  
  66. }

Output
  1. msgget succeeded
  2. -1 bytes were sent
  3. -1 bytes were sent
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC