Advertisement
Guest User

Untitled

a guest
May 20th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9.  
  10. #define MSGSIZE 100
  11. #define MSGTYPE1 1
  12. #define MSGTYPE2 2
  13. #define MSGTYPE3 3
  14.  
  15. struct message
  16. {
  17.   long mtype;
  18.   char mtext[MSGSIZE];
  19. };
  20.  
  21. typedef struct message message;
  22.  
  23. size_t msgln(char *msg, ssize_t code)
  24. {
  25.   if (code <= 0)
  26.   {
  27.     return 0;
  28.   }
  29.   if (msg[code - 1] == '\n')
  30.   {
  31.     msg[code - 1] = '\0';
  32.   }
  33.   else
  34.   {
  35.     msg[code] = '\0';
  36.     ++code;
  37.   }
  38.   return code;
  39. }
  40.  
  41. void sendm(int queue)
  42. {
  43.   message buf;
  44.   buf.mtype = MSGTYPE1;
  45.   int isWork = 1;
  46.   do
  47.   {
  48.     ssize_t code;
  49.     code = read(STDIN_FILENO, buf.mtext, MSGSIZE - 1);
  50.     size_t length = msgln(buf.mtext, code);
  51.     if (code == 0)
  52.     {
  53.       buf.mtype = MSGTYPE2;
  54.       isWork = 0;
  55.     }
  56.     msgsnd(queue, &buf, length, 0);
  57.   } while (isWork);
  58. }
  59.  
  60. void waitm(int queue)
  61. {
  62.   message buf;
  63.   msgrcv(queue, &buf, MSGSIZE, MSGTYPE3, 0);
  64.   printf("\nQuit\n");
  65. }
  66.  
  67. int main()
  68. {
  69.   int queue;
  70.  
  71.   queue = msgget(123, IPC_CREAT | 0600);
  72.  
  73.   sendm(queue);
  74.   waitm(queue);
  75.   msgctl(queue, IPC_RMID, NULL);
  76.  
  77.   return EXIT_SUCCESS;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement