Home > Archive > Unix Programming > July 2006 > execlp question
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]
|
|
|
| I tried to run the following simple code.
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
char *env_init[] = { "USER=unknown", "PATH=/tmp", NULL };
int
main(void)
{
pid_t pid;
if ((pid = fork()) < 0) {
printf("fork error\n");
} else if (pid == 0) { /* specify pathname, specify environment */
printf("ppid = %d, pid = %d\n", getppid(), getpid());
if (execle("~/user1/echoall", "echoall", "myarg1",
"MY ARG2", (char *)0, env_init) < 0)
// err_sys("execle error");
printf("execle error\n");
}
if (waitpid(pid, NULL, 0) < 0)
// err_sys("wait error");
printf("wait error\n");
if ((pid = fork()) < 0) {
// err_sys("fork error");
printf("fork error\n");
} else if (pid == 0) { /* specify filename, inherit environment */
printf("ppid = %d, pid = %d\n", getppid(), getpid());
if (execlp("echoall", "echoall", "only 1 arg", (char *)0) < 0)
//err_sys("execlp error");
printf("execlp error\n");
}
exit(0);
}
Below is the result:
ppid = 21837, pid = 21838
execle error
wait error
ppid = 21838, pid = 21839
execlp error
ppid = 21837, pid = 21840
execlp error
If I run echoall individually, it runs correctly.
Why the execlp can not be invoked correctly?
Thanks a lot.
Jack
| |
| Maxim Yegorushkin 2006-07-17, 3:59 am |
|
Jack wrote:
> I tried to run the following simple code.
>
> #include <unistd.h>
> #include <sys/wait.h>
> #include <sys/types.h>
>
> char *env_init[] = { "USER=unknown", "PATH=/tmp", NULL };
>
> int
> main(void)
> {
> pid_t pid;
>
> if ((pid = fork()) < 0) {
> printf("fork error\n");
> } else if (pid == 0) { /* specify pathname, specify environment */
> printf("ppid = %d, pid = %d\n", getppid(), getpid());
> if (execle("~/user1/echoall", "echoall", "myarg1",
> "MY ARG2", (char *)0, env_init) < 0)
> // err_sys("execle error");
> printf("execle error\n");
> }
>
> if (waitpid(pid, NULL, 0) < 0)
> // err_sys("wait error");
> printf("wait error\n");
>
> if ((pid = fork()) < 0) {
> // err_sys("fork error");
> printf("fork error\n");
> } else if (pid == 0) { /* specify filename, inherit environment */
> printf("ppid = %d, pid = %d\n", getppid(), getpid());
> if (execlp("echoall", "echoall", "only 1 arg", (char *)0) < 0)
> //err_sys("execlp error");
> printf("execlp error\n");
> }
>
> exit(0);
> }
>
> Below is the result:
>
> ppid = 21837, pid = 21838
> execle error
> wait error
> ppid = 21838, pid = 21839
> execlp error
> ppid = 21837, pid = 21840
> execlp error
>
> If I run echoall individually, it runs correctly.
> Why the execlp can not be invoked correctly?
Change printf("execlp error\n") to perror("execlp") to see errno
description when it fails.
| |
|
|
Maxim Yegorushkin wrote:
> Jack wrote:
>
> Change printf("execlp error\n") to perror("execlp") to see errno
> description when it fails.
Thanks a lot.
The error is: No such file or directory
I made the following changes, it works now.
if (execle("/net/falcon/user1/sys-prog/echoall", "echoall", "myarg1",
//LINE1
"MY ARG2", (char *)0, env_init) < 0)
and
if (execlp("./echoall", "echoall", "only 1 arg", (char *)0) < 0)
//LINE2
At LINE1, I have to use the full path, not ~user1.
At LINE2, I have to use ./echoall. Why?
Jack
| |
| Maxim Yegorushkin 2006-07-17, 3:59 am |
|
Jack wrote:
[]
> I made the following changes, it works now.
> if (execle("/net/falcon/user1/sys-prog/echoall", "echoall", "myarg1",
> //LINE1
> "MY ARG2", (char *)0, env_init) < 0)
>
> and
>
> if (execlp("./echoall", "echoall", "only 1 arg", (char *)0) < 0)
> //LINE2
>
> At LINE1, I have to use the full path, not ~user1.
Globbing is performed by shell. You are not using shell here.
http://en.wikipedia.org/wiki/Globbing
> At LINE2, I have to use ./echoall. Why?
Because echoall happens to reside in your working folder.
| |
|
| > > At LINE2, I have to use ./echoall. Why?
>
> Because echoall happens to reside in your working folder.
Thanks a lot. The definition of execlp is:
int execlp(const char *filename, const char *arg0, ... /* (char *)0 */
);
It only require a filename. But i still need to use ./filename?
Jack
| |
| Pascal Bourguignon 2006-07-17, 7:00 pm |
| "Jack" <junw2000@gmail.com> writes:
>
> Thanks a lot. The definition of execlp is:
>
> int execlp(const char *filename, const char *arg0, ... /* (char *)0 */
> );
>
> It only require a filename. But i still need to use ./filename?
Why don't you read the manual page?
man execlp
[...]
The functions execlp and execvp will duplicate the actions of the
shell in searching for an executable file if the specified file
name does not contain a slash (/) character. The search path is
the path specified in the environment by the PATH variable. If
this variable isn't specified, the default path
``:/bin:/usr/bin'' is used. In addition, certain errors are
treated specially.
[...]
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Indentation! -- I will show you how to indent when I indent your skull!"
|
|
|
|
|