您的位置:首页 > 其它

消息队列

2016-05-07 09:02 288 查看
//消息队列
//msg.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <fcntl.h>
#include <sys/msg.h>

#define DEF_MAX_PATH ("./SHM")
#define DEF_STD_ERROR (-1)
#define DEF_MSG_SIZE (1024)

enum
{
enum_data_int=1,
enum_data_string,
enum_data_double
};
//msg struct

struct my_msg
{
long mtype;
char mtext[DEF_MSG_SIZE];
};

int main(int argc,char *argv[])
{
key_t key;
int msg_id;
struct msqid_ds msg_ds;
struct my_msg msg;
int data;
//create
key = ftok(DEF_MAX_PATH,atoi(argv[1]));
//open
msg_id = msgget(key, IPC_CREAT);
//write
while(1)
{
printf("Please input data:1.int 2.string 3.double\n");
scanf("%d",&msg.mtype);
printf("Please input match data:");
scanf("%d",&data);
*(int*)msg.mtext = data;
msgsnd(msg_id,&msg,sizeof(data),0);
if(data <=0)
{
break;
}
}
//get
msgctl(msg_id,IPC_STAT,&msg_ds);
printf("max_size:[%d] cur_size:[%d] count:[%d] creator:[%d] key:[%d]\n",msg_ds.msg_qbytes,
msg_ds.__msg_cbytes,msg_ds.msg_qnum,msg_ds.msg_perm.uid,msg_ds.msg_perm.__key);
//close
close(msg_id);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: