” CS350语言程序 写作、 辅导ProgrammingLab 3: My ShellCS350: Systems ProgrammingComputer Science, Emory UniversitySpring 2021mysh programa command line interpreter with input/output redirection Run command(s) in the background Redirect standard input stream from a file Redirect standard output to a file Redirect standard output to a file; append if file exists| Pipe standard Output of a command to standard of anotherRunning in the Background By default, shell runs command(s) in foreground does not prompt and fetch next command line until current command exits waits for child process(es) at end of Command line means run command(s) in background immediately print prompt and fetch next command line does not wait for child process(es)$ cmd arg1 arg2$ cmd2 arg1 no : run in foreground: run in backgroundFile I/O Redirection By default STDIN/STDOUT comes from/goes to the terminal redirect STDIN of current command from succeeding file redirect STDOUT of current command to succeeding file Create if file does not exist. Error if file exists. redirect STDOUT of current command to succeeding file Create if file does not exist. Append to file if exists.$ cmd f1$ cmd2 f1 f2 $ cmd3 redirect cmd1s stdin from f1redirect cmd2s stdin from f1 and stdout to f2 (with append); run in backgrounderror: no file specified for output redirectionPipe I/O Redirection By default STDIN/STDOUT comes from/goes to the terminal I redirect STDOUT of preceding command to pipe AND redirect STDIN ofsucceeding command from pipe$ cmd1 | cmd2 f1$ cmd1 f1 | cmd2 | cmd3 $ cmd1 f1 | cmd$ cmd1 | f2redirect cmd1s stdout to pipe;redirect cmd2s stdin from pipe;redirect cmd2s stdout to file f2redirect cmd1s stdout to 1st pipe;redirect cmd2s stdin from 1st pipe and stdout to 2nd pipe;redirect cmd3s stdin from 2nd pipe;run cmd1, cmd2 and cmd3 in backgrounderror: redirecting cmd1s stdout twice (ambiguous redirection)error: no command succeeding pipe (ambiguous redirection)Basic Shell Processingwhile(1) {commands = getCmd();foreach command in commands:create succeeding pipe for I/O*create child processopen files for I/O*redirect stdin from file or preceding pipe*redirect Stdout to file or succeeding*execute command in childtrack child pid in parentparent waits for foreground children*} *as necessaryRequirements Command lines are restricted to 1024 characters or less. You may use wait() and wait3(), but not waitpid(), wait4() nor any other variant. When executed in the foreground, mysh waits for all processes in a pipe to complete. Transient zombies may exist, but mysh should periodically clean them up Close unneeded file descriptors: When child calls exec(), only 0, 1, 2 should be open Handle all error cases, including operator misuse and missing commands on the command line. If a command line is malformed (i.e. syntactically incorrect), no part of it should be executed.(You do not need to check whether commands are valid or executable.)Tips You may use the provided tokens.[c,h] files to tokenize your command lines. Use fgets() to read input lines. Check fgets() and wait() for premature returns due to system interruption if fgets() or wait() fails and errno == EINTR, try the call again! Always check execvp() return value for failure In execvp()s argument vector, first element is the command and last element is NULL. Track foreground process pids to ensure all terminate before fetching next command line. Kill all stray processes left around after quitting mysh Parse Command line into composite commands. FWIW: I created a CmdSet struct and a Cmd struct: CmdSet contains an array of Cmds, one for each command,and foreground/background flag Cmd Struct contains the commands argument vector, input filename (if redirected) and output filename(if redirected) Consider parsing the command line completely before executing any part of it. Consider a background process terminating while a foreground process running: make suremysh doesnt fetch a new Command line before a foreground process terminates.请加QQ:99515681 或邮箱:99515681@qq.com WX:codehelp
“
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。