943,972 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 616
  • C RSS
Nov 6th, 2009
0

Message Queue Error

Expand Post »
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
Similar Threads
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Merge sort or an alternative..
Next Thread in C Forum Timeline: non preemptive scheduler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC