计算机系统基础实训六-ShellLab实验

2024-06-23 08:44

本文主要是介绍计算机系统基础实训六-ShellLab实验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实验目的与要求

1、让学生更加理解进程控制的概念和具体操作方法;

2、让学生更加理解信号的概念和具体使用方法;

3、让学生更加理解Unix shell程序的原理和实现方法;

实验原理与内容

shell是一种交互式的命令行解释器,能代表用户运行程序。shell反复打印一个提示符等待stdin上的命令行,然后按照命令行的内容执行命令,如下图所示:

命令行是由空格分隔的ASCII字符串。命令行的第一个字符串要么是一个内置命令的名称,要么是一个可执行文件的路径名,剩下的字符串则为命令行参数。如果命令行的第一个字符串是一个内置命令,则shell会立即在当前进程中执行该命令。如果命令行的第一个字符串不是一个内置命令,shell会假定该字符串是一个可执行文件的路径名,在这种情况下shell会创建一个子进程并在子进程的上下文中加载和执行该可执行文件。每运行一个可执行文件将创建一个子进程,这些子进程组成shell的工作集,工作集中的各个子进程可以通过Unix管道进行连接。

如果命令行以符号“&”结尾,那么程序会在后台执行,这意味着shell不会等待程序的终止,shell会立即打印提示符并等待下一个命令输入。否则,程序会在前台执行,这意味着shell会等待程序终止后才能接收下一个命令行的输入。因此,在某一时刻系统中最多只能有一个前台任务,但是可以有任意数量的后台任务。

例如,输入命令行“jobs”会使得shell执行“jobs”这个内置命令。如果输入“/bin/ls -l -d”,则shell会在前台运行“/bin/ls”可执行文件,一般来说shell会保证程序会从可执行文件的main()函数开始执行,main()函数的声明如下所示:

int main(int argc, char *argv[])

在这个例子里参数argc和argv会有以下的值:

·argc == 3

·argv[0] == “/bin/ls”

·argv[1]== “-l”

·argv[2]== “-d”

如果输入命令“/bin/ls -l -d &”,则shell会在后台执行“/bin/ls”程序。

Unix shell支持任务控制的概念,它允许用户将任务在后台和前台之间来回切换,并更改任务中进程的状态(运行、停止或终止)。输入ctrl-c将导致一个SIGINT信号被传递到前台任务中的每个进程。这个SIGINT信号的默认操作是终止进程。类似地,输入ctrl-z会传递SIGTSTP信号给前台任务中的每个进程。SIGTSTP的默认操作是让一个进程处于停止状态,直到接收SIGCONT唤醒信号。Unix shell还提供各种支持任务控制的内置命令。例如:

• jobs: 列出正在运行或已经停止的后台任务.  

• bg <job>: 将一个停止的后台任务启动起来.

• fg <job>: 将一个正在执行的或已经停止的后台任务切换到前台并运行起来.  

• kill <job>:终止一个任务.

本实验的内容是编写一个简单的shell程序“tsh”,“tsh”要具有以下功能:

(1)命令行提示符字符串应为“tsh> ”。

(2)用户输入的命令行应由一个命令名称以及0个或多个参数所组成,命令名称和各个参数之间用一个或多个空格隔开。如果命令名称是一个内置命令,则tsh将在当前进程马上执行该命令然后才能接收下一个命令行输入。否则tsh应该假设该命令名称是一个可执行文件,并在子进程的上下文中加载和执行该可执行文件。

(3)tsh不需要有管道和IO重定向功能。

(4)输入ctrl-c(或ctrl-z)将发送一个SIGINT(或SIGTSTP)信号到当前前台任务以及该任务的任何子进程(它fork出来的任何子进程)。如果当前没有前台任务,则信号不应该产生任何作用。

(5)如果命令行是以“&”结尾的话,则命令要在后台执行,否则要在前台执行。

(6)tsh要给每一个任务分配一个正整数作为进程ID(PID)或任务ID(JID)。JID在命令行通过“%”进行引用,例如“%5”代表JID 5。而PID在命令行中直接通过数字引用,例如“5”代表PID 5。

(7)tsh要支持下列内置命令:

    quit:终止并退出tsh程序;

    jobs:列出所有后台任务;

    bg <job>:给<job>发送一个SIGCONT信号使其在后台继续运行起来,<job>参数可以是PID或JID。

    fg <job>:给<job>发送一个SIGCONT信号使其在前台继续运行起来,<job>参数可以是PID或JID。

(8)tsh要回收其所有的僵死子进程。如果任何进程因接收到没有在其进程中被捕获的信号而终止,那么tsh应该识别到这个事件,并打印一条带有进程PID和对该信号描述的信息。

实验设备与软件环境

1.Linux操作系统—64位 Ubuntu 18.04

2. C编译环境(gcc)

3. 计算机

实验过程与结果(可贴图)

1、BuiltinCmd()函数的实现:通过比较用户输入与预设的内置指令列表,决定是否直接在当前进程内执行对应功能。若匹配成功,则直接调用相应函数并返回1;否则,表明该指令非内置,返回0以进一步处理

2、Eval()函数解析逻辑

主要的思路:可以通过书上P525的eval函数写法和所需的功能来完成函数

  • 创建处理输入的数据的函数,调用parseline函数解析命令行
  • 使用builtin_cmd( )函数判断内置命令,如果是的话直接执行
  • 如果不是内置命令的话,就先阻塞信号,再调用fork创建子进程
  • 在子进程中,首先接触阻塞,设置id号,调用execve来执行job
  • 父进程判断作业是否后台运行,是的话调用addjob函数将子进程job加入job链表中,解除阻塞,调用waifg函数等待前台运行完成。不是的话后台工作则打印进程组jid和子进程pid以及命令行字符串。

在执行新程序之前,子程序必须确保解开对SIGCHLD信号的阻塞。父进程必须使用sigprocmask在它派生子进程之前也就是调用fork()函数之前阻塞SIGCHLD信号,之后解除阻塞;在通过调用addjob将子进程添加到作业列表之后,再次使用sigprocmask,解除阻塞。

自动测试tsh:

·按test01.txt测试,对比tshref的运行输出;

·按rtest01.txt测试,对比tshref的运行输出;

得出的结果一致

·trace03.txt测试tsh(对比tshref)命令及前台作业功能,正常:

3、sigchld_handler()函数:处理信号(课本P544)

函数原型:void sigchld_handler(int sig),参数为信号类型

一个进程可以通过调用 waitpid() 函数来等待它的子进程停止。如果回收成功,则返回为子进程的 PID, 如果 WNOHANG, 则返回为 0, 如果其他错误,则为 -1。

用while循环调用waitpid直到它所有的子进程停止。

3、sigchld_handler()函数:处理信号(课本P544)

函数原型:void sigchld_handler(int sig),参数为信号类型

一个进程可以通过调用 waitpid() 函数来等待它的子进程停止。如果回收成功,则返回为子进程的 PID, 如果 WNOHANG, 则返回为 0, 如果其他错误,则为 -1。

用while循环调用waitpid直到它所有的子进程停止。

4、sigint_handler()函数

调用函数fgpid返回前台进程pid

如果当前进程pid不为0,那么调用kill函数发送SIGINT信号给前台进程组

在2中调用kill函数如果返回值为-1表示进程不存在。输出error

5、waitfg()函数:等待一个前台作业结束,或者说是阻塞一个前台的进程直到这个进程变为后台进程

函数原型:void waitfg(pid_t pid) ,参数为进程ID

判断当前的前台的进程组pid是否和当前进程的pid是否相等,如果相等则sleep直到前台进程结束。

4、do_bgfg()函数:实现内置命令bg 和 fg

函数原型:void do_bgfg(char **argv),参数为argv 参数列表

首先通过getjobjid函数判断输入的pid/jid对应的进程是否存在;

无论是bg还是fg,都需要让其继续运行(不论为停止状态还是运行状态);

如果是bg,将作业状态设置为BG表示后台运行;

如果是fg,将作业状态设置为FG表示前台运行,并等待前台作业终止;

前3关做完了,后面的关卡看一下trace__的文件

