Thread: Why doesn't this dup2 functionality work?

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

    Why doesn't this dup2 functionality work?

    Consider this code:

    Code:
    #include <unistd.h>     //dup2, STDIN_FILENO, STDERR_FILENO
    
    int main(void)
    {
      dup2(STDIN_FILENO, STDOUT_FILENO);
      while(1);
      return 0;
    }
    Basically, I'm trying to redirect stdin into stdout.
    The way I am trying to do this is directly - Any message coming in through the input socket fb goes directly into the stdout socket fb without buffering anything in memory in my program.
    dup2 copies over socket so STDIN and STDOUT were the same. I was hoping this would allow for any signal coming from STDIN to go to STDOUT.

    Unfortunately this does not work. Is there any way possible to get this to work?
    This would be a good optimization over buffering it in between..
    I know it may sound impossible due to some limitation in linux, but I want to see if there is something I don't know that may enable it.
    Maybe blocking vs nonblocking mode? Or something else?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    [Note: this is a unix programming question, not a C programming question].

    It doesn't work because you're not using dup2() for its intended purpose.

    dup2() is for duplicating file descriptors, not for piping data from one stream to another. Look up the pipe() function if you want to redirect the output from one program to the input of another (but not the reverse).

    If you want data copied from stdin to stdout, then you need a loop that reads data from stdin (for example, by calling fgetc()) and writes it to stdout (for example, by calling fputc()).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Do you see anything wrong with this code?:
    Code:
    #include <unistd.h>     //dup2, STDIN_FILENO, STDOUT_FILENO
    
    
    int main(void)
    {
      int socketPipe[2]; //0 = read, 1 = write
      pipe(socketPipe);
    
    
      dup2(socketPipe[0], STDIN_FILENO);
      dup2(socketPipe[1], STDOUT_FILENO);
    
    
      while(1);
      return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by LiteHacker View Post
    Do you see anything wrong with this code?:
    Code:
    #include <unistd.h>     //dup2, STDIN_FILENO, STDOUT_FILENO
    
    
    int main(void)
    {
      int socketPipe[2]; //0 = read, 1 = write
      pipe(socketPipe);
    
    
      dup2(socketPipe[0], STDIN_FILENO);
      dup2(socketPipe[1], STDOUT_FILENO);
    
    
      while(1);
      return 0;
    }
    Order of operations seem wrong to me. Your seem to be piping garbage in to garbage out.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    pipe doesn't make much sense outside the context of two processes talking to each other, a parent and child (who can inherit a set of pipes from the parent). Thus, you would need two processes (e.g. by using fork). You should read through some examples and tutorials to get a handle on those functions and how they're used. Here, Let me google that for you. This link in particular looks useful: c - Using dup2 for piping - Stack Overflow.

    Also, as the SO article suggests, read the man pages for pipe and dup2. The pipe man page actually has a nice pipe example.

    EDIT: I may have slightly misunderstood what you want. If you simply want stdin be echoed to stdout exactly as is, then no, I don't think what you want is possible with some pipe/dup2 trickery. I think the solution is much more complex than that. Is there a reason you're trying to optimize, or is this just premature optimization?
    Last edited by anduril462; 09-30-2013 at 07:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  2. What doesn't this work.
    By bobbelPoP in forum C++ Programming
    Replies: 11
    Last Post: 08-11-2008, 02:31 PM
  3. this doesn't seem to work
    By k_rock923 in forum C++ Programming
    Replies: 8
    Last Post: 02-29-2004, 09:32 PM
  4. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM
  5. Why doesn't it work?
    By pimping machinery in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2001, 12:17 AM