M.I.T. DEPARTMENT OF EECS
|6.033 - Computer System Engineering||UNIX Hands-On Assignment|
Complete the following hands-on assignment. Do the activities described, and submit your solutions using the online submission site by 11:59p. This assignment is longer than the DNS one. Before attempting this hands-on, you should read The Unix Time-Sharing System, which is also assigned for this recitation. You might also find Section 2.5 of the book useful for questions 8-19.
Log into an Athena machine. (athena.dialup.mit.edu is ok) and get access to the Athena command prompt. If you logged into an Athena dialup machine, you should see the prompt in your ssh client. If you logged into an Athena workstation you may need to open a terminal window. The prompt should look something like this (the "no-knife" part may say something different):
Use the add command to gain access to the 6.033 utilities you will use in this assignment.
no-knife:~> add 6.033
Some of the directions in this hands-on assume your UNIX shell is tcsh. Run the following command to ensure you're running the appropriate shell. After you run "tcsh -f" your prompt will change to a single ">".
no-knife:~> tcsh -f
We'll start off with an extremely simple example that most of you are probably familiar with already:
Here, we are first changing into the /bin directory, which contains many of the executable commands for the system. The command
athena% cd /bin
athena% ls -1 | more
ls -1gives us a listing of all the files in the current directory with one file per line. (Note that
-1is the numeral "one", not the letter "L".) We then pipe the output from
lsto the command
more, which displays the results one page at a time. (Press the space bar to show the next page. In order to quit the enumeration, press "q".) You can refer to the manual pages for
moreto see more details and options for each command. Manual pages let you read information about various commands on UNIX systems; to use them, run
athena% man command
command is the command you are interested
in. If you are unfamiliar with manual pages, you may want to try
for information on the man command itself. Keep in mind that the manual pages for basic commands vary from system to system (much as the commands themselves do).
athena% man man
Now, try this:
This runs the same
athena% cd /bin
athena% ls -1 | grep p | more
ls -1command, but only lists the executable files which happen to contain the letter "p" somewhere in their names.
The point here is to observe that you can chain together multiple commands using the pipe character ( | ), and the output from each command will be passed to the input of the next, in left-to-right order. This allows you to treat any command that uses standard input and output as a primitive from which you can build more complex and useful commands.
Now, we'd like you to figure out on your own how to solve some problems by chaining different commands together.
If you aren't already familiar with these commands, you may want to briefly skim through their man pages to familiarize yourself with what they do. You will probably need to use some of the options for the different commands in order to solve these problems.
Here are the commands you may find useful:
cat fmt grep head ls ps sort tail top wc yes (*)
(*) On some Athena machines, the
yes command isn't
available. However, if you are doing this assignment on Athena you
can use the command
gyes, which is functionally
gyes is located in the "gnu" locker, so
before you can use it you need to add the locker. You can do so with
the following command:
athena% add gnu
(If you are curious about Athena's locker system, you can run
man lockers for more information. The command
whichlocker can be used to determine which locker
contains a given command. The
whichlocker command itself
resides in the "outland" locker. For more info on other lockers, look at
this SIPB article)
Once you have added the gnu locker, you can use
yes. Some Athena machines seem to lack the
manual pages for both
(hereafter referred to just as
yes). In case the man
pages are missing on the machine you are using, here is a brief
description of what
yes is very
simple; it just outputs a string repeatedly until killed. It takes
either one or zero arguments; if you give it a string as an argument
it will output that string until it is killed (you can kill a process by
pressing control-c). If you give it no
arguments, it will output the string "y" until it is killed, which
explains why it is named
For each of the outputs listed below, find one sequence of commands connected by pipes that produces the output. For each problem, turn in the command sequence that you used to generate the requested output. (Do NOT turn in the output itself.)
A listing of all processes that you are currently running on
the Athena machine you are using, sorted by the command name in
alphabetical order (i.e. a process running
should be listed before a process running
output should consist only of the processes you are running,
and nothing else (i.e. if you are running 6 processes, the output
should only have 6 lines).
The number of words in the file
/usr/share/dict/words (*) which do not contain any of
the letters a, e, i, o, or u.
[* Note: On some Unix/Linux systems, the dictionary has the filename /usr/dict/words]
5x6 matrix of entries of alternating
0's. It should look like this:
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
A "long" listing of the smallest 5 files in the
/etc directory whose name contains
the string ".conf", sorted by increasing file size.
Now we'd like to explore something slightly different, having to do with file redirection as discussed in section 6.2-6.4 of the paper. The authors explain that the following two commands are functionally equivalent (except that you have to remove the temp file afterwards in the second case):
athena% ls | head -1
athena% ls > temp; head -1 < temp
Question 5: Try the above commands in a few different directories. What happens if you try both of the commands in the /etc directory on athena? How else can the second command not produce the same output as the first? Can you think of any negative side effects that the second construction might cause for the user? You might want to think about the commands you used to solve the first four questions and consider their behavior.
The UNIX paper authors explain in section 6.3 that a user can type two commands together in parenthesis separated by a semicolon, and redirect the output to a file. The file will then contain the concatenation of the two commands. The example from the paper is roughly:
athena% (date ; ls) > temp1 &
Note that this example uses the & operator to run the process asynchronously. That is, the shell will accept and run another command without waiting for the first to finish. The authors also mention that one can use the & operator multiple times on one line. For example, we can do almost the same thing:
athena% (date & ls) > temp2 &
See if you can figure out for yourself what exactly the difference is between using ";" and "&" in the examples above.
Let's explore this for ourselves. First, we will write a very simple variation on the "yes" program that you encountered earlier on in this assignment. To do so, we will use the "command file" functionality described in section 6.4 of the paper. Most people call these command files "shell scripts", since they are essentially simple scripts that are executed by the shell.
First, start up a copy of emacs editing a new file called "myyes".
athena% emacs myyes
Now, enter the following lines into your file:
#!/bin/sh echo y sleep 1 echo n
Hints: If you are having trouble with backspace, use the delete key.
Save the file (Ctrl-x Ctrl-s) and exit emacs (Ctrl-x Ctrl-c). If you don't already know what the
sleep commands do, look them up in
the man pages. Lastly, make the file executable by running the
athena% chmod a+rx myyes
Now let's try running the following two commands:
athena% (./myyes ; ./myyes) > temp3
athena% (./myyes & ./myyes) > temp4
Note: Some fast multicore machines may occasionally give unexpected answers, where some output is lost - originally unexpected even to the 6.033 staff. If this occurs for you on your home machine, you may want to think briefly about why this occurs.
Compare the two temp files. Based on your understanding of
file I/O in UNIX, what is going on here, and why? Is this different
from what you would expect? (If there is more than one difference
between the two files, it is the ordering of the letters y and n that
we are interested in).
Question 7: The paper describes the Unix system call interface in some detail. In particular, the read and write system calls do not take the offset as an argument. Why did the Unix designers not include the offset as an argument to read and write? How would an application write to a specific offset in a file?
When you log in, the system sets your current working directory to your home directory, your personal name space where you can create your own files, directories and links. You can view the contents of your current working directory with the ls command (see above). Use the pwd command to learn the absolute path of your current working directory. This will tell you where you are in the directory name space even if you move around in the directories.
The output of pwd reveals where the Athena administrators store your home directory. For example, /afs/athena.mit.edu/user tells us that the your home directory is stored as a user in the Athena name space.
The stat program reports detailed information about a file including its inode number, link count, file type and other metadata. To use it, type stat followed by a file name at the command prompt. Run stat on your home directory:
> stat .
Now create a directory named 6.033-handson2 in your home directory using the mkdir command. You can learn more about the mkdir command with man mkdir.
> mkdir 6.033-handson2
Use ls to verify that the new directory exists. Now change your current working directory to your new 6.033-handson2 directory using the cd command and verify that your working directory has changed using pwd.
View the contents of your new directory using ls -a -l. ls normally hides the directories "." and "..", but the -a option forces it to show them.
> cd 6.033-handson2
> ls -a -l
Question 8: Change to the '.' entry in your new directory. What happens to your working directory? Next, change to the '..' entry. What happens to your working directory?
Question 9: Describe a scenario where you might need to use the '.' directory.
Change your current directory back to your new 6.033-handson2 directory and stat the current directory; note the link count. Now create a couple files in your new directory using the touch command and stat the directory again.
> stat .
> touch foo bar
> stat .
Question 10: What has changed in the stat output and why has it changed?
Now create a subdirectory baz in 6.033-handson2 and stat the directory once more.
> mkdir baz
> stat .
Question 11: What has changed in the stat output this time and why has it changed? Why does the link count only change when you create a new directory?
The ln command can create both hard links and soft (symbolic) links. Read the man page for more information.
> man ln
First stat your file foo and read the output information. Then create a hard-link named foo-lnk and stat both foo and foo-lnk.
> stat foo
> ln foo foo-lnk
> stat foo
> stat foo-lnk
Note that everything about foo and foo-lnk is identical except for their names. If you modify foo you will see the modifications in foo-lnk.
> echo Hello >> foo
> cat foo-lnk
Now create a symbolic link to foo and note that the symbolic link differs from the original file in several ways. Creating the symbolic link does not increase the link count of foo and the symbolic link does not share an inode with foo.
> stat foo
> ln -s foo foo-slnk
> stat foo
> stat foo-slnk
Now cd to the 6.033 Athena locker with the command:
Question 12: One reason for supporting symbolic links is to allow linking from one disk to another disk, but you can also create a symbolic link to a file on the same disk. Name one advantage and one disadvantage of using symbolic links on the same disk.
Your home directory is accessible by the path /mit/YOUR_USERNAME (replace YOUR_USERNAME with your username). Try to change to your home directory with the command:
> cd /mit/6.033
> cd ../YOUR_USERNAME
Like your home directory, the 6.033 locker's absolute path is much longer than /mit/6.033 and /mit/6.033 is only a symbolic link. You can learn the absolute path name by typing pwd or by typing:
Question 13: What happened? Why?
> ls -l /mit
Question 14: You can reach the 6.033 locker with the path /afs/athena.mit.edu/course/6/6.033. Why does Athena also provide the /mit/6.033 symbolic link?
Question 15: How would you change the file system to make this command (
cd /mit/6.033; cd ../YOUR_USERNAME) actually change to your home directory?
You can configure your shell to search the current working directory by adding '.' to the PATH using these commands:
> echo $PATH
Now, cd to /mit/6.033 and run our demo program using the following commands. Your shell will find the "demo" program because it is in your working directory.
> setenv OLDPATH $PATH
> setenv PATH .:$PATH
Oh no, something terrible just happened! Just kidding, the demo program did not actually do anything. Verify that nothing happened with ls.
> cd /mit/6.033
> ls -l
Usually, it is a bad idea of have '.' in your PATH, because it is easy to run the wrong programs by accident. Instead, you can use '.' explicitly to run programs in your working directory like this:
Question 16: What happened to ls? Why isn't it listing files like it did before? (Hint: set your path back to its original state:
setenv PATH $OLDPATH)
In this final question we take a quick peek the systems calls that a program issues to the operating systems using the program strace. Run the following command:
> strace ls
strace shows all the system calls that ls makes. Look for one of the write systems calls, and answer the following question:
Question 17: What does 1 represent in the first argument of write?
You may have to consult section 3.6 of the Unix paper to answer the question.
Question 18: How long did this assignment take you to complete up to this point?
Question 19: Chapter 2.5 describes how (in UNIX) a file's inode maps to the file's data blocks using direct pointers and indirect blocks. An alternative strategy, as used in FAT, stores a file's data as a linked list of FAT entries, each corresponding to a block (cluster). Name one advantage and one disadvantage of the linked list strategy.
Question 20: Some shells like bash try to make '..' always work properly, namely, cd /mit/6.033; cd ../$USER will place you in your home directory. Does bash always get this behavior correct?
|Go to 6.033 Home Page|