AmidamaruZXC

Untitled

May 8th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 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.    
  13.     int msqid;
  14.     char pathname[]="09-1a.c";
  15.     key_t  key;
  16.     int i,len;
  17.  
  18.     struct mymsgbuf
  19.     {
  20.        long mtype;
  21.        char mtext[81];
  22.     } mybuf;
  23.  
  24.  
  25.  
  26.     /* Create or attach message queue  */
  27.    
  28.     key = ftok(pathname, 0);
  29.    
  30.     if ((msqid = msgget(key, 0666 | IPC_CREAT)) < 0){
  31.        printf("Can\'t get msqid\n");
  32.        exit(-1);
  33.     }
  34.  
  35.  
  36.     /* Send information */
  37.    
  38.     for (i = 1; i <= 5; i++){
  39.        
  40.        mybuf.mtype = 1;
  41.        strcpy(mybuf.mtext, "This is text message");
  42.        len = strlen(mybuf.mtext)+1;
  43.        
  44.        if (msgsnd(msqid, (struct msgbuf *) &mybuf, len, 0) < 0){
  45.          printf("Can\'t send message to queue\n");
  46.          msgctl(msqid, IPC_RMID, (struct msqid_ds *) NULL);
  47.          exit(-1);
  48.        }
  49.     }    
  50.        
  51.     /* Send the last message */  
  52.        
  53.     mybuf.mtype = LAST_MESSAGE;  
  54.     len         = 0;  
  55.        
  56.     if (msgsnd(msqid, (struct msgbuf *) &mybuf, len, 0) < 0){
  57.        printf("Can\'t send message to queue\n");
  58.        msgctl(msqid, IPC_RMID, (struct msqid_ds *) NULL);
  59.        exit(-1);
  60.     }
  61.  
  62.     return 0;    
  63. }
Advertisement
Add Comment
Please, Sign In to add comment