《Linux C编程实战》笔记:实现自己的myshell

2024-01-03 20:52

本文主要是介绍《Linux C编程实战》笔记:实现自己的myshell,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ok,考完试成功复活

这次是自己的shell命令程序的示例

流程图:

关键函数

1.void print_prompt()

函数说明:这个函数打印myshell提示符,即“myshell$$”.

2.void get_input(char *buf)

函数说明:获得一条指令,buf用来存放输入的命令。命令过长会终止程序;以换行符\n作为结束

3.void explain_input(char *buf,int *argcount,char arglist[100][256])

函数说明:解析buf中存放的命令,把选项放在arglist中,同时把argcount的值改成选项的个数。比如“ls -l /tmp”,则arglist[0],arglist[1].arglist[2]分别是"ls","-l","/tmp"。

4.do_cmd(int argcount,char arglist[100][256])

函数说明:执行命令

5.int find_command(char *command)

函数说明:功能是分别在当前目录,/bin目录,/usr/bin目录下查找命令的可执行程序。

函数源码

准备代码

首先是一些头文件和定义的代码

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<dirent.h>
//头文件里都是以前出现过的,我就不再说每个的用处了
#define normal 0 //一般的命令
#define out_redirect 1 //输出重定向
#define in_redirect 2 //输入重定向
#define have_pipe 3 //命令中有管道
void print_prompt();//打印提示符
void get_input(char *);//得到输入的命令
void explain_input(char *,int *,char [][256]);//对输入的命令进行解析
void do_cmd(int,char [][256]);//执行命令
int find_command(char *);//查找命令中的可执行程序

main函数

因为这个程序模块化的很清楚,直接看main函数不会看不懂

int main(int argc,char **argv){int i;int argcount=0;//这个是存选项数的char arglist[100][256];//这个是存选项的char **arg=nullptr;char *buf=nullptr;//这个是得到输入的数组//buf=new char[256];buf=(char *)malloc(256);//用C的malloc声明if(buf==nullptr){perror("malloc failed");//perror也出现过好多次了,详见我以前写的内容exit(-1);}while (1)//循环读入命令{memset(buf,0,256);//先把buf清空print_prompt();//打印提示符get_input(buf);//读入命令//如果是exit或logout就退出if(strcmp(buf,"exit\n")==0||strcmp(buf,"logout\n")==0)break;for(i=0;i<100;i++)//把arglist清空arglist[i][0]='\0';argcount=0;explain_input(buf,&argcount,arglist);//通过这个函数去解释buf里面的命令,函数会更新argcount和arglistdo_cmd(argcount,arglist);//执行命令}if(buf!=nullptr){//退出后记得清空内存free(buf);buf=nullptr;}exit(0);
}

print_prompt函数

最简单的一集,打印提示符就完了

void print_prompt(){printf("myshell$$ ");
}

get_input函数

void get_input(char *buf){int len=0;char ch;//古法读入字符串,长度最长是256,遇到换行停止ch=getchar();while (len<256&&ch!='\n'){buf[len++]=ch;ch=getchar();}if(len==256){printf("command is too long \n");exit(-1);}buf[len]='\n';len++;buf[len]='\0';//记得最后添个'\0'
}

explain_input函数

void explain_input(char *buf,int *argcount,char arglist[][256]){//解释后的结果会存到arglist里//具体解释的方式就是根据空格分割,分割后的每个字符串存到arglist里//也就C语言没有split函数,不然至于这么麻烦吗...char *p=buf;char *q=buf;int number=0;while (1){if(p[0]=='\n') break;if(p[0]==' ') p++;//首先跳过空格走到一个字符串的开头else{q=p;//然后指针q指向这个字符串的开头,用q来遍历字符串直到下一个空格或者换行(换行代表结束)number=0;while (q[0]!=' '&&q[0]!='\n'){number++;//顺便记录字符串的长度(其实不用number也行,q-p不就得了,书上反正用了number)q++;}//循环后现在p是字符串的开头,q是字符串结尾的下一个空格位置strncpy(arglist[*argcount],p,number+1);//把分割的字符串赋值给arglistarglist[*argcount][number]='\0';//C风格的字符串记得结尾加'\0'(*argcount)++;选项数加一p=q;//p直接跳到q的位置继续循环}}
}

do_cmd函数

这个函数才是关键好吧,执行输入的指令

void do_cmd(int argcount,char arglist[][256]){int flag=0;//重定向的标志int how=0;//具体是什么重定向,取值是define的那些值int background=0;//标志是否有后台运行符 &int status;//这是给waitpid用的int i;int fd;//这是文件描述符char *arg[argcount+1];char *argnext[argcount+1];//这是给管道用的char *file;//文件名pid_t pid;//进程号for(i=0;i<argcount;i++){arg[i]=(char *)arglist[i];//首先先把arglist的值复制到arg里面(其实我也不是很明白为什么要重开一个数组)}arg[argcount]=nullptr;//数组多开的那个放空指针//查看命令是否有后台运行符for(i=0;i<argcount;i++){if(strncmp(arg[i],"&",1)==0){//看一下最开始的函数描述,这个示例代码的后台运行符&只能出现在最后if(i==argcount-1){background=1;arg[argcount-1]=nullptr;break;}else{//所以如果不是出现在最后就提示命令错误printf("wrong command\n");return;}}}for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],">")==0){flag++;how=out_redirect;if(arg[i+1]==nullptr)flag++;}if(strcmp(arg[i],"<")==0){flag++;how=in_redirect;if(i==0)flag++;}if(strcmp(arg[i],"|")==0){flag++;how=have_pipe;if(arg[i+1]==nullptr)flag++;if(i==0)flag++;}}if(flag>1){printf("wrong command\n");return;}if(how==out_redirect){for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],">")==0){file=arg[i+1];arg[i]=nullptr;}}}if(how==in_redirect){for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],"<")==0){file=arg[i+1];arg[i]=nullptr;}}}if(how==have_pipe){for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],"|")==0){arg[i]=nullptr;int j;for(j=i+1;arg[j]!=nullptr;j++)argnext[j-i-1]=arg[j];argnext[j-i-1]=arg[j];break;}}}if((pid=fork())<0){printf("fork error\n");return;}switch (how){case 0:if(pid==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}execvp(arg[0],arg);exit(0);}break;case 1:if(pid==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd=open(file,O_RDWR|O_CREAT|O_TRUNC,0644);dup2(fd,1);execvp(arg[0],arg);exit(0);}break;case 2:if(pid==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd=open(file,O_RDONLY);dup2(fd,0);execvp(arg[0],arg);exit(0);}break;case 3:if(pid==0){pid_t pid2;int status2;int fd2;if((pid2=fork())<0){printf("fork2 error\n");return;}else if(pid2==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd2=open("/tmp/youdonotknowfile",O_WRONLY|O_CREAT|O_TRUNC,0644);dup2(fd2,1);execvp(arg[0],arg);exit(0);}if(waitpid(pid2,&status2,0)==-1)printf("wait for child process error\n");if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd2=open("/tmp/youdonotknowfile",O_RDONLY);dup2(fd,0);execvp(argnext[0],argnext);if(remove("/tmp/youdonotknowfile"))printf("remove error");exit(0);}break;default:break;}if(background==1){printf("[process id %d]\n",pid);return;}if(waitpid(pid,&status,0)==-1)printf("wait for child process eerror\n");
}

代码里没有注释的地方是在这里具体说明,单靠注释应该还是很难说清的

首先是这段代码

//查看命令里是否有重定向符号for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],">")==0){flag++;how=out_redirect;if(arg[i+1]==nullptr)flag++;}if(strcmp(arg[i],"<")==0){flag++;how=in_redirect;if(i==0)flag++;}if(strcmp(arg[i],"|")==0){flag++;how=have_pipe;if(arg[i+1]==nullptr)flag++;if(i==0)flag++;}}if(flag>1){printf("wrong command\n");return;}

首先要说明,本程序只能支持处理一个重定向符,所以既有输入重定向又有输出重定向是错误的,这是最底下flag>1的一个意思,即重定向符太多了。

但是代码里还有其他地方也有flag++的操作,这是为什么?

这需要一点点linux重定向的知识。你光写个>是没用的,肯定是要加文件 比如这样的命令:

echo "hello" > test.txt

所以像 '>'后面就不能是空了

还有像管道符,它肯定不能是命令的第一个。

