当前位置:网站首页>Implementation of exec function and shell
Implementation of exec function and shell
2022-06-12 19:38:00 【Lee Neo】
exec function
problem : Why? bash The child process created is different from its own ?
execl、execlp、execle/
function :
Replace (replace) The impression of the current process (process image)
The point is replace: It's understandable , Because the cells of the body are always aging and changing , Seven years later , Your body cells are not the original cells ," Replace " Into new cells ;
execl Will replace the buffer , So the front needs to brush new flow ;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
puts("Begin...");
fflush(NULL); stay execl Need to brush the new flow before
execl("/bin/date", "date", "+%s", NULL);
perror("execl()");
exit(1);
puts("End...");
exit(0);
}
shell The working process of the command line :few Trilogy
- fork()、exec()、wait() Three important functions of a process ;
- Blood spurts : This is it. shell The working process of the command line :
1、shell The main program fork Subprocesses shell
2、excel take shell Replace bit command line ( Such as date) Binary program ;
3、 Subroutines date work ;
4、shell Parent program wait Collect the body of the subroutine ;
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
//1、 The parent process fork();
printf("Beginning\n");
fflush(NULL);
pid_t pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
else if (pid == 0)//2、 Subprocesses work
{
execl("/bin/date","date",NULL);//3、 The child process is replaced as a whole ;
perror("execl");
exit(1);
}
//1、 Parent process corpse collection ;
wait(NULL);
printf("End\n");
exit(0);
}Why does the parent-child process print on a terminal : Because the file descriptors of the parent and child processes point to the same terminal
- Hacker virus means 1: adopt execl(argv0) Hide the virus process as a regular process name ;

shell programmatic Simple implementation :
#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)
{
// Execute internal commands
printf("Internal command is not executed\n");
}
else
{
// Execute external command
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);
}
边栏推荐
- Wangxuegang room+paging3
- system()
- Reading small program based on wechat e-book graduation design (4) opening report
- API call display, detailed API of Taobao, tmall and pinduoduo commodity pages, and return of APP side original data parameters
- Can't understand kotlin source code? Starting with the contracts function~
- [image denoising] image denoising based on anisotropic filtering with matlab code
- Fault analysis | a case of MySQL remote slave database replication delay
- 基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
- 解释器文件
- VC hacon joint programming genimage3extern writeimage
猜你喜欢

Demand and business model innovation - demand 3- demand engineering process

Business opportunities with an annual increase of 3billion - non cage eggs or a new blue ocean for export to ASEAN

WinCC7.5 SP1调整画面尺寸以适应显示分辨率的方法

In 2021, the global fire pump drive power revenue is about $381million, and it is expected to reach $489.3 million in 2028
![[digital ic/fpga] data accumulation output](/img/58/8d10e41a7bc837feba677f1e0b1ceb.png)
[digital ic/fpga] data accumulation output
![[observation] Huawei's next generation data center](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] Huawei's next generation data center "adds momentum" to Guangxi's low-carbon and high-quality development

Wechat e-book reading applet graduation design works (1) development outline

The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved

“即服务”,未来已来,始于现在 | IT消费新模式,FOD按需计费

Php+flash large file breakpoint continuation function sharing
随机推荐
Axure RP 9 for Mac(交互式产品原型设计工具)中文版
Jenkins各配置选项介绍原创
Wangxuegang room+paging3
Equipment management - borrowing / returning module interface code
什么是数据驱动
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
The execution results of i+=2 and i++ i++ under synchronized are different
Demand and business model innovation-4-strategy
基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告
今晚7:00 | PhD Debate 自监督学习在推荐系统中的应用
Php+flash large file breakpoint continuation function sharing
编程工具下载地址
Is it really hopeless to choose electronic engineering and be discouraged?
[notes for question brushing] line segment tree
Hardware test - why not use grounding clip for ripple test
Global and Chinese smart government industry market research and investment risk outlook report 2022-2028
Add, delete, modify and query mysql, common MySQL commands
Demand and business model analysis -6- five topics
在 Traefik Proxy 2.5 中使用/开发私有插件(Traefik 官方博客)
Shell arrays and functions