Advertisement
pizzaboy192

example parent is based on.

Oct 17th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. /* #include <cstdlib> */
  2. #include <iostream>
  3. #include <cerrno>
  4. #include <limits>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <sys/ipc.h>
  8. #include <sys/msg.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. using namespace std;
  13.  
  14. #define BUFSIZ 512
  15.  
  16. /*
  17. This program sends two types of messages of type 5 and type 1. Two different
  18. receiving processes receive the messages of different types (msg_receiver and
  19. msg_receiver2).
  20. */
  21.  
  22.  
  23. struct my_msg_st {
  24. long int my_msg_type;
  25. char some_text[BUFSIZ];
  26. };
  27.  
  28.  
  29. int main(int argc, char** argv)
  30. {
  31.  
  32. int running = 1;
  33. int msgid;
  34. struct my_msg_st some_data;
  35. struct my_msg_st return_data;
  36. char buffer[BUFSIZ];
  37. long int msg_to_receive = 11;
  38.  
  39. /* Set up the message queue */
  40. msgid = msgget((key_t) 1234, 0666 | IPC_CREAT);
  41.  
  42. if (msgid == -1) {
  43. cout << "msgget failed" << endl;
  44. exit(EXIT_FAILURE);
  45. }
  46.  
  47. /* Input message; send message to the queue until an "end" is encountered. */
  48. while (running) {
  49. cout << "Enter some text (5): ";
  50. // get the text to be put into the queue.
  51. cin.getline(buffer,BUFSIZ);
  52. // set message type
  53. some_data.my_msg_type = 5;
  54. // copy message into shared memory
  55. strcpy(some_data.some_text, buffer);
  56. // if the message send is set to error, end.
  57. if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
  58. cout << "msgsnd failed" << endl;
  59. exit(EXIT_FAILURE);
  60. }
  61. // else print "sent message"
  62. cout << "You sent a message." << endl;
  63. // get text for second output.
  64. cout << "Enter some text (1): ";
  65. cin.getline(buffer,BUFSIZ);
  66. // set message type
  67. some_data.my_msg_type = 1;
  68. // copy data
  69. strcpy(some_data.some_text, buffer);
  70. //check for error
  71. if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
  72. cout << "msgsnd failed" << endl;
  73. exit(EXIT_FAILURE);
  74. }
  75.  
  76. cout << "You sent a message." << endl;
  77.  
  78. // INIT TEST OF THREE RUNNING
  79. cout << "Enter some text (15): ";
  80. cin.getline(buffer,BUFSIZ);
  81. // set message type
  82. some_data.my_msg_type = 15;
  83. // copy data
  84. strcpy(some_data.some_text, buffer);
  85. //check for error
  86. if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
  87. cout << "msgsnd failed" << endl;
  88. exit(EXIT_FAILURE);
  89. }
  90.  
  91. cout << "You sent a message." << endl;
  92. // exit if text is "end"
  93. if (strncmp(buffer, "end", 3) == 0) {
  94. running = 0;
  95. }
  96. }
  97.  
  98. exit(EXIT_SUCCESS);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement