- 积分
- 101
贡献111
飞刀0 FD
注册时间2011-11-29
在线时间20 小时

扫一扫,手机访问本帖 
|
本帖最后由 豆腐脑 于 2014-12-25 08:32 编辑
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define MAX_TEXT 2048
struct my_msg_st
{
long int my_msg_type;
char some_text[MAX_TEXT];
};
int main(void)
{
int running=1;
struct my_msg_st sme_data;
int msgid2=0;
int smdi=0;
char buffer[BUFSIZ];
msgid2=msgget((key_t)124, 0666 | IPC_CREAT); //创建消息队列成功返回0
if(msgid2==-1)
{ printf("msgget failed with error:\n");
exit(EXIT_FAILURE);
}
printf("msgget ID with %d:\n", msgid2); //创建消息队列成功输出0
while(running)
{
printf("enter some text:");
fgets(buffer,BUFSIZ, stdin);
sme_data.my_msg_type=1;
strcpy(sme_data.some_text,buffer);
if(msgsnd(msgid2, (void *)&sme_data, MAX_TEXT, 0)==-1)
{ fprintf(stderr, "msgsed failed\n");
exit(EXIT_FAILURE);
}
if(strncmp(buffer, "sam", 3)==0)
{
system("ipcs -q");
running=0;
}
system("ipcs -q");
}
exit(EXIT_SUCCESS);
}
第一次运行后,输出0
第二、三。。。。次运行后输出为整数,为什么?????????????重启虚拟机后运行的结果:
|
|