码迷,mamicode.com
首页 > 系统相关 > 详细

linux 消息队列

时间:2019-02-26 17:41:19      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:获得   nbsp   linux   int   mem   pcs   print   ems   class   

/*
*删除 msgctl(msgid,IPC_RMID,0); ipcrm -q id ipcs -q 查看
*创建 msgqueue 时指定 key 为 IPC_PRIVATE 读取时将不能获得key指定的队列,所以需要自己定key
*/

  



#include<sys/ipc.h> #include<sys/msg.h> #include<sys/types.h> #include<unistd.h> #include<stdlib.h> #include<memory.h> #include<string.h> char path[100]={0}; typedef struct{ long mtype; char buff[1024]; }MSG; key_t getKey(){ getcwd(path,sizeof(path)-1); return ftok(path,0); } int main(){ int msgid=msgget(getKey(),IPC_CREAT|0666); MSG msg; memset(&msg,0,sizeof(msg)); msg.mtype=1; char buff[100]="1234567890asdfghjklqwertyuiopzxcvbnm,./"; memcpy(msg.buff,buff,sizeof(buff)); msgsnd(msgid,&msg,sizeof(MSG)-8,0); }
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<memory.h>
#include<string.h>
char path[100]={0};

typedef struct{
    long mtype;
    char buff[1024];
}MSG;

key_t getKey(){
    getcwd(path,sizeof(path)-1);
    return ftok(path,0);
}


int main(){
    int msgid=msgget(getKey(),0666);
    printf("msg id is %d\n",msgid);
    MSG     msg;
    memset(&msg,0,sizeof(msg));
    msgrcv(msgid,&msg,sizeof(msg)-8,1,IPC_NOWAIT);
    printf("rcv %s\n",msg.buff);
}

 

linux 消息队列

标签:获得   nbsp   linux   int   mem   pcs   print   ems   class   

原文地址:https://www.cnblogs.com/libing029/p/10438224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!