Linux系统编程-进程替换

一. 概念

当进程成功调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,开始执行新程序而不再执行自己后面的代码。调用exec并不创建新进程,所以调用exec前后该进程的pid并未改变。

在这里插入图片描述

二. 程序替换函数

#include<unistd.h>  // 他们的头文件,调用的时候记得包含
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 execve(const char *path, char *const argv[], char *const envp[]);  // 这个是系统接口
/*
1. l(list) : 表示参数采用列表
2. v(vector) : 参数用数组
3. p(path) : 有p自动搜索环境变量PATH 
4. e(environ) : 表示自己维护环境变量
5. 这些函数如果调用成功则加载新的程序从启动代码开始执行,不再返回。如果调用出错则返回-1所以exec函数只有出错的返回值而没有成功的返回值
6. 事实上,只有execve是真正的系统调用,其它五个函数最终都调用execve
*/

三. 六大函数调用使用例子

3.1 execl

// int execl(const char *path, const char *arg, ...);
// path是程序的路径(绝对路径和相对路径都可以)
// arg...是在命令行运行该程序是输入的参数,最后以NULL结束
void test_execl()
{
    execl("/bin/ls", "ls", "-a", NULL);  // Ubuntu的ls命令的绝对路径是/bin/ls,其他系统自测
}

3.2 exelp

// int execlp(const char *file, const char *arg, ...);
// file是程序文件的名字
void test_execlp()
{
    execlp("ls", "ls", "-a", NULL);
}

3.3 execle

// int execle(const char *path, const char *arg, ...,char *const envp[]);
// envp是我们自己传过去的环境变量
void test_execle()
{
    const char* envp[] =
    {
        "hello",
        "你好",
        "我是环境变量",
        NULL
    };
    execle("./test", "test", NULL, envp);  // 这里是用C写的一个叫test的可执行文件(其他语言写的也可以的)
}

3.4 execv

// int execv(const char *path, char *const argv[]);
void test_execv()
{
	char* const argv[] =
    {
        "ls",
        "-l",
        NULL
    };
    execv("/bin/ls", argv);
}

3.5 execvp

// int execvp(const char *file, char *const argv[]);
void test_execvp()
{
	char* const argv[] =
    {
        "ls",
        "-l",
        NULL
    };
    execvp("ls", argv);
}

3.6 execve

// int execve(const char *path, char *const argv[], char *const envp[]); 
void test_execvp()
{
    const char* envp[] =
    {
        "hello",
        "你好",
        "我是环境变量",
        NULL
    };
    char* const argv[] =
    {
        "test",
        NULL
    };
    execve("./test", argv, envp);
}

四. 实现一个minishell

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/wait.h>

#define NUM 64
#define CMD_NUM 16
int main()
{
    char command[NUM];
    while (1)
    {
        char* argv[CMD_NUM] = { NULL };
        command[0] = '\0';
        printf("[li@ThinkPad:~#] ");
        fflush(stdout);    

        // 1. 获取输入的字符串
        fgets(command, NUM, stdin);
        command[strlen(command) - 1] = '\0';
        const char *sep = " ";

        // 2. 将字符串解析成命令一个个命令字符串
        argv[0] = strtok(command, sep);
        int i = 1;
        while (argv[i] = strtok(NULL, sep))
        {
            i++;
        }

        // 3. 内建命令调用函数
        if (strcmp(argv[0], "cd") == 0)
        {
            if (NULL != argv[1])
            {
                chdir(argv[1]);
            }
            continue;
        }

        // 4. 创建子进程调用替换函数去执行命令
        if (0 == fork())
        {
            execvp(argv[0], argv);
            exit(0);
        }
        int status = 0;
        waitpid(-1, &status, 0);
        //printf("exit code: %d\n", (status >> 8) & 0xff);
        //printf("%s\n", command);
    }
    return 0;
}
  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

柿子__

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

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

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

打赏作者

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

抵扣说明:

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

余额充值