第四关-后台与前台任务处理:当命令以"&"%"标识符结束时,表明任务需在后台异步执行,shell随即打印提示符准备接收新的命令。否则,任务在前台执行,即shell等待该任务完成才继续。此外,确保了前台仅单一任务,但可同时有多个后台任务并发。

第五关-处理jobs内置命令:分别运行了前台echo、后台myspin、前台echo、后台myspin,然后需要实现一个内置命令job,功能是显示目前任务列表中的所有任务的所有属性

第六关-信号处理机制:集成对SIGINT和SIGTSTP信号的响应,分别对应于用户按下Ctrl-C与Ctrl-Z。信号处理逻辑确保无前台任务时,信号不触发任何动作;反之,适配对任务发送信号并执行默认行为——终止或暂停。

第七关-仅将SIGINT转发给前台作业:第七关测试的是只将SIGINT转发给前台作业。这里的命令行其实根据前面的就很好理解了,就是给出两个作业,一个在前台工作,另一个在后台工作,接下来传递SIGINT指令,然后调用内置指令jobs来查看此时的工作信息,来对比出是不是只将SIGINT转发给前台作业。

第八关-仅将SIGTSTP转发到前台作业:需要将SIGTSTP转发给前台作业。根据这个信号的作用,也就是该进程会停止直到下一个SIGCONT也就是挂起,让别的程序继续运行。这里也就是运行了后台程序,然后使用jobs来打印出进程的信息。

第九关-进程bg内置命令:在第八关的测试文件之上的一个更加完整的测试,这里也就是在停止后,输出进程信息之后,使用bg命令来唤醒进程2,也就是刚才被挂起的程序,接下来继续使用Jobs命令来输出结果。

第十关-进程fg内置命令:将后台的进程更改为前台正在运行的程序。测试文中进程1根据&可以知道,进程1是一个后台进程。先使用fg命令将其转化为前台的一个程序,接下来停止进程1,然后打印出进程信息,这时候进程1应该是前台程序同时被挂起了,接下来使用fg命令使其继续运行,使用jobs来打印出进程信息

第十一关-将SIGINT转发给前台进程组中的每个进程:需要将SIGINT发给前台进程组中的每个进程。ps –a 显示所有进程,这里是有两个进程的,mysplit创建了一个子进程,接下来发送指令SIGINT,所以进程组中的所有进程都应该停止,接下来调用pl来查看该进程组中的每个进程是否都停止了。

第十二关-将SIGTSTP转发到前台进程组中的每个进程:该测试程序是为了测试将SIGTSTP转发给前台进程组中的每个进程。与上一关相同,只需要相应的进程被挂起即可。

第十三关-重新启动进程组中的每个已停止的进程:该程序是为了测试重新启动进程组中的每个停止的进程。这里也就是使用fg来唤醒整个工作,中间使用ps -a来查看停止整个工作和唤醒整个工作的区别。

第十四关-简单的错误处理:为了测试简单的错误处理。这里的测试文件,也就是测试fg和bg后面的参数,我们知道fg和bg后面需要一个JID或者是PID,其中JID是加上%的整型数。其余参数都应该报错,或是没有参数也应该报错。接下来测试的功能,都在上面的关卡测试过了

第十五关-结合在一起

第十六关-测试shell是否能够处理来自其他进程而不是终端的SIGTSTP和SIGINT信号:这个测试文件的具体含义就是,用户程序向job 2传送了中止信号,所以最后会输出进程2被中止的信息。同时,mystop需要自己停止才能给别的进程发送信号,所以中间也会出现进程1被中止的信息

实验总结

在本次Shell实验室实践中,我深入探索了构建小型Shell程序的核心机制,特别是在进程控制、信号处理及作业管理方面的应用。实验的关键环节如下:

    命令解析与执行逻辑定制:我首先开发了Builtin_Cmd()函数,该函数通过比对用户输入与内置命令列表,如quit、jobs、bg、fg,迅速执行对应操作或返回控制流。若非内置,则通过eval()函数处理。此函数负责解析用户输入,通过parseline()解析命令行,确定作业是前台(FG)还是后台(BG)执行。

    进程与作业管理:针对作业执行,我实现了后台(&)命令的逻辑,允许作业在后台独立运行而不阻塞shell。利用fork()创建子进程,并通过setpgid确保子进程拥有独立进程组,避免后台作业接收前台信号干扰。addjob()管理作业状态,记录进程ID与作业ID,PID与JID的映射。

    信号响应机制:我设计了信号处理器,如sigint_handler()与st_handler(),以响应SIGINT(Ctrl+C)和SIGTSTP(Ctrl+Z)信号。确保前台作业正确响应信号,后台作业不受影响。此外,sigchld_handler()确保僵死进程得以回收,通过waitpid()监控并清理。

    作业控制命令实现:do_bgfg()和do_fg()内置命令,分别用于将作业从前台切换到后台或恢复到前台。通过getjobjid()检索作业信息,无论作业状态如何,bg命令将作业设为后台,fg则等待作业前台完成。

    错误处理与测试:我进行了全面的测试,如通过trace文件模拟实际场景,如后台echo命令后跟&、jobs列出作业、以及fg与bg命令的交互,确保了作业控制逻辑的准确性。此外,我解决了子进程成为僵尸进程未回收的问题,确保了系统资源的有效管理。

    通过本实验,不仅增强了对Unix Shell程序构建原理的深入理解,而且提升了信号处理、进程控制、作业管理及异常处理等实用技巧。实验成果体现在能够编写出一个具备基本功能的简化Shell,能有效处理用户命令、管理作业,以及正确响应信号,展现了良好的系统交互能力。

所有代码

