如何解决从C程序写入要在bash脚本中读取的命名管道
我试图从C程序写入命名管道,并在bash脚本中从该管道读取。我正在使用两个终端,首先运行c程序,然后运行bash脚本。 bash脚本从管道读取数据,因为一旦执行,C程序便被解除阻塞,但是$ line始终为空。
打印到终端的唯一内容是“完成阅读”。我不确定为什么该值为空。
Bash脚本:
#!/bin/bash
pipe=/tmp/myfifo
if read line <$pipe; then
echo $line
fi
echo "done reading"
C程序:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
void WritePipe()
{
int fd1;
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo,0666);
char s1[50] = "Hello From C++ Program";
fd1 = open(myfifo,O_WRONLY);
write(fd1,s1,strlen(s1) + 1);
close(fd1);
}
int main()
{
WritePipe();
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。