同时使用execvp输入重定向问题? [英] Input redirection problem while using execvp?
问题描述
我实现了一个简单的程序,它模拟 $ ls -l命令| WC -c
使用简单的管道和execvp调用命令的执行。
现在重定向stdin和stdout后,当执行程序,shell提示符消失,它等待进入关键是pressed。
解决这个问题的任何方式。 PLZ批评我的code也..
感谢
/ *创建管道* /
RET_VAL =管(pipe_fd);
/ *错误检查* /
如果(ERROR == RET_VAL)
{
PERROR(管子·创建错误。\\ n);
_exit(失败);
}/ *叉第一个孩子* /
pid_one =叉();如果(0 == pid_one)/ *子进程块* /
{
/ *第一子进程* /
/ *的printf(第一子流程为%d \\ n,GETPID()); * / / *重定向到标准输出子过程中的一个管道的写端* /
dup2(pipe_fd [1],1);
/ *关闭管道读端* /
关闭(pipe_fd [0]); execvp(cmd_one_tokens [0],cmd_one_tokens); / *如果execvp回报那么如果一定失败* /
的printf(未知的命令\\ n);
//出口(10);
}
否则/ *主过程块* /
{
/ *的printf(主要工艺为%d \\ n,GETPID()); * / / *等待第一子进程结束* /
//等待(安培;状态); / *的printf(第一个孩子的退出状态为%d \\ n,WEXITSTATUS(状态)); * / / *叉第二子* /
pid_two =叉(); 如果(0 == pid_two)/ *第二个孩子过程块* /
{
/ *标准输入重定向到管道的子进程读取端有两个* /
dup2(pipe_fd [0],0); //关闭(0); / *接近正常标准输入* /
// DUP(pipe_fd [0]); / *使stdib相同工艺流程图[0] * /
/ *关闭管道写端* /
关闭(pipe_fd [1]); / *第二子进程* /
/ *的printf(二子流程为%d \\ n,GETPID()); * / execvp(cmd_two_tokens [0],cmd_two_tokens);
/ *如果execvp回报那么如果一定失败* /
的printf(未知的命令\\ n); }
否则/ *主过程块* /
{
/ *的printf(主要工艺为%d \\ n,GETPID()); * /
状态= -1; / *复位状态* / / *等待第二子过程没有挂方式*完成/
waitpid函数(-1,&安培;状态,WNOHANG);
/ *的printf(第二个孩子的退出状态为%d \\ n,WEXITSTATUS(状态)); * / }
}
您必须关闭管道文件描述符的主要过程,第二个分叉后。直到关闭它们,子进程(厕所
)将等待上主进程仍有打开管道输入。你必须非常小心,关闭管道的所有不必要的结束。
I have Implemented a simple program which simulates $ls -l | wc -c
command execution using simple pipes and execvp calls.
Now After redirecting stdin and stdout ,when executes the program , shell prompt disappears and it waits for the enter key to be pressed.
Any way of solving this issue . Plz criticize my code also..
Thanks
/* Create pipe */
ret_val=pipe(pipe_fd);
/*Error check */
if(ERROR==ret_val)
{
perror("Pipe creation error \n");
_exit(FAILURE);
}
/*Fork First Child */
pid_one = fork() ;
if( 0 == pid_one ) /*child process block */
{
/* First sub process */
/*printf("First sub process is %d \n",getpid());*/
/* redirect stdout to pipe's write end for sub process one*/
dup2(pipe_fd[1],1);
/*close pipe read end */
close(pipe_fd[0]);
execvp(cmd_one_tokens[0],cmd_one_tokens);
/* if execvp returns then if must have failed */
printf("Unknown Command \n ");
//exit(10);
}
else /*main process block */
{
/*printf(" Main process is %d \n",getpid());*/
/*Wait for first sub process to finish */
//wait(&status);
/*printf("Exit status of first child is %d \n ", WEXITSTATUS(status) );*/
/*Fork second subprocess */
pid_two = fork();
if( 0 == pid_two ) /*second child process block */
{
/* redirect stdin to pipe's read end for sub process two */
dup2(pipe_fd[0],0);
// close(0); /* close normal stdin */
// dup(pipe_fd[0]); /* make stdib same as pfds[0] */
/*close pipe write end */
close(pipe_fd[1]);
/* Second sub process */
/*printf("Second sub process is %d \n",getpid()); */
execvp(cmd_two_tokens[0] , cmd_two_tokens);
/* if execvp returns then if must have failed */
printf("Unknown Command \n ");
}
else /*main process block */
{
/* printf(" Main process is %d \n",getpid()); */
status=-1; /*reset status */
/*Waiting for the second sub process to finish in No hang fashion */
waitpid ( -1 , &status ,WNOHANG);
/*printf("Exit status of second child is %d \n ", WEXITSTATUS(status) ); */
}
}
You have to close the pipe file descriptors in the main process, after the second is forked. Until you have closed them, the child process (wc
) will wait for input on the pipe which the main process still has open. You must be very careful to close all the unneeded ends of the pipe.
这篇关于同时使用execvp输入重定向问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!