|
Home > Archive > Unix Programming > September 2005 > how to use command line argument in a process launched by execvp()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to use command line argument in a process launched by execvp()
|
|
| au.faisal 2005-09-23, 7:50 am |
| Hi guys
I am having a problem with my school assignment. I have to write
programm which uses fork to create a new process and then replace with
another process by using execvp. now up to this point this is fine. but
the problem is the replaced function will take value in command line
argument to use it as its own status.
what i did is following:
char *file_name = "child"; /*child is the child programm*/
char *file_name1[] = {"child"; NULL};
if((pid = fork()) <0)
{
perror("ERROR");
exit(EXIT_FAILURE);
}
else
{
if(execvp(file_name, file_name1) == -1)
{
perror("ERROR");
exit(EXIT_FAILURE);
}
}
then in child programm I declared like this:
int
main(int argc, char* argv[])
{
int status;
status = atoi(argv[1]);
}
Can any one tell me how to do this.
| |
| David Schwartz 2005-09-23, 7:50 am |
|
"au.faisal" <au.faisal@gmail.com> wrote in message
news:1127464048.702596.5190@g44g2000cwa.googlegroups.com...
> I am having a problem with my school assignment. I have to write
> programm which uses fork to create a new process and then replace with
> another process by using execvp. now up to this point this is fine. but
> the problem is the replaced function will take value in command line
> argument to use it as its own status.
>
> what i did is following:
>
> char *file_name = "child"; /*child is the child programm*/
> char *file_name1[] = {"child"; NULL};
>
> if((pid = fork()) <0)
> {
> perror("ERROR");
> exit(EXIT_FAILURE);
> }
> else
> {
> if(execvp(file_name, file_name1) == -1)
> {
> perror("ERROR");
> exit(EXIT_FAILURE);
> }
> }
>
> then in child programm I declared like this:
>
> int
> main(int argc, char* argv[])
> {
> int status;
> status = atoi(argv[1]);
> }
>
> Can any one tell me how to do this.
Two things:
1) You forgot to set up the arguments for the child. You passed it only
its file name.
2) You call 'execvp' in both the parent and the child!
DS
| |
| au.faisal 2005-09-23, 7:50 am |
| David Schwartz wrote:
> "au.faisal" <au.faisal@gmail.com> wrote in message
> news:1127464048.702596.5190@g44g2000cwa.googlegroups.com...
>
Thanks for the quick reply. This place is really helpful. Actually I am
a newbie in the unix system programming. so it's a bit defficult for me
understand everything straight away.
[vbcol=seagreen]
> Two things:
>
> 1) You forgot to set up the arguments for the child. You passed it only
> its file name.
Can you please explain a bit more. should i put the child status in the
char "*file_name1[]" arrey something like this char *file_name1[] =
{"child", status, NULL}; and then the value of the status is taken from
the command line argument in the parent programm.
> 2) You call 'execvp' in both the parent and the child!
I didn't understand why I need to call execvp in child program. Because
in child all it's doing is taking the message passed from parent
through pipe as from stdin and pass this message as stdout to the next
child through another pipe. Parent creats all these pipes and put
theses pipes among these "childs". now child is single program whch
copyed three times using the execvp function by parent. now would you
please explain why I need use execvp function in the child program.
> DS
Thanks
Faisal
| |
| SM Ryan 2005-09-23, 5:55 pm |
| "au.faisal" <au.faisal@gmail.com> wrote:
# David Schwartz wrote:
# > "au.faisal" <au.faisal@gmail.com> wrote in message
# > news:1127464048.702596.5190@g44g2000cwa.googlegroups.com...
# >
# > > I am having a problem with my school assignment. I have to write
# > > programm which uses fork to create a new process and then replace with
# > > another process by using execvp. now up to this point this is fine. but
# > > the problem is the replaced function will take value in command line
# > > argument to use it as its own status.
# > >
# > > what i did is following:
# > >
# > > char *file_name = "child"; /*child is the child programm*/
# > > char *file_name1[] = {"child"; NULL};
This is the argument vector. By convention the first argument is
the name or path of execked function, but it is not required by
the kernel. To pass additional arguments, just include more strings.
char *file_name1[] = {"child","argv[1]","argv[2]",...,0};
You can also dynamically allocate the argv array with malloc,
compute array values, etc, using the usual miscellanous C
functiions.
# > > if((pid = fork()) <0)
# > > {
# > > perror("ERROR");
# > > exit(EXIT_FAILURE);
# > > }
You probably want to distinguish three cases
pid < 0, fork error
pid > 0, this is the parent process
and pid is the child process
pid = 0, this is the child process
# > 2) You call 'execvp' in both the parent and the child!
#
# I didn't understand why I need to call execvp in child program. Because
exec functions do not create new processes. You use fork() for that.
exec functions release the memory (but not process information),
and then load a new program and begin it in the same process.
if your parent is going to immediately exit after the fork,
it has no useful work to do managing pipes or waiting for status
after the fork, you can just exec in the original process and
not bother to fork. In a shell if you do something like
command p1 p2 ... pn
the shell forks; the child execs command while the parent waits
for the child; the parent shell resumes its dialog when the child
completes. If instead you do
exec command p1 p2 ... pn
the shell replaces itself with command runs to completion.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Don't say anything. Especially you.
|
|
|
|
|