管道execvp linux命令从孩子到父进程
我尝试用C语言编写一个程序,该程序在输入中采用简单的Linux命令,例如“ LS /Home”。 此命令在子进程中使用“ ExecVP”执行,并将其发送到要在屏幕上显示的父进程。
我的问题是程序正在运行,没有显示任何内容,并且不要回馈提示。我必须用“ ctrl + c”退出。
问题在时循环还是我的管道无处可发送我的命令?
感谢您的反馈
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
int fd[2];
int pid;
pipe(fd);
if((pid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(pid == 0)
{
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
execvp(argv[1], &argv[1]);
}
else
{
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
char result;
while(read(fd[0], &result, sizeof(result) > 0))
{
write(fd[1], &result, sizeof(result));
}
wait(NULL);
}
close(fd[1]);
close(fd[0]);
return(0);
}
I try to write a program in C language that takes a simple Linux command in input like "ls /home".
This command is executed in child process with "execvp" and sent to the parent process to be displayed on the screen..
My problem is that the program is running and display nothing and don't give back the prompt. I have to quit with "ctrl + C".
Is the problem in the while loop or my pipe send my command nowhere?
Thanks for your feedback
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
int fd[2];
int pid;
pipe(fd);
if((pid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(pid == 0)
{
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
execvp(argv[1], &argv[1]);
}
else
{
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
char result;
while(read(fd[0], &result, sizeof(result) > 0))
{
write(fd[1], &result, sizeof(result));
}
wait(NULL);
}
close(fd[1]);
close(fd[0]);
return(0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在父母中,读取循环有问题。 前者相比
与
,在您的程序中,使用了比较
sizeof(result)&gt的结果。 0
作为缓冲区的 size ,并且只要读取
返回非零值。请注意,
结果
是char
,sizeof(char)
is 保证 be1
。1&gt; 0
是1
,恰好是缓冲区的大小,因此部分工作。循环将继续发生错误(-1
),并将继续尝试编写输出。它还试图从
fd [0]
中读取,该>在不需要此重复后关闭。
另外,
正在尝试将输出写回管道。也就是说,父母较早关闭了此文件描述符(
关闭(FD [1]);
)。请注意
关闭(FD [1]);
else> else 块是多余的(冗余关闭可以是更复杂的程序中的错误来源)。在儿童中,您应该处理
execvp
失败的事件,因为如果功能成功,它将永远不会返回。execvp
在失败状态下运行的任何预期运行(在您的程序中,即Else Block之后的行)。粗略的示例:
In the parent, the read loop has problems. Compare
to
The former, in your program, is using the result of the comparison
sizeof(result) > 0
as the size of the buffer, and looping as long asread
returns a nonzero value.Note that
result
is achar
, andsizeof (char)
is guaranteed to be1
.1 > 0
is1
, which just happens to be the size of your buffer, so this partially works. The loop will continue on error (-1
) though, and will continue to attempt writing output.It is also trying to read from
fd[0]
, which was closed afterThis duplication is not needed.
Additionally,
is attempting to write the output back into the pipe. That said, the parent closed this file descriptor earlier (
close(fd[1]);
).Note the
close(fd[1]);
outside of theelse
block is redundant (and redundant closes can be a source of bugs in more complex programs).In the child, you should handle the event that
execvp
fails, because if the function succeeds, it never returns. Anything expected to run afterexecvp
is operating in a failure state (in your program, that is the lines after the else block).A cursory example: