shell中的后台运行如何实现

hhjing001 2016-09-12 02:13:48
原理如下图。代码是MIT 6.828 上看到的。理解了一些,但不知道怎么才能识别“&”进行后台运行。。求大神。。

#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 <dirent.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 setup_redirection(struct redircmd*);
void dup2_wrapped(int, int);
char *search_path(char*);

void
setup_redirection(struct redircmd *cmd) {
int redirection_fd = 0;
if (cmd->type == '>') {
redirection_fd = open(cmd->file, cmd->mode, S_IRWXU);
} else {
redirection_fd = open(cmd->file, cmd->mode);
}
if (redirection_fd < 0) {
fprintf(stderr, "failed to open file for redirection\n");
}
dup2_wrapped(redirection_fd, cmd->fd);
}

void
dup2_wrapped(int old_fd, int new_fd) {
int result = dup2(old_fd, new_fd);
if (result < 0) {
fprintf(stderr, "failed to dup file descrition\n");
}
}

char *
search_path(char *exe) {
DIR *d;
struct dirent *dir;
char *paths = getenv("PATH");
char *path_dir = strtok(paths, ";");
while (path_dir !=NULL) {
d = opendir(path_dir);
if (d == NULL) {
fprintf(stderr, "cannot open dir");
} else {
while((dir = readdir(d)) != NULL) {
if (strcmp(dir->d_name, exe) == 0){
char *final_path = malloc(strlen(path_dir) + strlen(exe) + 2);
final_path = strcat(final_path, path_dir);
final_path = strcat(final_path, "/");
final_path = strcat(final_path, exe);
return final_path;
}
}
}
}
return exe;
}

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);
execv(search_path(ecmd->argv[0]), ecmd->argv);
fprintf(stderr, "exec not implemented\n");
break;

case '>':
case '<':
rcmd = (struct redircmd*)cmd;
fprintf(stderr, "redir not implemented\n");
setup_redirection(rcmd);
runcmd(rcmd->cmd);
break;

case '|':
pcmd = (struct pipecmd*)cmd;
int result = pipe(p);
if (result == -1) {
fprintf(stderr, "pipe not implemented\n");
}
if (fork1() == 0) {
close(p[0]);
dup2_wrapped(p[1], STDIN_FILENO);
close(p[1]);
runcmd(pcmd->left);
}
if (fork1() == 0) {
close(p[1]);
dup2_wrapped(p[0], STDIN_FILENO);
close(p[0]);
runcmd(pcmd->right);
}
close(p[0]);
close(p[1]);
wait(&r);
wait(&r);
exit(0);
break;
}
exit(1);
}


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
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;
}

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;
}
else if(buf[0] == 'q' && buf[1] == 'u' &&\
buf[2] == 'i' && buf[3] == 't'){
printf("good bye :)\n");
return 0;
}
if(fork1() == 0)
runcmd(parsecmd(buf));
wait(&r);
}
exit(0);
}
...全文
726 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