dup2()和exec() [英] dup2() and exec()

查看:111
本文介绍了dup2()和exec()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int
main( int argc, char **argv)
{
    int pfds[ 2], i;
    size_t pbytrd;
    pid_t childpid;
    char buffer[ 200];

    pipe( pfds);
    if( ( childpid = fork() ) == -1)
    {
            perror( "fork error: ");
            exit( 1);
    }
    else if( childpid == 0)
    {
            close( pfds[ 0]);
            dup2( pfds[1], 1);
            close( pfds[ 1]);
            for( i = 0; i < 10; i++)
               printf("Hello...");
            execlp( "xterm","xterm","-e","./sample_a", (char *) 0);
            exit( 0);
}
else
{

        close( pfds[ 1]);
        for( ; ; )
        {
            if( ( pbytrd = read( pfds[ 0], buffer, sizeof( buffer) ) ) == -1)
            {
                perror(" read error: ");
                exit( 1);
            }
            else if( pbytrd == 0)
            {

                write( fileno( stdout), "Cannot read from pipe...\n", strlen( "Cannot read from pipe...\n") );
                exit( 0);
            }
            else
            {
                write( fileno( stdout), "MESSAGE: ", strlen( "MESSAGE: ") );
                write( fileno( stdout), buffer, pbytrd);
            }
        }       
}   
return( 0);

}

我的sample_a.c代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int
main( int argc, char **argv)
{
        int i;
        for( i= 0; i< 10; i++)
        write( fileno( stdout), "Hello...", strlen( "Hello...") );
    return( 0);
 }

在上面的代码中,我真正想做的是: 1)将子进程的输出重定向到管道,并从管道中读取父进程,然后将其打印输出到stdout.

我能够将子进程输出("printf")从stdout重定向到管道,但是我不能将execlp的子进程xterm重定向到管道.

有人可以帮我吗?

解决方案

xterm是终端仿真器.它执行您提供的程序(sample_a),将其输入和输出连接到其自身,以便该程序在其标准输入中接收用户输入,然后将发送到其标准和错误输出的任何内容打印给用户. /p>

您的程序正在做的是将xterm输出连接到管道,并尝试读取该管道.但是xterm是GUI程序,它通常不会将数据写入其输出.也许不清楚您到底想达到什么目的.

如果删除xterm部分,它将按预期工作,也就是说,父进程将看到sample_a写入输出的内容.

        execlp("./sample_a", "./sample_a", (char *) 0);

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int
main( int argc, char **argv)
{
    int pfds[ 2], i;
    size_t pbytrd;
    pid_t childpid;
    char buffer[ 200];

    pipe( pfds);
    if( ( childpid = fork() ) == -1)
    {
            perror( "fork error: ");
            exit( 1);
    }
    else if( childpid == 0)
    {
            close( pfds[ 0]);
            dup2( pfds[1], 1);
            close( pfds[ 1]);
            for( i = 0; i < 10; i++)
               printf("Hello...");
            execlp( "xterm","xterm","-e","./sample_a", (char *) 0);
            exit( 0);
}
else
{

        close( pfds[ 1]);
        for( ; ; )
        {
            if( ( pbytrd = read( pfds[ 0], buffer, sizeof( buffer) ) ) == -1)
            {
                perror(" read error: ");
                exit( 1);
            }
            else if( pbytrd == 0)
            {

                write( fileno( stdout), "Cannot read from pipe...\n", strlen( "Cannot read from pipe...\n") );
                exit( 0);
            }
            else
            {
                write( fileno( stdout), "MESSAGE: ", strlen( "MESSAGE: ") );
                write( fileno( stdout), buffer, pbytrd);
            }
        }       
}   
return( 0);

}

My sample_a.c code is below:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int
main( int argc, char **argv)
{
        int i;
        for( i= 0; i< 10; i++)
        write( fileno( stdout), "Hello...", strlen( "Hello...") );
    return( 0);
 }

In the above code, what I really want to do is: 1) Redirect output from the child process to pipe and have parent read from the pipe and print it out to stdout.

I am able to redirect child process output( "printf") from stdout to the pipe but I am not able to redirect execlp's child process, xterm, output to the pipe.

Can anybody help me this?

解决方案

xterm is a terminal emulator. It executes the program you provided (sample_a), connecting its input and outputs to itself, so that the program receives the user input in its standard input, and prints to the user whatever the it sends to its standard and error outputs.

What your program is doing is to connect the xterm output to the pipe, and trying to read that. But xterm is a GUI program, it does not normally write data to its outputs. Perhaps it is not really clear what exactly you are trying to achieve.

If you remove the xterm part, it should work as you expect, that is, the parent process will see what sample_a writes to the output.

        execlp("./sample_a", "./sample_a", (char *) 0);

这篇关于dup2()和exec()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