当前位置:网站首页>exec函数、shell的实现
exec函数、shell的实现
2022-06-12 19:35:00 【Lee Neo】
exec函数
问题:为什么bash创建的子进程和自己的不一样?
execl、execlp、execle/
功能:
替换(replace)当前的进程印象(process image)
重点在于replace:可以这么理解,由于身体的细胞一直在衰老更替,七年之后,你身体的细胞都不是原来的细胞了,"替换"成新的细胞了;
execl会替换缓冲区,因此前面需要刷新流;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
puts("Begin...");
fflush(NULL); 在execl前需要刷新流
execl("/bin/date", "date", "+%s", NULL);
perror("execl()");
exit(1);
puts("End...");
exit(0);
}
shell命令行的工作过程:few三部曲
- fork()、exec()、wait()进程的三个重要函数;
- 血脉喷张:这就是shell命令行的工作过程:
1、shell主程序fork子进程shell
2、excel将shell替换位命令行(如date)的二进制程序;
3、子程序date干活;
4、shell父程序wait给子程序收尸;
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
//1、父进程fork();
printf("Beginning\n");
fflush(NULL);
pid_t pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
else if (pid == 0)//2、子进程干活
{
execl("/bin/date","date",NULL);//3、子进程整体替换;
perror("execl");
exit(1);
}
//1、父进程收尸;
wait(NULL);
printf("End\n");
exit(0);
}父子进程为什么会打印在一个终端:因为父子进程的文件描述符相同指向同一个终端
- 黑客病毒手段1:通过execl(argv0)将病毒进程隐藏成常规进程名;

shell程序的 简单实现:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glob.h>
#include <sys/wait.h>
#define DELIMS " \t\n"
struct cmd_st
{
glob_t globres;
};
static void prompt(void)
{
printf("mysh-0.1$ ");
}
static void parse(char *line, struct cmd_st *res)
{
char *tok;
int i = 0;
while(1)
{
tok = strsep(&line, DELIMS);
if(tok == NULL)
break;
if(tok[0] == '\0')
continue;
glob(tok, GLOB_NOCHECK | GLOB_APPEND * i, NULL, &res->globres);
i = 1;
}
}
int main()
{
char *linebuf = NULL;
size_t linebuf_size = 0;
struct cmd_st cmd;
while(1)
{
pid_t pid;
prompt();
if(getline(&linebuf, &linebuf_size, stdin) < 0)
break;
parse(linebuf, &cmd);
if(0)
{
// 执行内部命令
printf("Internal command is not executed\n");
}
else
{
// 执行外部命令
pid = fork();
if( pid < 0)
{
perror("fork()");
exit(1);
}
if(pid == 0)
{
execvp(cmd.globres.gl_pathv[0], cmd.globres.gl_pathv);
perror("execvp()");
exit(1);
}
else
{
wait(NULL);
}
}
}
exit(0);
}
边栏推荐
- EFCore调优
- Details of thansmitablethreadlocal
- 【图像去噪】基于各向异性滤波实现图像去噪附matlab代码
- 7:00 tonight | application of PhD debate self supervised learning in Recommendation System
- Jenkins中pipeline对接CMDB接口获取主机列表的发布实践原创
- Global and Chinese smart government industry market research and investment risk outlook report 2022-2028
- ISCC2022
- synchronized下的 i+=2 和 i++ i++执行结果居然不一样
- Leetcodesql: count the number of students in each major
- Mysql database (28): Variables
猜你喜欢

Leetcodesql: count the number of students in each major

基于微信电子书阅读小程序毕业设计毕设作品(4)开题报告

VC hacon joint programming genimage3extern writeimage

Implementation of VGA protocol based on FPGA
![[image denoising] image denoising based on anisotropic filtering with matlab code](/img/3d/ee2e36b15b5db2502e43f6945685c5.png)
[image denoising] image denoising based on anisotropic filtering with matlab code

RT-Thread 模拟器 simulator 搭建 LVGL 的开发调试环境

3D object detection

5G R17标准冻结,主要讲了些啥?

基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告

Research Report on the overall scale, major manufacturers, major regions, products and application segments of lifeboats in the global market in 2022
随机推荐
Standard library template learning introduction original
The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved
腾讯云TDP-virt-viewer win客户端的软件使用
leetcodeSQL:578. Questions with the highest response rate
基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告
Embedded development: 6 necessary skills for firmware engineers
基于微信电子书阅读小程序毕业设计毕设作品(6)开题答辩PPT
【图像去噪】基于正则化实现图像去噪附matlab代码
Report on the development status of China's asset appraisal industry and suggestions for future strategic planning 2022-2027
API call display, detailed API of Taobao, tmall and pinduoduo commodity pages, and return of APP side original data parameters
Shell 数组和函数
The solution of BiliBili video list name too long and incomplete display
Detailed explanation of IO flow basic knowledge -- file and IO flow principle
In 2021, the global fire pump drive power revenue is about $381million, and it is expected to reach $489.3 million in 2028
简单理解防抖函数
Mysql database experiment I data definition
Understand Jack Dorsey's web5 from the ppt on page 16
合理地配置线程池
A journey of database full SQL analysis and audit system performance optimization
IO流基础知识详解--文件及IO流原理