Chapter 5
Week 4: Creating a small shell interface
You must submit your work to the appropriate submission point in Gradescope, which will
be automatically marked. You should submit a single file called my_shell.c. Any other
files you submit will not be marked. Although you do not need to include any additional
supporting documentation or report, we do expect that your code is well written, tested and
commented.
Deadline: Week 6 of teaching. Thursday. 7th of Novem ber, 2024. 14:00. Extensions of up to 7 days are available.
Weighting: 40% of the final module mark.
In this coursework you will demonstrate:
• An understanding of how processes are created by the operating system.
• An understanding of file descriptors and their relationship to pipes and redirection.
• The ability to program components of an operating system.
Exercise
In this coursework you will implement a simple shell for the xv6 operating system. This
new shell will be implemented as a user space program. Before you attempt this coursework,
make sure you have gone through most of the formative assessment exercises in the preceding
weeks and convinced yourself that you know how various parts work. Where you have doubts,
read relevant parts again and redo the coursework, which will make you spot new things and
gain a deeper understanding of the material. You should provide your implementation in a
new file called my_shell.c. You may use any helper functions provided by the xv6 kernel or
user libraries. For each of the following items implement the feature into your shell, as you
progress the features to implement become harder. This exercise should not require you to
modify any file other than my_shell.c and the Makefile.
To start with clone the repository containing the starting code and copy my_shell.c
from it into your xv6 user/ directory:
29
gitclonehttps://github.com/mmikaitis/COMP2211−shell−template.gitModifytheMakefileaccordinglyandrebuildxv6.Itwillnotcompilebecausemyshell.cisnotfinalisedyet.However,italsocontainssomecommentsthatshouldhelpinfinishingtheintendedstructure.Yourtaskistofinishwritingmethodsgetcmd,runcommand,andmain,byinsertingcode代写COMP2211Creatingasmallshellinterfaceintheindicatedlocations.Noothermethodsshouldbedeveloped.Youareallowedtolookatadefaultxv6shellsourcecodeaswellaslearnaboutimplementingshellsusingexternalresources.However,youarerequiredtofollowtheuniquestructureoutlinedinthetemplateandarenotallowedtosupplyanycodewhichwasnotdevelopedsolelybyyourself,startingfromdesignstage.Ifyoudependhighlyonsomeonlinetutorialsthenyouneedtodeclarethesourcesinthecomments,whichincludeslargelanguagemodels.Ifyoudiscussearlyideaswithsomeoneinthelabyoushouldmakesurethatyoudon’tendupwithsimilarcodestructure;youshouldnotcodetogether.Gradescopewillrunasimilaritycheckofyoursubmissionandifthelogicofthenewcodeisreportedtobesimilartosomeoneelse’s,thesubmissionwillbecarefullycheckedmanuallyandreportedasacademicintegrityviolationifrequired.Seethiswebsiteforsomedetail.Thesimilaritycheckisresilienttochangingvariablesnamesoraddingcommentsandnewlines.Goingthroughacademicintegrityinterviewsisadauntingprocessandmayresultinseveredelaystoyourdegreeprogression.Itisbettertosubmitnothingthansubmitthecodethatwaspartiallydevelopedbyothers.Ifyouarebehind,speaktothelabdemonstratorsandthemoduleleadforguidanceonbestwaysforward.Part1:Executesimplecommands(5Marks)Implementtheexecutionofsimplecommands.Yourshellshouldbeableto:•Prompttheuserforacommandbyprinting“>>>”asacommandprompt.•Executeacommandinputtedtothecommandprompt.•Loopindefinitelyuntiltheshellisexited.•Handlethe“cd”command—youwillnoticethatthiscommandwillneedtobetreatedasaspecialcase.Donotforgettostress−testyoursimpleshellbeforemovingontoadvancedfeatures.Theautomaticmarkingwillbetestingitonvariouscasesandmarkswillbedeductedifitdoesnotworkwhenthesamecommandisprovidedinadifferentformat,suchaswithextra30spacing.Forexample,consider(notetheamountandlocationofspacecharacterswhichmayimpacttheshelliftheyarenotdetected): echo hello world
echohelloworldOnceyouarecomfortablethatyouhavetestedyourshellwithanypossiblecommandthatcouldrevealbugs,moveontoimplementthefollowingadvancedfeatures.Part2:Input/Outputredirection(6Marks)ImplementInput/Outputredirection.Yourshellshouldbeabletohandletwoelementredirections.Forexample, echo "Hello world" > temp
cat<tempPart3:Pipes(6Marks)Implementpipes.Yourshellshouldbeabletohandletwoelementpipelines.Forexample, cat README | grep github
Part 4: Additional features (8 Marks)
Implement the following advanced features:
- Implement multi-element pipelines. For example,
$ ls | grep test | cat
- Implement non-trivial combinations of pipes and redirection. For example,
$ ls | grep test | cat > myoutput
- Implement the “;” operator that allows a list of shell commands to be given and
executed sequentially.
ls∣greptest∣cat>myoutput;catmyoutputMarkingGradescopewillrun26testcommandsandawardamarkoutof25.Thecommandsthatwillberunarenotdisclosedandyouarerequiredtousecreativitytothinkofvariousscenarioswhichmaybreakyourshellandtestitthoroughlybeforesubmitting.3outof25marks31willbeawardedtothosewhospotthreeespeciallytrickycasesofspecifyingcommandsandimplementtheirshellstogetaroundthem.Therearemanywaystotypecommands,somestraightforwardasshownabove,andsomenot,suchaswhenpeopletypecommandswithoutusinganyspacesorwitharbitrarynumberofspacesinvariousplaces.Yourshellshouldberesilienttothisambiguityinspecifyingcommands.Thosestudentswhospentmoretimeinthinkingaboutvarioustestcasesandcheckthemwillgetmoremarksthanthosewhoonlytryafewstraightforwardcommandslistedabove.Hereareafewexamplecommandsrunninginthenewcompletedshelltogetyoustarted:xv6kernelisbootinghart2startinghart1startinginit:startingsh my_shell
mkdir tempdir
ls
. 1 1 1024
.. 1 1 1024
README 2 2 2292
cat 2 3 35080
echo 2 4 33960
forktest 2 5 16080
grep 2 6 38512
init 2 7 34424
kill 2 8 33888
ln 2 9 33712
ls 2 10 37016
mkdir 2 11 33952
rm 2 12 33936
sh 2 13 56504
stressfs 2 14 34816
usertests 2 15 179160
grind 2 16 49736
wc 2 17 36024
zombie 2 18 33288
my_shell 2 19 40032
console 3 20 0
tempdir 1 21 32
cd tempdir
../ls
. 1 21 32
32
.. 1 1 1024
cd ..
ls
. 1 1 1024
.. 1 1 1024
README 2 2 2292
cat 2 3 35080
echo 2 4 33960
forktest 2 5 16080
grep 2 6 38512
init 2 7 34424
kill 2 8 33888
ln 2 9 33712
ls 2 10 37016
mkdir 2 11 33952
rm 2 12 33936
sh 2 13 56504
stressfs 2 14 34816
usertests 2 15 179160
grind 2 16 49736
wc 2 17 36024
zombie 2 18 33288
my_shell 2 19 40032
console 3 20 0
tempdir 1 21 32
echo hello
hello
echo hello
hello
cat README | grep xv6
xv6 is a re-implementation of Dennis Ritchie’s and Ken Thompson’s Unix
Version 6 (v6). xv6 loosely follows the structure and style of v6,
xv6 is inspired by John Lions’s Commentary on UNIX 6th Edition (Peer
(kaashoek,rtm@mit.edu). The main purpose of xv6 is as a teaching
cat README| grep xv6
xv6 is a re-implementation of Dennis Ritchie’s and Ken Thompson’s Unix
Version 6 (v6). xv6 loosely follows the structure and style of v6,
xv6 is inspired by John Lions’s Commentary on UNIX 6th Edition (Peer
(kaashoek,rtm@mit.edu). The main purpose of xv6 is as a teaching
It is worth to note that the default xv6 does not pass all of our expected tests. For
example:
33
xv6 kernel is booting
hart 2 starting
hart 1 starting
init: starting sh
cd. cd .
cannot cd .
mkdirtemp cd temp
cd.. cd temp
cannot cd temp
$
Submission
You are required to submit only my_shell.c. See Minerva.
WX:codinghelp