当前位置:网站首页>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);
}
边栏推荐
- Reading small program based on wechat e-book graduation design (4) opening report
- QT -- how to get the contents of selected cells in qtableview
- Shell arrays and functions
- Demand and business model innovation-4-strategy
- ThansmittableThreadLocal详解
- 简单理解防抖函数
- [notes for question brushing] line segment tree
- The execution results of i+=2 and i++ i++ under synchronized are different
- VC hacon joint programming genimage3extern writeimage
- 基于微信电子书阅读小程序毕业设计毕设作品(6)开题答辩PPT
猜你喜欢
RT thread simulator builds lvgl development and debugging environment
“即服务”,未来已来,始于现在 | IT消费新模式,FOD按需计费
选电子工程被劝退,真的没前景了?
Demand and business model innovation - demand 4- overview of demand acquisition
unity websockt一些知识:
How does Eldon's ring of the law get lune quickly? Introduction to the fastest and safest method for obtaining lune
DACOM G150 dual-mode earphones make sound for love and protect the healthy growth of children's hearing
In 2021, the global fire pump drive power revenue is about $381million, and it is expected to reach $489.3 million in 2028
Wechat e-book reading applet graduation design completion works (3) background function
In 2021, the global revenue of electro-optical modulator (EOM) is about USD 360.3 million, and it is expected to reach USD 704.4 million in 2028
随机推荐
Typescript decorator is basically used
Cookie & Session & kaptcha驗證碼
Native servlet - upload & download of files
synchronized下的 i+=2 和 i++ i++执行结果居然不一样
PHP converts total seconds to hours, minutes and seconds
Cookie & session & kaptcha verification code
First build green, then build city
基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
运算器的基本结构
asp. Net using JSON to interact with API data
Fault analysis | a case of MySQL remote slave database replication delay
How does Eldon's ring of the law get lune quickly? Introduction to the fastest and safest method for obtaining lune
In 2021, the global revenue of electro-optical modulator (EOM) is about USD 360.3 million, and it is expected to reach USD 704.4 million in 2028
94. analyze the content in the web page
JDBC接口总结
选电子工程被劝退,真的没前景了?
WinCC7.5 SP1调整画面尺寸以适应显示分辨率的方法
Reading small program graduation design based on wechat e-book (5) assignment
Learning summary in March
Standard library template learning introduction original