Thread: Implementing pipe in my own unix shell

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    Implementing pipe in my own unix shell

    Hi,

    I am trying to implement pipes in my own unix shell. Below is the code.

    Code:
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include "scanner.h"
    
    
    int main(int argc, char **argv)
    {
        
        int pipefd[2];
        int pid;
        printf("\n[myShell--%s]$", getenv("PWD")); 
    
        while(1)
        {
            char *cat_args[] = {"cat", "f1", NULL};
            char *wc_args[] = {"wc", "-l", NULL};
            int pid1, status;
         // make a pipe (fds go in pipefd[0] and pipefd[1])
    
            pipe(pipefd);
            pid1 = fork();
            if(pid1 == 0)
            {
    
                pid = fork();
    
                if (pid == 0)
                {
                    // replace standard input with input part of pipe
                    dup2(pipefd[0], 0);
                    // close unused hald of pipe
                    close(pipefd[1]);
                    // execute wc
                    execvp("wc", wc_args);
                }
                else
                {
                    // replace standard output with output part of pipe
                    dup2(pipefd[1], 1);
                    // close unused unput half of pipe
                    close(pipefd[0]);
                      // execute cat
                    execvp("cat", cat_args);
                }
            }
            else
            {
                wait(NULL);
                printf("[myShell--%s]$", getenv("PWD"));
            }
        }
    }
    I want to execute the command and go back to my own prompt. But instead it goes on printing "[myShell]. If I remove one parent fork, then it execute fine, but exits my shell.


    Thanks in advance !!
    Last edited by prashant8809; 09-08-2012 at 01:27 PM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    With only one fork(), the parent process image is replaced (cf man execv) with that of "wc", so of course your loop doesn't exist anymore;
    "wc" terminates and its child ("cat" in your case) is terminated.

    The inner part of your program (piping cat and wc) is fine, but you need to rework the logic around that.
    You outer fork() is just looping on the creation of child processes printing "[myShell..." and serve no purpose.
    Try to implement a function that wrap execv() in order to execute your composite shell command without disturbing your main process looping on a (so far non-existing!) prompt.

    PS: also, check system calls returns for errors.
    Last edited by root4; 09-08-2012 at 03:34 PM.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Thanks for the reply. I understood what I need to do regarding pipes.

    Another quick question. I am creating a unix like shell and need to implement pipes. Till now I have used array of char pointers as my data structure to store and execute commands. Now I need to store the commands(more than one, which was not the case before) and the arguments(of more than one command) that are separated by "|". So do I need to use linked list and change the whole code or is there any other way out ??

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You can continue to use an array, just set an upper limit. To get it for your actual real terminal as an example try:

    Code:
    getconf ARG_MAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementing 3 stage pipe in C
    By cylus99 in forum C Programming
    Replies: 3
    Last Post: 02-22-2012, 01:48 PM
  2. Unix pipe simulation
    By TaviO! in forum C Programming
    Replies: 4
    Last Post: 03-19-2008, 12:47 AM
  3. Implementing ! functionality for Korn shell
    By Yasir_Malik in forum Linux Programming
    Replies: 1
    Last Post: 06-14-2004, 08:24 PM
  4. Implementing ! functionality for Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 1
    Last Post: 06-13-2004, 05:27 PM
  5. unix shell
    By duffy in forum Tech Board
    Replies: 3
    Last Post: 10-16-2002, 03:11 AM