/* * tsh - A tiny shell program with job control* * <Put your name and login ID here>*/
#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 */
};
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) */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);   /* ctrl-c */Signal(SIGTSTP, sigtstp_handler);  /* ctrl-z */Signal(SIGCHLD, sigchld_handler);  /* Terminated or stopped child *//* This one provides a clean way to kill the shell */Signal(SIGQUIT, sigquit_handler); /* 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.  
*/
void eval(char *cmdline) 
{char* argv[MAXARGS];   //execve()函数的参数int state = UNDEF;  //工作状态,FG或BG sigset_t set;pid_t pid;  //进程id// 处理输入的数据if(parseline(cmdline, argv) == 1)  //解析命令行,返回给argv数组state = BG;elsestate = FG;if(argv[0] == NULL)  //命令行为空直接返回return;// 如果不是内置命令if(!builtin_cmd(argv)){if(sigemptyset(&set) < 0)unix_error("sigemptyset error");if(sigaddset(&set, SIGINT) < 0 || sigaddset(&set, SIGTSTP) < 0 || sigaddset(&set, SIGCHLD) < 0)unix_error("sigaddset error");//在它派生子进程之前阻塞SIGCHLD信号,防止竞争 if(sigprocmask(SIG_BLOCK, &set, NULL) < 0)unix_error("sigprocmask error");if((pid = fork()) < 0)  //fork创建子进程失败 unix_error("fork error");else if(pid == 0)  //fork创建子进程{// 子进程的控制流开始if(sigprocmask(SIG_UNBLOCK, &set, NULL) < 0)  //解除阻塞unix_error("sigprocmask error");if(setpgid(0, 0) < 0)  //设置子进程id unix_error("setpgid error");if(execve(argv[0], argv, environ) < 0){printf("%s: command not found\n", argv[0]);exit(0);}}// 将当前进程添加进job中,无论是前台进程还是后台进程addjob(jobs, pid, state, cmdline);// 恢复受阻塞的信号 SIGINT SIGTSTP SIGCHLDif(sigprocmask(SIG_UNBLOCK, &set, NULL) < 0)unix_error("sigprocmask error");// 判断子进程类型并做处理if(state == FG)waitfg(pid);  //前台作业等待elseprintf("[%d] (%d) %s", pid2jid(pid), pid, cmdline);  //将进程id映射到job id   }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 */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.  */
int builtin_cmd(char **argv) 
{if(!strcmp(argv[0], "quit"))  //如果命令是quit,退出exit(0);else if(!strcmp(argv[0], "bg") || !strcmp(argv[0], "fg"))  //如果是bg或者fg命令,执行do_fgbg函数 do_bgfg(argv);else if(!strcmp(argv[0], "jobs"))  //如果命令是jobs,列出正在运行和停止的后台作业listjobs(jobs);elsereturn 0;     /* not a builtin command */return 1;
}/* * do_bgfg - Execute the builtin bg and fg commands*/
void do_bgfg(char **argv)
{int num;struct job_t *job;// 没有参数的fg/bg应该被丢弃if(!argv[1]){  //命令行为空printf("%s command requires PID or %%jobid argument\n", argv[0]);return ;}// 检测fg/bg参数,其中%开头的数字是JobID,纯数字的是PIDif(argv[1][0] == '%'){  //解析jidif((num = strtol(&argv[1][1], NULL, 10)) <= 0){printf("%s: argument must be a PID or %%jobid\n",argv[0]);//失败,打印错误消息return;}if((job = getjobjid(jobs, num)) == NULL){printf("%%%d: No such job\n", num); //没找到对应的job return;}} else {if((num = strtol(argv[1], NULL, 10)) <= 0){printf("%s: argument must be a PID or %%jobid\n",argv[0]);//失败,打印错误消息return;}if((job = getjobpid(jobs, num)) == NULL){printf("(%d): No such process\n", num);  //没找到对应的进程 return;}}if(!strcmp(argv[0], "bg")){// bg会启动子进程,并将其放置于后台执行job->state = BG;  //设置状态 if(kill(-job->pid, SIGCONT) < 0)  //采用负数发送信号到进程组 unix_error("kill error");printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);} else if(!strcmp(argv[0], "fg")) {job->state = FG;  //设置状态 if(kill(-job->pid, SIGCONT) < 0)  //采用负数发送信号到进程组 unix_error("kill error");// 当一个进程被设置为前台执行时,当前tsh应该等待该子进程结束waitfg(job->pid);} else {puts("do_bgfg: Internal error");exit(0);}return;
}/* * waitfg - Block until process pid is no longer the foreground process*/
void waitfg(pid_t pid)
{struct job_t *job = getjobpid(jobs, pid);if(!job) return;// 如果当前子进程的状态没有发生改变,则tsh继续休眠while(job->state == FG)// 使用sleep的这段代码会比较慢,最好使用sigsuspendsleep(1);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.  */
void sigchld_handler(int sig)
{int status, jid;pid_t pid;struct job_t *job;if(verbose)puts("sigchld_handler: entering");/*以非阻塞方式等待所有子进程waitpid 参数3:1.     0     : 执行waitpid时, 只有在子进程 **终止** 时才会返回。2. WNOHANG   : 若子进程仍然在运行,则返回0 。注意只有设置了这个标志,waitpid才有可能返回03. WUNTRACED : 如果子进程由于传递信号而停止,则马上返回。只有设置了这个标志,waitpid返回时,其WIFSTOPPED(status)才有可能返回true*/while((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0){// 如果当前这个子进程的job已经删除了,则表示有错误发生if((job = getjobpid(jobs, pid)) == NULL){printf("Lost track of (%d)\n", pid);return;}jid = job->jid;//接下来判断三种状态 // 如果这个子进程收到了一个暂停信号(还没退出) if(WIFSTOPPED(status)){printf("Job [%d] (%d) stopped by signal %d\n", jid, job->pid, WSTOPSIG(status));job->state = ST;  //状态设为挂起 }// 如果子进程通过调用 exit 或者一个返回 (return) 正常终止else if(WIFEXITED(status)){if(deletejob(jobs, pid))if(verbose){printf("sigchld_handler: Job [%d] (%d) deleted\n", jid, pid);printf("sigchld_handler: Job [%d] (%d) terminates OK (status %d)\n", jid, pid, WEXITSTATUS(status));}}// 如果子进程是因为一个未被捕获的信号终止的,例如SIGKILLelse {if(deletejob(jobs, pid)){  //清除进程if(verbose)printf("sigchld_handler: Job [%d] (%d) deleted\n", jid, pid);}printf("Job [%d] (%d) terminated by signal %d\n", jid, pid, WTERMSIG(status));  //返回导致子进程终止的信号的数量}}if(verbose)puts("sigchld_handler: exiting");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)
{if(verbose)puts("sigint_handler: entering");pid_t pid = fgpid(jobs);if(pid){// 发送SIGINT给前台进程组里的所有进程// 需要注意的是,前台进程组内的进程除了当前前台进程以外,还包括前台进程的子进程。// 最多只能存在一个前台进程,但前台进程组内可以存在多个进程if(kill(-pid, SIGINT) < 0)unix_error("kill (sigint) error");if(verbose){printf("sigint_handler: Job (%d) killed\n", pid);}}if(verbose)puts("sigint_handler: exiting");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)
{if(verbose)puts("sigstp_handler: entering");pid_t pid = fgpid(jobs);struct job_t *job = getjobpid(jobs, pid);if(pid){if(kill(-pid, SIGTSTP) < 0)unix_error("kill (tstp) error");if(verbose){printf("sigstp_handler: Job [%d] (%d) stopped\n", job->jid, pid);}}if(verbose)puts("sigstp_handler: exiting");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);
}

这篇关于计算机系统基础实训六-ShellLab实验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086671

相关文章

可视化实训复习篇章

前言: 今天,我们来学习seaborn库可视化,当然,这个建立在Matplotlib的基础上,话不多说,进入今天的正题吧!当然,这个是《python数据分析与应用》书中,大家有需求的可以参考这本书。 知识点: Matplotlib中有两套接口分别是pyplot和pyylab,即绘图时候主要导入的是Matplotlib库下的两个子模块(两个py文件)matplotlib.pyplot和matp

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

ps基础入门

1.基础      1.1新建文件      1.2创建指定形状      1.4移动工具          1.41移动画布中的任意元素          1.42移动画布          1.43修改画布大小          1.44修改图像大小      1.5框选工具      1.6矩形工具      1.7图层          1.71图层颜色修改          1

操作系统实训复习笔记(1)

目录 Linux vi/vim编辑器(简单) (1)vi/vim基本用法。 (2)vi/vim基础操作。 进程基础操作(简单) (1)fork()函数。 写文件系统函数(中等) ​编辑 (1)C语言读取文件。 (2)C语言写入文件。 1、write()函数。  读文件系统函数(简单) (1)read()函数。 作者本人的操作系统实训复习笔记 Linux

[FPGA][基础模块]跨时钟域传播脉冲信号

clk_a 周期为10ns clk_b 周期为34ns 代码: module pulse(input clk_a,input clk_b,input signal_a,output reg signal_b);reg [4:0] signal_a_widen_maker = 0;reg signal_a_widen;always @(posedge clk_a)if(signal_a)

00 - React 基础

1. React 基础 安装react指令 可参考: 官网官网使用教程 如: npx create-react-app 项目名如:npx create-react-app react-redux-pro JSX JSX 是一种 JavaScript 的语法扩展,类似于 XML 或 HTML,允许我们在 JavaScript 代码中编写 HTML。 const element =

AI赋能天气:微软研究院发布首个大规模大气基础模型Aurora

编者按:气候变化日益加剧,高温、洪水、干旱,频率和强度不断增加的全球极端天气给整个人类社会都带来了难以估计的影响。这给现有的天气预测模型提出了更高的要求——这些模型要更准确地预测极端天气变化,为政府、企业和公众提供更可靠的信息,以便做出及时的准备和响应。为了应对这一挑战,微软研究院开发了首个大规模大气基础模型 Aurora,其超高的预测准确率、效率及计算速度,实现了目前最先进天气预测系统性能的显著

【软考】信息系统项目管理师(高项)备考笔记——信息系统项目管理基础

信息系统项目管理基础 日常笔记 项目的特点:临时性(一次性)、独特的产品、服务或成果、逐步完善、资源约束、目的性。 临时性是指每一个项目都有确定的开始和结束日期独特性,创造独特的可交付成果,如产品、服务或成果逐步完善意味着分步、连续的积累。例如,在项目早期,项目范围的说明是粗略的,随着项目团队对目标和可交付成果的理解更完整和深入时,项目的范围也就更具体和详细。 战略管理包括以下三个过程