how to use command line argument in a process launched by execvp()
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > how to use command line argument in a process launched by execvp()




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    how to use command line argument in a process launched by execvp()  
au.faisal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-05 12:50 PM

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.






[ Post a follow-up to this message ]



    Re: how to use command line argument in a process launched by execvp()  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-05 12:50 PM


"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







[ Post a follow-up to this message ]



    Re: how to use command line argument in a process launched by execvp()  
au.faisal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-05 12:50 PM

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 onl
y
> 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






[ Post a follow-up to this message ]



    Re: how to use command line argument in a process launched by execvp()  
SM Ryan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-05 10: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. bu
t
# > > 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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:11 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register