Linux下使用system()和execv()实现对外部程序的调用

6 篇文章 0 订阅

Linux下使用system()和execv()实现对外部程序的调用


system()函数

system()函数的原型为:

#include <stdlib.h>
int system(const char *__command);

system()函数调用/bin/sh来执行参数指定的命令,/bin/sh一般是一个软连接,指向某个具体的shell,比如bash,-c选项是告诉shell从字符串command中读取命令。关于system函数的详细分析请参考:http://blog.csdn.net/linluan33/article/details/8097916

因此,想用system()函数调用外部程序便十分简单,例如调用/home/usrname/hello/目录下的helloworld可执行文件:

system("/home/usrname/hello/helloworld");

exec函数说明

exec是函数族提供了一个在进程中执行另一个进程的方法。它可以根据指定的文件名或目录名找到可执行文件,并用它来取代原调用进程的数据段、代码段和堆栈段,在执行完之后,原调用进程的内容除了进程号外,其他全部被新程序的内容替换了。另外,这里的可执行文件既可以是二进制文件,也可以是Linux下任何可执行脚本文件。
exec函数族的6个函数原型分别为:

#include <unistd.h>
int execl(const char *path, const char *arg, ...)
int execv(const char *path, char *const argv[])
int execle(const char *path, const char *arg, ..., char *const envp[])
int execve(const char *path, char *const argv[], char *const envp[])
int execlp(const char *file, const char *arg, ...)
int execvp(const char *file, char *const argv[])

函数返回值:成功 -> 函数不会返回,出错 -> 返回-1,失败原因记录在error中。
关于exec函数族的具体分析请参考: http://blog.csdn.net/zhengqijun_/article/details/52852074

使用execv()函数调用/home/usrname/hello/目录下的helloworld可执行文件:

 char* const argv_execv[]={"helloworld","Andyoyo",NULL};//第一个字符串helloworld为可执行文件名,第二个字符串Andyoyo为传递给helloworld的参数
    if(fork()==0)
        if(execv("/home/usrname/hello/helloworld",argv_execv)<0)
            {perror("Error on execv");
                return -1;
            }

测试源码:

helloworld.cc

//helloworld.cc
#include <iostream>
using namespace std;

int main(int argc,char **argv)
{
    cout<<"------This is hello progress------"<<endl;
    char *name  =argv[1];
    cout<<"Hello "<<name<<endl;
    cout<<"Exit hello progress..."<<endl;
    return 0;
}

mainprogress.cc

//mainprogress.cc
#include <iostream>
#include "stdlib.h"
#include "unistd.h"
#include "stdio.h"
using namespace std;

int main()
{
    cout<<"---This is main progress---"<<endl;

    //方式1:使用system函数,需要头文件stdlib.h
    cout<<"使用system函数调用外部程序"<<endl;
    system("./helloworld Andyoyo");//Andyoyo为传入的参数

    //方式2:execv函数,需要头文件unistd.h
    cout<<"使用execv函数调用外部程序"<<endl;
    char* const argv_execv[]={"helloworld","Andyoyo",NULL};
    if(fork()==0)
        if(execv("/home/usrname/hello/helloworld",argv_execv)<0)
            {perror("Err on execv");
                return -1;
            }
    cout<<"Exit main process..."<<endl;       
    return 0;
}

在终端使用g++编译上面两个.cc文件,并运行progress

g++ -o helloworld helloworld.cc
g++ -o mainprogress mainprogress.cc
sudo ./mainprogress

运行后终端输出如下:

---This is main progress---
使用system函数调用外部程序
------This is hello progress------
Hello Andyoyo
Exit hello progress...
使用execv函数调用外部程序
Exit main process...
------This is hello progress------
Hello Andyoyo
Exit hello progress...

运行结果发现当主进程通过system()调用helloworld时,当helloworld执行完后会回到主进程,但是通过execv()调用helloworld时,子进程会覆盖主进程,以至于先出现Exit main process…而后进入helloworld进程。


参考网站:
[1]http://blog.csdn.net/linluan33/article/details/8097916
[2]http://blog.csdn.net/zhengqijun_/article/details/52852074

  • 9
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值