linux下msgsend函数引用,消息队列。 msgsend msgrcv。系统V在C系统调用(Linux)

您好我正在使用一些共享内存在不同的进程读取和写入数据。我正在使用消息队列来存储数据在读取和写入操作之间发生更改的消息。消息队列。 msgsend msgrcv。系统V在C系统调用(Linux)

/* struct that defines a message */

struct msgbuf{

long mtype; /* must be positive */

int childId; //ID of child sending message

int bufferChanged; //Buffer at which value was changed

int beforeValue; //Value before child sleeps

int afterValue; //Value after child sleeps

};

所以在阅读,写作和检查的变化过程店的消息通过以下方式

struct msgbuf msg = {BUFFER_CHANGED, id, position, read, bufferArr[position]};

if(msgsnd(msqid, &msg, sizeof(msg), 0)== -1){

perror("msgsnd in read.write");

}

这是工作的罚款。哦,顺便说一下,我如何创建消息队列。

#define BUFFER_CHANGED 1

qKey = ftok("./", 'A');

msqid = msgget(qKey, (IPC_CREAT | 0666));

/*Perform the following if the call is unsuccessful.*/

if(msqid == -1){

printf ("\nThe msgget call failed, error number = %d\n", errno);

}

/*Return the msqid upon successful completion.*/

else{

printf ("\nMessage queue successful. The msqid = %d\n", msqid);

//exit(0);

}

所以我的问题是,我不太清楚如何从队列中检索消息并将它们显示在屏幕上。我一直在阅读msgrcv()系统调用,但对我来说不是很清楚。

rc = msgrcv(msqid, &msg, sizeof(msg), BUFFER_CHANGED, IPC_NOWAIT);

rc为int因为msgrcv()返回一个int。我如何将这个int指向实际的消息?如何从消息中读取内容,以便我可以显示它们?我假设这应该在某种循环中完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值