C 在这段代码中我应该在哪里使用wait和waitpid?

C 在这段代码中我应该在哪里使用wait和waitpid?,c,fork,pipe,execvp,C,Fork,Pipe,Execvp,我编写了一个程序,它使用管道模拟类似于“$ls-l | wc-c”的命令。 现在我无法找出在这段代码中应该在哪里使用wait或waitpid 我应该在哪里关闭管道? 请查看我的代码并提出建议 int main ( int argc , char *argv[] ) { char *cmd_one_tokens[MAX]; /* Array of pointer to hold tokens of first command */ char *cmd_two_tokens[MAX]; /

我编写了一个程序,它使用管道模拟类似于“$ls-l | wc-c”的命令。 现在我无法找出在这段代码中应该在哪里使用wait或waitpid

我应该在哪里关闭管道? 请查看我的代码并提出建议

int main (   int argc , char *argv[] )
{

char *cmd_one_tokens[MAX];  /* Array of pointer to hold tokens of first command */
char *cmd_two_tokens[MAX];  /* Array of pointer to hold tokens of second command */

pid_t pid_one = -1 , /* variable to hold process id of first sub process */
      pid_two = -1 ; /* variable to hold process id of second sub process */
int status    = -1 ; /* variable to read the status of the child process */
int pipe_fd[2] = {-1,-1}; /* Array to hold descriptors returned by pipe sys call */
int ret_val    =-1 ; /* variable to hold return values by system calls */

/*Validate Number of command line arguments */
if(3 != argc)
{
    printf("Provide Appropriate Arguments  \n <lab3> < \"arg1\" > < \"arg2\" > \n");
    _exit(FAILURE);

}
/*Parse first command and get the tokens */
parse_command(argv[1],cmd_one_tokens);
/*Parse second command and get the tokens */
parse_command(argv[2],cmd_two_tokens);


/* 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() ;
//Error check
if( 0 == pid_one ) /*child process block */
{

    /* 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 */
    //Error check 

}
else /*main process block */
{

    /*Wait for first sub process to finish */
    waitpid ( pid_two , &status ,0); // <-------changes

    /*Fork second subprocess */
    pid_two = fork();
            //Error check
    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 pipe write end */
        close(pipe_fd[1]);

        execvp(cmd_two_tokens[0] , cmd_two_tokens); 
        /* if execvp returns then if must have failed */
        //Error check 

    }
    else        /*main process block */
    {
        status=-1; /*reset status */

        /*Waiting for the second sub process to finish in No hang fashion */
             waitpid ( pid_two , &status ,0);  // <-------changes

    }
}
return(SUCCESS);        

}/*End of main */
intmain(intargc,char*argv[])
{
char*cmd_one_令牌[MAX];/*指针数组,用于保存第一个命令的令牌*/
char*cmd_two_tokens[MAX];/*用于保存第二个命令的标记的指针数组*/
pid_t pid_one=-1,/*变量用于保存第一个子进程的进程id*/
pid_two=-1;/*变量,用于保存第二个子进程的进程id*/
int status=-1;/*变量,用于读取子进程的状态*/
int pipe_fd[2]={-1,-1};/*数组,用于保存管道系统调用返回的描述符*/
int ret_val=-1;/*变量,用于通过系统调用保存返回值*/
/*验证命令行参数的数量*/
如果(3!=argc)
{
printf(“提供适当的参数\n<\“arg1\”><\“arg2\”>\n”);
_退出(失败);
}
/*解析第一个命令并获取令牌*/
parse_命令(argv[1],cmd_one_令牌);
/*解析第二个命令并获取令牌*/
parse_命令(argv[2],cmd_两个_标记);
/*创建管道*/
ret_val=管道(管道fd);
/*错误检查*/
如果(错误==返回值)
{
perror(“管道创建错误\n”);
_退出(失败);
}
/*第一个孩子*/
pid_one=fork();
//错误检查
if(0==pid_one)/*子进程块*/
{
/*将stdout重定向到子进程1的管道写入端*/
dup2(管道fd[1],1);
/*关闭管道读取端*/
关闭(管道_fd[0]);
execvp(cmd_one_令牌[0],cmd_one_令牌);
/*如果execvp返回,则if必须失败*/
//错误检查
}
else/*主进程块*/
{
/*等待第一个子进程完成*/

waitpid(pid_-two,&status,0);//您可以在父进程中放置while循环(在生成子进程之后)连续调用
wait
,直到两个子进程终止:

while (wait(&status) != -1);

在您的特定情况下,您可以避免
waitpid

您可以在父进程中放置while循环(子进程生成后)连续调用
wait
,直到两个子进程终止:

while (wait(&status) != -1);

在您的特定情况下,您可以避免
waitpid

我真希望您已经从
fork(2)获取并检查了返回值
在再次发布之前查看错误。是的,我已经这样做了。我尝试正确地放置等待,有时它工作正常,有时它会在ps上显示管道。我真希望您已经从
fork(2)获取并检查了返回值
在再次发布之前查看错误。是的,我已经这样做了。我尝试正确地放置等待,有时它工作正常,有时它在ps上显示pipe_w…我使用等待两个子进程,但它挂起。当我看到它使用ps时,它显示pipe_w……。挂起是
等待
的作用:-)你能更具体一点吗?也许是一些c孩子们不会终止吗?好的,现在我要编辑代码并向你展示我到底写了什么…….这个答案是一个很好的提示!对我来说也很有用。我使用了等待两个子进程,但它挂起。当我看到它使用ps时,它给出了管道w…….挂起是
等待
的作用:-)你能更具体一点吗?也许有些是t那么孩子们不会终止吗?好的,现在我要编辑代码并向您展示我到底写了什么…………这个答案是一个很好的提示!对我也很有用。