pipes implementation in C

This is a discussion on pipes implementation in C within the Linux Programming forums, part of the Platform Specific Boards category; Hello, I'm trying to write a simple shell in C. How do I implement redirection (< , >) and pipes ...

  1. #1
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42

    pipes implementation in C

    Hello,
    I'm trying to write a simple shell in C.
    How do I implement redirection (< , >) and pipes '|' in C ?
    Thank You

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by khdani View Post
    Hello,
    I'm trying to write a simple shell in C.
    How do I implement redirection (< , >) and pipes '|' in C ?
    Thank You
    You will need pipe(), dup() and/or dup2(), open(), close(), fork(), and exec() in some magical combination

    There are a lot of references to this on the web. If you start there, I think we'd be happy to work on specific questions with you.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42
    that's a specific question how do i implement '>'
    for example "ls -l >out.txt"
    my shell can execute commands like "ls -l" or any other command.
    i don't think i need pipe() for |, it's probably implemented with files...
    Last edited by khdani; 01-09-2009 at 03:00 PM.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by khdani View Post
    that's a specific question how do i implement '>'
    for example "ls -l >out.txt"
    The problem is, none of us really have time to write up the several hundred word response that would completely describe how to do that.

    Here's some pseudocode, but I'm not going to fill in all the blanks

    Code:
    fd = open("out.txt")
    if fork() == parent:
        close(fd)
        wait for the child
    else:
        close(1)
        dup(fd)
        exec("/bin/ls")
    In other words, the shell opens the output file. Then it forks itself to create a child. The child closes its stdout descriptor, and calls dup() to replace that with the fd of the open file. Then it executes the ls program. The parent, meanwhile, closes the fd (since it doesn't need it) and waits for the child to finish running.

    If you want more details, I'll trade you -- provide some code.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by khdani View Post
    i don't think i need pipe() for |, it's probably implemented with files...
    I don't know why you'd think that. That's exactly where you would use pipe().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 02:47 PM
  2. Pipes
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 03-31-2009, 04:09 AM
  3. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  4. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 07:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21