进程间通信-消息队列

互斥:矛盾 

同步:协作

消息队列

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

  • int msgget(key_t key,int msgflg);
  • int msgctl(int msgid,int cmd,stuct msqid_ds *buf);
  • cmd:
  •   IPC_RMID
  •   IPC_STAT
  •   IPC_SET
  • int msgend(int msqid,const void *msgp,sie_t msgsz,int msgflg);
    • msgflg:
      • IPC_NOWAT表示队列蛮不等待,返回EAGAIN
  • ssize_t msgrcv(int msqid,void *msgp, size_t msgsz,long msgtyp,int msgflg);
  • 基于消息队列的客户端
    •  1 #include <unistd.h>
       2 #include <sys/types.h>
       3 #include <sys/ipc.h>
       4 #include <sys/msg.h>
       5 #include <stdlib.h>
       6 #include <stdio.h>
       7 #include <errno.h>
       8 #include <string.h>
       9 
      10 #define ERR_EXIT(m)\
      11     do\
      12     {\
      13         perror(m);\
      14         exit(EXIT_FAILURE);\
      15     }while(0)
      16 
      17 #define MSGMAX 8192
      18 
      19 struct msgbuf
      20 {
      21     long mtype;
      22     char mtext[MSGMAX];
      23 };
      24 
      25 void echo_cli(int msgid)
      26 {
      27     int n;
      28     int pid;
      29     pid = getpid();    
      30     struct msgbuf msg;
      31     memset(&msg,0,sizeof(msg));
      32     *((int*)msg.mtext) = pid;
      33     msg.mtype = 1;
      34     while(fgets(msg.mtext+4,MSGMAX, stdin) != NULL)
      35     {
      36         msg.mtype = 1;
      37         if(msgsnd(msgid,&msg,4+strlen(msg.mtext+4), 0) < 0)
      38             ERR_EXIT("msgsnd");
      39 
      40         memset(msg.mtext+4, 0, MSGMAX-4);
      41         if((n = msgrcv(msgid, &msg, MSGMAX, pid, 0)) < 0)
      42             ERR_EXIT("msgrcv");
      43 
      44         fputs(msg.mtext+4,stdout);
      45         memset(msg.mtext+4, 0, MSGMAX-4);
      46     }
      47 }
      48 
      49 int main(int argc,char* argv[])
      50 {
      51     int msgid;
      52     msgid = msgget(1234,0);
      53     if(msgid == -1)
      54         ERR_EXIT("msgget failed\n");
      55     printf("msgget ok,msgid=%d\n",msgid);
      56     echo_cli(msgid);
      57 }
  • 基于消息队列的服务器
    •  1 #include <unistd.h>
       2 #include <sys/types.h>
       3 #include <sys/ipc.h>
       4 #include <sys/msg.h>
       5 #include <stdlib.h>
       6 #include <stdio.h>
       7 #include <errno.h>
       8 #include <string.h>
       9 
      10 #define ERR_EXIT(m)\
      11     do\
      12     {\
      13         perror(m);\
      14         exit(EXIT_FAILURE);\
      15     }while(0)\
      16 
      17 #define MSGMAX 8192
      18 struct msgbuf
      19 {
      20     long mtype;
      21     char mtext[MSGMAX];
      22 };
      23 
      24 void echo_srv(int msgid)
      25 {
      26     int n;
      27     struct msgbuf msg;
      28     memset(&msg, 0, sizeof(msg));
      29     while(1)
      30     {
      31         if((n = msgrcv(msgid,&msg,MSGMAX,1,0)) < 0)
      32             ERR_EXIT("msgsnd");
      33         int pid;
      34         pid = *((int*)msg.mtext);
      35         
      36         fputs(msg.mtext+4,stdout);
      37         msg.mtype = pid;
      38         msgsnd(msgid, &msg, n, 0);
      39     }
      40 }
      41 
      42 int main(int argc,char* argv[])
      43 {
      44     int msgid;
      45     msgid = msgget(1234,IPC_CREAT | 0666);
      46     if(msgid == -1)
      47         ERR_EXIT("msgget failed\n");
      48     printf("msgget ok,msgid=%d\n",msgid);
      49 
      50     echo_srv(msgid);
      51 }

猜你喜欢

转载自www.cnblogs.com/zhaohu/p/8997903.html