Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1579668
  • 博文数量: 239
  • 博客积分: 1760
  • 博客等级: 上尉
  • 技术积分: 1595
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-08 23:53
文章分类

全部博文(239)

文章存档

2016年(1)

2015年(28)

2014年(53)

2013年(42)

2012年(50)

2011年(65)

分类: LINUX

2014-06-03 14:23:13

#include
#include
#include
#include
#define MAX_LINE 80
#define MY_MQ_ID 1233

typedef struct
{
    long type; 
    int tag;
    int * m;
    //download_arg_t *;
    //upload_arg_t *;
}MY_TYPE_T;
int main(  )
    {
        int msgid,ret;
        int b;
        //create the message queue with the id MY_MQ_ID
        msgid=msgget( MY_MQ_ID,0666|IPC_CREAT );
        if( msgid>=0 )
             printf( "Created a Message Queue,message queue identifier is %d\n",msgid );
        //modify the size of message queue
        struct msqid_ds buf;
        ret=msgctl( msgid,IPC_STAT,&buf );
        printf( "The origianl size of queue is %d\n",buf.msg_qbytes );
       
        buf.msg_qbytes=1000;
        ret=msgctl( msgid,IPC_SET,&buf );
        if( ret==0 )
            printf( "Size sucessfully changed for queue,message queue identifier is %d\n",msgid );
        //send a message
        b = 5;
        MY_TYPE_T myMessage;
        myMessage.type=1L;
        myMessage.tag=2;  
        myMessage.m=&b;
        ret=msgsnd( msgid,( struct msgbuf* )&myMessage,sizeof( MY_TYPE_T ),0 );
        if( ret!=-1 )
            printf( "Message send successfully.\n" );
        //read a message
        MY_TYPE_T recMessage;
        ret=msgrcv( msgid,( struct msgbuf* )&recMessage,sizeof(MY_TYPE_T),1,0 );
        if( ret!=-1 )
            {
                printf( "\nRead a message from the queue\n" );
                printf( "Message Type:%ld\n",recMessage.type );
                printf( "Message Type:%d\n",recMessage.tag );
                printf( "Message Type:%d\n",*(recMessage.m) );
            }
        //destroy a message queue
        ret=msgctl( msgid,IPC_RMID,NULL );
        if( ret!=-1 )
            printf( "Message queue %d sucessfully removed.\n",msgid );
       
        return 0;
    }


转载自:http://blog.csdn.net/lifan5/article/details/7588540

阅读(1356) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~