command line options for child process

Discussion in 'C Programming' started by Vandana, Feb 18, 2010.

  1. Vandana

    Vandana Guest

    Hi,

    I would like to know how to redirect the stdin of the parent
    process to the child process.

    In my program on the command line, I pass the input file. For ex:

    ../a.out (command line options) < in.txt

    In the C Program, I create a copy of the argv, this is because the
    child process needs to be run with small modifications in the command
    line parameters. Now I create the copy, make the modifications, then
    after the fork, for the child process I use the modified command line
    parameters. Is there a cleaner way to do this. Also, how do I make the
    child process read the input from in.txt?

    int main(int argc, char*argv[]) {

    char *argv_parent[];
    char *argv_child[];

    /*create a copy of the argv */

    argv_parent = duplicate(argv);
    argv_child = {"xx",yy", NULL};

    process_id = fork()

    if (process_id == 0) {
    /* this is child process */
    argv = argv_child;

    } else
    /* parent process */
    argv = argv_parent;
    return 0;
    }

    I run the program like this
    ../a.out (command line options) < in.txt

    My program seems to work, but I dont know how to pass the input file
    (in.txt) to the child process.
    Could someone please help me.

    Thanks,
    Vandana
     
    Vandana, Feb 18, 2010
    #1
    1. Advertisements

  2. Vandana

    Eric Sosman Guest

    Although you're writing in C, your question is not about
    the C language but about the environment your program runs in.
    A Unix-oriented forum like comp.unix.programmer will be able to
    give you better help.

    <off-topic>

    1) If all the parent does is modify the command-line and
    then launch the child, fork() doesn't seem necessary: Just do
    the execXX() in the parent.

    2) All the parent's file descriptors are inherited by the
    child, including descriptor 0. The only likely problem is if
    the parent has already read something from stdin, and the I/O
    library has read a little extra and is holding it in a buffer:
    The buffer and its contents, if any, are *not* inherited. But
    if you haven't touched stdin, there should be no difficulty.

    3) If I'm wrong about either of (1) and (2), check with the
    experts in comp.unix.programmer to find out about it. Follow-ups
    set.

    </off-topic>
     
    Eric Sosman, Feb 18, 2010
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.