当前位置:网站首页>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);
}
边栏推荐
- In 2021, the global revenue of chlorinated polyvinyl chloride (CPVC) was about $1809.9 million, and it is expected to reach $3691.5 million in 2028
- A journey of database full SQL analysis and audit system performance optimization
- 基于微信电子书阅读小程序毕业设计毕设作品(8)毕业设计论文模板
- Equipment management - borrowing / returning module interface code
- How does Eldon's ring of the law get lune quickly? Introduction to the fastest and safest method for obtaining lune
- 简单理解防抖函数
- 设备管理-借还模块1
- easycode一键生成插件自定义模板
- [image denoising] image denoising based on anisotropic filtering with matlab code
- 模塊八作業
猜你喜欢

Reading small program based on wechat e-book graduation design (4) opening report

基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能

VC hacon joint programming genimage3extern writeimage

Research Report on the overall scale, major manufacturers, major regions, products and application segments of lifeboats in the global market in 2022

3GPP RAN第一次F2F会议,都干了些啥?
![[blockbuster release] ant dynamic card, enabling the app home page to realize agile update](/img/a6/62caef27b917bbb4c5529de46bc7f9.jpg)
[blockbuster release] ant dynamic card, enabling the app home page to realize agile update

Leetcodesql: count the number of students in each major

【图像去噪】基于各向异性滤波实现图像去噪附matlab代码

leetcodeSQL:602. Friend application II: who has the most friends

【图像去噪】基于正则化实现图像去噪附matlab代码
随机推荐
PHP converts total seconds to hours, minutes and seconds
In 2021, the global revenue of chlorinated polyvinyl chloride (CPVC) was about $1809.9 million, and it is expected to reach $3691.5 million in 2028
EFCore调优
Pyinstaller packaging tutorial packaging resource files
Is it really hopeless to choose electronic engineering and be discouraged?
Negative remainder problem
Wincc7.5 SP1 method for adjusting picture size to display resolution
Equipment management - borrowing and returning module 1
基于微信电子书阅读小程序毕业设计毕设作品(5)任务书
Detailed explanation of yolox network structure
Download and configuration of nuitka packaging tutorial
DACOM G150 dual-mode earphones make sound for love and protect the healthy growth of children's hearing
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
模块八作业
Promise to solve hell function calls can be used infinitely
Understand Jack Dorsey's web5 from the ppt on page 16
Reading small program graduation design based on wechat e-book (5) assignment
Market scale forecast and future competitive trend outlook report of China's plastic and cosmetic industry 2022-2028
"As a service", the future has come, starting from the present | new mode of it consumption, FOD billing on demand
META-INF、WEB-INF分别是什么?