exec函数族简单用法

     exec函数族中真正意义上的系统调用只有execve(),

     exec函数族的作用是根据指定的文件名,找到可执行文件,并用它来取代调用进程的内容。

在调用进程内部执行一个可执行文件。

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

int main()
{
    int ret;
//1、execl函数
/** int execl(const char *path, const char *arg, ...) **/
//第一个参数指定路径,最后一个参数为NULL
#if 0 
    ret = execl("/mnt/hgfs/hushuai/CPP/0724/bubble","./bubble", NULL);
    if (ret < 0)
    {
        perror("execl");
        return -1;
    }
#endif

//2、execlp函数
/** int execlp(const char *file, const char *arg, ...) **/
//execlp()会从PATH环境变量所指的目录中查找符合参数的file的文件名
#if 0
    ret = execlp("ls", "ls", "-al", "/mnt", NULL);
    if (ret < 0)
    {
        perror("execlp");
        return -1;
    }
#endif

//3、execve函数
/** int execve(const char *filename, char *const argv[], char *const envp[]) **/
#if 0
    cihar *argv[] = {"ls", "-al", "/mnt", NULL};
    char *envp[] = {"PATH = /bin", NULL};//将路径设为新的环境变量
    ret = execve("/bin/ls", argv, envp);
    if (ret < 0)
    {
        perror("execve");
        return -1;
    }
#endif

//4、execvp函数
/** int execvp(const char *file, char *const argv[]) **/
#if 0
    char *argv[] = {"vim", "/mnt/hgfs/hushuai/CPP/0724/bubble.c", NULL};
    ret = execvp("vim", argv);
    if (ret < 0)
    {
        perror("execvp");
        return -1;
    }
#endif

//5、system函数
/** int system(const char *string) **/
#if 1
    ret = system("ls /mnt/hgfs/hushuai/CPP/0724");
    if (ret < 0)
    {
        perror("system");
        return -1;
    } 
#endif
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值