Chinaunix首页 | 论坛 | 博客
  • 博客访问: 398451
  • 博文数量: 119
  • 博客积分: 1470
  • 博客等级: 上尉
  • 技术积分: 1258
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-24 13:50
文章分类

全部博文(119)

文章存档

2018年(6)

2017年(11)

2016年(4)

2013年(8)

2012年(1)

2011年(2)

2010年(4)

2009年(37)

2008年(16)

2006年(30)

我的朋友

分类: LINUX

2006-12-11 15:51:10

#include
#include
#include
#include

struct mymsgbuf{
    long mtype;/*Message type*/
    int request;/*Work request number*/
    double salary;/*Employee's salary*/
}msg;


int open_queue(key_t keyval)
{
    int qid;
    if((qid=msgget(keyval,IPC_CREAT|0660))==-1)
    {
        return(-1);
    }
    return(qid);
}

int send_message(int qid,struct mymsgbuf *qbuf)
{
    int result,length;
    /*The length is essentially the size of the structure minus sizeof(mtype)*/
    length=sizeof(struct mymsgbuf)-sizeof(long);
    if((result=msgsnd(qid,qbuf,length,0))==-1)
    {
        return(-1);
    }
    return(result);
}

main()
{
    int qid, i;
    key_t msgkey;
    struct mymsgbuf msg;

    /*Generateour IPC key value*/
    //msgkey=ftok(".",'m');
    /*Open/createthequeue*/
    if((qid=open_queue(0x889))==-1){
        perror("open_queue");
        exit(1);
    }
    /*Load up the message with a r bitrary test data*/
    msg.mtype=1;/*Message type must be a positive number!*/
    msg.request=1;/*Data element#1*/
    msg.salary=1000.00;/*Data element #2(my yearly salary!)*/
    /*Bombsaway!*/
    for(i=0;i<10;i++){
        if((send_message(qid,&msg))==-1){
            perror("send_message");
            exit(1);
        }
    }
}

上面是写QUEUE

下面是读QUEUE
#include
#include
#include
#include
#include

#define MAX 1024

/*typedef struct
{
    long type;
    char buf[MAX];
} buffer;
*/
typedef struct mymsgbuf{
    long mtype;
    int request;
    double salary;
} buffer;


int main(void)
{
    long key, id, i;
    int iNum;
    buffer buff;
    key=0x889;

    id = msgget(key,0660|IPC_CREAT);

    if(id < 0){
        fprintf(stderr, "%s\n", "msgget Error!!");
        exit(1);
    }
    i=0;
    while(1){
        fprintf(stderr, "%s\n", "start");
        iNum =msgrcv(id, &buff, MAX, 1, MSG_NOERROR);
        fprintf(stderr, "buff = %f  i= %d\n", buff.salary, ++i);
    }


    return(0);
}
MSG_NOERROR 如果QUEUE里没有东西就等着直到有东西为止!!

用ipcs  -qa 可是看到队列里的消息!
阅读(1018) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~