#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 可是看到队列里的消息!
阅读(1047) | 评论(0) | 转发(0) |