file descriptor redirection

This is a discussion on file descriptor redirection within the C Programming forums, part of the General Programming Boards category; Dear All, can any one help me regarding this. I am executing an process. I dont want its input and ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    19

    file descriptor redirection

    Dear All,

    can any one help me regarding this.

    I am executing an process.
    I dont want its input and output to be displayed on screen. Is it possible to assign the input and output file descriptors to /dev/null..
    if so can any one give me a sample code.

    Thanks in advance.
    Last edited by msenthil; 05-23-2007 at 02:03 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I'm not sure what redirecting stdin to /dev/null would do, but it would look like this:
    Code:
    $ ./myprocess < /dev/null > /dev/null
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    You can do this also $ ./myprocess > /dev/null 2>&1
    or you can do $./myprocess | 2>&1
    S_ccess is waiting for u. Go Ahead, put u there.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    19
    Dear All,


    Is this works?

    int main()
    {
    fd = open("/dev/null", O_RDWR);
    dup2(fd,0);
    dup2(fd,1);
    dup2(fd,2);
    whiel(1)
    {
    printf("Hello world");
    perror("Error");
    }

    }
    or it creates any problem to my process?
    Can any one explain me problem arise when i redirect STDIN to /dev/null.
    Last edited by msenthil; 05-23-2007 at 05:59 AM.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    134

    Thumbs up

    The code you have written will work fine.
    But it wont show u anything on terminal.
    neither it will ask to input any nor show output.

    But what exactly u need to do?
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    19
    Dear maven,

    My process previously gets inputs from user and displays errors/output in terminal.Also it reads files and does some functionalities.
    now i want to run my process in background, it should not ask for input, and it should not display output or errors in terminal. But i want its file related operations to be done by it.

    Is it correct to redirect input file descriptor to /dev/null? or does it creates any problem internally?

    Kindly reply.....

    Thanks..

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Does your process actually read anything from STDIN?

    If it does, then reading from /dev/null will just give you an infinite stream of \0, which your program isn't going to make much sense of.

    If you're not reading input, and not interested in output, the usual thing to do is just close descriptors, not redirect them.
    As in
    close(0);
    close(1);
    close(2);
    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.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    19
    The problem is i when close the input/output/error file descriptors and then if i open an new file, the file descriptor 0 is assigned to this new file and all my general outputs like "Success" is dumped in the newly opened file.

    My program has thousands of printfs and scanf's and i cant mask them all ....

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    It looks like you're stuck with the approach you have then, it you want to prevent 0,1,2 from being re-used by other open commands later on.
    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.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    134

    Red face

    Do you mean to say ... Through same function, once u want to get input and give output from terminal and once u dont want to get input and give output from terminal.
    S_ccess is waiting for u. Go Ahead, put u there.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    19
    Ya u r rite i have one boolean check like this
    Code:
    #define TERMINALMASK  0 or 1
    int main ()
    {
    
    if (TERMINALMASK == SET)
    {
    fd = open("/dev/null", O_RDWR);
    dup2(fd,0);
    dup2(fd,1);
    dup2(fd,2);
    close(fd);
    }
    else
    {
    /*do nothing*/
    }
    whiel(1)
    {
    When i get message from other process or i get user input to change file content
    {
    open a file
    read file 
    write file
    printf("success");
    close file;
    }
    }
    
    return 0;
    }
    This is sample code.
    i want to run my process like deamon. The user should not know weather it is running or not.
    So if i assign output and error fd to /dev/null it wont create problem.
    I want to know is it proper to assign input fd to /dev/null?
    Last edited by msenthil; 05-24-2007 at 03:12 AM.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ignoring the infinite syntax errors with the above code, Where has 'SET' come from?
    It's while not whiel, usually psuedo code isn't C mixed in with some wacky made up syntax, it's supposed to describe what your doing (such as an algorithm).

    So basically you want to run it in the background?

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    19
    I didnt give entire code.....

    i gave that code for understanding , how code looks.. pls ignore the syntax dar...

    i wnat to know if i do dup2(fd,2); in my code is it correct and work fine?

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why do you insist on sending it to /dev/null?, your not going to see any performance increase...

    Test it! And see if it works, don't ask if its going to work, technically it's legal but im not sure it *should* be done to the 'special streams' (stdin, stdout and stderr). However those streams exist for a reason...

    Quote Originally Posted by http://man.he.net/man2/dup2

    int dup2(int oldfd, int newfd);

    Quote Originally Posted by http://man.he.net/man2/dup2
    dup2 makes newfd be the copy of oldfd, closing newfd first
    if necessary.
    So its
    Code:
    if(dup2(2, fd) == -1)
    {
        /* failed to... */
    }
    And if you close fd, your probably going to close '2'... You should also really refer to the stream by it's name, (stdin, stdout and stderr) since some wacky system might define them differently, unless they're not allowed?
    Last edited by zacs7; 05-24-2007 at 04:47 AM.

  15. #15
    Registered User
    Join Date
    May 2007
    Posts
    19
    Ok Fine.....
    Thanks every one for ur support.....
    Thanks again..
    Last edited by msenthil; 05-24-2007 at 04:45 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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