AmidamaruZXC

Untitled

May 8th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 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;
  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.     for (int i = 0; i < 5; ++i) {
  37.         mybuf.mtype = 1;
  38.         mybuf.info.sinfo = 1337;
  39.         mybuf.info.finfo = 13.37;
  40.         len = sizeof(mybuf.info);
  41.  
  42.         if (msgsnd(msqid, &mybuf, len, 0) < 0) {
  43.             printf("Can't send message to queue\n");
  44.             msgctl(msqid, IPC_RMID, (struct msqid_ds *) NULL);
  45.             exit(-1);
  46.         }
  47.     }
  48.  
  49.     mybuf.mtype = LAST_MESSAGE;
  50.     len = 0;
  51.     if (msgsnd(msqid, &mybuf, len, 0) < 0) {
  52.         printf("Can't send message to queue\n");
  53.         msgctl(msqid, IPC_RMID, (struct msqid_ds *) NULL);
  54.         exit(-1);
  55.     }
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment