简体   繁体   English

如何写到管叉?

[英]How to write to the pipe fork?

I have the following function which execute command via fork and execvp. 我有以下功能,通过fork和execvp执行命令。 my script that I launch in fork is listening for input data. 我在fork中启动的脚本正在侦听输入数据。 How I can send data to myscript? 如何将数据发送到myscript?

int external_command()
{

    int pfds[2];
    if (pipe(pfds) < 0)
        return -1;

    if ((uproc.pid = fork()) == -1)
        return -1;

    if (uproc.pid == 0) {
        /* child */

        const char *argv[4];
        int i = 0;
        argv[i++] = "/bin/sh";
        argv[i++] = "myscript.sh";
        argv[i++] = NULL;

        close(pfds[0]);
        dup2(pfds[1], 1);
        close(pfds[1]);

        execvp(argv[0], (char **) argv);
        exit(ESRCH);

    } else if (uproc.pid < 0)
        return -1;

    /* parent */
    close(pfds[1]);

    int status;
    while (wait(&status) != uproc.pid) {
        DD("waiting for child to exit");
    }

    char buffer[64];
    ssize_t rxed;
    char *c;
    int t;

    //read from fork pipe
    *value = NULL;
    while ((rxed = read(pfds[0], buffer, sizeof(buffer))) > 0) {
        if (*value)
            t = asprintf(&c, "%s%.*s", *value, (int) rxed, buffer);
        else
            t = asprintf(&c, "%.*s", (int) rxed, buffer);

        if (t == -1) return -1;

        free(*value);
        *value = strdup(c);
        free(c);
    }
    // how to write to the pipe fork?
}

A pipe is unidirectional. 管道是单向的。 Call pipe twice, to get one pair of file descriptors for reading data from the child (as you already have) and another to sent data to the child (which will be new). 调用两次pipe ,以获取一对文件描述符(用于从子级读取数据)(如您所拥有的),以及另一对文件描述符,以将数据发送至子级(这将是新的)。 You'll need to do the same sort of dup2 and close magic as you're already doing, in order to set up that new pipe as stdin for the child. 您需要像已经做的一样执行相同的dup2close魔术,以便将该新管道设置为孩子的stdin。

I guess you mean that you want to write to the child process stdin from the parent process? 我猜你是说要从父进程写入子进程stdin For that you need to create two pipes. 为此,您需要创建两个管道。 One that is used as stdout in the child, like you have now, and the other have to be used for stdin is much the same fashion as you do now (but with the indexes reversed of course). 像您现在一样,其中一个用作孩子的stdout ,另一个必须用作stdin ,与您现在使用的方式大致相同(但索引当然会颠倒)。

Of course, you can't wait for the child before you write to it, as then you might have a deadlock if the child needs input to continue. 当然,您不能wait孩子之后再写它,因为如果孩子需要输入才能继续,则可能会陷入僵局。

basics of a pipe 管道基础

please study this bit of code so you learn what you are doing 请研究这段代码,以便您了解自己在做什么

int fd[2];  /*write(fd[1],buffer,strlen)
            / read(fd[0],buffer2,SIZE)*/
pid_t cpid;

if(pipe(fd)==-1){
    perror("pipe");
    exit(EXIT_FAILURE);
}
if((cpid=fork())<0){/*  FORK INIT  */
    printf("\n\tFORK ERROR\n");
    exit(1);
}
if(cpid==0){            /*SON*/
    close(fd[0]);
    /********CODE******/
    if((write(fd[1],final2,strlen(final2)))<0){
        perror("\n\tWRITE ERROR");
    }
    close(fd[1]);
}else{                  /*FATHER*/
    close(fd[1]);
    if((read(fd[0],aler,NN))<0){
        perror("\n\tREAD ERROR");   
    }
    wait(NULL);
    /********CODE******/
    close(fd[0]);
}

in this case the parent process(father) read() information from the new(son) process 在这种情况下,父进程(父亲)从new(son)进程中读取()信息

if you want BI-directional read() write() create 2 pipes(fd1[2],fd2[2]) 如果您想进行BI方向的 read()write()创建2个管道(fd1 [2],fd2 [2])

use the same logic as above, father reads, close the write end fd1[1], son writes, close the read end fd1[0] 使用与上述相同的逻辑,父亲读,关闭写端fd1 [1],儿子写,关闭读端fd1 [0]

vice-versa to the other direction 反之亦然

father writes, close the read end fd2[0], son reads, close the write end fd2[1] 父亲写,关闭读端fd2 [0],儿子读,关闭写端fd2 [1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM