直接看代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#define MAX_MSG 128
struct mssgbuf
{
long type;
char buf[MAX_MSG];
};
int main()
{
int msqid;
int err;
key_t key;
key = ftok(".",'b');
if(key < 0)
{
printf("fait to creat value\n");
return -1;
}
msqid = msgget(key,IPC_CREAT|0666);
if(msqid < 0)
{
perror("failse to masgget");
return -1;
}
printf("success to get msqid %d\n",msqid);
system("ipcs -q");
struct mssgbuf sendbuf;
printf("enter your message:\n");
fgets(sendbuf.buf,MAX_MSG,stdin);
sendbuf.type = 1;
err = msgsnd(msqid,(void *)&sendbuf,strlen(sendbuf.buf)+1,0);
if(err < 0)
{
perror("write message fail");
return -1;
}
printf("success to write message\n");
printf("err = %d\n", err);
system("ipcs -q");
err = msgctl(msqid,IPC_RMID,NULL);
if(err < 0)
{
printf("fail to delete message queue\n");
return -1;
}
system("ipcs -q");
return 0;
}
以上是修改过的代码, 重点在sendbuf.type = 1这句,不加上这句的话 就会出现如题所说的错误。