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

发布于 2024-10-31 13:21:29 字数 2486 浏览 4 评论 0原文

我编写了一个模拟 '$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 */

I have written a program which simulates '$ls -l | wc -c ' like commands using pipes.
Now I am not able to find out where should I use wait or waitpid in this code.

Also where should I close pipes ?
Please review my code and suggest .

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-11-07 13:21:29

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

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

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

You can place a while loop in the parent process (after the children are spawned) calling wait continuously until both children terminate:

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

In your particular situation you can avoid waitpid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文