介绍

本实验,通过写一个简单的 Unix Shell 程序,来熟悉进程控制和信号的概念。

首先去官网下载 lab,将 lab 上传至服务器后,完成下面的步骤:

  1. 执行 tar xvf shlab-handout.tar 解压缩。
  2. 执行 make 编译并连接一些测试例程。
  3. tsh.c 中输入你的姓名。

tsh.c(tiny shell) 中已经提供了一个简单的 Unix Shell 的框架。完成 tsh.c 中剩余的空函数体。

关于 Unix Shell 的用处,在书中已经有详细介绍,这里就不过多说。我们要实现的 shell 名为 tiny shell,与 Unix Shell 一样,例如执行:

tsh> jobs

tiny shell 就会打印出所有的后台 job。

执行

tsh> /bin/ls -l -d

表示在前台执行 ls 程序,其中 ls 程序的入口,int main(int argc, char *argv[]) 的参数内容如下:

  1. argc == 3
  2. argv[0] == “/bin/ls”
  3. argv[1] == “-l”
  4. argv[2] == “-d”

值得注意的是,Unix Shell 支持 job control,job control 允许用户将 job 的执行,在前台与后台之间来回切换,还能改变一个 job 中进程的状态(running, stopped, or terminated)。

关于 tsh.c

我们实现的 tiny shell(tsh.c) 应该有如下功能:

  1. 提示符为 “tsh> “
  2. 输入命令应该包括 name 和 argument,以空格隔开。如果 name 是内置命令,tsh 必须立即处理。如果非内置命令,tsh 将 name 当做可执行文件的路径,将该文件加载进 job 中执行(ps:此处 job 也指 tsh 创建的子进程)
  3. tsh 不用支持管道( )和 I/O 重定位(< 和 >)
  4. 键入 ctrl-c (ctrl-z) 后,将会发送 SIGINT (SIGTSTP) 信号到当前的前台 job,以及这个 job 的子进程。如果没有前台 job,信号无效
  5. tsh 支持 & 符号
  6. 每个 job 使用 tsh 指定的 process ID 或者 job ID 来区分,”%5” 指 JID 为 5,”5” 指 PID 为 5
  7. tsh 支持一下内置命令:quit(退出 Unix shell);jobs(列出所有的后台 job);bg (向 job 发送 SIGCONT 信号,并后台重启);同理,fg 指前台重启
  8. tsh 应该回收所有的僵尸子进程。如果一个 job 接受到一个信号,但没有 catch 住,tsh 应该打印出 job 的 PID 和该信号的描述

测试工具

Reference solution

lab 提供了 tshref 来作为解题参考,也就是 tsh 完成后,同样的输入,在 tsh 和 tshref 中应该有相同的输出。

Shell driver

lab 提供的 sdriver.pl 将 tiny shell 运行为一个子进程,向它发送 trace file 中指定的命令和信号。

键入一下命令查看帮助。

unix> ./sdriver.pl -h
Usage: sdriver.pl [-hv] -t <trace> -s <shellprog> -a <args>
Options:
    -h Print this message
    -v Be more verbose
    -t <trace> Trace file
    -s <shell> Shell program to test
    -a <args> Shell arguments
    -g Generate output for autograder

例如,执行 ./sdriver.pl -t trace01.txt -s ./tsh -a "-p" 或者 make test01

为了方便,lab 中提供了 tshref.out,里面是执行所有 trace file 的标准输出答案。

