【发布时间】:2017-07-06 07:37:55
【问题描述】:
我正在尝试实现一个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
}
我不知道我在哪里出错了。任何帮助表示赞赏。如果需要任何其他信息,也请告诉我。
【问题讨论】:
-
首先你必须检查
mkfifo返回值... -
检查
errno看看发生了什么:if (result < 0) { perror ("mkfifo"); exit (2); }。然后看看the man -
好的,所以错误是:
FILE_EXIST。我删除了管道并再次执行mkfifo的 0 退出值。 -
要解锁这种情况,您可以轻松地与阅读器启动另一个线程。之后,我开始考虑在发送方准备整个缓冲区,包含 10 个值,并将它们一次性写入管道。因此读取器将被唤醒(在阻塞模式下),它可以在一次读取中读取所有值。
标签: c ipc named-pipes