不过这个示例代码里,我怀疑它是故意的,>也不能是第一个吧,<后面我也不知道可以不跟文件名的命令,所以最好是把管道那段的判断替换<和>。

所以如果出现这种错误,flag就会超过1,然后报命令格式错误。

然后是这里

if(how==out_redirect){for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],">")==0){file=arg[i+1];//得到文件名arg[i]=nullptr;}}}if(how==in_redirect){for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],"<")==0){file=arg[i+1];//得到文件名arg[i]=nullptr;}}}if(how==have_pipe){for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],"|")==0){arg[i]=nullptr;int j;for(j=i+1;arg[j]!=nullptr;j++)//把管道符后面的全都存到argnext里来argnext[j-i-1]=arg[j];argnext[j-i-1]=arg[j];break;}}}

这段还是能理解的,主要是获得文件名,文件名 自然是跟在重定向符后面了。(你看这里的<后面就跟文件名了,所以我说上面有问题)

这里设置arg[i]=nullptr我估计是为了减少遍历吧,示例代码里for循环的终止条件都是arg[i]!=nullptr 处理完这部分在此处设为nullptr,这样后面遍历就不用遍历到这里了、

对于管道,像一般的管道命令:echo "Hello, World!" | grep "Hello"

实际上相当于两个shell命令,所以会用到子进程来操作(实际上整个命令的处理都是要用子进程),那么就要单独把管道符后面的命令先存起来了

最后是这么一长串

    //创建子进程if((pid=fork())<0){printf("fork error\n");return;}switch (how)//根据how来进行不同的处理{case 0://命令符中不含重定向和管道if(pid==0){//pid=0是子进程if(!(find_command(arg[0]))){//看这个命令是不是系统有的printf("%s:command not fount\n",arg[0]);exit(0);}execvp(arg[0],arg);//***exit(0);}break;case 1://有输出重定向if(pid==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd=open(file,O_RDWR|O_CREAT|O_TRUNC,0644);//向文件里写dup2(fd,1);execvp(arg[0],arg);exit(0);}break;case 2://有输入重定向if(pid==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd=open(file,O_RDONLY);//只需要从文件里读,所以只用O_RDONLYdup2(fd,0);execvp(arg[0],arg);exit(0);}break;case 3://有管道if(pid==0){pid_t pid2;int status2;int fd2;//再创建一个子进程if((pid2=fork())<0){printf("fork2 error\n");return;}else if(pid2==0){//这是管道的子进程了if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd2=open("/tmp/youdonotknowfile",O_WRONLY|O_CREAT|O_TRUNC,0644);dup2(fd2,1);execvp(arg[0],arg);exit(0);}if(waitpid(pid2,&status2,0)==-1)printf("wait for child process error\n");if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd2=open("/tmp/youdonotknowfile",O_RDONLY);dup2(fd,0);execvp(argnext[0],argnext);if(remove("/tmp/youdonotknowfile"))printf("remove error");exit(0);}break;default:break;}if(background==1){printf("[process id %d]\n",pid);return;}if(waitpid(pid,&status,0)==-1)printf("wait for child process error\n");
}

最主要的代码无疑是execvp(arg[0],arg);这是什么意思呢?

其实在《Linux C编程实战》笔记:进程操作之退出,执行,等待-CSDN博客 出现过,不过还是稍微解释一下

execvp是让子进程执行另一个程序,那这个程序是啥?就是我们给出的参数arg[0]所指定的可执行文件,比如说

char *args[] = {"ls", "-l", NULL}; execvp("ls", args);

那子进程就会执行ls命令。

那arg[0]其实就是我们的指令了,arg也是我们指令的具体参数,所以子进程会帮我们执行指令。

还要再解释一下,execvp会先在当前路径下找有没有名为arg[0]的可执行文件,没有的话会在环境变量下找,而默认的环境变量是包含/bin的,所以为什么我们要先find_command。而像shell命令都是已经存好了的,无需我们再写一个。这样执行execvp会到/bin下执行一般的shell命令。

dup2函数在《Linux C编程实战》笔记:一些系统调用-CSDN博客有讲解,不知道的可以去看看

当然如果连open调用也不知道的话可以看这里《Linux C编程实战》笔记:文件读写-CSDN博客

 有个疑问是怎么就实现了输入输出的重定向?