思路提示

  1. 仔细阅读书中 Chapter 8 (Exceptional Control Flow)
  2. 利用好 trace file,从简单的 trace01.txt 开始
  3. 灵活运用 waitpid() 的 WUNTRACED 和 WNOHANG 参数
  4. 在实现相关 signal handler 时,使用 “-pid” 替换 “pid” 作为 kill() 的参数,将 SIGINT 和 SIGTSTP 信号发送给所有的前台进程。
  5. 注意在 waitfg()sigchld_handler() 之间的任务分配。建议在 waitfg() 中使用带有 sleep() 的无限循环;在 sigchld_handler() 中只执行对 waitfg() 一次的调用。虽然也有其他解决办法,比如在 waitfg()sigchld_handler() 都调用 waitpid(),但是在 sigchld_handler() 里执行回收更加简单。
  6. eval() 中,执行 fork() 前,父进程必须用 sigprocmask() 来阻塞 SIGCHLD 信号,然后解除阻塞,最后在调用 addjob() 前再次执行 sigprocmask()。因为子进程继承了父进程的阻塞向量,在需要的时候也应该解除 SIGCHLD 信号的阻塞。父进程在 sigchld_handler() 中执行回收时,由于阻塞了 SIGCHLD,也有效的避免了 race
  7. 当你在标准 Unix Shell 中运行 tiny shell 时,tiny shell 是运行在前台进程组的,如果 tiny shell 创建一个子进程,这个子进程默认也会成为前台进程组的一员。因为键入 ctrl-c 会发送 SIGINT 信号到前台进程组的所有进程,那么 tiny shell 以及它创建的子进程,都会收到 SIGINT 信号,这显然是不对的。解决办法就是:在 fork() 后,execve() 前,子进程调用 setpgid(0, 0),会将自己放进一个新的进程组,而不是存在于前台进程组。这样就保证了前台进程中始终只有一个 tiny shell。当你键入 ctrl-c,tiny shell 会 catch 到 SIGINT 信号,并将信号转发给相应的前台 job。

评估方案

总分 90 分,一共 16 个 trace 文件,每个 5 分;代码风格占 10 分,包括注释和检查系统调用的返回值。

代码

/* 
 * tsh - A tiny shell program with job control
 * 
 * WU 14011
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>

/* Misc manifest constants */
#define MAXLINE 1024   /* max line size */
#define MAXARGS 128    /* max args on a command line */
#define MAXJOBS 16     /* max jobs at any point in time */
#define MAXJID 1 << 16 /* max job ID */

/* Job states */
#define UNDEF 0 /* undefined */
#define FG 1    /* running in foreground */
#define BG 2    /* running in background */
#define ST 3    /* stopped */

/* 
 * Jobs states: FG (foreground), BG (background), ST (stopped)
 * Job state transitions and enabling actions:
 *     FG -> ST  : ctrl-z
 *     ST -> FG  : fg command
 *     ST -> BG  : bg command
 *     BG -> FG  : fg command
 * At most 1 job can be in the FG state.
 */

/* Global variables */
extern char **environ;   /* defined in libc */
char prompt[] = "tsh> "; /* command line prompt (DO NOT CHANGE) */
int verbose = 0;         /* if true, print additional output */
int nextjid = 1;         /* next job ID to allocate */
char sbuf[MAXLINE];      /* for composing sprintf messages */

struct job_t
{                          /* The job struct */
    pid_t pid;             /* job PID */
    int jid;               /* job ID [1, 2, ...] */
    int state;             /* UNDEF, BG, FG, or ST */
    char cmdline[MAXLINE]; /* command line */
};
/* TODO 当定义完下面的结构数组后,是否已经为结构数组分配了空间 ? */
struct job_t jobs[MAXJOBS]; /* The job list */
/* End global variables */

/* Function prototypes */

/* Here are the functions that you will implement */
void eval(char *cmdline);
int builtin_cmd(char **argv);
void do_bgfg(char **argv);
void waitfg(pid_t pid);

void sigchld_handler(int sig);
void sigtstp_handler(int sig);
void sigint_handler(int sig);

/* Here are helper routines that we've provided for you */
int parseline(const char *cmdline, char **argv);
void sigquit_handler(int sig);

void clearjob(struct job_t *job);
void initjobs(struct job_t *jobs);
int maxjid(struct job_t *jobs);
int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline);
int deletejob(struct job_t *jobs, pid_t pid);
pid_t fgpid(struct job_t *jobs);
struct job_t *getjobpid(struct job_t *jobs, pid_t pid);
struct job_t *getjobjid(struct job_t *jobs, int jid);
int pid2jid(pid_t pid);
void listjobs(struct job_t *jobs);

void usage(void);
void unix_error(char *msg);
void app_error(char *msg);
typedef void handler_t(int);
handler_t *Signal(int signum, handler_t *handler);

/*
 * main - The shell's main routine 
 */
int main(int argc, char **argv)
{
    char c;
    char cmdline[MAXLINE];
    int emit_prompt = 1; /* emit prompt (default) */

    /* Redirect stderr to stdout (so that driver will get all output
     * on the pipe connected to stdout) */
    // TODO dup2() 的使用
    dup2(1, 2);

    /* Parse the command line */
    while ((c = getopt(argc, argv, "hvp")) != EOF)
    {
        switch (c)
        {
        case 'h': /* print help message */
            usage();
            break;
        case 'v': /* emit additional diagnostic info */
            verbose = 1;
            break;
        case 'p':            /* don't print a prompt */
            emit_prompt = 0; /* handy for automatic testing */
            break;
        default:
            usage();
        }
    }

    /* Install the signal handlers */

    /* These are the ones you will need to implement */
    Signal(SIGINT, sigint_handler);   /* SIGINT   2   Term    Interrupt from keyboard -> ctrl-c*/
    Signal(SIGTSTP, sigtstp_handler); /* SIGTSTP  20  Stop    Stop typed at terminal -> ctrl-z*/
    Signal(SIGCHLD, sigchld_handler); /* SIGCHLD  17  Ign     Child stopped or terminated */

    /* This one provides a clean way to kill the shell */
    Signal(SIGQUIT, sigquit_handler); /* SIGQUIT  3   Core    Quit from keyboard */

    /* Initialize the job list */
    initjobs(jobs);

    /* Execute the shell's read/eval loop */
    while (1)
    {

        /* Read command line */
        if (emit_prompt)
        {
            printf("%s", prompt);
            fflush(stdout);
        }
        if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin))
            app_error("fgets error");
        if (feof(stdin))
        { /* End of file (ctrl-d) */
            fflush(stdout);
            exit(0);
        }

        /* Evaluate the command line */
        eval(cmdline);
        fflush(stdout);
        fflush(stdout);
    }

    exit(0); /* control never reaches here */
}

/* 
 * eval - Evaluate the command line that the user has just typed in
 * 
 * If the user has requested a built-in command (quit, jobs, bg or fg)
 * then execute it immediately. Otherwise, fork a child process and
 * run the job in the context of the child. If the job is running in
 * the foreground, wait for it to terminate and then return.  Note:
 * each child process must have a unique process group ID so that our
 * background children don't receive SIGINT (SIGTSTP) from the kernel
 * when we type ctrl-c (ctrl-z) at the keyboard.  
 * 
 * 参考书本 p755
*/
void eval(char *cmdline)
{
    pid_t pid;
    int bg;
    char *argv[MAXARGS];
    char buf[MAXLINE];
    sigset_t set_one, set_all, oldset;

    sigaddset(&set_one, SIGCHLD);
    sigfillset(&set_all);

    strcpy(buf, cmdline);
    bg = parseline(buf, argv);

    if (argv[0] == NULL)
        return; /* Ignore empty lines */

    if (!builtin_cmd(argv))
    {
        /* fork() 前先阻塞 SIGCHLD 信号 */
        sigprocmask(SIG_BLOCK, &set_one, &oldset);
        if ((pid = fork()) == 0) /* child process */
        {
            /* 保证前台进程只有 tiny shell */
            setpgid(0, 0);
            /* 此时子进程与父进程阻塞信号集相同,应解除对 SIGCHLD 信号的解除 */
            sigprocmask(SIG_SETMASK, &oldset, NULL);
            /* 开始执行子进程 */
            if (execv(*argv[0], argv) < 0)
            {
                printf("%s: Command not found.\n", argv[0]);
                exit(0);
            }
        }

        if (!bg)
        {
            /* 如果 pid 以前台 job 执行 */
            waitfg(pid);

        }
    }

    /* 将子进程 addjob(),按照书上应该在 addjob() 前,应该对父进程阻塞所有的信号*/
    sigprocmask(SIG_BLOCK, &set_all, &oldset);
    // int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline)
    addjob(jobs, pid, *argv[argv.length - 1] == "&" ? BG : FG, cmdline);
    sigprocmask(SIG_SETMASK, &oldset, NULL);
    return;
}

/* 
 * parseline - Parse the command line and build the argv array.
 * 
 * Characters enclosed in single quotes are treated as a single
 * argument.  Return true if the user has requested a BG job, false if
 * the user has requested a FG job.  
 */
int parseline(const char *cmdline, char **argv)
{
    static char array[MAXLINE]; /* holds local copy of command line */
    char *buf = array;          /* ptr that traverses command line */
    char *delim;                /* points to first space delimiter */
    int argc;                   /* number of args */
    int bg;                     /* background job ? */

    strcpy(buf, cmdline);
    buf[strlen(buf) - 1] = ' ';   /* replace trailing '\n' with space  TODO why? */
    while (*buf && (*buf == ' ')) /* ignore leading spaces */
        buf++;

    /* Build the argv list */
    argc = 0;
    if (*buf == '\'')
    {
        buf++;
        delim = strchr(buf, '\'');
    }
    else
    {
        delim = strchr(buf, ' ');
    }

    while (delim)
    {

        argv[argc++] = buf;
        *delim = '\0';

        buf = delim + 1;
        while (*buf && (*buf == ' ')) /* ignore spaces */
            buf++;

        if (*buf == '\'')
        {
            buf++;
            delim = strchr(buf, '\'');
        }
        else
        {
            delim = strchr(buf, ' ');
        }
    }
    argv[argc] = NULL;

    if (argc == 0) /* ignore blank line */
        return 1;

    /* should the job run in the background? */
    if ((bg = (*argv[argc - 1] == '&')) != 0)
    {
        argv[--argc] = NULL;
    }
    return bg;
}

/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.
 * 
 * tiny shell 的内置命令:quit,jobs,bg <job>,fg <job>
 * 只有返回值为 0 时,是非内置命令
 * 参考书本 p755,有一个简单的实现
 */
int builtin_cmd(char **argv)
{
    /* The strcmp() function compares the two strings s1 and s2.  
     * It returns an integer less than, equal to, or greater than zero if s1 is found, 
     * respectively, to be less than, to match, or be greater than s2. 
     */
    if (!strcmp(argv[0], "quit"))
    {
        exit(0);
    }
    else if (!strcmp(argv[0], "jobs"))
    {
        /* 列出所有后台 job */
        for (i = 0; i < MAXJOBS; i++)
        {
            if (job[i].state == BG)
            {
                printf("job [%d] %d %s\n", jobs[i].jid, jobs[i].pid, jobs[i].cmdline);
            }
        }
        return 1;
    }
    else if (!strcmp(argv[0], "bg"))
    {
        /*  */
        kill(argv[1], SIGCONT);
        return 1;
    }
    else if (!strcmp(argv[0], "fg"))
    {
        kill(argv[1], SIGCONT);
        return 1;
    }

    return 0; /* not a builtin command */
}

/* 
 * do_bgfg - Execute the builtin bg and fg commands
 * bg/fg <job> (向 job 发送 SIGCONT 信号,并后台/前台重启)
 * restart 是指的 terminated 后 restart 还是 stop 后 restart
 * 通过查看 man 手册:SIGCONT   19,18,25    Cont    Continue if stopped
 * 得知是 stopped(ctrl-z) 接受 SIGCONT 信号 restart
 */
void do_bgfg(char **argv)
{
    job_t job;

    for (int i = 0; i < MAXJOBS; i++)
    {
        if (*argv[1] == jobs[i].jid || *argv[1] == jobs[i].pid)
        {
            job = jobs[i];
        }
    }
    kill(*argv[1], SIGCONT);
    /* TODO 不能只给一个标记吧,前台/后台 job 应该还有其他区别 */
    job.state = *argv[argv.length - 1] == "&" ? BG : FG;
    return;
}

/* 
 * waitfg - Block until process pid is no longer the foreground process
 * 
 * 调用此函数等待 pid 不再是 foreground process
 */
