Like Tree2Likes
  • 2 Post By grumpy

Why doesn't this dup2 functionality work?

This is a discussion on Why doesn't this dup2 functionality work? within the Linux Programming forums, part of the Platform Specific Boards category; 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 ...

  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,266
    [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()).
    Salem and stahta01 like this.
    Right 98% of the time, and don't care about the other 3%.

  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
    2,553
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    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, 05: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, 08: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-03-2001, 11:17 PM

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