Assignment3.pdf

‫ﺁﺯﻣﺎﻳﺸﮕﺎﻩ ﺳﻴﺴﺘﻢﻋﺎﻣﻞ‬
۱۳۸۵-۸۶ ‫ﻧﻴﻤﺴﺎﻝ ﺍﻭﻝ‬
‫ﺩﺍﻧﺸﮑﺪﻩ ﻣﻬﻨﺪﺳﻲ ﮐﺎﻣﭙﻴﻮﺗﺮ‬
۸۵/۰۹/۱۸ : ‫ﻣﻮﻋﺪ ﺗﺤﻮﻳﻞ‬
‫ﺗﻤﺮﻳﻦ ﺳﻮﻡ‬
Writing Shell : ‫ﻣﻮﺿﻮﻉ‬
environment ‫ﻫﺪﻑ ﺍﺯ ﺍﻧﺠﺎﻡ ﺍﻳﻦ ﭘﺮﻭﮊﻩ ﭘﻴﺎﺩﻩﺳﺎﺯﻯ ﻳﻚ ﻣﻔﺴﺮ ﻓﺮﻣﺎﻥ ﺍﺳﺖ ﻛﻪ ﺍﻣﻜﺎﻥ ﻛﺎﺭ ﺑﺎ ﭘﺮﺩﺍﺯﻩﻫﺎ ﻭ ﻣﺪﻳﺮﻳﺖ‬
.‫ﻫﺎ ﻭ ﻛﺎﺭ ﺑﺎ ﻓﺎﻳﻞﻫﺎ ﻭ ﺷﺎﺧﻪﻫﺎ ﺭﺍ ﻣﻰﺩﻫﺪ‬variable
:‫ ﺧﻮﺩ ﺭﺍ ﺑﺎ ﻗﺎﺑﻠﻴﺖﻫﺎﻯ ﺯﻳﺮ ﭘﻴﺎﺩﻩﺳﺎﺯﻯ ﻛﻨﻴﺪ‬myshell.cpp ‫ﺑﺮﻧﺎﻣﻪ‬
1.
2.
3.
4.
5.
6.
7.
8.
9.
Internal commands
Execute external commands
Execute commands in background
A built-in command to find all PATH directories that an executable lives in
A built-in version of the ls command
Change and display environment variables.
Input and output redirection
Simple pipelines
Filename expansion and completion
1. Internal commands
:‫ﺑﺮﻧﺎﻣﻪ ﺷﻤﺎ ﺑﺎﻳﺪ ﺑﻪ ﺷﻜﻞ ﺯﻳﺮ ﺍﺟﺮﺍ ﺷﻮﺩ‬
user% myshell
.‫ " ﻧﻤﺎﻳﺶ ﺩﺍﺩﻩ ﺷﻮﺩ‬myshell>" ‫ﺩﺭ ﻫﻨﮕﺎﻡ ﺁﻣﺎﺩﮔﻰ ﻣﻔﺴﺮ ﻓﺮﻣﺎﻥ ﺑﺮﺍﻯ ﺩﺭﻳﺎﻓﺖ ﺩﺳﺘﻮﺭ ﺟﺪﻳﺪ ﺑﺎﻳﺪ ﻋﺒﺎﺭﺕ‬
:‫ ﺑﺎﻳﺪ ﻗﺎﺩﺭ ﺑﻪ ﺍﺟﺮﺍﻱ ﺩﺳﺘﻮﺭﺍﺕ ﺩﺍﺧﻠﻲ ﺯﻳﺮ ﺑﺎﺷﺪ‬myshell
•
cd <directory> - change the current default directory to <directory>. If the directory
does not exist an appropriate error should be reported.
• pstree - show the system processes tree. In Linux, init process is the parent of all
other processes running in the system and has no parent. Ideally your program output
would be like the output of Linux 'pstree' command. But a list of process and his
parent is sufficient.
• pwd – current working directory
• exit - quit the shell
• whereis – refer to section 4
• ls – refer to section 5
Note: You must not use ps, ls, fork or any other built-in available commands in your programs!
Instead use C basic functions and libraries.
2. Execute external commands
.‫ ﺑﺎﻳﺪ ﻗﺎﺩﺭ ﺑﻪ ﺍﺟﺮﺍﻱ ﺩﺳﺘﻮﺭﺍﺕ ﺧﺎﺭﺟﻲ ﻛﺎﺭﺑﺮ ﺑﺎﺷﺪ‬myshell
myshell> a.out
Note: an empty command line should result in no errors, just a new prompt being printed.
3. Background processes
،‫ ﺑﺎ ﺍﺿﺎﻓﻪ ﻛﺮﺩﻥ ﻛﺎﺭﺍﻛﺘﺮ ’&‘ ﺑﻪ ﺍﻧﺘﻬﺎﻯ ﺩﺳﺘﻮﺭ‬.‫ ﭘﺸﺘﻴﺒﺎﻧﻰ ﻛﻨﺪ‬background ‫ ﺑﺎﻳﺪ ﺍﺯ ﺍﺟﺮﺍﻯ ﺩﺳﺘﻮﺭﺍﺕ ﺩﺭ‬myshell
‫ ﻣﻔﺴﺮ ﻓﺮﻣﺎﻥ‬background ‫ ﺑﺎ ﺍﺗﻤﺎﻡ ﻳﻚ ﭘﺮﺩﺍﺯﻩ‬.‫ ﭘﺮﺩﺍﺯﻩ ﻗﺒﻠﻰ ﭼﺎﭖ ﺷﺪﻩ ﻭ ﺧﻂ ﻓﺮﻣﺎﻥ ﺁﺯﺍﺩ ﻣﻰﮔﺮﺩﺩ‬PID
‫" ﺭﺍ ﺩﺭ ﺧﺮﻭﺟﻰ‬MYSHELL EXIT n (nonzero exit status)" ‫" ﻭ ﻳﺎ‬MYSHELL DONE (exit status 0)" ‫ﻋﺒﺎﺭﺕ‬
.‫" ﭼﺎﭖ ﻣﻲﮔﺮﺩﺩ‬MYSHELL TERMINATED" ‫ ﺑﺴﺘﻪ ﺷﻮﺩ‬kill ‫ﭼﺎﭖ ﻣﻰﻛﻨﺪ ﻭ ﺍﮔﺮ ﭘﺮﺩﺍﺯﻩ ﺑﻮﺳﻴﻠﻪ ﺳﻴﮕﻨﺎﻟﻰ ﻣﺎﻧﻨﺪ‬
Note: If myshell exits normally, it should terminate all background processes and print their
demise before myshell itself exits. Use the atexit() system call to arrange for this.
4. Check executable paths
‫ ﭘﻴﺎﺩﻩ ﺳﺎﺯﻱ ﻛﻨﻴﺪ ﺗﺎ ﺑﻮﺳﻴﻠﻪ ﺁﻥ ﺗﺸﺨﻴﺺ ﺩﻫﻴﺪ ﻛﻪ ﺁﻳﺎ ﻫﺮ ﺑﺮﻧﺎﻣﻪ‬mywhereis ‫ ﺑﻪ ﻧﺎﻡ‬built-in ‫ﺩﺭ ﺍﻳﻦ ﻗﺴﻤﺖ ﻳﻚ ﺗﺎﺑﻊ‬
‫ ﺑﺎﻳﺪ ﺑﻪ ﺗﺮﺗﻴﺒﻲ‬PATH ‫ ﺯﻳﺮ ﺷﺎﺧﻪ ﻫﺎﻱ‬.‫ ﺍﺳﺖ‬whereis ‫ ﺍﻳﻦ ﺩﺳﺘﻮﺭ ﺑﺴﻴﺎﺭ ﺷﺒﻴﻪ‬.‫ ﻫﺴﺖ ﻳﺎ ﻧﻪ‬PATH ‫ﺍﺟﺮﺍﻳﻲ ﺩﺭ ﻣﺴﻴﺮ‬
‫ ﺩﻗﻴﻘﺎ ﻳﻚ ﺁﺭﮔﻮﻣﺎﻥ ﻭﺭﻭﺩﻱ ﺩﺍﺭﺩ ﻭ ﺁﻥ ﻧﺎﻡ ﺩﺳﺘﻮﺭﻱ‬mywhereis ‫ ﺗﺎﺑﻊ‬.‫ﻛﻪ ﻇﺎﻫﺮ ﻣﻲﺷﻮﻧﺪ ﻣﻮﺭﺩ ﺟﺴﺘﺠﻮ ﻗﺮﺍﺭ ﺑﮕﻴﺮﻧﺪ‬
.‫ﺍﺳﺖ ﻛﻪ ﺑﺮﺍﻱ ﺁﻥ ﺟﺴﺘﺠﻮ ﻣﻲﺷﻮﺩ‬
myshell> mywhereis man
man: /usr/bin/man /usr/local/man /usr/share/man
myshell> mywhereis random_name
random_name: No Match
myshell> mywhereis
MYSHELL ERROR: usage mywhereis executable_name
myshell> mywhereis man chsh
MYSHELL ERROR: usage mywhereis executable_name
myshell> setenv PATH
myshell> mywhereis chsh
chsh: No Match
myshell>
Note: mywhereis doesn't have to handle built-in commands in myshell. If PATH is empty or
undefined, that is not an error: it's just a No Match. If mywhereis can't access a particular path
directory, that is also not an error; just skip that path element and go to the next.
5. Built-in custom ls
:‫ﺑﺎﻳﺪ ﻣﻮﺍﺭﺩ ﺯﻳﺮ ﺭﺍ ﻧﻤﺎﻳﺶ ﺩﻫﺪ‬
.inode values, file type and size
.‫" ﺑﺎﺷﺪ‬other" ‫" ﻳﺎ‬symbolic link" ‫" ﻭ‬directory" ‫" ﻭ‬regular file" : ‫ﻧﻮﻉ ﻓﺎﻳﻞ ﻣﻲﺗﻮﺍﻧﺪ ﻳﻜﻰ ﺍﺯ ﻣﻮﺍﺭﺩ‬
myshell> ls
infile outfile aa@
myshell> myls
283013
directory
512 .
823308
directory
3584 ..
283033 symbolic link
6 aa
283020 regular file
5 infile
283163 regular file
16 outfile
Note: myls will print the files in sorted order. If myls can't access the current working directory
or a particular file, it will print a MYSHELL ERROR message saying why the file couldn't be
accessed.
6. Handling environment variables
:‫ﺑﺮﺍﻯ ﺍﻳﻦ ﻗﺴﻤﺖ ﺳﻪ ﺗﺎﺑﻊ ﺑﺎ ﻣﺸﺨﺼﺎﺕ ﺯﻳﺮ ﭘﻴﺎﺩﻩﺳﺎﺯﻯ ﻛﻨﻴﺪ‬
setenv variable_name [new_value]
getenv variable_name
printenv
:‫ﻧﻤﻮﻧﻪﺍﻱ ﺍﺯ ﻛﺎﺭﺑﺮﺩ ﺁﻧﺮﺍ ﺩﺭ ﻟﻴﺴﺖ ﺯﻳﺮ ﺑﺒﻴﻨﻴﺪ‬
user% myshell
myshell> getenv EDITOR
vi
myshell> setenv EDITOR emacs
myshell> getenv EDITOR
emacs
myshell>
.‫ ﻭ ﻣﻘﺎﺩﻳﺮ ﺁﻧﻬﺎ ﺭﺍ ﭼﺎﭖ ﻣﻰﻛﻨﺪ‬environment variable ‫ ﻟﻴﺴﺘﻲ ﺍﺯ ﺗﻤﺎﻡ‬printenv
myshell> printenv
EDITOR=emacs
HOME=/home/BS82/user
...
Note: If setenv receives only one argument, set the environment variable to an empty string. If
the user sets an already-existing environment variable, replace the old value. If getenv receives a
variable name that doesn't exist, it should print an error message.
7. Input and Output Redirection
:‫ﺑﺮﻧﺎﻣﻪ ﺷﻤﺎ ﺑﺎﻳﺪ ﺗﻤﺎﻡ ﻗﺎﺑﻠﻴﺖﻫﺎﻯ ﻣﺜﺎﻝ ﺯﻳﺮ ﺭﺍ ﭘﺸﺘﻴﺒﺎﻧﻰ ﻛﻨﺪ‬
myshell> ls
infile
myshell> cat infile
hello
myshell> cat < infile > outfile
myshell> ls
infile outfile
myshell> cat outfile
hello
myshell> cat < infile >> outfile
myshell> cat outfile
hello
hello
myshell> cat < infile > outfile
myshell> cat outfile
hello
myshell>
Note: >> results in appending to the outfile, while > first erases the outfile.
.‫ ﻧﻮﺷﺘﻪ ﻣﻰﺷﻮﺩ‬output redirect ‫ ﻗﺒﻞ ﺍﺯ‬input redirect ‫ﺑﺮﺍﻯ ﺭﺍﺣﺘﻰ ﻛﺎﺭ ﻓﺮﺽ ﻛﻨﻴﺪ ﻫﻤﻮﺍﺭﻩ‬
8. Pipelines
:‫ﻣﺎﻧﻨﺪ ﻣﺜﺎﻝ ﺯﻳﺮ‬
myshell> ls
infile outfile
myshell> ls | wc
2
Note: To make things simpler, we will not mix pipelines and redirect. We will also assume
pipelines wont be invoked as a background process, so we won't have a | and & in the same
input.
9. Filename Expansion and Completion
myshell expands arguments before calling exec() or executing built-in commands.
myshell> ls
infile outfile aa@
myshell> ls *file
infile outfile
myshell also completes partial or incomplete filename on pressing the TAB key.
myshell> ls
infile infile1 infile2 outfile aa@
myshell> ls in < TAB >
infile infile1 infile2
‫ﺗﺬﮐﺮﺍﺕ‬
.‫ﺍﻳﻦ ﺗﻤﺮﻳﻦ ﻣﻲﺑﺎﻳﺴﺖ ﺑﻪ ﺻﻮﺭﺕ ﺍﻧﻔﺮﺍﺩﻱ ﻳﺎ ﺩﺭ ﻗﺎﻟﺐ ﮔﺮﻭﻩﻫﺎﻱ ﺩﻭ ﻧﻔﺮﻩ ﺍﻧﺠﺎﻡ ﺷﻮﺩ‬
•
‫ﺑﺨﺸﻲ ﺍﺯ ﻧﻤﺮﻩ ﺗﻤﺮﻳﻦ ﻣﺮﺑﻮﻁ ﺑﻪ ﺍﻧﺠﺎﻡ ﭘﺮﻭﮊﻩ ﻭ ﮐﺪ ﺁﻥ ﺍﺳﺖ ﻭ ﺑﺨﺸﻲ ﻧﻴﺰ ﻣﺮﺑﻮﻁ ﺑﻪ ﮔـﺰﺍﺭﺵ ﮐﺘﺒـﻲ ﭘـﺮﻭﮊﻩ‬
•
.‫ﺍﺳﺖ‬
.‫ ﺍﺭﺳﺎﻝ ﮔﺮﺩﺩ‬[email protected] ‫ﺗﻤﺮﻳﻦ ﺗﺎ ﺗﺎﺭﻳﺦ ﻣﻘﺮﺭ ﺑﻪ ﺁﺩﺭﺱ‬
OS LAB-Assignment3 :‫ﻋﻨﻮﺍﻥ‬
ƒ
‫ ﻧﺎﻡ ﻭ ﺷﻤﺎﺭﻩ ﺩﺍﻧﺸﺠﻮﻳﻲ ﺍﻋﻀﺎﻱ ﮔﺮﻭﻩ‬:‫ﻣﺤﺘﻮﻱ‬
ƒ
‫ ﺑﺎ ﻧـﺎﻡ‬zip ‫ ﻭ ﮔﺰﺍﺭﺵ ﮐﺘﺒﻲ ﺩﺭ ﻗﺎﻟﺐ ﻳﮏ ﻓﺎﻳﻞ‬makefile ‫ ﺗﻤﺎﻡ ﻓﺎﻳﻞﻫﺎﻱ ﺑﺮﻧﺎﻣﻪ ﺑﻪ ﻫﻤﺮﺍﻩ‬: ‫ﭘﻴﻮﺳﺖ‬
ƒ
•
student ID-assignment3
.‫ ﺗﻤﺮﻳﻦ ﻣﻲﺑﺎﻳﺴﺖ ﺑﻪ ﺻﻮﺭﺕ ﺣﻀﻮﺭﻱ ﺗﺤﻮﻳﻞ ﺩﺍﺩﻩ ﺷﻮﺩ‬،‫• ﺩﺭ ﺗﺎﺭﻳﺨﻲ ﮐﻪ ﻣﺘﻌﺎﻗﺒﹰﺎ ﺍﻋﻼﻡ ﺧﻮﺍﻫﺪ ﺷﺪ‬
.‫• ﺑﻪ ﺗﻤﺮﻳﻨﺎﺕ ﻣﺸﺎﺑﻪ ﻧﻤﺮﻩ ﺻﻔﺮ )ﺑﻪ ﻋﻨﻮﺍﻥ ﻧﻤﺮﻩ ﻧﻬﺎﻳﻲ ﺩﺭﺱ( ﺗﻌﻠﻖ ﺧﻮﺍﻫﺪ ﮔﺮﻓﺖ‬