代做shell | oop代做 | assignment代写 – CMPT 300

CMPT 300

代做shell | oop代做 | assignment代写 – 这是一个关于oop的题目, 主要考察了关于oop的内容,是一个比较经典的题目, 涵盖了shell/oop等程序代做方面, 这个项目是assignment代写的代写题目

shell代写代写 代写shell

assignment 2

Our new shell – cshell

Marks: 80 marks (plus 20 bonus marks)

Notes:

1. Failure to follow the instructions and rules may lead to failed test cases and/or a final grade of 0.

  1. Youcandothisassignmentindividuallyorinateamoftwo.Ifyouaredoingitinagroup, only one submission per group is required. For example:
IfPersonAandBareinonegroup,onlyonepersonhastoaccepttheassignmentlinkand
do the submission.
Person A: upload all files (.txt, .c), group.txt.
Person B: no submission required.
Forgroupassignment,submita" group.txt "indicatingtheGithubidsofeachmember. For
example: if I am working with John in a group then group.txt will be as below:
hazra.imran
Johndoe
  1. Youmaysubmitmultiple timesuntilthedeadline.Gradepenaltieswillbe imposed for late submissions (see the course outline for the details).
  2. Always plan before coding.
  3. AllthecodesinthislabmustbedoneusingClanguageonly.Nootherlanguagesshouldbe used.
  4. Use function-level and inline comments throughout your code. We will not be specificallygrading documentation.However, rememberthatyouwillnotbeableto commentonyourcodeunlesssufficientlydocumented. Takethetimetodocumentyour code as you develop it properly.
  5. Wewillcarefullyanalyzethecodesubmittedtolookforplagiarismsigns,so pleasedonotdoit!Ifyouareunsureaboutwhatisallowed,pleasetalktoan instructor or a TA.

Coding Rules

 You have to follow the file name as specified in the instructions. 
 Makefile : Makefile provides the following functionality:
 all : compiles your program (this is the default behavior), producingan
executable file named the same as the C file.
 clean : deletes the executable file and any intermediatefiles (.o, specifically)
 You will receive 0 if your makefile fails. 
o Check your build to ensure that there are no errors. 
o Visit TA's programming office hours to get help.

Assignment Goals

To understand the relationship between OS command interpreters (shells), system calls, and the kernel. To design and implement an extremely simple shell and system call (Bonus).

Forthisassignment,youshouldunderstandtheconceptsofenvironmentvariables,system calls, standard input and output, I/O redirection, parent and child processes,current directory, pipes, jobs, foreground and background, signals, and end-of-file.

Background OScommandinterpreteristheprogram thatpeopleinteractwithtolaunchandcontrol programs.OnUNIXsystems,thecommandinterpreterisusuallycalledthe shell .Itisa user-levelprogramthatgivespeopleacommand-lineinterfacetolaunch,suspend,orkill otherprograms.sh,ksh,csh,tcsh,bash,… areallexamplesofUNIXshells.(Itmightbe usefultolookatthemanualpagesoftheseshells,forexample,type:"mancsh").Although mostofthecommandspeopletypeonthepromptarethenameofotherUNIXprograms (suchaslsormore),shellsrecognizesomespecialcommands(calledinternalcommands) whicharenotprogramnames.Forexample,theexitcommandterminatestheshell,andthe cdcommandchanges thecurrentworking directory.Shellsdirectlymakesystemcallsto execute these commands instead of forking a child process to handle them.

Inthisassignment,youwilldevelopa simpleshell .Theshellacceptsusercommandsand thenexecuteseachcommandseparately.Theshellprovidestheuserwithaprompttoenter thenextCommand.Onetechniqueforimplementingashellinterfaceistohavetheparent processfirstreadwhattheuserentersonthecommandlineandthencreateaseparatechild processthatperformstheCommand.Unlessotherwisespecified,theparentprocesswaits forthechildtoexitbeforecontinuing.However,UNIXshellstypicallyalsoallowthechild processtoruninthebackground-orconcurrently-aswellbyspecifyingtheampersand(&) attheendoftheCommand.Theseparatechildprocessiscreatedusingthefork()system call,andtheuser’sCommandisexecutedbyusingoneofthesystemscallsintheexec() family.

We can summarize the structure of the shell as follows

  1. print out a prompt
  2. read a line of input from the user
  3. parse the line into the program name, and an array of parameters
  4. use the fork() system call to spawn a new child process the child process then uses the exec() system call to launch the specified program the parent process (the shell) uses the wait() system call to wait for the child to terminate
  5. when the child (i.e. the launched program) finishes, the shell repeats the l oop by jumping to 1.

Task 1: [80 marks] Build a new shell (cshell)

Youwilldevelopacommand-lineinterpreterorshellsupporting theenvironmentvariablesand history of executed commands. Let us call it cshell. The cshell will s upport basic shell functionalities.

cshell recognizes the following lines: It recognizes lines of the form $= It recognizes lines of the form , where is a name of built-in command.

cshell will also support the following built-in commands: exit, the shell terminates on this Command. This mustbe implemented for a clean exit of the program. log, the shell prints history of executed commandswith time and return code print, the shell prints argument given to this Command theme, the shell changes the color of and output

When cshell takesa non-built-incommand (likels,pwd,whoami),itisexecutedinthechild process,andCommand’soutputisprinted. cshell createsachildprocessusing fork() system call,then cshell waitsforthechildprocesstoterminatevia wait() systemcall.Childprocess executes a non-built-in command using exec() and its analogues.

Youhavetocreateapipefromtheparentprocesstothechild,using pipe() .Thenyoumust redirectthestandardoutput(STDOUT_FILENO)anderroroutput(STDERR_FILENO)using dup or dup2 tothepipe,andintheparentprocess,readfromthepipe.Thisisneededto control the output of commands.

So cshell should be able to parse the command nameand its arguments. Clearly, the hint is that the command name always goes first, and arguments are separated by space.

Two modes

Our cshell will work in two modes: interactive modeand script mode.

The interactive mode is activated when cshell startswithout command line arguments.

Interactive mode is essentially the following loop: Print out a prompt Read line Parse line, and if the line is non-valid, print an error message

If the line was valid, do what should be done.

Script mode For script mode, it should start like ./cshell

The script is a file containing a set of lines e.g. $VAR1= …..

…..

In script mode, the shell does the following foreach line of the file: Read the line of the file Parse line, if the line is non-valid, print an error message If the line was valid, do what should be done. The program must exit cleanly after executing in script mode.

You must submit myscript.txt showing your input forscript mode. The last input must be the log command. For example:

Note : For the above myscript.txt, the program mustexit cleanly after execution.

Environment variables The shell will support the inner environment variables. Each environment variable could be stored in a struct like

typedef struct {
char *name;
char *value;
} EnvVar;

When cshell takesalineoftheform$=,itshouldallocateanewEnvVar structwithname=andvalue=.Allenvironmentvariablescouldbestoredin somearray.EnvVar’snameshouldbeunique.Ifavariablealreadyexists,itsvalueshouldbe updated.

If some argument takes the form $, cshell shouldlook up stored environment variables, find the one with name= andsubstitute argument with environment variable’s value.Ifthecorrespondingvariabledoesnotexist,anerrormessagemustbe printed.

Parsingoflinesoftheform$=shouldbesimplegiventhatitstartswith$ symbol andvariable name andvalueseparatedby =sign.Parsingoflinesoftheform getsalittlemorecomplicated,shellshouldcheckif starts with $ symbol.

Built-in commands Wementionedearlierthattheshellwouldsupportfourbuilt-incommands exit,print,theme and log.

  1. The cshellmuststartwithcshell$(noothercharactersor welcomemessagesare allowed)
  1. The exit is simple. It just terminates the shell witha message Bye! Note.The outputshould be exactly same as shownbelow(e.g. Byemusthave exclamation sign, Bye should be case sensitive, no other messages, symbols allowed)
  2. The print commandtakesargumentsandjustprintsthem. Some examples:
  3. The log commandhistoryofexecutedcommandswithtimeandreturncode.Soshell should store for each executed command struct like typedef struct { char *name; struct tm time; return value; } Command; Note: struct tm defined in <time.h>
  4. So the log command prints an array of such structs.
  1. The theme commandtakesoneargument:anameofacolour.Andthen,theshell usingANSIescapecodeschangesthecolourofitsoutput.Cshellshouldsupport three colours (red, green and blue). For anyother color, cshellmust displaya message unsupported theme (Its case sensitive).

Hint on the steps to get started

 Read the manpages for fork(), execvp(), wait (), dup2(),open(), read(),fgets(),
write(), exit(), malloc(), realloc(), strtok(), strdup() system calls
 Write a loop to read a line and split it into tokens
 Write a function to parse tokens
 Come up with how to store environment variables and log information
 Write a function to handle built-in commands and to execute external files (execvp)
 Handle reading from file
 Be aware of memory leaks. You can manage time with <time.h> library.
 You cannot use any third-party libraries.

Sample output:

  1. Using the ./cshell in an interactive mode:
  1. Using the ./cshell script.txt in script mode:

Testcases:

Run the ./cshell in an interactive mode: Run the built-in commands and verify the output. The commands must include print, theme, log and exit. Assign values to any variables in your C program and print the updated values. Run non-built-in (pwd, ls) commands and verify the output.

Run the ./cshell in script mode: Place all the commands in a .txt file and run cshell bypassing this file The result should be the output of all the commands in the file. Place an error case in your file. For example, have a line as "failure," and the output should print missing Command.

Error outputs

BONUS QUESTION -This is optional.

Bonus :[20 marks] Add a new system call The task is to add a new system call to the kernel, which will perform the following actions: acceptsapointerfromtheuser’sapplicationtoastringofASCIIcharacterswithcodes 32-127 and the length of this string in bytes;

convertsstringcharactersintherange0x61-0x7A(a-z)touppercaseandreturns

the string.

Steps on how to add the system call:

  1. Make sure you have the source code of the Linux Kernel
  2. Create a folder with your C file and a Makefile in the root directory of the kernel sources
  3. Add the system call to the system call table
  4. Define the macros associated with each system call
 In arch/x86/include/asm/unistd_32.h:
  • add the definition for our new system call
  • incremented the value of the macro NR_SYSCALLS In arch/x86/include/asm/unistd_64.h add the macro definition In include/linux/syscalls.h add the prototype of the system call
  1. The kernel maintains a Makefile as well. Add your directory in the core-y field
  2. Compile the kernel

References:

  • https://arvindsraj.wordpress.com/2012/10/05/adding-hello-world-system-call-to-linux/
  • compiling the kernel: https://www.howopensource.com/2011/08/how-to-compile-and-install-linux-kernel-3-0-in -ubuntu-11-04-10-10-and-10-04/
  • Add your own system call to the Linux kernel

For Ubuntu 20.04 users: https://dev.to/jasper/adding-a-system-call-to-the-linux-kernel-5-8-1-in-ubuntu-20-04-lts-2g a

Hint on the steps to get started You should read how to recompile the kernel and try to do this Write system call login in regular function and test it Add a new system call

Now, integratethe systemcallintotheshell :Addanewcommand uppercase which prints a string modified by your new system call.

Testcase: Pass any random string to your program, and the result should print the value in an uppercase on the terminal.

Submission Instructions:

For A2, the concrete required deliverables are:

  • group.txt
  • Myscript.txt
  • cshell.c (with bonus question code if any)
  • .h files (if any)
  • Makefile
  • Bonus question – Also submit a snapshot showing your terminal’s output displaying the result of the implemented system call.

Grading Criteria

Rubric (Out of 80) Incorrect makefile: -80 (0 grade)

Correct makefile: +10 marks Interactive mode exit + Interactive mode log + 10 Interactive mode print + Interactive mode theme + Interactive mode – non-built-in command + Script mode + Bonus (optional) uppercase +

FAQ
  1. I am just about to start the assignment 2. Can i still work in csil?
Working in csil is fine for Task 1. But for the bonus question, you will need to setup
new syscall on your own virtual environment.