void waitfg(pid_t pid)
{
    while(1){
        sleep();
    }
    
    return;
}

/*****************
 * Signal handlers
 *****************/

/* 
 * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever
 *     a child job terminates (becomes a zombie), or stops because it
 *     received a SIGSTOP or SIGTSTP signal. The handler reaps all
 *     available zombie children, but doesn't wait for any other
 *     currently running children to terminate.  
 * 
 * 通过上面的注释可以得知,kernel 将 SIGCHLD 信号发送给 parent process,而不是 child process,这里的 parent process
 * 是标准 Unix Shell,还是我们实现的 tiny shell。
 * 
 * 而且一个 job stop 后也会接受到这个信号,但是由于 stop 后,还有可能通过 SIGCONT 信号 restart
 * 所以只有 terminated 后才会在这个 handler 中将 job 回收。
 * 
 * 即只回收已经成为 zombie 的 child process
 */
void sigchld_handler(int sig)
{
    int olderrno = errno;
    pid_t pid;
    sigset_t set_all, set_old;
    sigfillset(&set_all);
    /* 使用 WUNTRACED | WNOHANG 作为参数,为了避免 tiny shell 一直阻塞 */
    while ((pid = waitpid(-1, NULL, WUNTRACED | WNOHANG)) > 0)
    {
        sigprocmask(SIG_BLOCK, &set_all, &set_old);
        deletejob(pid);
        sigprocmask(SIG_SETMASK, &set_old, NULL);
    }

    errno = olderrno;
    return;
}

/* 
 * sigint_handler - The kernel sends a SIGINT to the shell whenver the
 *    user types ctrl-c at the keyboard.  Catch it and send it along
 *    to the foreground job.  
 */
void sigint_handler(int sig)
{
    /* 来自键盘的 ctrl-c 中断 SIGINT 信号,应该只发送到唯一的 foreground job */
    /* 由于我们是在标准 Unix Shell 下,执行的 tiny shell,所以 tiny shell 对于 Unix Shell 来说
     * 就是唯一的一个 foreground job,上面注释中指的 foreground job 是相对于 tiny shell 来说的,
     * 
     */
    for (int i = 0; i < MAXJOBS; i++)
    {
        if (jobs[i].state == FG)
        {
            kill(jobs[i].pid, SIGINT);
        }
    }

    return;
}

/*
 * sigtstp_handler - The kernel sends a SIGTSTP to the shell whenever
 *     the user types ctrl-z at the keyboard. Catch it and suspend the
 *     foreground job by sending it a SIGTSTP.  
 */
void sigtstp_handler(int sig)
{
    for (int i = 0; i < MAXJOBS; i++)
    {
        if (jobs[i].state == FG)
        {
            kill(jobs[i].pid, SIGTSTP);
        }
    }

    return;
}

/*********************
 * End signal handlers
 *********************/

/***********************************************
 * Helper routines that manipulate the job list
 **********************************************/

/* clearjob - Clear the entries in a job struct */
void clearjob(struct job_t *job)
{
    job->pid = 0;
    job->jid = 0;
    job->state = UNDEF;
    job->cmdline[0] = '\0';
}

/* initjobs - Initialize the job list */
void initjobs(struct job_t *jobs)
{
    int i;

    for (i = 0; i < MAXJOBS; i++)
        clearjob(&jobs[i]);
}

/* maxjid - Returns largest allocated job ID */
int maxjid(struct job_t *jobs)
{
    int i, max = 0;

    for (i = 0; i < MAXJOBS; i++)
        if (jobs[i].jid > max)
            max = jobs[i].jid;
    return max;
}

/* addjob - Add a job to the job list */
int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline)
{
    int i;

    if (pid < 1)
        return 0;

    for (i = 0; i < MAXJOBS; i++)
    {
        if (jobs[i].pid == 0)
        {
            jobs[i].pid = pid;
            jobs[i].state = state;
            jobs[i].jid = nextjid++;
            if (nextjid > MAXJOBS)
                nextjid = 1;
            strcpy(jobs[i].cmdline, cmdline);
            if (verbose)
            {
                printf("Added job [%d] %d %s\n", jobs[i].jid, jobs[i].pid, jobs[i].cmdline);
            }
            return 1;
        }
    }
    printf("Tried to create too many jobs\n");
    return 0;
}

