Thread: Implementing 3 stage pipe in C

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Implementing 3 stage pipe in C

    Hi, I'm trying to implement the 3 stage pipe 'who | cut -d" " -f 1 | sort -u' as a C program (basically i want returned a list of users logged into the system but i want to use pipes.

    I've worked out how to implement a two stage pipe (i think but it may be wrong)...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    
    int main(void)
    {
        int pfds[2];
    
    
        pipe(pfds);
    
    
        if (!fork()) {
            close(1);       /* close normal stdout */
            dup(pfds[1]);   /* make stdout same as pfds[1] */
            close(pfds[0]); /* we don't need this */
            execlp("who, "who", NULL);
        } else {
            close(0);       /* close normal stdin */
            dup(pfds[0]);   /* make stdin same as pfds[0] */
            close(pfds[1]); /* we don't need this */
            execlp(cut", "cut", "-d", "-f 1");
        }
    
    
        return 0;
    }
    Can someone tell me how I'd convert that to a three stage? And if what i've done is right? thanks
    Last edited by Salem; 02-22-2012 at 11:31 AM. Reason: removed font crazyness from the code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Start with a function which tells you how many | are in the command line.

    Enhance that with a function which returns an argv[] for each | separated command.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    sorry i'm pretty now to C coding I don't really know how to implement what your talking about?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    As pointed out by Salem, a multistage pipeline is parsed first and executed later.
    Hint: *nix shells first parse the pipeline from left to right, then execute it from right to left.

    And think recursion!
    Last edited by itCbitC; 02-22-2012 at 01:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe multiple programs using pipe() and c
    By HaitiBoy in forum C Programming
    Replies: 1
    Last Post: 12-07-2010, 05:19 PM
  2. last stage is stuck in school work
    By vinuk23 in forum C Programming
    Replies: 8
    Last Post: 03-05-2009, 04:03 AM
  3. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  4. Using texture stage states for shadows
    By VirtualAce in forum Game Programming
    Replies: 3
    Last Post: 06-17-2002, 01:36 PM
  5. Anyone in here in the learning stage of (SDL | Win32)?
    By Golden Bunny in forum Game Programming
    Replies: 9
    Last Post: 05-24-2002, 04:31 PM