Frage

Ich habe ein Programm vorbereitet, die emuliert Schale (cmd) Schnittstellenleitungen verwendet wird. Es gibt zwei Versionen des Programms: 1. Unter Verwendung einer Rohrleitung (ein Rohr von Eltern auf Kinder-Kommunikation verwendet wird) 2. Verwendung von Doppelrohr (unter Verwendung von zwei Rohren von Eltern auf Kinder und Eltern von Kind zu kommunizieren).

So, das erste Programm bietet gewünschte Schnittstelle und arbeitet, wie ich will, aber ich kann das gleiche Ergebnis (Schnittstelle) im zweiten Programm (mit dup2 () und ähnlichem) nicht erreichen.

Also, ich auf Ihre Hilfe Relais und legen Sie die beiden Codes unten.

B. S .: Sie können kompilieren und versuchen, beiden Programme mit der gleichen Art und Weise, diese Befehle:

gcc $ prog1.c -o prog1

Als nächstes Lassen Sie uns laufen:

$ ./prog1

Als nächstes wollten sie laufen neuen Terminal und versuchen, einige Daten zu input.txt schreiben:

$ echo pwd> input.txt

Und dann das Ergebnis in dem ersten Anschluss sehen.

(Dies funktioniert gut für das erste Programm, aber ich brauche diese Arbeit wit die gleiche Schnittstelle im zweiten Programm zu erhalten)

CODE des ersten Programms (WORKING FINE):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>

void do_child(int data_pipe[]) {
    int c;
    int rc;
    close(data_pipe[1]);

    dup2(data_pipe[0], 0); /* This string provides the desired interface of the program */

    char* cmd[] = { "bash", (char *)0 };
    execvp("bash", cmd);

    while ((rc = read(data_pipe[0], &c, 1)) > 0) 
    {
        putchar(c);
    }
    exit(0);
}

void do_parent(int data_pipe[])
{
    int c;
    int rc;
    FILE *in;

    close(data_pipe[0]);

    while (1)
    {
        in = fopen("input.txt", "r");
        while ((c = fgetc(in)) > 0) 
        {
            rc = write(data_pipe[1], &c, 1);
            if (rc == -1) 
            {
                perror("Parent: write");
                close(data_pipe[1]);
                exit(1);
            }
        }
        fclose(in);
    }
    close(data_pipe[1]);
    exit(0);
}

int main(int argc, char* argv[])
{
    int data_pipe[2];
    int pid;
    int rc;

    umask(0);
    mknod("input.txt", S_IFIFO|0666, 0);

    rc = pipe(data_pipe);
    if (rc == -1) 
    {
        perror("pipe");
        exit(1);
    }
    pid = fork();
    switch (pid) 
    {
    case -1:
        perror("fork");
        exit(1);
    case 0:
        do_child(data_pipe);
    default:
        do_parent(data_pipe);
    }
    return 0;
}

CODE des zweiten Programms (NEED KORRIGIERT EIN WENIG ZU):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>

/* Original version got from http://www.iakovlev.org */

int parent_to_child[2];
int child_to_parent[2];

void do_parent()
{
    int c;
    char ch;
    int rc;
    FILE *in;

    close(child_to_parent[1]); /* we don't need to write to this pipe.  */
    close(parent_to_child[0]); /* we don't need to read from this pipe. */

    while (1)
    {
        in = fopen("input.txt", "r");
        while ((c = fgetc(in)) > 0) {
            ch = (char)c;
            /* write to child */
            rc = write(parent_to_child[1], &ch, 1);
            if (rc == -1) {
                perror("child: write");
                close(child_to_parent[0]);
                close(parent_to_child[1]);
                exit(1);
            }
            /* read back from child */
            rc = read(child_to_parent[0], &ch, 1);
            c = (int)ch;
            if (rc <= 0) {
                perror("parent: read");
                close(child_to_parent[0]);
                close(parent_to_child[1]);
                exit(1);
            }
            putchar(c);
        }
        fclose(in);
    }
    close(child_to_parent[0]);
    close(parent_to_child[1]);
    exit(0);
}

