Advertisement
pizzaboy192

Example child is based on

Oct 17th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 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 <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. using namespace std;
  14.  
  15. #define BUFSIZ 512
  16.  
  17. struct my_msg_st {
  18. long int my_msg_type;
  19. char some_text[BUFSIZ];
  20. };
  21.  
  22.  
  23. int main(int argc, char** argv)
  24. {
  25.  
  26. int running = 1;
  27. int msgid;
  28. struct my_msg_st some_data;
  29. long int msg_to_receive = 1;
  30.  
  31. /* Set up the message queue */
  32. msgid = msgget((key_t) 1234, 0666 | IPC_CREAT);
  33.  
  34. if (msgid == -1) {
  35. cout << "msgget failed" << endl;
  36. exit(EXIT_FAILURE);
  37. }
  38.  
  39. /* Retrieve messages from the queue until an "end" is encountered. */
  40. while (running) {
  41. if (msgrcv(msgid, (void *)&some_data, BUFSIZ,
  42. msg_to_receive, 0) == -1) {
  43. cout << "msgrev failed" << endl;
  44. exit(EXIT_FAILURE);
  45. }
  46.  
  47. cout << "You wrote: " << some_data.some_text << endl;
  48.  
  49. if (strncmp(some_data.some_text, "end", 3) == 0) {
  50. running = 0;
  51. }
  52. sleep(1);
  53. }
  54.  
  55. if (msgctl(msgid, IPC_RMID, 0) == -1) {
  56. cout << "msgctl(IPC_RMID) failed" << endl;
  57. exit(EXIT_FAILURE);
  58. }
  59.  
  60. exit(EXIT_SUCCESS);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement