命名 PIPE 卡在打开状态

Named PIPE is stuck on open

我正在尝试实现一个 named PIPE IPC 方法,每次调用 sendToPipe 函数时我都会发送 float 值 (10)。这是 sendToPipe 函数 -

int fd;
char *fifoPipe = "/tmp/fifoPipe";

void sendToPipe(paTestData *data){

    int readCount   = 10;
    fd      = open(fifoPipe, O_WRONLY);   // Getting stuck here

    // Reading 10 sample float values from a buffer from readFromCB pointer as the initial address on each call to sendToPipe function.
    while(readCount > 0){

        write(fd, &data->sampleValues[data->readFromCB], sizeof(SAMPLE));  // SAMPLE is of float datatype
        data->readFromCB++;
        readCount--;
    }

    close(fd);

    //  Some code here
}

而且我已经在我的 main 中初始化了 named PIPE :

int main(){

    // Some code
    mkfifo(fifoPipe, S_IWUSR | S_IRUSR);

    // Other code
}

我不知道哪里错了。任何帮助表示赞赏。如果需要任何其他信息,也请告诉我。

总结所有评论点:

  1. 程序是 "freezing" 因为管道的另一边没有 reader。
  2. 第一个程序启动后,管道就创建好了。下一个程序启动将 return FILE_EXIST 错误。
  3. 要一次性将 10 个值写入管道,使 reader 能够一次接收所有这些值,您应该准备一个缓冲区,然后打开管道并写入(阻塞模式) .作为旁注,请注意 reader 方面:读取函数不会授予一次检索整个缓冲区的权限,因此您必须检查 returned 读取数据编号。

感谢@LPs 指出问题所在。进入 PIPE 后,我的每个样本都在等待读取。然而,我想要一个实现,其中我的 reader 线程可以一次读取所有 10 个样本。这是我的实现。 -

void pipeData(paTestData *data){

    SAMPLE *tempBuff = (SAMPLE*)malloc(sizeof(SAMPLE)*FRAME_SIZE);
    int readCount   = FRAME_SIZE;

    while(readCount > 0){

        tempBuff[FRAME_SIZE - readCount] = data->sampleValues[data->readFromCB];        
        data->readFromCB++;
        readCount--;
    }

    fd = open(fifoPipe, O_WRONLY);

    write(fd, tempBuff, sizeof(tempBuff));
    // Reader thread called here

    close(fd);
}