void do_child()
{
    int c;
    char ch;
    int rc;

    close(parent_to_child[1]); /* we don't need to write to this pipe.  */
    close(child_to_parent[0]); /* we don't need to read from this pipe. */

    //dup2(parent_to_child[0], STDIN_FILENO);
    //dup2(child_to_parent[1], STDOUT_FILENO);

    /* Some dup2() routines must be added here 
    to get this working as the first program above */

    char* cmd[] = { "bash", (char *)0 };
    execvp("bash", cmd);

    while (read(parent_to_child[0], &ch, 1) > 0) {
        c = (int)ch;
        ch = (char)c;
        putchar(ch);
        rc = write(child_to_parent[1], &ch, 1);
        if (rc == -1) {
            perror("child: write");
            close(parent_to_child[0]);
            close(child_to_parent[1]);
            exit(1);
        }
    }
    close(parent_to_child[0]);
    close(child_to_parent[1]);
    exit(0);
}

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

    umask(0);
    mknod("input.txt", S_IFIFO|0666, 0);    

    rc = pipe(parent_to_child);
    if (rc == -1) {
        perror("main: pipe parent_to_child");
        exit(1);
    }

    rc = pipe(child_to_parent);
    if (rc == -1) {
        perror("main: pipe child_to_parent");
        exit(1);
    }

    pid = fork();
    switch (pid) {
    case -1:
        perror("main: fork");
        exit(1);
    case 0:
        do_child();
    default:
        do_parent();
    }
    return 0;
}
War es hilfreich?

Lösung

Der große Unterschied ist hier:

    while ((c = fgetc(in)) > 0) {
        ch = (char)c;
        /* write to child */
        rc = write(parent_to_child[1], &ch, 1);
        /* .... */
        /* read back from child */
        rc = read(child_to_parent[0], &ch, 1);
        /* .... */
        putchar(c);
    }

Wie ich faul bin zu kompilieren / Test für Sie, würde ich einfach spekulieren, dass die Eltern in der Lese blockiert ist (). Da andere Seite (bash in Kindprozess) nicht jedes geschriebenen Zeichen zurück Echo garantiert. Oder es könnte auch entscheiden, mehr als ein Zeichen zu drucken, was Ihr Code ist nicht in der Lage Handhabung.

Im Fall, dass Sie Umfrage haben (), um zu sehen, ob es etwas gibt, oder nicht zu lesen. Oder stellen Sie die O_NONBLOCK Flagge auf der child_to_parent [0] mit fcntl (F_SETFL) und wenn errno == EAGAIN, einfach überspringen Sie die read () Zweig. Und Schleife, solange es noch Zeichen zu lesen.

.

Edit1 BTW total verpasst ich das Teil: Sie in do_parent () Schleife zur Verwendung Umfrage haben () auf der sowohl child_to_parent[0] und in, da andere Seite etwas schreiben könnte (read () wouldn‘ t-Block), auch wenn Sie dies nicht tun write () ein beliebiges Zeichen zu.

Andere Tipps

Dank dir scheint es, ich habe es funktioniert.

So, hier wird aktualisiert Code von do_parent:

void do_parent()
{
    int c;
    char ch;
    int rc;
    FILE *in;

    struct pollfd fds[2];
    int pol_ret;

    fds[0].fd = child_to_parent[0];

    close(child_to_parent[1]); /* we don't need to write to this pipe.  */
    close(parent_to_child[0]); /* we don't need to read from this pipe. */

    while (1)
    {   
        in = fopen("input.txt", "r");
        fds[1].fd = fileno(in);
        pol_ret = poll(fds, 2, 500);

        while ((c = fgetc(in)) > 0) {
            ch = (char)c;
            /* write to child */
            rc = write(parent_to_child[1], &ch, 1);
            if (rc == -1) {
                perror("child: write");
                close(child_to_parent[0]);
                close(parent_to_child[1]);
                exit(1);
            }
            /* read back from child */
            if (fds[0].revents & POLLIN)
            {
                rc = read(child_to_parent[0], &ch, 1);
                c = (int)ch;
                if (rc <= 0) {
                    perror("parent: read");
                    close(child_to_parent[0]);
                    close(parent_to_child[1]);
                    exit(1);
                }
                putchar(c);
            }
        }
        fclose(in);
    }
    close(child_to_parent[0]);
    close(parent_to_child[1]);
    exit(0);
}

Auch habe ich habe dieses Bild in do_child ():

dup2(parent_to_child[0], STDIN_FILENO);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top