Advertisement
xeritt

Передача через msgsnd и msgrecv сложной структуры

Jul 26th, 2017
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. //---------------- mymsg.c
  2. #ifndef __MYMSG_H__
  3. #define __MYMSG_H__
  4.  
  5. #include<string.h>
  6. #include<time.h>
  7. #include<sys/ipc.h>
  8. #include<sys/msg.h>
  9. #include<sys/errno.h>
  10.  
  11. #define MSGPERM 0600    // msg queue permission
  12. #define MSGTXTLEN 128   // msg text length
  13. #define MSGKEY 32769
  14. struct msg_buf {
  15.   long mtype;
  16.   char mtext[MSGTXTLEN];
  17.   int len;
  18. } msg;
  19. #endif
  20.  
  21. //---------------- send.c
  22. #include <stdio.h>
  23. #include "mymsg.h"
  24.  
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27.  
  28. //extern int errno;
  29.  
  30. int get_queue_ds( int qid, struct msqid_ds *qbuf )
  31. {
  32.     if( msgctl( qid, IPC_STAT, qbuf) == -1){
  33.                 return(-1);
  34.     }
  35.     return(0);
  36. }
  37.  
  38. int main(int argc, char **argv)
  39. {
  40.     int msgqid, rc;
  41.    
  42.     msgqid = msgget(MSGKEY, MSGPERM|IPC_CREAT|IPC_EXCL);
  43.     if (msgqid < 0) {
  44.         perror(strerror(errno));
  45.         printf("failed to create message queue with msgqid = %d\n", msgqid);
  46.         return 1;
  47.     } else
  48.         printf("message queue %d created\n",msgqid);
  49.  
  50.     // message to send
  51.     msg.mtype = 1; // set the type of message
  52.     msg.len = sizeof(msg)-sizeof(long);//getpid();
  53.     sprintf (msg.mtext, "%s\n", "a text msg..."); /* setting the right time format by means of ctime() */
  54.     printf ("a len msg = %d\n", msg.len); /* setting the right time format by means of ctime() */
  55.  
  56.   // send the message to queue
  57.     rc = msgsnd(msgqid, &msg, sizeof(msg)-sizeof(long), 0); // the last param can be: 0, IPC_NOWAIT, MSG_NOERROR, or IPC_NOWAIT|MSG_NOERROR.
  58.     if (rc < 0) {
  59.         perror( strerror(errno) );
  60.         printf("msgsnd failed, rc = %d\n", rc);
  61.         return 1;
  62.     } else
  63.         printf("Message send done!\n");
  64.        
  65.     return 0;
  66. }
  67.  
  68. //---------------- recv.c
  69. #include <stdio.h>
  70. #include "mymsg.h"
  71. int main(int argc, char **argv)
  72. {
  73.     int msgqid, rc;
  74.    
  75.     msgqid = msgget(MSGKEY, MSGPERM|IPC_EXCL);
  76.     if (msgqid < 0) {
  77.         perror(strerror(errno));
  78.         printf("failed to create message queue with msgqid = %d\n", msgqid);
  79.         return 1;
  80.     } else
  81.         printf("message queue %d created\n", msgqid);
  82.     // read the message from queue
  83.     rc = msgrcv(msgqid, &msg, sizeof(msg)-sizeof(long), 0, 0);
  84.     if (rc < 0) {
  85.         perror( strerror(errno) );
  86.         printf("msgrcv failed, rc=%d\n", rc);
  87.         return 1;
  88.     }
  89.     printf("received msg: %s\n", msg.mtext);
  90.     printf("received len: %d\n", msg.len);
  91.  
  92.     // remove the queue
  93.     rc=msgctl(msgqid,IPC_RMID,NULL);
  94.     if (rc < 0) {
  95.         perror( strerror(errno) );
  96.         printf("msgctl (return queue) failed, rc=%d\n", rc);
  97.         return 1;
  98.     }
  99.     printf("message queue %d is gone\n",msgqid);   
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement