LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2007, 01:46 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
dup and dup2 useful?


Hi,

I was watching to dup and dup2 description, but I can't understand how uselful this primitives can be. Are they really useful?



Thanks,
 
Old 10-20-2007, 01:49 PM   #2
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
The following example is a pipe example. Please, see the question inside the code.

Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>


void piping(char* ptr)
{
  int status;
  int pid[2];
  int pipe_fd[2];

  char *prog1_argv[4];
  char *prog2_argv[2];

  char* args[getNrOftokens(ptr, "|")];

  //tokenizer( ptr, "|", args );

/*   for int i = 0; i< ,.asd,sa.d,sd */
/*   if( ptr[len-2] == ' ' ) */
/*     ptr[len-2] = 0; */
  /*
   * Build argument list
   */

  prog1_argv[0] = "ls";
  prog1_argv[1] = "-lR";
  prog1_argv[2] = "/";
  prog1_argv[3] = NULL;


  prog2_argv[0] = "more";
  prog2_argv[1] = NULL;

  /*
   * Create the pipe
   */
  if (pipe(pipe_fd) < 0)


    perror ("pipe failed");
      exit (errno);
    }

  /*
   * Create a process space for the ls
   */
  if ((pid[0]=fork()) < 0)
    {
      perror ("Fork failed");
      exit(errno);
    }

  if (!pid[0])// Child
    {
      /*
       * Set stdout to pipe
       */
      printf("%d\t%d\n", pipe_fd[0], pipe_fd[1]);
      close (pipe_fd[0]);// Close stdin
      dup2 (pipe_fd[1], 1);<<<------------------ Is this really usefeul? I don't understand why.
      printf("%d\t%d\n", pipe_fd[0], pipe_fd[1]);

      close (pipe_fd[1]);

      /* Execute the ls */
      execute (prog1_argv[0], prog1_argv);
    }

  if (pid[0])//Parent
    {
      /*
       * We're in the parent
       */

      /*
       * Create a process space for the more
       */

    perror ("pipe failed");
      exit (errno);
    }

  /*
   * Create a process space for the ls
   */
  if ((pid[0]=fork()) < 0)
    {
      perror ("Fork failed");
      exit(errno);
    }

  if (!pid[0])// Child
    {
      /*
       * Set stdout to pipe
       */
      printf("%d\t%d\n", pipe_fd[0], pipe_fd[1]);
      close (pipe_fd[0]);// Close stdin
      dup2 (pipe_fd[1], 1);
      printf("%d\t%d\n", pipe_fd[0], pipe_fd[1]);

      close (pipe_fd[1]);

      /* Execute the ls */
      execute (prog1_argv[0], prog1_argv);
    }

  if (pid[0])//Parent
    {
      /*
       * We're in the parent
       */

      /*
       * Create a process space for the more
       */
if ((pid[1]=fork()) < 0)
        {
          perror ("Fork failed");
          exit(errno);
        }

      if (!pid[1])
        {
          /*
           * We're in the child
           */

          /*
           * Set stdin to pipe
           */
          close (pipe_fd[1]);
          dup2 (pipe_fd[0], 0);
          close (pipe_fd[0]);

          /* Execute the more */
          execute (prog1_argv[0], prog1_argv);
          //execvp (prog2_argv[0], prog2_argv);
        }

      /* This is the parent */
      close(pipe_fd[0]);
      close(pipe_fd[1]);

      waitpid (pid[1], &status, 0);
      printf ("Done waiting for more.\n");

    }
}
 
Old 10-20-2007, 07:22 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Yes, "dup2()" is useful. In situations with concurrency (and the possibility of race conditions), it might even be essential.

Look at this link for more info:
http://www.linuxquestions.org/questi...40#post2931140
 
Old 10-22-2007, 10:48 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You need dup2 in cases where you want to pipe standard input and output between two programs as opposed to having everything taken from or sent to the terminal. dup can be very useful when opening a temporary stream on a file descriptor: when the stream is closed so is the file descriptor, so you need to duplicate it so the original isn't closed when the temporary stream is. dup2 is also useful for replacing a file descriptor at the process level. It allows you to change the underlying file for a file descriptor, allowing multiple threads, functions, and contexts to seamlessly write across multiple files.
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
close(fd) after dup(fd) Ayman.mashal Programming 1 06-04-2007 06:16 AM
DUP2 Help studentlb Programming 6 11-23-2006 04:21 PM
upgrade fromfc3 to fc4 levt tons of dup packages hivtop Fedora 3 06-10-2006 01:02 AM
Channel Bonding: DUP packets received from ping ricky_ds Linux - Networking 2 04-29-2005 11:39 AM
Duplicate (DUP)! ping packet cause mattsin Linux - Networking 0 10-17-2003 05:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration