'execl' query

M

Mike

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.
 
K

Keith Thompson

Mike said:
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.
 
A

Andrew Smallshaw

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.
 
K

Keith Thompson

Ian Collins said:
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.
 
M

Martin Ambuhl

Mike said:
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.
 
C

CBFalconer

Mike said:
.... 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.
 
K

Kenny McCormack

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???
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top