/* deletejob - Delete a job whose PID=pid from the job list */
int deletejob(struct job_t *jobs, pid_t pid)
{
    int i;

    if (pid < 1)
        return 0;

    for (i = 0; i < MAXJOBS; i++)
    {
        if (jobs[i].pid == pid)
        {
            clearjob(&jobs[i]);
            nextjid = maxjid(jobs) + 1;
            return 1;
        }
    }
    return 0;
}

/* fgpid - Return PID of current foreground job, 0 if no such job */
pid_t fgpid(struct job_t *jobs)
{
    int i;

    for (i = 0; i < MAXJOBS; i++)
        if (jobs[i].state == FG)
            return jobs[i].pid;
    return 0;
}

/* getjobpid  - Find a job (by PID) on the job list */
struct job_t *getjobpid(struct job_t *jobs, pid_t pid)
{
    int i;

    if (pid < 1)
        return NULL;
    for (i = 0; i < MAXJOBS; i++)
        if (jobs[i].pid == pid)
            return &jobs[i];
    return NULL;
}

/* getjobjid  - Find a job (by JID) on the job list */
struct job_t *getjobjid(struct job_t *jobs, int jid)
{
    int i;

    if (jid < 1)
        return NULL;
    for (i = 0; i < MAXJOBS; i++)
        if (jobs[i].jid == jid)
            return &jobs[i];
    return NULL;
}

/* pid2jid - Map process ID to job ID */
int pid2jid(pid_t pid)
{
    int i;

    if (pid < 1)
        return 0;
    for (i = 0; i < MAXJOBS; i++)
        if (jobs[i].pid == pid)
        {
            return jobs[i].jid;
        }
    return 0;
}

/* listjobs - Print the job list */
void listjobs(struct job_t *jobs)
{
    int i;

    for (i = 0; i < MAXJOBS; i++)
    {
        if (jobs[i].pid != 0)
        {
            printf("[%d] (%d) ", jobs[i].jid, jobs[i].pid);
            switch (jobs[i].state)
            {
            case BG:
                printf("Running ");
                break;
            case FG:
                printf("Foreground ");
                break;
            case ST:
                printf("Stopped ");
                break;
            default:
                printf("listjobs: Internal error: job[%d].state=%d ",
                       i, jobs[i].state);
            }
            printf("%s", jobs[i].cmdline);
        }
    }
}
/******************************
 * end job list helper routines
 ******************************/

/***********************
 * Other helper routines
 ***********************/

/*
 * usage - print a help message
 */
void usage(void)
{
    printf("Usage: shell [-hvp]\n");
    printf("   -h   print this message\n");
    printf("   -v   print additional diagnostic information\n");
    printf("   -p   do not emit a command prompt\n");
    exit(1);
}

/*
 * unix_error - unix-style error routine
 */
void unix_error(char *msg)
{
    fprintf(stdout, "%s: %s\n", msg, strerror(errno));
    exit(1);
}

/*
 * app_error - application-style error routine
 */
void app_error(char *msg)
{
    fprintf(stdout, "%s\n", msg);
    exit(1);
}

/*
 * Signal - wrapper for the sigaction function
 */
handler_t *Signal(int signum, handler_t *handler)
{
    struct sigaction action, old_action;

    action.sa_handler = handler;
    sigemptyset(&action.sa_mask); /* block sigs of type being handled */
    action.sa_flags = SA_RESTART; /* restart syscalls if possible */

    if (sigaction(signum, &action, &old_action) < 0)
        unix_error("Signal error");
    return (old_action.sa_handler);
}

/*
 * sigquit_handler - The driver program can gracefully terminate the
 *    child shell by sending it a SIGQUIT signal.
 */
void sigquit_handler(int sig)
{
    printf("Terminating after receipt of SIGQUIT signal\n");
    exit(1);
}