4,带 e 的exec函数:execle表示,将环境变量传递给需要替换的进程
从上述的函数原型中我们发现:extern char **environ;
此处的environ是一个指针数组,它当中的每一个指针指向的char为“XXX=XXX”
environ保存环境信息的数据可以env命令查看:
它由shell进程传递给当前进程,再由当前进程传递给替换的新进程
示例:execle.c
#include#include#include
int main(int argc, char *argv[])
{//char * const envp[] = {"AA=11", "BB=22", NULL};
printf("Entering main ...\n");intret;
ret=execl("./hello", "hello", NULL);//execle("./hello", "hello", NULL, envp);
if(ret == -1)
perror("execl error");
printf("Exiting main ...\n");return 0;
}
hello.c
#include#include
extern char**environ;int main(void)
{
printf("hello pid=%d\n", getpid());inti;for (i=0; environ[i]!=NULL; ++i)
{
printf("%s\n", environ[i]);
}return 0;
}
可知原进程确实将环境变量信息传递给了新进程
那么现在我们可以利用execle函数自己给的需要传递的环境变量信息:
示例程序:execle.c
#include#include#include
int main(int argc, char *argv[])
{char * const envp[] = {"AA=11", "BB=22", NULL};
printf("Entering main ...\n");intret;//ret =execl("./hello", "hello", NULL);
ret =execle("./hello", "hello", NULL, envp);if(ret == -1)
perror("execl error");
printf("Exiting main ...\n");return 0;
}
hello.c
#include#include
extern char**environ;int main(void)
{
printf("hello pid=%d\n", getpid());inti;for (i=0; environ[i]!=NULL; ++i)
{
printf("%s\n", environ[i]);
}return 0;
}
确实将给定的环境变量传递过来了
要怎样做才能正常执行(主要是execl函数运行php文件。),并输入exit退出?
execl会载入你调用的程序,覆盖原有代码段,相当于你本来的程序的代码段被替换成execl执行的了。
所以execl后面的都不会输出了。
正确的应该是fork一个子进程,在子进程中调用execl。
如果我把execl 放到用pthread_create创建的线程是不是也一样会覆盖原有的代码?
我测试过也是一样会退出。。
一定要用fork 吗?
execl是覆盖进程的代码段,所以如果你原来的程序还需要正常退出的话,就要fork一个子进程。
另外,如果需要execl调用的这个程序执行完成,就需要在父进程中等待,可以调用waitpid。
具体的你man一下看看。
如果用了fork 。执行execl 后会不会停止 子进程的呢?
fork以后,子进程拷贝父进程的几乎所有特性,包括pc,也就是说,在子进程中,还是从你fork的地方开始运行的。然后你execl,覆盖掉的是子进程的代码段,与父进程无关。
pthread_create创建的子线程与父线程共用同一代码段!只是寄存器和栈不同!
execl不是创建一个新进程,Linux只有fork能创建新进程
execl成功是不会返回的,会执行新的程序。
execl失败才会返回
View Code
我感觉execl的第一个参数和第二个参数有重叠,帮我 ...
如题,我学习了一下execl函数的用法:
#include
intmain()
{
execl("ls", "ls", "-al");return 0;
}
OK,什么也不打印,于是我改了一下:
#include
intmain()
{
execl("/bin/ls", "ls", "-al", 0);return 0;
}
很奇怪啊,execl第一个参数必须是path,我如果把调用语句改成execl("/bin", "ls", "-al", 0);
发现也不行。那岂不是说这个path参数必须包含可执行程序的名字本身? 可是这样的话,execl的第二个参数就是文件名啊,第二个参数岂不是多余了?非常疑惑,求解释!path里面包含了file的名字,那还要第二个file参数干什么呢?感觉多余?
第2个参数将传给进程,也就是argv[0],进程可以通过查看面子有多大,就能看人下菜碟-哦是随机应变了!。
果然是高手!我验证了你的说法,第二个参数是argv[0],可以是任何东西,不必是可执行文件名。如下图所示:
View Code
三,fcntl()函数中的FD_CLOEXEC标识在exec系列函数中的作用
#include
#include
int fcntl(int fd, int cmd, ... /* arg */ );
File descriptor flags
The following commands manipulate the flags associated with a file
descriptor. Currently, only one such flag is defined: FD_CLOEXEC, the
close-on-exec flag. If the FD_CLOEXEC bit is 0, the file descriptor
will remain open across an execve(2), otherwise it will be closed.
//如果FD_CLOEXEC标识位为0,则通过execve调用后fd依然是打开的,否则为关闭的
F_GETFD (void)
Read the file descriptor flags; arg is ignored.
F_SETFD (long)
Set the file descriptor flags to the value specified by arg.
如:fcntl(fd, F_SETFD, FD_CLOEXEC);
1关闭(标准输出关闭)ls -l无法将结果显示在标准输出