• 进程间通信-消息队列


    互斥:矛盾 

    同步:协作

    消息队列

    #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
        ");
        55     printf("msgget ok,msgid=%d
        ",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
        ");
        48     printf("msgget ok,msgid=%d
        ",msgid);
        49 
        50     echo_srv(msgid);
        51 }
    作者:长风 Email:844064492@qq.com QQ群:607717453 Git:https://github.com/zhaohu19910409Dz 开源项目:https://github.com/OriginMEK/MEK 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利. 感谢您的阅读。如果觉得有用的就请各位大神高抬贵手“推荐一下”吧!你的精神支持是博主强大的写作动力。 如果觉得我的博客有意思,欢迎点击首页左上角的“+加关注”按钮关注我!
  • 相关阅读:
    语言混编总结二
    可执行文件与符号(表)
    iOS错误报告中关于崩溃地址的分析
    Find the build UUID in a Crash Report
    Symbolicating Crash Reports With atos
    iOS crash log 解析 symbol address = stack address
    Address space layout randomization
    ASLR(Address space layout randomization)地址空间布局随机化
    缓冲区溢出详解
    如何快速查看将C反汇编的代码
  • 原文地址:https://www.cnblogs.com/zhaohu/p/8997903.html
Copyright © 2020-2023  润新知