dup2 prob

This is a discussion on dup2 prob within the C Programming forums, part of the General Programming Boards category; Actually the title is a little misleading butthat is where the problem occurs. Those of you familiar with unix should ...

  1. #1
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    dup2 prob

    Actually the title is a little misleading butthat is where the problem occurs. Those of you familiar with unix should know what I'm talking about.

    Basically I'm using dup2() to redirect ouput meant for stdout to a file using the top command the problem is that the process hangs. I've guessed that top's not releasing the file descriptor once it's done (I'm passing it the instruction to dump just once into the file) but that's just a guess.
    Now advanced unix-ers, please correct me if I'm wrong on this but with using the dup2() command I don't need to save the stdout file descriptor before I redirect it to the file which means when I'm done and I close it stdout should be returned to normal ops.

    For those of you wanting to run the code, I attached it to my post in the networking board (should be on 1st page as it's recent)
    Thanx for your time.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > dup2(fd, STDOUT);
    If you're trying to make stdout go to the file fd refers to, then its
    dup2(STDOUT,fd);

    > I attached it to my post in the networking board
    Pasting a URL would be a great help in future
    un-terminated stream
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    thanx for the reply. Basically I'm trying to redirect everything meant for standard output to a file. I tried the correction you gave it doesn't store to the file.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > if ((execl("/usr/bin/top","-b", "-n", "1", NULL)) < 0)
    You missed off argv[0] (which is currently -b)

    if ((execl("/usr/bin/top","/usr/bin/top","-b", "-n", "1", NULL)) < 0)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thanx man that did the trick but I have a slight problem. There is a work around but it's not efficient.
    Code:
    if (fork() == 0)
    {
            if ((fd = open(fdump, O_CREAT | O_RDWR, 0660)) < 0)
            fprintf (stderr,"ERROR(opening file): %s.\n", strerror(errno));
    
                    dup2(fd, STDOUT);
    
            if ((execl("/usr/bin/top", "/usr/bin/top","-b", "-n","1", NULL)) < 0)
            fprintf (stderr,"ERROR(execl): %s.\n", strerror(errno));
            close(fd);
    
            read(fd, tmpBUF, msgSZ);  <--------------- I don't seem to be getting here at all.
            printf("before piping: %s./n/n", tmpBUF);
            close(pd[READ]);                //close read end of the pipe
            write(pd[WRITE], tmpBUF, strlen(tmpBUF));
            //printf("Sent to Parent: %s.\n", );
            close(pd[WRITE]);                       //close write end
            close(fd);
            exit(0);
    I can't get the print out of the buffer before send the contents to the parent. In fact after that execl() command it exits prematurely to the parent.
    Last edited by WDT; 04-03-2005 at 03:47 PM. Reason: more to add
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    You do know what execl() does right?
    It replaces the CURRENT process with a NEW process.
    Any statements after a successful execl() call do not happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thought so. I wasn't sure whether when it exits it takes the child with. But thanx for re-affirming my suspicions
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 07:53 PM
  2. IDE prob
    By MicroFiend in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2004, 04:28 PM
  3. Clipping plane prob?
    By gazsux in forum Game Programming
    Replies: 7
    Last Post: 07-04-2003, 10:49 PM
  4. prob function call
    By mouse163 in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 12:16 PM

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