我的流程代码有问题:
FILE *fp;
int i,
counter; // Liczba liter w wiadomosci
wchar_t buffer[1024], *line;
struct MsgStructure WB; // Write Buffor
setlocale(LC_ALL, "pl_PL.UTF-8");
while(run)
{
fp = fopen(FIFO, "r");
fwide(fp, 1);
while ((line = fgetws(buffer, sizeof buffer / sizeof buffer[0], fp)) != NULL)
{
counter = 0;
for (i = 0; line[i] != L'\0'; i++)
if (iswalpha(line[i]))
counter++;
WB.Msg = counter;
if ((WriteToQueue( qid, &WB )) == -1)
{
perror("Error\n");
}
}
fclose(fp);
}
我的程序从 FIFO 文件中读取,然后计算字母的数量,然后我想将它写入队列,但我收到一个错误,我无法写入队列,因为“参数错误”
我的结构:
struct MsgStructure {
long int MsgType;
int Msg;
};
WriteToQueue 是一个简单的函数:
int WriteToQueue( int qid, struct MsgStructure *qbuf ){
int result, BufSize;
BufSize = sizeof(struct MsgStructure) - sizeof(long);
result = msgsnd( qid, qbuf, BufSize, 0);
return(result);
}
我的消息类型是 int,计数器也是 int。我不知道为什么这不起作用。也许这是 setlocale 的问题?? 队列正在其他进程中创建。
最佳答案
如评论中所述:
EINVAL Invalid msqid value, or nonpositive mtype value,
or invalid msgsz value (less than 0 or greater than
the system value MSGMAX).
前者已被排除,后者显然不成立,即BufSize
正在sizeof(int)
在示例代码中。
叶子 mtype<=0
.从手册页:
The msgp argument is a pointer to caller-defined
structure of the following general form:
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
};
这符合 msgrcv()
谁的msgtyp
参数,如果为 0,则具有特殊含义。
解决方案是设置MsgType
在 struct MsgStructure
大于 0。
关于c - 队列 Linux 无法写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847118/