分类: 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