gpt4 book ai didi

c - 从管道 C 读取

转载 作者:行者123 更新时间:2023-11-30 15:37:23 24 4
gpt4 key购买 nike

我需要创建一个新进程来运行 xxx 程序,加载 xxx 程序,发送从管道读取的数据“xxx”,但每当我收到此错误“错误:无法从标准输入读取”我尝试编译。

pid_t pid;
pid = fork();
int fd[2];
int ret;

ret = pipe(fd);

if (ret == -1)
{
perror("pipe failed");
exit(1);
}

if (pid == -1)
{
perror("fork failed");
exit(1);
}

else if (pid == 0) //Child
{
char buff[10];
int validate;
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
read(STDIN_FILENO, buff, sizeof(buff));
xxx = execlp("./xxx", "./xxx", NULL); //run xxx
exit(validate);
}

else //Parent //WRITE STDIN TO PIPE
{
close(fd[0]);



//writes data to pipe



close(fd[1]);

非常感谢您的帮助!

最佳答案

您需要在 fork 之前创建管道!

这里是代码+修复应该如何完成C pipe, fork, dup, and exec()

关于c - 从管道 C 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22244812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com