冬天OS(三十七):shabby_shell

2023-11-21 08:41
文章标签 shell os 三十七 冬天 shabby

本文主要是介绍冬天OS(三十七):shabby_shell,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

--------------------------------------------------------

shabby_shell

--------------------------------------------------------

起名为 shabby_shell 是因为他太简单了 ...

 

·Init 函数
void Init()
{int fd_stdin = open("/dev_tty0", O_RDWR);assert(fd_stdin == 0);int fd_stdout = open("/dev_tty0", O_RDWR);assert(fd_stdout == 1);printf("Init() is running ...\n");/* extract `cmd.tar' */untar("/cmd.tar");char *tty_list[] = {"/dev_tty1", "/dev_tty2"};int i;for (i = 0; i < sizeof(tty_list) / sizeof(tty_list[0]); i++){int pid = fork();if (pid != 0){ /* parent process */printf("[parent is running, child pid:%d]\n", pid);}else{ /* child process */printf("[child is running, pid:%d]\n", getpid());close(fd_stdin);close(fd_stdout);shabby_shell(tty_list[i]);assert(0);}}while (1){int s;int child = wait(&s);printf("child (%d) exited with status: %d.\n", child, s);}assert(0);
}

 ——在 fork 之后,如果是父进程,接着 fork 下一个 shell 或者退出循环循环为每个子进程提供 wait 服务,如果是子进程,阻塞在 shabby_shell 执行 shell!
 

·shabby_shell 函数
void shabby_shell(const char *tty_name)
{int fd_stdin = open(tty_name, O_RDWR);assert(fd_stdin == 0);int fd_stdout = open(tty_name, O_RDWR);assert(fd_stdout == 1);char rdbuf[128];while (1){write(1, "$ ", 2);int r = read(0, rdbuf, 70);rdbuf[r] = 0;int argc = 0;char *argv[PROC_ORIGIN_STACK];char *p = rdbuf;char *s;int word = 0;char ch;do /* 解析输入的字符串,拆分字符串,构造字符串指针数组 */{ch = *p;if (*p != ' ' && *p != 0 && !word){s = p;word = 1;}if ((*p == ' ' || *p == 0) && word){word = 0;argv[argc++] = s;*p = 0;}p++;} while (ch);argv[argc] = 0; /* 最后的 0 参数 */int fd = open(argv[0], O_RDWR); /* 打开子程序文件 */if (fd == -1){if (rdbuf[0]){write(1, "{", 1);write(1, rdbuf, r);write(1, "}\n", 2);}}else{close(fd);int pid = fork();if (pid != 0){ /* parent */int s;wait(&s);}else{   /* child */execv(argv[0], argv);}}}close(1);close(0);
}

——遇见正确的命令,fork + exec + exit 一遍,这一趟回来之后继续执行 shell !

 

运行

OK,Happy birthday to Tinix !!!

这篇关于冬天OS(三十七):shabby_shell的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/qq_42306471/article/details/103541256
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/401179

相关文章

shell脚本自动删除30天以前的文件(最新推荐)

《shell脚本自动删除30天以前的文件(最新推荐)》该文章介绍了如何使用Shell脚本自动删除指定目录下30天以前的文件,并通过crontab设置定时任务,此外,还提供了如何使用Shell脚本删除E... 目录shell脚本自动删除30天以前的文件linux按照日期定时删除elasticsearch索引s

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

龙蜥操作系统Anolis OS-23.x安装配置图解教程(保姆级)

《龙蜥操作系统AnolisOS-23.x安装配置图解教程(保姆级)》:本文主要介绍了安装和配置AnolisOS23.2系统,包括分区、软件选择、设置root密码、网络配置、主机名设置和禁用SELinux的步骤,详细内容请阅读本文,希望能对你有所帮助... ‌AnolisOS‌是由阿里云推出的开源操作系统,旨

python中os.stat().st_size、os.path.getsize()获取文件大小

《python中os.stat().st_size、os.path.getsize()获取文件大小》本文介绍了使用os.stat()和os.path.getsize()函数获取文件大小,文中通过示例代... 目录一、os.stat().st_size二、os.path.getsize()三、函数封装一、os

shell脚本快速检查192.168.1网段ip是否在用的方法

《shell脚本快速检查192.168.1网段ip是否在用的方法》该Shell脚本通过并发ping命令检查192.168.1网段中哪些IP地址正在使用,脚本定义了网络段、超时时间和并行扫描数量,并使用... 目录脚本:检查 192.168.1 网段 IP 是否在用脚本说明使用方法示例输出优化建议总结检查 1

jenkins 插件执行shell命令时,提示“Command not found”处理方法

首先提示找不到“Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具执行shell命令是可以执行。奇怪的就是通过jenkinsSSH插件无法执行,经一番折腾各种搜索发现是jenkins没有加载/etc/profile导致。 【解决办法】: 需要在jenkins调用shell脚

站长常用Shell脚本整理分享(全)

站长常用Shell脚本整理分享 站长常用Shell脚本整理分享1-10 站长常用Shell脚本整理分享11-20 站长常用Shell脚本整理分享21-30 站长常用Shell脚本整理分享31-40 站长常用Shell脚本整理分享41-50 站长常用Shell脚本整理分享51-59 长期更新

Shell脚本实现自动登录服务器

1.登录脚本 login_server.sh #!/bin/bash# ReferenceLink:https://yq.aliyun.com/articles/516347#show all host infos of serverList.txtif [[ -f ./serverList.txt ]]thenhostNum=`cat ./serverList.txt | wc -l`e

[轻笔记]ubuntu shell脚本切换conda环境

source /home/yourhostname/anaconda3/etc/profile.d/conda.sh # 关键!!!conda activate env_name

[轻笔记] ubuntu Shell脚本实现监视指定进程的运行状态,并能在程序崩溃后重启动该程序

根据网上博客实现,发现只能监测进程离线,然后对其进行重启;然而,脚本无法打印程序正常状态的信息。自己通过不断修改测试,发现问题主要在重启程序的命令上(需要让重启的程序在后台运行,不然会影响监视脚本进程,使其无法正常工作)。具体程序如下: #!/bin/bashwhile [ 1 ] ; dosleep 3if [ $(ps -ef|grep exe_name|grep -v grep|