【Linux篇】exec族函数

一.exec族函数的作用

我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序。因为调用exec函数并不创建新进程,所以前后进程的ID并没有改变。

exec函数族分别是:execl, execlp, execle, execv, execvp, execvpe

#include <unistd.h>
extern char **environ;

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);

二.execl函数

•编程示例 1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
    printf("before execl\n");
    if(execl("./echoarg","echoarg","abc",NULL) == -1)
    {
        printf("execl failed!\n"); 
	perror("why");     
    }
    printf("after execl\n");
    return 0;
}

•编程示例 2

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    printf("this pro get system date:\n");
    if(execl("/bin/date","date",NULL,NULL) == -1)
    {
        printf("execl failed!\n"); 
	    perror("why");     
    }
    printf("after execl\n");
    return 0;
}

三. execlp()函数

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    printf("this pro get system date:\n");
    if(execlp("ps","ps",NULL,NULL) == -1)
    {
        printf("execl failed!\n"); 
	perror("why");     
    }
    printf("after execl\n");
    return 0;
}

四.execvp()函数 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    printf("this pro get system date:\n");
    char *argv[] = {"ps","-l",NULL};		
	
    if(execvp("ps",argv) == -1)
    {
        printf("execl failed!\n"); 
	perror("why");     
    }
    printf("after execl\n");
    return 0;
}

 五.execv()函数 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    printf("this pro get system date:\n");
    char *argv[] = {"ps","-l",NULL};		
	
    if(execv("/bin/ps",argv) == -1)
    {
        printf("execl failed!\n"); 
	perror("why");     
    }
    printf("after execl\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
exec函数Linux系统中的一个系统调用,用于在当前进程中执行一个新的程序,替换当前进程的代码和数据,从而使新程序成为当前进程。 exec函数有多个变种,包括execl、execle、execlp、execvexecvp等。它们的区别在于参数的传递方式和可执行文件的搜索路径等不同。 一般情况下,exec函数会覆盖当前进程的地址空间,因此,调用exec函数后,原进程的代码和数据都会被新进程替换,从而新进程获得了原进程的权限和资源。因此,exec函数常用于创建子进程并在子进程中执行一个新的程序。 示例代码: ```c #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { printf("This is the original process.\n"); // 创建子进程 pid_t pid = fork(); if (pid == -1) { perror("fork failed"); return -1; } else if (pid == 0) { // 在子进程中执行ls命令 execl("/bin/ls", "ls", "-l", NULL); perror("execl failed"); return -1; } else { // 父进程等待子进程结束 wait(NULL); printf("Child process finished.\n"); } return 0; } ``` 在上面的代码中,我们创建了一个子进程,并在子进程中执行了ls命令。在执行execl函数时,第一个参数是可执行文件的路径,第二个参数是可执行文件的名称,后面的参数是传递给可执行文件的参数列表。如果execl函数执行成功,则不会返回,否则会返回-1,并输出错误信息。在父进程中,我们使用wait函数等待子进程结束,并输出相应的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿gao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值