Named PIPE is stuck on open

时间:2017-07-06 07:37:56

标签: c ipc named-pipes

I am trying to implement a named PIPE IPC method in which I am sending float values (10) each time the sendToPipe function is being called. Here is the sendToPipe function -

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
}

And I have initialized the named PIPE in my main here :

int main(){

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

    // Other code
}

I am not getting where am I going wrong here. Any help is appreciated. Also let me know if any other information is required.

2 个答案:

答案 0 :(得分:1)

总结所有评论点:

  1. 节目是"冻结"因为管道的另一边没有读卡器。
  2. 第一个程序启动后,管道已创建。下一个程序启动将返回FILE_EXIST错误。
  3. 要将一个镜头中的10个值写入管道,使读者能够一次性接收所有这些值,您应该准备一个缓冲区,然后打开管道并写入它(阻塞模式)。作为旁注,请注意读者方:读取功能不允许在镜头中检索整个缓冲区,因此必须检查返回的读取数据编号。

答案 1 :(得分:1)

感谢@LPs指出出了什么问题。进入PIPE后我的每个样品都在等待阅读。然而,我想要一个实现,其中我的读者线程可以一次读取所有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);
}