代码之家  ›  专栏  ›  技术社区  ›  Ashish K

命名管道卡在打开位置

  •  2
  • Ashish K  · 技术社区  · 7 年前

    我正在尝试实现一个 named PIPE IPC float 每次 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
    }
    

    我已经初始化了 named PIPE main 在这里:

    int main(){
    
        // Some code
        mkfifo(fifoPipe, S_IWUSR | S_IRUSR);
    
        // Other code
    }
    

    2 回复  |  直到 7 年前
        1
  •  3
  •   LPs    6 年前

    总结所有评论点:

    1. 程序“冻结”,因为管道的另一侧没有读取器。
    2. 在第一次程序启动后,创建了管道。下一个项目将重新启动 FILE_EXIST 错误
        2
  •  1
  •   Ashish K    7 年前

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