'execl' query

Discussion in 'C Programming' started by Mike, Jan 29, 2009.

  1. Mike

    Mike Guest

    Hello everyone,

    thanks for everyone's comments on my code yesterday. it's been really
    helpful and i've learned a lot.

    i've been doing some more tests with child processes, this time using exec
    with fork.

    i am puzzled by this code:

    /* a.c, gets compiled to executable a */

    main()
    {
    int i; /* integer */
    printf("enter an integer");
    scanf("%d", &i);
    printf("you entered %d", i);
    }


    /* b.c */

    main()
    {
    if(!fork())
    {
    close(0);
    execl("./a", 0);
    }
    }


    The terminal output of program b is:

    enter an integeryou entered -1209056688

    without ever pausing to actually ask the user for input. what gives?
    does it have something to do with the two processes sharing file
    descriptors for stdin? thanks in advance for the help.
     
    Mike, Jan 29, 2009
    #1
    1. Advertising

  2. Mike <> writes:
    > thanks for everyone's comments on my code yesterday. it's been really
    > helpful and i've learned a lot.
    >
    > i've been doing some more tests with child processes, this time using exec
    > with fork.
    >
    > i am puzzled by this code:

    [snip]

    I am puzzled by your persistence in posting Unix-specific questions to
    *the wrong newsgroup*. You'll get better answers in
    comp.unix.programmer, where the people who actually know this stuff
    hang out.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 29, 2009
    #2
    1. Advertising

  3. Mike

    Ian Collins Guest

    Keith Thompson wrote:
    >
    > I am puzzled by your persistence in posting Unix-specific questions to
    > *the wrong newsgroup*.


    There be trolls in these parts.

    --
    Ian Collins
     
    Ian Collins, Jan 29, 2009
    #3
  4. On 2009-01-29, Mike <> wrote:

    > main()
    > {
    > if(!fork())
    > {
    > close(0);
    > execl("./a", 0);
    > }
    > }


    First of all, this isn't ANSI or ISO C and this message should
    really be posted to a Unix group such as comp.unix.programmer.

    However, looking at your problem you aren't providing an argv[0]:
    this needs providing explicitly. Try execl("./a", "./a", 0) and
    see what happens. I'm surprised at the error you are getting,
    though and to be honest I can't really explain it, but I'd certainly
    correct the execl() first and see if the problem goes away.

    --
    Andrew Smallshaw
     
    Andrew Smallshaw, Jan 29, 2009
    #4
  5. Ian Collins <> writes:
    > Keith Thompson wrote:
    >> I am puzzled by your persistence in posting Unix-specific questions to
    >> *the wrong newsgroup*.

    >
    > There be trolls in these parts.


    Possibly, but I prefer to be fairly slow to reach the conclusion that
    it's deliberate. Possibly he's been getting answers from AT and/or
    HfC, and therefore assumes that this is a good place to get
    information.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 29, 2009
    #5
  6. Mike wrote:

    > thanks for everyone's comments on my code yesterday. it's been really
    > helpful and i've learned a lot.


    > i've been doing some more tests with child processes, this time using exec
    > with fork.


    It is time you learned: neither fork nor exec* are part of the C
    language. If you have such functions, they are implementation-supplied
    for implementation-specific purposes. Any questions about them belong
    in a newsgroup for your implementation, since they have nothing to do
    with the C language, and everything to do with your implementation.

    The fact that the code you post never has the #include lines for your
    required headers is disturbing. If your compiler accepts such code
    without diagnostics, you need to turn on the warnings for the use of
    undeclared identifiers. If you look at the required headers, you will
    note that they are _not_ among the ones that are part of the C language.

    If you believe you are getting help with exec* and fork here, you are
    probably mistaken. In any case, anyone providing such "help" is doing
    so in the clear knowledge that they are making non-topical posts are
    likely among the handful of trolls trying to destroy this newsgroup.
    Knowing that, you should be live to the possibility that they mean you
    no good either. Such people frequently feed incorrect information to
    the unsuspecting. By posting in a newsgroup for you platform or
    implementation, you will be asking the people who are experts in that
    field, not people who are experts in the different field of the C
    programming language, which is the case in comp.lang.c.
     
    Martin Ambuhl, Jan 29, 2009
    #6
  7. Mike

    CBFalconer Guest

    Mike wrote:
    >

    .... snip ...
    >
    > i am puzzled by this code:
    >
    > /* a.c, gets compiled to executable a */
    >
    > main()
    > {
    > int i; /* integer */
    > printf("enter an integer");
    > scanf("%d", &i);
    > printf("you entered %d", i);
    > }


    NEVER use a scanf family call without checking the returned value.
    Also, main returns an int. Say so, and do so.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Jan 29, 2009
    #7
  8. In article <>,
    Keith Thompson <> wrote:
    >Ian Collins <> writes:
    >> Keith Thompson wrote:
    >>> I am puzzled by your persistence in posting Unix-specific questions to
    >>> *the wrong newsgroup*.

    >>
    >> There be trolls in these parts.

    >
    >Possibly, but I prefer to be fairly slow to reach the conclusion that
    >it's deliberate. Possibly he's been getting answers from AT and/or
    >HfC, and therefore assumes that this is a good place to get
    >information.


    Wouldn't want him to get that impression (that CLC is a good place to
    get information)...

    Gosh no. Heaven forbid. That's the trouble with trolls; they
    completely subvert the intended purpose of this group. Geez, giving
    people help - what is this world coming to???
     
    Kenny McCormack, Jan 29, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PC

    pipe() dup2() fork() execl() select()

    PC, Aug 9, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,323
  2. python newbie

    execl difficulty

    python newbie, Dec 14, 2003, in forum: Python
    Replies:
    9
    Views:
    5,197
  3. Chris Green

    Popen3-style os.execl?

    Chris Green, May 18, 2004, in forum: Python
    Replies:
    4
    Views:
    358
    Michael Hudson
    May 19, 2004
  4. Erik Johnson

    execl() and inheritied streams

    Erik Johnson, Sep 28, 2004, in forum: Python
    Replies:
    2
    Views:
    413
    Erik Johnson
    Sep 28, 2004
  5. shalombi

    Execl() in windows

    shalombi, Sep 23, 2005, in forum: C Programming
    Replies:
    1
    Views:
    741
    Keith Thompson
    Sep 24, 2005
Loading...

Share This Page