Unix Programming - fork and execlp()

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2006 > fork and execlp()





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 fork and execlp()
Roka100@gmail.com

2006-07-28, 1:22 am

Hi all,

I wander why the program below didnot do printf().

if((pid = fork()) < 0 ){
perror("");
}else if(pid == 0){
execlp(buf,buf,(char *)0);
printf("Why this cannot be printed??");
return -1;
}

It seems execlp() ended child process when finished.
Why?
Thanks.

Logan Shaw

2006-07-28, 1:22 am

Roka100@gmail.com wrote:
> I wander why the program below didnot do printf().
>
> if((pid = fork()) < 0 ){
> perror("");
> }else if(pid == 0){
> execlp(buf,buf,(char *)0);
> printf("Why this cannot be printed??");
> return -1;
> }
>
> It seems execlp() ended child process when finished.


execlp() and the other forms of exec() do not end a process.
However, they do REPLACE the program running in the process
with a different program, coming from the executable you
specify.

If you want to run another program and have control return
to the process that ran the other program, use system()
instead of exec(). The call to system() will fork another
process, run exec() in that new process, and then run wait()
or similar so that the call to system() blocks until the
child process has finished. Or, if you want more control,
you can do the fork(), exec(), and wait() yourself.

- Logan
Gordon Burditt

2006-07-28, 1:22 am

>I wander why the program below didnot do printf().
>
>if((pid = fork()) < 0 ){
> perror("");
>}else if(pid == 0){
> execlp(buf,buf,(char *)0);
> printf("Why this cannot be printed??");
> return -1;
>}
>
>It seems execlp() ended child process when finished.


execlp() returns ONLY IF IT FAILED.
If you want to do something after running the other program, fork(),
wait for the child to complete, then do it (in the parent).

Roka100@gmail.com

2006-07-28, 1:22 am


> execlp() returns ONLY IF IT FAILED.

Thanks .
But it sounds interesting that do not return anything unless it failed.

Maxim Yegorushkin

2006-07-28, 7:27 am


Roka100@gmail.com wrote:
> Thanks .
> But it sounds interesting that do not return anything unless it failed.


It does not return, rather than returning nothing, when there is no
error.

joe@invalid.address

2006-07-28, 1:24 pm

"Roka100@gmail.com" <Roka100@gmail.com> writes:

> Thanks .
> But it sounds interesting that do not return anything unless it failed.


How can it? It's not there any more if the exec succeeded. Some other
program is running instead.

Joe
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com