Advertisement
cheungtifan

msg.c

Oct 30th, 2011
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <sys/msg.h>
  2. #include <stdio.h>
  3.  
  4. struct my_msg {
  5.     long mtype;
  6.     struct msg_inf {
  7.         char src[24];
  8.         char msg[512];
  9.     } msg;
  10. };
  11.  
  12. int main () {
  13.     key_t key;
  14.     int mqid;
  15.     size_t msg_size;
  16.  
  17.     msg_size = sizeof(struct my_msg) - sizeof(long);
  18.     key = ftok("/tmp/ssd", 'b');
  19.     printf("K0\t%02x\t%s\t%d\n", key, "/tmp/ssd", 'b');
  20.     key = ftok("/tmp/msg", 'a');
  21.     printf("K\t%02x\t%s\t%d\n", key, "/tmp/msg", 'a');
  22.     mqid = msgget(key, 0666 | IPC_CREAT);
  23.     struct my_msg msg = {2, {"system", "Horrrrrry"}};
  24.     struct my_msg rmsg;
  25.     msgsnd(mqid, &msg, msg_size, 0);
  26.     // rcv
  27.     printf("RET\t%ld\n", msgrcv(mqid, &rmsg, msg_size, 2, 0));
  28.     // print
  29.     printf("R\tmtype\t%ld\n", rmsg.mtype);
  30.     printf("R\tsrc\t\"%s\"\n", rmsg.msg.src);
  31.     printf("R\tmsg\t\"%s\"\n", rmsg.msg.msg);
  32.     return;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement