2.6 exec函数族

news/2024/7/24 10:08:30 标签: exec函数族, linux

目录

exec函数族作用图解:

execl函数:

execlp函数:

execv函数:

execve函数:


一系列功能相同或相似的函数统称为函数族。

exec函数族的一般使用方法:先fork一个子进程,然后在子进程中调用exec函数族中的某个函数。


exec函数族作用图解:

调用成功后,从a.out的main函数开始执行。


前6个为标准C库函数,最后一个为linux库函数。

execl函数:

#include <unistd.h>
int execl(const char *path, const char *arg, ...);
参数:
    -path:需要指定的可执行程序的路径或名称
        如a.out  /home/a.out,推荐使用绝对路径
    -arg:是执行可执行文件所需要的参数列表
        第一个参数一般没有什么作用,为了方便,一般写的是执行的程序的名称
        从第二个参数开始往后,就是程序执行所需要的参数列表
        参数最后需要以NULL结束(NULL也称为哨兵)
返回值:
    只有当调用失败,才会有返回值,返回-1,并且设置errno
    如果调用成功,没有返回值

hello.c:

#include <stdio.h>
int main()
{
    printf("hello\n");
    return 0;
}

open.c:

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

int main()
{
    //创建一个子进程,在子进程中执行exec函数族中的函数
    pid_t id=fork();
    if(id>0)
    {
        printf("I am a parent process,pid=%d\n",getpid());
        sleep(1);
    }
    else if(id==0)
    {
        int ret=execl("hello","hello",NULL);
        if(ret==-1)
        {
            perror("execl");
            return -1;
        }
        printf("I am a child process,pid=%d\n",getpid());
    }
    for(int i=0;i<3;i++)
    {
        printf("i=%d,pid=%d\n",i,getpid());
    }
    return 0;
}

在open.c中用execl调用hello这个可执行程序。

运行结果:

execl函数可以执行自定义的可执行程序:要么可执行程序在目录中,要么输入绝对路径

execl函数可以执行系统自带的可执行程序:只能输入绝对路径

如执行ps aux命令,只能写成execl("/bin/ps","ps","aux",NULL);,而不能写成execl("ps","ps","aux",NULL);

否则,找不到文件或目录,如下:

如果不想写绝对路径,可以使用下面的execlp函数。


execlp函数:

#include <unistd.h>
int execlp(const char *file, const char *arg, ...);
    会到环境变量中查找指定的可执行文件,如果找到了就执行,找不到就执行不成功
参数:
    -file:需要执行的可执行文件的文件名

open.c:

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

int main()
{
    pid_t id=fork();
    if(id>0)
    {
        printf("I am a parent process,pid=%d\n",getpid());
        sleep(1);
    }
    else if(id==0)
    {
        int ret=execlp("ps","ps","aux",NULL);
        if(ret==-1)
        {
            perror("execl");
        }
        printf("I am a child process,pid=%d\n",getpid());
    }
    for(int i=0;i<3;i++)
    {
        printf("i=%d,pid=%d\n",i,getpid());
    }
    return 0;
}

可以正确运行,如下图:


execv函数:

#include <unistd.h>
int execv(const char *path, char *const argv[]);
argv是需要的参数的一个字符串数组
//使用示例:
char *argv[]={"ps","aux",NULL};
execv("ps",argv);
//和execl的区别是调用的可执行程序的参数是直接写还是先放到一个数组中,l表示list,是以列表的形式,v表示vector,先放到数组中

execve函数:

#include <unistd.h>
int execve(const char *filename, char *const argv[],char *const envp[]);



//怎么用?多个环境变量怎么写?

参考:牛客网 C++高薪求职项目《Linux高并发服务器开发》2.6 exec函数族

专属优惠链接:

https://www.nowcoder.com/courses/cover/live/504?coupon=AvTPnSG


http://www.niftyadmin.cn/n/1032334.html

相关文章

2-1选择排序法

思想&#xff1a;选择最小的元素&#xff0c;放在未排序数组之首&#xff0c;即位置i 0到i-1是已经排序好的数组 定义一个变量&#xff0c;记录本次循环中找到的最小值的下标 将最小元素与i位置元素互换 #include <iostream>using namespace std; //参数为数组和数组…

2-2使用模板编写算法

上一节中的排序算法只能对整型数组进行排序 下面用函数模板进行改进&#xff1a; #include <iostream> #include "student.h"using namespace std; //参数为数组和数组中元素的个数 template<class T> void selectionSort(T a[],int n) {int i0;int mi…

2-3随机生成算法测试用例

前面两节的测试用例是手工输入的&#xff0c;下面用随机函数生成测试用例&#xff1a; #include <iostream> #include "testhelper.h"using namespace std; //参数为数组和数组中元素的个数 template<class T> void selectionSort(T a[],int n) {int i0…

2-4测试算法的性能

#include <iostream> #include "testhelper.h"using namespace std; //参数为数组和数组中元素的个数 template<class T> void selectionSort(T a[],int n) {int i0;int minIndex0;for(i0;i<n;i){ //寻找[i,n)区间里的最小值minIndexi; //minIndex表…

2.7进程退出、孤儿进程、僵尸进程

目录 1.进程退出 2.孤儿进程 3.僵尸进程 1.进程退出 #include <stdlib.h> void exit(int status);#include <unistd.h> void _exit(int status);status参数&#xff1a;是进程退出时的一个状态信息。父进程回收子进程资源的时候可以获取到。测试exit函数的程序&…

2.8wait函数

目录 1.进程回收 2.wait函数 程序版本1&#xff1a; 程序版本2&#xff1a; 程序版本3&#xff1a; 程序版本4&#xff1a; 1.进程回收 2.wait函数 #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status);功能&#xff1a;等待任意一个子进…

2.9 waitpid函数

#include <sys/types.h> #include <sys/wait.h>pid_t waitpid(pid_t pid, int *status, int options);功能&#xff1a;回收指定进程号的子进程&#xff0c;可以设置是否阻塞参数&#xff1a;-pid&#xff1a;pid > 0&#xff1a;某个子进程的ID&#xff0c;回收…

2.10进程间通信简介

参考&#xff1a;牛客网 C高薪求职项目《Linux高并发服务器开发》2.10进程间通信简介 专属优惠链接&#xff1a; https://www.nowcoder.com/courses/cover/live/504?couponAvTPnSG