当前位置:网站首页>进程(番外):自定义shell命令行解释器
进程(番外):自定义shell命令行解释器
2022-08-02 03:32:00 【RNGWGzZs】

---------------------加油!
本篇的理论较少,当然代码量也很少就仅仅是上传的代码而已。
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<string.h>
#define MAX_ORDER 1024
#define MAX_LINE 32
int main()
{
//命令 输入
char cmd[MAX_ORDER];
char* line[MAX_LINE]; //指令参数 拆开
//shell 脚本是一直运行的状态
while(1)
{
printf("\n[[email protected] my_shell]# ");
fgets(cmd,MAX_ORDER,stdin); //读取
cmd[strlen(cmd)-1]='\0'; //解决\n
//printf("%s\n",cmd);
line[0]=strtok(cmd," ");
int i=1;
while(line[i]=strtok(NULL," ")){
i++;
}
//创建子进程 进行调用
pid_t id=fork();
if( id == 0 )
{
//child 进行执行
execvp(line[0],line);
// 执行谁 怎样执行
}
int status=0;
pid_t ret=waitpid(id,&status,0); //此时父进程只需要等就行了
if(ret >0)
{
printf("exit code:%d\n",WEXITSTATUS(status));
}
}
return 0;
}
最终代码就段落。
祝你好运~
边栏推荐
猜你喜欢
随机推荐
2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
Industry where edge gateway strong?
为什么D类音频功放可以免输出滤波器
【plang 1.4.4】编写茶几玛丽脚本
2020 - AAAI - 图像修复 Image Inpainting论文导读 -《Region Normalization for Image Inpainting》
【plang 1.4.6】Plang高级编程语言(发布)
2019 - ICCV - 图像修复 Image Inpainting 论文导读《StructureFlow: Image Inpainting via Structure-aware ~~》
阿里云华为云对比分析
字符串匹配(蛮力法+KMP)
蛮力法求解凸包问题
Cadence allegro导出Gerber文件(制板文件)图文操作
C语言教程 - 制作单位转换器
Personal image bed construction based on Alibaba Cloud OSS+PicGo
汇编语言跳转指令总结
How to remotely debug PLC?
同时求最大值与最小值(看似简单却值得思考~)
使用Vercel托管自己的网站
【NTC 热敏电阻与 Arduino 读取温度】
读取FBX文件踩坑清单
78XX 79XX多路输出电源









