Advertisement
akela43

sysVmsgqueue

Mar 26th, 2023
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/ipc.h>
  4. #include <sys/msg.h>
  5. #include <errno.h>
  6. int main(int argc, char **argv)
  7. {
  8.         key_t key = ftok("/tmp/msg.temp", 1);
  9.         if (key == -1) {
  10.          printf("key: -1"); return 1;
  11.  
  12.         }
  13.         struct message {
  14.             long mtype;
  15.             char mtext[80];
  16.         } Msg;
  17.         int msgq = msgget(key, IPC_CREAT| 0666);
  18.         if (-1 == msgq)
  19.         {
  20.                 printf("msgq: -2");
  21.                 printf("%d", errno);
  22.                 return 2;
  23.         }
  24.         int r = msgrcv(msgq, &Msg, sizeof(Msg.mtext), 0, 0 );
  25.         FILE *f = fopen("/home/box/message.txt", "w");
  26.         fwrite(Msg.mtext, sizeof(char), r, f);
  27.         fclose(f);
  28.  
  29.         msgctl(msgq, IPC_RMID, 0);
  30.         return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement