• IPC--消息队列


    //rec.c

    #include<stdlib.h>
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/ipc.h>
    #include<sys/msg.h>
    
    typedef struct {
        long msgtype;
        char msgdata[512];
    }msgbuf;
    
    int main()
    {
        msgbuf msgRec;
        int iRet = -1;
        int iMsgID = -1;
        //create msgque
        iMsgID = msgget((key_t)1111,0660|IPC_CREAT);
        if(iMsgID == -1){
            printf("error 1!");
            return -1;
        }
        while(1){
           memset(msgRec.msgdata,0x00,sizeof(msgRec.msgdata));
            //rec data param 3 means
            iRet =msgrcv(iMsgID,(void*)&msgRec,512,1,0);
    
            if(iRet == -1){
                printf("error 2!");
                return -2;
            }
           printf("You wrote: %s
     len is [%d]
    ",msgRec.msgdata,strlen(msgRec.msgdata));
    
        }
        return 1;
    
        if(msgctl(iMsgID, IPC_RMID, 0) == -1)
        {
            printf("error 3!");
                return -3;
        }
    }
    

      snd.c

    input data:
    cd^H^H^C
    root@ubuntu:/usr/src/ipc# ls
    #include<stdlib.h>
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/ipc.h>
    #include<sys/msg.h>
    
    typedef struct {
        long msgtype;
        char msgdata[512];
    } msgbuf;
    
    
    int main()
    {
        msgbuf Sendmsg;
        char sBuf[512];
        int iMsgID = -1;
        int iRet = -1;
        //create msgque
        iMsgID = msgget((key_t)1111,0660|IPC_CREAT);
        if(iMsgID == -1){
            printf("error 111!");
            return -1;
        }
    
        while(1){
            printf("input data:
    ");
            memset(sBuf,0x00,sizeof(sBuf));
            fgets(sBuf,512,stdin);
            Sendmsg.msgtype = 1;
    //      memcpy(Sendmsg.msgdata,sBuf,strlen(sBuf));
            strcpy(Sendmsg.msgdata,sBuf);
    
            //send
            iRet =msgsnd(iMsgID,(void*)&Sendmsg,strlen(Sendmsg.msgdata),0);
            printf("send data = [%s]
     len = [%d]
    ",Sendmsg.msgdata,strlen(Sendmsg.msgdata));
            if(iRet == -1){
                printf("error 2");
                return -2;
            }
        }
        return 1;
    }

    缺少自信的人啊,要快快快快成长!!!

  • 相关阅读:
    Unity3d资源管理分析
    Unity3D之MonoBehaviour的可重写函数整理
    Unity3D脚本学习——运行时类
    A*寻路初探(转载)
    360sdk网游支付服务
    总结使用Unity 3D优化游戏运行性能的经验
    Unity3D之Lightmap详解
    「luogu2714」四元组统计
    「BZOJ1066」[SCOI2007]蜥蜴
    「luogu1251」餐巾计划问题
  • 原文地址:https://www.cnblogs.com/ashen/p/4543048.html
Copyright © 2020-2023  润新知