4

I'm trying to pass list of strings through pipe to child process where it should display through /bin/cat using execl(). I had it working earlier except that the pipe did not close so the program kept waiting. Don't know what I did and now it is not working at all. Can someone see my code and tell me what am I doing wrong that the str data is not being displayed by cat in child process?

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

    char *str[] = {"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"};
    int fds[TOTAL_CHILDREN];
    int writeFds;
    int catPID;
    int status;

    FILE * write_to_child;

    //create pipe
    if (pipe(fds) == -1) {
        perror("creating pipe: failed");
        exit(EXIT_FAILURE);
    }
    pipe(fds);
    //create subprocess for cat child

    switch (catPID) {
        case 0: // successful creation of child
            close(fds[1]); //close write side from parents
            close(0); //close stdin
            dup(fds[0]); //connect pipe from execl cat to stdin

            execl("/bin/cat", "cat", (char *) 0);
            perror("exec failed!");
            exit(20);
            break;

        case -1: //failure
            perror("fork failed: cat process");
            exit(EXIT_FAILURE);

        default: //parent process
            close(fds[0]);

            writeFds = fds[1];
            write_to_child = fdopen(fds[1], "w");

            if (write_to_child == NULL) {
                perror("write to pipe failed");
                exit(EXIT_FAILURE);
            }
            break;


    }

    int i;
    for (i = 0; i < 9; i++){
        fprintf(write_to_child, "%s\n", str[i]);
    }

    fclose(write_to_child);
    close(writeFds);

    wait(&status);

    return (EXIT_SUCCESS);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
codeBarer
  • 2,238
  • 7
  • 44
  • 75
  • see this link http://stackoverflow.com/questions/9405985/linux-3-0-executing-child-process-with-piped-stdin-stdout – qwr May 12 '13 at 17:28
  • 2
    If you're going to ask a question about `fork()`, you should probably call `fork()` in the program. – Barmar May 12 '13 at 17:37
  • Tnanks, that is true! – codeBarer May 12 '13 at 17:45
  • 1
    _I had it working earlier ... Don't know what I did and now it is not working at all_ — This is why you should use a VCS (version control system) to manage code. When something is working well enough, you can save a version so that you have a record you can go back to if you mess up again later. Even on toy programs for classes, there can be advantages to using a VCS during development. (I keep quite a lot of my SO answers in a `git` repository, and if I need to work on something tricky from a question, I create a branch, copy and save the original code, and then start work on my answer.) – Jonathan Leffler May 12 '13 at 18:02
  • See also [C system calls `pipe()`, `fork()` and `exec()`](http://stackoverflow.com/questions/16500247/c-system-calls-pipe-fork-and-execl) — same OP. Also quite similar to [How to loop through stdin pipe output to a child — `execl()` command in C](http://stackoverflow.com/questions/16502255/how-to-loop-through-stdin-pipe-output-to-a-child-execl-command-in-c) — different OP. – Jonathan Leffler May 12 '13 at 18:07
  • @user2250263 see this link http://stackoverflow.com/posts/16497153/edit . it is similar question that was given yesterday but i answered shortly . now i posted full answer . it can be helpful to you too – qwr May 12 '13 at 18:34

1 Answers1

3

You probably want to add the line

catPID = fork();

and I'm not sure why you've got pipe(fds) twice

parkydr
  • 7,596
  • 3
  • 32
  • 42
  • you debug like a ninja! Thanks so much. I don't know what I was doing but I deleted the fork and duplicated the pipe. Would you happen to know of any resources that I can us to reference these system calls? Now that you have fixed my problem I' want to read all values from cat to sort. I guess I create pipe from first child to another fork()'d child? – codeBarer May 12 '13 at 17:42
  • 1
    man is always a good command for looking up system calls, which are in section 2. You could do another pipe, but why not just replace cat with sort? cat is only copying stdin to stdout. – parkydr May 12 '13 at 17:48
  • That is what I did repaced cat with sort but after I get the sorted value I have to remove duplicate words. – codeBarer May 12 '13 at 17:54
  • sort -u will remove duplicates – parkydr May 12 '13 at 17:56