当前位置:网站首页>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);
}
边栏推荐
- 编程工具下载地址
- 运算器的基本结构
- Original publishing practice of pipeline in Jenkins docking with CMDB interface to obtain host list
- RT-Thread 模拟器 simulator 搭建 LVGL 的开发调试环境
- Hardware test - why not use grounding clip for ripple test
- Is it really hopeless to choose electronic engineering and be discouraged?
- RT thread simulator builds lvgl development and debugging environment
- Leetcodesql: count the number of students in each major
- First build green, then build city
- Programming tool download address
猜你喜欢

First build green, then build city

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

系统 日志

解释器文件

system()

Promise to solve hell function calls can be used infinitely

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

Is it really hopeless to choose electronic engineering and be discouraged?

【数字IC/FPGA】数据累加输出

基于微信电子书阅读小程序毕业设计毕设作品(6)开题答辩PPT
随机推荐
Cookie & Session & kaptcha驗證碼
VC hacon joint programming genimage3extern writeimage
硬件测试之—纹波测试为什么不要使用接地夹子
EASYCODE one click plug-in custom template
Reading applet based on wechat e-book graduation design (2) applet function
JDBC接口总结
Demand and business model innovation - demand 3- demand engineering process
Report on the development status of China's asset appraisal industry and suggestions for future strategic planning 2022-2027
基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告
The 14th five year development plan and investment prospect analysis report of China's oil and gas pipeline engineering construction 2022-2027
用户权限和组权限
5g R17 standard is frozen. What does it say?
Wincc7.5 SP1 method for adjusting picture size to display resolution
BannerViewPager
Module 8 operation
在 Traefik Proxy 2.5 中使用/开发私有插件(Traefik 官方博客)
API call display, detailed API of Taobao, tmall and pinduoduo commodity pages, and return of APP side original data parameters
基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
New product launch
【刷题笔记】线段树