Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I start the program and I don't get the message "Succeded with dup2..." after the execution of dup2, neither do I get an error message. What is wrong? /* ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    7

    Problems with the function dup2

    Hello,

    I start the program and I don't get the message "Succeded with dup2..." after the execution of dup2, neither do I get an error message.
    What is wrong?

    /*
    * pipetest.c
    *
    * Created on: 17 okt 2009
    * Author: Anders
    */

    #include <sys/types.h> /* defines the type pid_t */
    #include <errno.h> /* defines the error control variable errno */
    #include <stdio.h> /* defines stderr, to which error messages are written */
    #include <stdlib.h> /* defines exit() */
    #include <unistd.h> /* defines pipe */

    #define PIPE_READ_SIZE (1)
    #define PIPE_WRITE_SIDE (1)

    pid_t child_pid; /* process id that is returned from fork() */

    int main (int argc /* Contains nr of variables in the program call */
    , char **argv, /*contains pointers to the addresses of the parameters in the program call */
    char **envp /*Contains all the environment variables. */
    )
    {

    int pipefiledesc [2];
    int i;
    int envp_length;
    int nbytes;
    int return_value;


    /* Create the pipe. */
    if (pipe (pipefiledesc) == -1)
    {
    fprintf (stderr, "Pipe failed.\n");
    return EXIT_FAILURE;
    }
    else
    {
    printf ("Succeded to create pipe \n");
    }

    if ( (dup2 ( pipefiledesc [PIPE_WRITE_SIDE ], STDOUT_FILENO ) ) == -1)
    {
    fprintf (stderr, "Parent cannot dup.\n");
    exit( 1 );
    }
    else
    printf ("Succeded with dup2...");

    }

    Thanks!


    Anders Branderud
    proofofaCreator.wordpress.com

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    What exactly are you trying to accomplish here? It looks like you are trying to make your pipe output stream to coincide with stdout so any output to stdout goes to the pipe. If dup2() succeeded, then the results of printf("Succeeded...") will go to the pipe and not to the terminal. So, change that to fprintf(stderr, "Succeeded...") and see if that works since you haven't redirected stderr, only stdout.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    7
    Rubberman: Thanks for the help!


    Anders Branderud
    proofofaCreator.wordpress.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...