Linux:fork 和 exec 联合使用创建 bash

该博客介绍了如何结合fork和exec系统调用在C语言中创建一个简单的命令行解释器。程序读取用户输入,解析命令和参数,然后使用exec系列函数来执行命令。涉及的关键技术包括进程创建、命令解析和环境变量的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

exec 系列替换过程:

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[]); //系统调用

path:新替换的程序的路径名称
arg :传给新程序主函数的第一个参数,一般为程序的名字
arg 后面是剩余参数列表,参数个数可变,必须以空指针作为最后一个参数

fork 和 exec 联合使用创建一个全新的进程 bash

#include<stdio.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<pwd.h>
#define argc 10
char* get_cmd(char* buff,char* argv[])
{
    if(buff==NULL||argv==NULL)
    {
        return NULL;
    }//如果参数为0返回空
    char* s=strtok(buff," ");//获取第一个命令
    int i=0;
    while(s!=NULL)
    {
        argv[i]=s;//将命令打入数组
        i++;
        s=strtok(NULL," ");//继续获取剩余参数
    }
    return argv[0];//返回第一个命令
}
void print_info()
{
    char *s ="$";
    int uid=getuid();
    if(uid==0)
    {
        s="#";
    }
    struct  passwd *p=getpwuid(uid);
    if(p==NULL)
    {
        printf("$");
        fflush(stdout);
        return;
    }
    char hostname[128]={0};
    gethostname(hostname,128);

    char curr_dir[256]={0};
    getcwd(curr_dir,256);

    printf("\033[1;34m%s@%s\033[0m \033[1;35m%s\033[0m%s",p->pw_name,hostname,curr_dir,s);
    fflush(stdout);
}
int main()
{
    while(1)
    {
        print_info();
        char buff[128]={0};
        fgets(buff,128,stdin);
        buff[strlen(buff)-1]='\0';
        
        char * argv[argc]={0};
        char* cmd=get_cmd(buff,argv);//cmd是第一个参数,好区别命令和参数
        
        if(cmd==NULL)
        {
            continue;
        }

        else if(strcmp(cmd,"exit")==0)
        {
            break;
        }
        else
        {
        pid_t pid=fork();
        if(pid==-1)
        {
            continue;
        }
        if(pid==0)
        {
            execvp(cmd,argv);//cmd是命令,argv是后面的参数
            printf("error\n");
            exit(0);
        }
        }
        wait(0);
    }
    exit(0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小新 蜡笔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值