[clug-progsig] C Question about using execlp() in Linux

Reno L llgg_716_2000 at yahoo.com
Sun Sep 5 23:15:36 PDT 2004


Sorry, my bad, you are all right. I made so much typo
when I copied from Linux terminal to the email. But, I
did compile successfully in the terminal. 
> >int main(void)
> >{
> >        ...
> >
> >	if ( ( int pid = fork() ) < 0 )
> >		printf(" fork error ");
> >	else if (pid == 0) {
> >		print(" It's child process now.\n");
> [>>>>>>>>>>>>> printf, not print ] 
  You are right, my typo.

> >if (execlp("ls","-l",(char *)0)<0)//call shell "ls
-l"
> [>>>>>>>>>>>>>	no, no, no! ]

  Yes, it was compiled and ran in my terminal. But I
know what's wrong with it now. Should be:
execlp("ls","ls","-l",(char *)0). last (char *) 0 is
indeed needed.
					  	// in child process.
> >	printf("execlp error); //error handle output
> [>>>>>>>>>>>>> missing close quote ]
Opps, you are right here.
> >	} else{
> >		sleep(2);
> >		printf("this is parent process.");
> > 	}
> >	printf(" pid = %d\n", getpid());
> >	exit(0); 
> >}
> >
> 
> This shouldn't have compiled or linked.
My typos, but it was compiled in the terminal.

> You've misunderstood the semantics of the exec*()
> functions. They don't
> run a command from a shell. 
Could agree half of this part. 
An executable text file that contains a shell
procedure (only the execlp and execvp subroutines
allow this type of new-process image file). 

> The exec*() functions *replace* the current
> process image with that of the exec'ed program. That
> is, if the exec is
> successful it won't return because the code to
> return to no longer
> exists in that process image. To run a command in a
> shell see the
> system() function. (try 'man exec' and 'man system')
Again, over 6 exec* functions, only the execlp and
execvp subroutines allow this type of new-process
image file.


> Also the (char *)0 should not be in the execlp()
> call. The null
> termination is needed only when supplying a vector
> of argument pointers
> (to terminate it.)
I could not agree this part. the (char *)0 is needed.
In the reference, I checked the man and book, said,
"the final command-line argument is followed by a null
pointer, if this null pointer is specified by the
constant 0, we must explicitly cast it to a pointer. "


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the clug-progsig mailing list