c - 队列 Linux 无法写入

标签 c linux

我的流程代码有问题:

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,则具有特殊含义。

解决方案是设置MsgTypestruct MsgStructure大于 0。

关于c - 队列 Linux 无法写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847118/

相关文章:

c - 如何修改CMakeList.txt : library found, 但是有链接错误

linux - 如何通过ssh发送三个管道的数据?

php - 设置 Apache 虚拟主机 (CentOs)

java - JNI 在 android/java 中解析 jstring 或 char* 而不使用 std

c - 现有文件的 LD 错误

c - scanf() 将换行符保留在缓冲区中

c++ - 只写指针类型

linux - Linux 中处理的 AVRCP 关键事件

linux - 为什么 OpenCL 拒绝在 Ubuntu 20.04 下兼容硬件?

java - 使用 Eclipse(或其他软件)进行远程 Java 开发?