我目前正在做麻省理工学院的开放课件操作系统课程(https://ocw.mit.edu/courses/6-828-operating-system-engineering-fall-2012/),我正在努力完成第一门课的作业,那就是实现你自己的小shell。
我的代码如下:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdint.h>
// Simplifed xv6 shell.
#define MAXARGS 10
// All commands have at least a type. Have looked at the type, the code
// typically casts the *cmd to some specific cmd type.
struct cmd {
int type; // ' ' (exec), | (pipe), '<' or '>' for redirection
};
struct execcmd {
int type; // ' '
char *argv[MAXARGS]; // arguments to the command to be exec-ed
};
struct redircmd {
int type; // < or >
struct cmd *cmd; // the command to be run (e.g., an execcmd)
char *file; // the input/output file
int mode; // the mode to open the file with
int fd; // the file descriptor number to use for the file
};
struct pipecmd {
int type; // |
struct cmd *left; // left side of pipe
struct cmd *right; // right side of pipe
};
int fork1(void); // Fork but exits on failure.
struct cmd *parsecmd(char*);
// Execute cmd. Never returns.
void
runcmd(struct cmd *cmd)
{
int p[2], r;
struct execcmd *ecmd;
struct pipecmd *pcmd;
struct redircmd *rcmd;
if(cmd == 0)
exit(0);
switch(cmd->type){
default:
fprintf(stderr, "unknown runcmd\n");
exit(-1);
case ' ':
ecmd = (struct execcmd*)cmd;
if(ecmd->argv[0] == 0)
exit(0);
if (strcmp(ecmd->argv[0], "ls") == 0) {
execvp("ls", ecmd->argv);
fprintf(stderr, "execvp ls failed\n");
}
if (strcmp(ecmd->argv[0], "echo") == 0) {
perror("This is from echo:\n");
execvp("echo", ecmd->argv);
fprintf(stderr, "execvp failed\n");
}
if (strcmp(ecmd->argv[0], "grep") == 0) {
//perror("This is from grep:\n");
execvp("grep", ecmd->argv);
fprintf(stderr, "execvp failed\n");
}
if (strcmp(ecmd->argv[0], "cat") == 0) {
//perror("This is from grep:\n");
execvp("cat", ecmd->argv);
fprintf(stderr, "execvp failed\n");
}
break;
case '>':
case '<':
rcmd = (struct redircmd*)cmd;
fprintf(stderr, "redir not implemented\n");
// Your code here ...
runcmd(rcmd->cmd);
break;
case '|':
pcmd = (struct pipecmd*)cmd;
struct execcmd *left = (struct execcmd*)pcmd->left;
struct execcmd *right = (struct execcmd*)pcmd->right;
int i;
int pd[2];
pipe(pd);
int pid1 = fork1();
int pid2 = fork1();
if(!pid1) {
// close(STDOUT_FILENO);
// close(pd[0]);
execvp(left->argv[0], left->argv);
dup2(pd[1], 1);
exit(0);
}
if(!pid2) {
// close(STDIN_FILENO);
// close(pd[1]);
dup2(pd[0], 0);
execvp(right->argv[0], right->argv);
exit(0);
}
if(pid1 > 0) {
int status2;
int status;
if(waitpid(pid1, &status, 0) == -1){
perror("waitpid1");
}
if(waitpid(pid2, &status2, 0) == -1) {
perror("waitpid2");
};
// close(pd[0]);
// close(pd[1]);
}
break;
}
}
int
getcmd(char *buf, int nbuf)
{
if (isatty(fileno(stdin)))
fprintf(stdout, "$ ");
memset(buf, 0, nbuf);
fgets(buf, nbuf, stdin);
if(buf[0] == 0) // EOF
return -1;
return 0;
}
int
main(void)
{
static char buf[100];
int fd, r;
// Read and run input commands.
while(getcmd(buf, sizeof(buf)) >= 0){
if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){
// Clumsy but will have to do for now.
// Chdir has no effect on the parent if run in the child.
buf[strlen(buf)-1] = 0; // chop \n
if(chdir(buf+3) < 0)
fprintf(stderr, "cannot cd %s\n", buf+3);
continue;
}
if(fork1() == 0)
runcmd(parsecmd(buf));
wait(&r);
}
exit(0);
}
int
fork1(void)
{
int pid;
pid = fork();
if(pid == -1)
perror("fork");
return pid;
}
struct cmd*
execcmd(void)
{
struct execcmd *cmd;
cmd = malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd));
cmd->type = ' ';
return (struct cmd*)cmd;
}
struct cmd*
redircmd(struct cmd *subcmd, char *file, int type)
{
struct redircmd *cmd;
cmd = malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd));
cmd->type = type;
cmd->cmd = subcmd;
cmd->file = file;
cmd->mode = (type == '<') ? O_RDONLY : O_WRONLY|O_CREAT|O_TRUNC;
cmd->fd = (type == '<') ? 0 : 1;
return (struct cmd*)cmd;
}
struct cmd*
pipecmd(struct cmd *left, struct cmd *right)
{
struct pipecmd *cmd;
cmd = malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd));
cmd->type = '|';
cmd->left = left;
cmd->right = right;
return (struct cmd*)cmd;
}
// Parsing
char whitespace[] = " \t\r\n\v";
char symbols[] = "<|>";
int
gettoken(char **ps, char *es, char **q, char **eq)
{
char *s;
int ret;
s = *ps;
while(s < es && strchr(whitespace, *s))
s++;
if(q)
*q = s;
ret = *s;
switch(*s){
case 0:
break;
case '|':
case '<':
s++;
break;
case '>':
s++;
break;
default:
ret = 'a';
while(s < es && !strchr(whitespace, *s) && !strchr(symbols, *s))
s++;
break;
}
if(eq)
*eq = s;
while(s < es && strchr(whitespace, *s))
s++;
*ps = s;
return ret;
}
int
peek(char **ps, char *es, char *toks)
{
char *s;
s = *ps;
while(s < es && strchr(whitespace, *s))
s++;
*ps = s;
return *s && strchr(toks, *s);
}
struct cmd *parseline(char**, char*);
struct cmd *parsepipe(char**, char*);
struct cmd *parseexec(char**, char*);
// make a copy of the characters in the input buffer, starting from s through es.
// null-terminate the copy to make it a string.
char
*mkcopy(char *s, char *es)
{
int n = es - s;
char *c = malloc(n+1);
assert(c);
strncpy(c, s, n);
c[n] = 0;
return c;
}
struct cmd*
parsecmd(char *s)
{
char *es;
struct cmd *cmd;
es = s + strlen(s);
cmd = parseline(&s, es);
peek(&s, es, "");
if(s != es){
fprintf(stderr, "leftovers: %s\n", s);
exit(-1);
}
return cmd;
}
struct cmd*
parseline(char **ps, char *es)
{
struct cmd *cmd;
cmd = parsepipe(ps, es);
return cmd;
}
struct cmd*
parsepipe(char **ps, char *es)
{
struct cmd *cmd;
cmd = parseexec(ps, es);
if(peek(ps, es, "|")){
gettoken(ps, es, 0, 0);
cmd = pipecmd(cmd, parsepipe(ps, es));
}
return cmd;
}
struct cmd*
parseredirs(struct cmd *cmd, char **ps, char *es)
{
int tok;
char *q, *eq;
while(peek(ps, es, "<>")){
tok = gettoken(ps, es, 0, 0);
if(gettoken(ps, es, &q, &eq) != 'a') {
fprintf(stderr, "missing file for redirection\n");
exit(-1);
}
switch(tok){
case '<':
cmd = redircmd(cmd, mkcopy(q, eq), '<');
break;
case '>':
cmd = redircmd(cmd, mkcopy(q, eq), '>');
break;
}
}
return cmd;
}
struct cmd*
parseexec(char **ps, char *es)
{
char *q, *eq;
int tok, argc;
struct execcmd *cmd;
struct cmd *ret;
ret = execcmd();
cmd = (struct execcmd*)ret;
argc = 0;
ret = parseredirs(ret, ps, es);
while(!peek(ps, es, "|")){
if((tok=gettoken(ps, es, &q, &eq)) == 0)
break;
if(tok != 'a') {
fprintf(stderr, "syntax error\n");
exit(-1);
}
cmd->argv[argc] = mkcopy(q, eq);
argc++;
if(argc >= MAXARGS) {
fprintf(stderr, "too many args\n");
exit(-1);
}
ret = parseredirs(ret, ps, es);
}
cmd->argv[argc] = 0;
return ret;
}
我正在努力解决的部分是管道实现:
case '|':
pcmd = (struct pipecmd*)cmd;
struct execcmd *left = (struct execcmd*)pcmd->left;
struct execcmd *right = (struct execcmd*)pcmd->right;
int i;
int pd[2];
pipe(pd);
int pid1 = fork1();
int pid2 = fork1();
if(!pid1) {
// close(STDOUT_FILENO);
// close(pd[0]);
execvp(left->argv[0], left->argv);
dup2(pd[1], 1);
exit(0);
}
if(!pid2) {
// close(STDIN_FILENO);
// close(pd[1]);
dup2(pd[0], 0);
execvp(right->argv[0], right->argv);
exit(0);
}
if(pid1 > 0) {
int status2;
int status;
if(waitpid(pid1, &status, 0) == -1){
perror("waitpid1");
}
if(waitpid(pid2, &status2, 0) == -1) {
perror("waitpid2");
};
// close(pd[0]);
// close(pd[1]);
}
break;
我正在尝试执行echo "hahaha" | grep "h"
,但没有得到任何输出。echo和grep都是独立工作的。
我检查了left->argv[0]
和right->argv[0]
是否工作正常。
有人知道发生了什么吗?
PS:我的实现只是为了让你可以执行一个管道。我意识到如果我想让它可以执行2个以上的管道命令,我需要一个循环
2条答案
按热度按时间8mmmxcuj1#
下面是对代码的一点修改,主要问题是:
要测试您的代码和下面的代码,您不应该在grep命令参数中使用引号。(这是一个单独的解析问题/挑战)
测试:
$echo哈哈哈哈|格雷普阿
重写:
祝你好运
kb5ga3dv2#
我只是在这里猜测,但我认为你的问题是一个僵局。
在原始代码和更新后的代码中,您都可以调用
这将使当前进程停止并等待由
pid
标识的进程完成。* 但是 * 如果该进程没有完成,则waitpid
将永远不会返回!例如,如果管道的第一部分填满了管道的写缓冲区,就会发生这种情况,这将导致所有对管道的写操作都被阻塞,进程将无法继续。由于没有人从管道的另一端阅读,这就是会发生的情况。
您需要等待 parent 进程中的两个子进程。这两个子进程本质上应该做相同的事情:
1.设置标准输出/输入(
dup2
调用)1.运行程序(
exec
调用)在这方面不需要再做什么,尤其是不需要做任何会无限期拖延或阻碍这一进程的事情。
父进程需要执行以下操作:
1.启动第一个子进程
1.启动第二个子进程
1.等待两个进程结束
记住顺序是非常重要的,如果你在第1步和第2步之间等待第一个子进程,那么你又会遇到死锁。