实现功能
功能:实现UNIX外壳
题目来源:《操作系统概念第9版》P107页 项目1
详细功能:这个项目设计一个Unix外壳接口,可以接受用户命令,然后可以在一个单独进程中执行用户命令。
源代码
unix.c
#include <stdio.h> /* fflush */
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* pid_t */
#include <sys/wait.h> /* waitpid, WIFEITED */
#include <unistd.h> /* fork, exit, execvp */
int main() {
char *args[20]; /* 每条命令最大元素数 */
int state = 1;
while(state) {
printf("osh> ");
fflush(stdout); /* 刷新标准输出缓冲区 */
char c;
int i = 0; /* args的位置下标 */
do{
char *s = (char*)malloc(64); /* 字符串最大长度 */
scanf("%s", s);
c = getchar(); /* 读入分隔符 */
args[i] = s;
i++;
if(strcmp(args[0], "exit") == 0) { /* 若输入的第一个字符串为"exit",则退出Unix */
state = 0;
}
if(strcmp(args[i-1], "&") == 0) { /* 若当前输入的字符串为"&",则回退一格 */
i--;
}
}while(c == ' '); /* 每行输入结束判断 */
args[i] = NULL; /* 输入的命令行尾加NULL */
pid_t id = fork(); /* 调用fork创建子进程 */
if(id < 0) { /* 错误处理 */
perror("fork");
}
if(id == 0) { /* fork在子进程中会返回0 */
execvp(args[0], args); /* 执行输入的命令 */
exit(1); /* 退出子进程 */
}
else{ /* 在父进程中 */
int status = 0; /* 保持子进程退出状态 */
pid_t ret = waitpid(id, &status, 0); /* 让父进程等待id对应的子进程结束,成功则返回正值 */
if(ret > 0 && WIFEXITED(status)) {} /* WIFEXITED检查子进程是否正常退出 */
else{
perror("waitpid");
}
}
}
return 0;
}
Makefile
cc = gcc
filename = unix
src1 = $(filename).c
out = $(filename)
$(out): $(src1)
$(cc) -std=c99 -lpthread -o $(out) $(src1)
run:
./$(out)
clean:
rm -rf $(out)
如何使用
make # 编译
./unix # 运行可执行文件
> ls # unix外壳 查询⽬录
> echo hello # unix外壳 输出hello
> exit # unix外壳 退出unix外壳