dup2(fd, 0): 这一行代码将文件描述符 fd 复制到标准输入文件描述符 0 上。这意味着标准输入现在被重新定向到你打开的文件 file

答案就在这个dup2里,0是默认的标准输入文件描述符,自然1是标准输出文件描述符。我们的fd已经打开了指定的文件,然后通过dup2就指定了程序的标准输入输出重定向

如果你想将程序的输出重定向到一个文件,你可以使用类似如下的代码:

int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd == -1) {perror("open");exit(EXIT_FAILURE);
}// 将标准输出重定向到文件描述符 fd
dup2(fd, 1);// 执行输出操作,结果将写入 output.txt 文件
printf("This will be written to output.txt\n");// 关闭文件描述符 fd
close(fd);

虽然execvp更改了子进程执行的程序,但是标准输入输出是保持的,这也是为什么在输入重定向的例子中,通过 dup2 将文件描述符复制到标准输入后,execvp 执行的新程序将从这个文件描述符读取输入

至于管道那部分,实际上就是创建了一个子进程,让子进程先执行管道符|前面部分的命令,同时使用输出重定向把结果放到一个临时文件里,等子进程结束再在父进程执行|后面的命令,同时使用输入重定向从那个临时文件里读取之前运行的结果。最后把临时文件删除了。

至于waitpid这个函数,直接看《Linux C编程实战》笔记:进程操作之退出,执行,等待-CSDN博客有讲,内容太多我就不复述了。

find_command函数

主要就是目录的遍历

目录遍历不会的看这里《Linux C编程实战》笔记:目录操作-CSDN博客

int find_command(char *command){DIR *dp;struct dirent *dirp;const char *path[]={"./","/bin","/usr/bin",nullptr};//从这几个目录里找//这个意思是像 ./a.out这种命令,就不用看./了,所以加了2if(strncmp(command,"./",2)==0)command=command+2;int i=0;while (path[i]!=nullptr){if((dp=opendir(path[i]))==nullptr)printf("can not open /bin \n");while ((dirp=readdir(dp))!=nullptr){if(strcmp(dirp->d_name,command)==0){//找到了closedir(dp);return 1;}}closedir(dp);i++;}return 0;//没找到
}

最后打了半天的代码,也是幸好能成功运行

源码copy

