Linux exec函数族

相关函数:fork(),execl(),execle(),execlp(),execv(),execvp(),execve()

execl函数:

头文件:#include<unistd.h>
函数定义:intexecl(const char * path,const char *arg,....,(char*)0);

函数说明:execl()用来执行参数path字符串所代表的文件路径,接下来的参数代表执行该文件时传递过去的argv(0)、argv[1]……,最后一个参数必须用空指针(NULL)作结束。

返回值:如果执行成功则函数不会返回,执行失败则直接返回-1。

强调:函数定义中的最后一个参数必须为(char*)0

Linux系统下execl函数特点:

        当进程调用一种exec函数时,该进程完全由新程序代换,而新程序则从其main函数开始执行。因为调用exec并不创建新进程,所以前后的进程ID并未改变。exec只是用另一个新程序替换了当前进程的正文、数据、堆和栈段。

实例:

args.c
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int main(intargc , char *argv[])
{
        if(argc != 3)
        {
               printf("argc = %d\n",argc);
               printf("error!\n");
               exit(0);
        }
        char a[10],b[10];
        strcpy(a,argv[1]);
        strcpy(b,argv[2]);
        printf("a = %s b= %s\n",a,b);
        return 0;
}

gcc-o args args.c

生成可执行文件args.

execs.c
#include <unistd.h>
#include <stdlib.h>
int main()
{
        if(execl("./args","./args","hello","world",(char*)0) < 0)
               perror("Erroron execl");
        exit(EXIT_SUCCESS);    
}

gcc-o execs execs.c

生成可执行文件execs

运行:./execs

强调:execl()函数的第一个参数为执行的另外一个可执行文件的路径,如果是当前文件内,则一定为./filename,第二个参数为可执行文件名,加不加./都可以。最后一个参数一定为(char*)0。

加入进程fork()

execl.c
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
       pid_t pid;
       pid = fork();
 
       if(pid > 0)/*parent process*/
       {
                printf("Inparent process\n");
                printf("child_pid=%d\n",pid);                  
                exit(EXIT_SUCCESS);
       }
       else if(pid == 0)/*child process*/
       {
                printf("Inchild process\n");
                if(execl("./args","args","songzeyu","wahaha",(char*)0) < 0)
                        perror("error");
                printf("thesentence is not coming!\n");/*nerver calls printf*/
        }
       else
       {
                puts("forkfailure!");
                exit(EXIT_FAILURE);
       }
}

当前进程的正文都被替换了,那么execl后的语句,即便execl退出了,都不会被执行。如“printf("thesentence isnot coming!\n");/*nerver calls printf*/”永远不会执行。

args.c和上面的一样。

execlp函数:从PATH环境变量中查找文件并执行。

相关函数

fork,execl,execle,execv,execve,execvp

表头文件

#include<unistd.h>

定义函数

int execlp(const char * file,const char * arg,……)

函数说明

execlp()会从PATH 环境变量所指的目录中查找符合参数file的文件名,找到后便执行该文件,然后将第二个以后的参数当做该文件的argv[0]、argv[1]……,最后一个参数必须用空指针(NULL)作结束。

返回值

如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno 中。

execlp(execvp,..)函数一实行参数的命令和指定参数就将执行,但是执行后,相应程序将终止。 
如果想在其执行后,仍继续进行程序处理动作,就要通过fork生成子进程,在那个子进程内执行execlp等命令。execlp函数执行结束后,即使子进程终了后,其父进程仍然能够继续执行相应处理。

实例:

execlp.c
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
#if 0
char *envp[]= {"PATH = /args","USER =administrator","STATUS = testing",NULL}; /*PATH 不需要设置*/
#endif
pid_t pid;


pid = fork();

if(pid > 0)
{
wait(NULL);/*wait the child process waits,then parent process starts*/
printf("in parent process\n");
printf("child_pid=%d\n",pid);
printf("parent_pid=%d\n",getpid());
exit(EXIT_SUCCESS);

}
else if(pid == 0)
{
printf("in child process\n");
printf("child_pid=%d\n",getpid());
if(execlp("./args","args","songzeyu","wahaha",(char*)0)< 0)/*filename is pathname ,and not only filename*/
perror("error");
printf("the sentence is not coming\n");/*nerver calls the printf*/

}
else
{
perror("fork failure");
exit(EXIT_FAILURE);
} 


}

gcc -o execlp execlp.c

生成execlp可执行文件。

思考:execlp相比较execl的区别在哪里?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值