Advertisement
Guest User

Untitled

a guest
May 20th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sys/ipc.h>
  7. #include <sys/msg.h>
  8.  
  9. #define MSGSIZE 100
  10. #define MSGTYPE1 1
  11. #define MSGTYPE2 2
  12. #define MSGTYPE3 3
  13.  
  14. #define ALLTYPES 0
  15.  
  16. typedef struct message
  17. {
  18.   long mtype;
  19.   char mtext[MSGSIZE];
  20. } message;
  21.  
  22. void sendFinal(int queue)
  23. {
  24.   message buf = {MSGTYPE3, ""};
  25.   msgsnd(queue, &buf, 0, 0);
  26. }
  27.  
  28. void recvm(int queue)
  29. {
  30.   message msg;
  31.   ssize_t code;
  32.   while ((code = msgrcv(queue, &msg, MSGSIZE, 0, MSG_NOERROR)) > 0)
  33.   {
  34.     if (msg.mtype == MSGTYPE2)
  35.       break;
  36.     printf("msg: %s\n", msg.mtext);
  37.   }
  38. }
  39.  
  40. int main()
  41. {
  42.   int queue;
  43.   queue = msgget(123, IPC_CREAT);
  44.   printf("Connected\n");
  45.  
  46.   recvm(queue);
  47.   sendFinal(queue);
  48.  
  49.   return EXIT_SUCCESS;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement