CS3103编程 写作、Operating Systems程序

” CS3103编程 写作、Operating Systems程序CS3103: Operating SystemsSpring 2021Programming Assignment 11 IntroductionThe purpose of this assignment is to help you better understand process management and system calls in Linuxenvironment. To achieve this goal, you will use C/C++ to implement a mini shell that supports runningprograms not only in the foreground but also in the background: if a program is designated to run in theforeground, then your shell should load and run the program, and wait for the program to terminate beforeavailable for the next command line; in contrast, the shell does not wait for the program to terminate beforeawaiting the next command line if the program runs in the background.2 Requirements2.1 Prompt for User InputYour mini shell Should display the prompt for user input. For example, if running your mini shell in the Linuxterminal,$ . / s h e l lit should promptsh indicating being ready for user input.2.2 Foreground Execution of a ProgramYour mini shell should allow a program to run in the foreground, which has the following form:sh fg [ path o f e x e c u t a bl e f i l e ] [ a l i s t o f arguments ]For example, if you want to run the test program mentioned in Section 4 in the foreground, you can type:sh fg . / test h e l l o 2 5where ./test is the path of the test program, and hello 2 5 are the arguments. When running a process inthe foreground, the shell is unable to accept the next command from the user until the foreground processterminates. At any time, at most ONE process can run in the foreground.2.3 Background Execution of ProgramsThe shell should also allow programs to run in the background:sh bg [ path o f e x e c u t a bl e f i l e ] [ a l i s t o f arguments ]Unlike the fg Command, the shell is able to accept the next command immediately after executing the bgcommand. Below is an example:sh bg . / test h e l l o 2 5sh Also note that the shell should allow ANY number of processes to exist in the background simultaneously.12.4 Completion of ProgramsWhen a Foreground process or a background process completes and exits normally, your shell should display amessage. For example:sh fg . / test h e l l o 2 5h e l l oh e l l oh e l l oh e l l oh e l l oP ro c e s s 4096 completedsh 2.5 Ctrl-C and Ctrl-ZWhen your mini shell program is running in the Linux terminal, it will receive a SIGINT (SIGTSTP) signal iftyping Ctrl-C (Ctrl-Z) on the keyboard. Your shell program is expected to handle the signal and terminate(stop) the foreground process. If there is no foreground process running in the shell, then the signal shouldhave no effect.For each process that is terminated (stopped) by Ctrl-C (Ctrl-Z), the shell should display the correspondingmessage. For example, if a process with PID = 4096 is terminated by Ctrl-C, then the shell is expected todisplay the following message:sh P ro c e s s 4096 t e rmina t e dSimilarly, if a process with PID = 4096 is stopped by Ctrl-Z, then the shell is expected to display the followinginformation:sh P ro c e s s 4096 s topped2.6 List All ProcessesThe list command should list the information of all processes that are running in the background as well as allthe stopped processes. The information should include PID, state, path, and arguments. Note that terminatedprocesses are NOT Supposed to appear in the list. Therefore, you need to reap the process if it completes andexits normally, or it is terminated by Ctrl-C. Otherwise, the list command may behave unexpectedly. Here isan example with correct behavior:sh bg . / test t e s t 1 2 5sh bg . / test t e s t 2 5 10sh l i s t4096: running . / test t e s t 1 2 54097: running . / test t e s t 2 5 10sh fg . / test t e s t 3 2 5P ro c e s s 4098 te rmi na t e dsh fg . / test t e s t 4 5 10P ro c e s s 4099 s toppedsh l i s t4096: running . / test t e s t 1 2 54097: running . / test t e s t 2 5 104099: s topped . / test t e s t 4 5 10sh 2.7 Exit the ShellThe exit command should cause the shell program to exit. But before that, make sure that all processes havebeen terminated and reaped. Here is an example where there are two processes in the background beforeexecuting exit:2sh ex itP ro c e s s 4096 te rmi na t e dP ro c e s s 4097 te rmi na t e d$2.8 Handling Unexpected InputsYou can never Make any assumption about what users will input in your shell. For example, a user may inputan empty line, an invalid command, or a wrong path of the executable file. You are encouraged to come upwith these unexpected inputs as many as possible and handle them properly.3 Hints Study the man pages of the system calls used in your program. For example, the following commanddisplays the man pages of kill():$ man 2 k i l l Use fork() and execvp() so that the parent process accepts user input and the child process executesforeground/background processes. If a process is going to run in the foreground, use sigsuspend() or sleep() in a busy loop until theforeground process terminates. Some signals are useful in this assignment. For example, typing Ctrl-C will trigger a SIGINT signalautomatically, and typing Ctrl-Z will trigger a SIGTSTP signal automatically. Besides, sending aSIGTERM signal can terminate a process. Another signal that you might be interested in is theSIGCHLD sigal, which will be sent to the parent process by the kernel whenever a child process stops orterminates. Your program needs to handle these signals correctly. To handle signals, use signal() tobind a signal to the corresponding handler that you implement. By the way, kill() is a useful system callfor sending a signal (dont get confused by its name). When a process Terminates for any reason, the process becomes a zombie until it is reaped by itsparent. To reap a terminated child process (if any), use waitpid(-1, status, WNOHANG |WUNTRACED). This system call will return immediately, with a return value 0 if none of the childrenhas stopped or terminated, or with a return value equal to the PID of one of the stopped or terminatedchildren.When the parent reaps the terminated child, the kernel passes the childs exit status to theparent and then discards the terminated process, at which point it ceases to exist. Also note that the exitstatus may be useful when handling the SIGCHLD signal. When you run your shell from the standard Linux shell, your shell is running in the foreground processgroup of the standard Linux shell. If your shell then creates a child process, by default that child willalso be a member of the foreground process group. Since typing Ctrl-C sends a SIGINT to every processin the foreground group, typing Ctrl-C will send a SIGINT to your shell, as well as to every processthat your shell created. This is obviously not the desired behavior, since we hope only our shell canreceive this signal and Handle the signal properly. Here is the workaround: After the fork(), but beforethe execvp(), the child process should call setpgid(0, 0), which puts the child in a new process groupwhose group ID is identical to the childs PID. This ensures that there will be only one process, yourshell, in the foreground process group of the standard Linux shell. When you type Ctrl-C, your own shellshould catch the resulting SIGINT and handle it properly. When you use fork(), it is important that you do not create a fork bomb, which easily eats up all theresources allocated to you. If this happens, you can try to use the command kill to terminate yourprocesses ( https://cslab.cs.cityu.edu.hk/supports/unix-startup-guide). However, if you cannotlog into your account any more, you need to ask CSLab for help to kill your processes.34 Helper Programs4.1 args.cppThis example program shows how to read a line from terminal, as well as parsing (cutting) the string using thestrtok() function. To compile the program, use the following command:$ g++ a rg s . cpp l r e a d l i n e o a rg s4.2 test.cppThis program can be used to test your shell. It takes three arguments: the first argument is a single word to bedisplayed repeatedly; the second argument is the number of seconds between two consecutive displays of theword; the last argument is the Number of times the word to be displayed. For example, the following commanddisplays the word running 5 times in 2-second interval:$ . / test running 2 55 MarkingImportant Note: You program will be tested on our CSLab Linux servers (cs3103-01, cs3103-02, cs3103-03).You should describe clearly how to compile and run your program in the text file. If an executable filecannot be generated and running successfully on our Linux servers, it will be considered asunsuccessful. fg: 20% bg: 20% Ctrl-C: 10% Ctrl-Z: 10% list: 10% exit: 10% Reaping terminated processes correctly: 10% Handling unexpected inputs: 5% Programming style and in-program comments: 5%6 Submission This assignment is to be done individually or by a group of two students. You are encouraged to discussthe high-level design of your solution with your classmates but you must implement the program onyour own. Academic dishonesty such as copying another students work or allowing another student tocopy your work, is regarded as a serious academic offence. Each submission consists of two files: a source program file (.cpp file) and a text file containing userguide, if necessary, and all Possible outputs produced by your program (.txt file). Write down your name(s), eid(s) and student ID(s) in the first few lines of your program as comment. Use your student ID(s) to name your submitted files, such as 5xxxxxxx.cpp, 5xxxxxxx.txt for individualsubmission, or 5xxxxxxx_5yyyyyyy.cpp, 5xxxxxxx_5yyyyyyyy.txt for group submission. Only ONEsubmission is required for each group. Submit the files to Canvas. The deadline is 11:00am, 18-FEB-2021 (Thursday). No late submission will be accepted.47 Questions? This is not a programming Course. You are encouraged to debug the program on your own first. If you have any questions, please submit your questions to Mr ZHOU Zikang via the Discussion boardProgramming Assignment #1. To avoid possible plagiarism, do not post your Source code on the Discussion board. If necessary, you may also contact Mr ZHOU Zikang at zikanzhou2-c@my.cityu.edu.hk.如有需要,请加QQ:99515681 或WX:codehelp

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导