AmidamaruZXC

Untitled

May 8th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6.  
  7. #define LAST_MESSAGE 255
  8.  
  9. int main() {
  10.     int msqid;
  11.  
  12.     char pathname[] = "09-2a.c";
  13.  
  14.     key_t key;
  15.  
  16.     int len, maxlen;
  17.  
  18.     struct mymsgbuf {
  19.         long mtype;
  20.         struct {
  21.             short sinfo;
  22.             float finfo;
  23.         } info;
  24.     } mybuf;
  25.  
  26.     if ((key = ftok(pathname, 0)) < 0) {
  27.         printf("Can't generate key\n");
  28.         exit(-1);
  29.     }
  30.  
  31.     if ((msqid = msgget(key, 0666 | IPC_CREAT)) < 0) {
  32.         printf("Can't get msqid\n");
  33.         exit(-1);
  34.     }
  35.  
  36.     while (1) {
  37.         maxlen = sizeof(mybuf.info);
  38.         if (len = msgrcv(msqid, &mybuf, maxlen, 0, 0) < 0) {
  39.             printf("Can't receive message from queue\n");
  40.             exit(-1);
  41.         }
  42.  
  43.         if (mybuf.mtype == LAST_MESSAGE) {
  44.             msgctl(msqid, IPC_RMID, NULL);
  45.             exit(0);
  46.         }
  47.  
  48.         printf("message type = %ld, sInfo = %i, fInfo = %f\n", mybuf.mtype, mybuf.info.sinfo, mybuf.info.finfo);
  49.     }
  50.  
  51.     return 0;
  52. }
Add Comment
Please, Sign In to add comment