linux消息队列msgsnd msgrcv发送10个消息,Linux环境下 消息队列编程 碰到些问题 msgsnd发送10个数据,msgrcv只能接受9个,第一个接受不到...

#include #define N 10

typedef struct student

{

int sno;

char sname[15];

}STU;

typedef struct msgbuf

{

long mtype;

STU stu[N];

}MSGBUF;

main(int argc, char* argv[])

{

int i = 0;

MSGBUF mbf = {0};

mbf.mtype = 1;

int MSQID ;

errno = 0;

char *name = "./msgaaa";

key_t key = ftok(name,0);

MSQID = msgget(key, IPC_CREAT);

while(i < N)

{

mbf.stu[i].sno = i;

strcpy(mbf.stu[i].sname, "hello");

if (i == 1)

{

memset(mbf.stu[i].sname, 0, strlen(mbf.stu[i].sname));

strcpy(mbf.stu[i].sname, "aaa");

}

if (i == N-1)

{

memset(mbf.stu[i].sname, 0, strlen(mbf.stu[i].sname));

strcpy(mbf.stu[i].sname, "end");

}

// printf("------- %s ------", mbf.stu[i].sname);

msgsnd(MSQID, &mbf.stu[i], sizeof(mbf.stu[i]), 0);

if (-1 == errno)

{

perror("msgsnd");

exit(-1);

}

printf(" %d -- %s\n", mbf.stu[i].sno, mbf.stu[i].sname);

i++;

sleep(1);

}//while

msgctl(MSQID, IPC_RMID, NULL);

}

msgsnd 显示结果:

0 -- hello

1 -- aaa

2 -- hello

3 -- hello

4 -- hello

5 -- hello

6 -- hello

7 -- hello

8 -- hello

9 -- end

我知道了,可能是 不稳定信号的问题。信号丢失。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值