您的位置:首页 > 其它

消息队列函数 msgsnd 出现Invalid argument错误的解决办法

2017-08-15 09:15 465 查看
直接看代码:

#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这句,不加上这句的话 就会出现如题所说的错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