AmidamaruZXC

Untitled

May 8th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 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. #include <stdlib.h>
  7.  
  8. #define LAST_MESSAGE 255
  9.  
  10. int main(void)
  11. {
  12.     int msqid;
  13.     char pathname[]="09-1a.c";
  14.     key_t  key;
  15.     int len, maxlen;
  16.  
  17.     struct mymsgbuf
  18.     {
  19.        long mtype;
  20.        char mtext[81];
  21.     } mybuf;
  22.    
  23.     key = ftok(pathname, 0);
  24.    
  25.     if ((msqid = msgget(key, 0666 | IPC_CREAT)) < 0){
  26.        printf("Can\'t get msqid\n");
  27.        exit(-1);
  28.     }
  29.    
  30.     while (1) {
  31.        
  32.        maxlen = 81;
  33.        
  34.        if (( len = msgrcv(msqid, (struct msgbuf *) &mybuf, maxlen, 0, 0)) < 0){
  35.          printf("Can\'t receive message from queue\n");
  36.          exit(-1);
  37.        }
  38.  
  39.        if (mybuf.mtype == LAST_MESSAGE) {
  40.           msgctl(msqid, IPC_RMID, (struct msqid_ds *)NULL);
  41.           exit(0);
  42.        }
  43.        
  44.        printf("message type = %ld, info = %s\n", mybuf.mtype, mybuf.mtext);
  45.     }    
  46.  
  47.     return 0;      
  48. }
Advertisement
Add Comment
Please, Sign In to add comment