【发布时间】:2014-03-07 08:11:39
【问题描述】:
我需要创建一个新进程来运行 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]);
我们将不胜感激!
【问题讨论】:
-
如果您提供最少的可编译代码,您可以获得更多工程师的帮助。
-
为什么在调用
execlp之前从管道中读取?这将导致被执行的程序跳过管道中的前 10 个字节。