从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 (将#修改为@)