最后把所有代码一整个都放在这,不过是没有注释的,方便大家直接copy

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<dirent.h>
#define normal 0
#define out_redirect 1
#define in_redirect 2
#define have_pipe 3
void print_prompt();
void get_input(char *);
void explain_input(char *,int *,char [][256]);
void do_cmd(int,char [][256]);
int find_command(char *);
int main(int argc,char **argv){int i;int argcount=0;char arglist[100][256];char **arg=nullptr;char *buf=nullptr;//buf=new char[256];buf=(char *)malloc(256);if(buf==nullptr){perror("malloc failed");exit(-1);}while (1){memset(buf,0,256);print_prompt();get_input(buf);if(strcmp(buf,"exit\n")==0||strcmp(buf,"logout\n")==0)break;for(i=0;i<100;i++)arglist[i][0]='\0';argcount=0;explain_input(buf,&argcount,arglist);do_cmd(argcount,arglist);}if(buf!=nullptr){free(buf);buf=nullptr;}exit(0);
}
void print_prompt(){printf("myshell$$ ");
}
void get_input(char *buf){int len=0;char ch;ch=getchar();while (len<256&&ch!='\n'){buf[len++]=ch;ch=getchar();}if(len==256){printf("command is too long \n");exit(-1);}buf[len]='\n';len++;buf[len]='\0';
}void explain_input(char *buf,int *argcount,char arglist[][256]){char *p=buf;char *q=buf;int number=0;while (1){if(p[0]=='\n') break;if(p[0]==' ') p++;else{q=p;number=0;while (q[0]!=' '&&q[0]!='\n'){number++;q++;}strncpy(arglist[*argcount],p,number+1);arglist[*argcount][number]='\0';(*argcount)++;p=q;}}
}
void do_cmd(int argcount,char arglist[][256]){int flag=0;int how=0;int background=0;int status;int i;int fd;char *arg[argcount+1];char *argnext[argcount+1];char *file;pid_t pid;for(i=0;i<argcount;i++){arg[i]=(char *)arglist[i];}arg[argcount]=nullptr;for(i=0;i<argcount;i++){if(strncmp(arg[i],"&",1)==0){if(i==argcount-1){background=1;arg[argcount-1]=nullptr;break;}else{printf("wrong command\n");return;}}}for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],">")==0){flag++;how=out_redirect;if(arg[i+1]==nullptr)flag++;}if(strcmp(arg[i],"<")==0){flag++;how=in_redirect;if(i==0)flag++;}if(strcmp(arg[i],"|")==0){flag++;how=have_pipe;if(arg[i+1]==nullptr)flag++;if(i==0)flag++;}}if(flag>1){printf("wrong command\n");return;}if(how==out_redirect){for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],">")==0){file=arg[i+1];arg[i]=nullptr;}}}if(how==in_redirect){for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],"<")==0){file=arg[i+1];arg[i]=nullptr;}}}if(how==have_pipe){for(i=0;arg[i]!=nullptr;i++){if(strcmp(arg[i],"|")==0){arg[i]=nullptr;int j;for(j=i+1;arg[j]!=nullptr;j++)argnext[j-i-1]=arg[j];argnext[j-i-1]=arg[j];break;}}}if((pid=fork())<0){printf("fork error\n");return;}switch (how){case 0:if(pid==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}execvp(arg[0],arg);exit(0);}break;case 1:if(pid==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd=open(file,O_RDWR|O_CREAT|O_TRUNC,0644);dup2(fd,1);execvp(arg[0],arg);exit(0);}break;case 2:if(pid==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd=open(file,O_RDONLY);dup2(fd,0);execvp(arg[0],arg);exit(0);}break;case 3:if(pid==0){pid_t pid2;int status2;int fd2;if((pid2=fork())<0){printf("fork2 error\n");return;}else if(pid2==0){if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd2=open("/tmp/youdonotknowfile",O_WRONLY|O_CREAT|O_TRUNC,0644);dup2(fd2,1);execvp(arg[0],arg);exit(0);}if(waitpid(pid2,&status2,0)==-1)printf("wait for child process error\n");if(!(find_command(arg[0]))){printf("%s:command not fount\n",arg[0]);exit(0);}fd2=open("/tmp/youdonotknowfile",O_RDONLY);dup2(fd,0);execvp(argnext[0],argnext);if(remove("/tmp/youdonotknowfile"))printf("remove error");exit(0);}break;default:break;}if(background==1){printf("[process id %d]\n",pid);return;}if(waitpid(pid,&status,0)==-1)printf("wait for child process error\n");
}
int find_command(char *command){DIR *dp;struct dirent *dirp;const char *path[]={"./","/bin","/usr/bin",nullptr};if(strncmp(command,"./",2)==0)command=command+2;int i=0;while (path[i]!=nullptr){if((dp=opendir(path[i]))==nullptr)printf("can not open /bin \n");while ((dirp=readdir(dp))!=nullptr){if(strcmp(dirp->d_name,command)==0){closedir(dp);return 1;}}closedir(dp);i++;}return 0;
}

这篇关于《Linux C编程实战》笔记:实现自己的myshell的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/567032

相关文章

Python在二进制文件中进行数据搜索的实战指南

《Python在二进制文件中进行数据搜索的实战指南》在二进制文件中搜索特定数据是编程中常见的任务,尤其在日志分析、程序调试和二进制数据处理中尤为重要,下面我们就来看看如何使用Python实现这一功能吧... 目录简介1. 二进制文件搜索概述2. python二进制模式文件读取(rb)2.1 二进制模式与文本

Python中Tkinter GUI编程详细教程

《Python中TkinterGUI编程详细教程》Tkinter作为Python编程语言中构建GUI的一个重要组件,其教程对于任何希望将Python应用到实际编程中的开发者来说都是宝贵的资源,这篇文... 目录前言1. Tkinter 简介2. 第一个 Tkinter 程序3. 窗口和基础组件3.1 创建窗

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Django调用外部Python程序的完整项目实战

《Django调用外部Python程序的完整项目实战》Django是一个强大的PythonWeb框架,它的设计理念简洁优雅,:本文主要介绍Django调用外部Python程序的完整项目实战,文中通... 目录一、为什么 Django 需要调用外部 python 程序二、三种常见的调用方式方式 1:直接 im

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC