Recursive dice
In class Monday we implemented a simple program to partially simulate the repeated throwing of one die. Make a copy of the version we created in class. We noted that we need to make a change to use srand() so that rand() does not always give the same sequence of numbers. Note that since random number generators produce a sequence of random numbers, you only need to set the seed at the beginning (which we did) to ensure that the sequence will (likely) be different.
Once you have a version that feels truly random, modify it so that you have a function that rolls two dice, prints their values, and calculates the sum. Repeatedly call that function until the sum is some arbitrary desired value.
Make a copy of your working program to a new file (rdice.c). Modify the dice rolling function to be recursive, so that main() needs only call it once and it will call itself repeatedly until the desired sum is achieved. Give yourself some imaginary bonus points if you implement the function using tail recursion as that is usually most efficient. (Tail recursion is when the last step of the function is to call itself again.)
ssh, scp, sftp
As you know, ssh provides a secure mechanism to connect from one machine to another and start a shell. ssh also provides the foundation for a few other commands, such as scp and sftp, which you can think of as secure copy and secure ftp (file transfer protocol). The suns have each of these installed. You can run ssh at a shell to connect to another sun if you like, e.g.:tethys:~% ssh umbria
The authenticity of host 'umbria (128.180.120.82)' can't be established. RSA key fingerprint is b1:c2:0d:16:d3:98:5b:7a:85:6a:60:15:52:82:59:03. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'umbria,128.180.120.82' (RSA) to the list of known hosts. Password: Last login: Sun Jan 30 21:26:51 2011 from wume2 Sun Microsystems Inc. SunOS 5.10 Generic January 2005 LU CSE and ECE WorkStation Network ::::::::: Dept Labs are open: ::::::::: :: Mon - Thu 9:00 AM - 10:00 PM :: :: Fri 9:00 AM - 6:00 PM :: :: Sun 1:00 PM - 6:00 PM :: :: Packard Lab - Rooms 112 and 122 :: ::::::::::::::::::::::::::::::::::::::: 0-------------------------------------0 | Do NOT turn off Unix workstations. | | There may be remote users online. | 0-------------------------------------0 The SunLAB computers are running Solaris 10 update 8: Xeon: ariel caliban callisto dactyl enceladus eris ganymede iapetus io jupiter kiviuq mars mercury nereid puck saturn sycorax tethys titania triton Opteron: ceres chiron hydra nix pan xena SPARC: europa metis phoebe pluto umbria:~%So, the above demonstrates how I used ssh to connect from tethys to umbria and get a shell running there. Since I had not done so previously, ssh warned me that the secure fingerprint provided by umbria was not one that I had seen (and accepted before), and asks me to continue. It won't ask me that again (unless umbria's fingerprint were to change for some reason).
If I am running X-Window on my current machine and wanted to be able to run programs that open new windows from the new machine, I would use ssh -X instead of just ssh. Since the window display information is flowing through your secure ssh connection, all X11 commands are sent back to the source machine encrypted, just as your shell commands are.
The scp and sftp programs allow you to securely copy one (scp) or more (sftp) files from one machine to another. Since I have two user accounts, I could use scp to copy files from one to another, such as:
tethys:~% scp brian@metis:cse271/dot-emacs dot-emacs Password: dot-emacs 100% |*****************************| 2559 00:00 tethys:~% ls -l dot-emacs -rw-r--r-- 1 bdd3 ugrad 2559 Jan 25 10:55 dot-emacs tethys:~%We can use sftp to allow us to connect and use ftp-like commands to get and put files to a remote host, such as some of the university machines to which you have access.
So here I used sftp to copy a file from my current account (dot-emacs) to my university afs space by connecting to part of the HPC cluster of systems running linux in the LTS server room. (You can find out about more such systems here.)
tethys:~% sftp leaf.cc.lehigh.edu Connecting to leaf.cc.lehigh.edu... The authenticity of host 'leaf.cc.lehigh.edu (128.180.2.217)' can't be established. RSA key fingerprint is 8e:46:5a:ce:c3:ee:c0:0e:7d:30:61:11:c9:c0:75:50. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'leaf.cc.lehigh.edu,128.180.2.217' (RSA) to the list of known hosts. bdd3@leaf.cc.lehigh.edu's password: sftp> put dot-emacs Uploading dot-emacs to /afs/cc.lehigh.edu/home/bdd3/dot-emacs sftp> ls . .. .Xauthority .bash_history .profile .ssh dot-emacs private public yesterday sftp> quit tethys:~%Changing your password
You were given an initial password to access your CSE account. For increased security, it is often a good idea to periodically change it. Your university account requires you to change it every six months, but the CSE accounts are not so strict. When you do want to change it, you'll use the passwd command, as in:
Note that it requires that you know your old password first, and will prevent you from re-using passwords that are too similar. The best passwords are often those that are abbreviations for something that is absurdly funny or offensive (to make it easy to remember).
tethys:~% passwd passwd: Changing password for bdd3 Enter existing login password: _ New Password: _ passwd: The first 8 characters of the old and new passwords must differ by at least 4 positions. Please try again New Password: _ Re-enter new Password: _ passwd: password successfully changed for bdd3 tethys:~%Disk usage and quota
As you continue to use your CSE account for multiple classes and perhaps email and web pages, too, you will be using more and more storage. A new user's storage allocation is small, but you can request additional storage easily if needed (email to help@cse...).
There are two UNIX commands that can help you understand how much disk space you are using. The first is du, which stands for disk usage. By default it tells you how many blocks of disk you are using in the current directory and within any subdirectories. A block in Solaris is typically 512 bytes, but the -h option will convert it to something more useful. Try it out on your account and see how much storage you are currently using.
The second command is quota -v. It often shows how much total storage you have used on the filesystem and what your quota is. Back when we used quotas, it showed something like this:
So from this I could see that I was using 20MB in my home directory and my limit is 50 megabytes. The hard limit of 75MB allows me to temporarily (for a few days) use more than the 50MB if needed. If I try to create files that sum to more than 75MB, they will fail. You can try to see what your quota is, but most likely it will not show anything (since quotas are currently not being used).
tethys:~% quota -v Disk quotas for bdd3 (uid 43430): Filesystem usage quota limit timeleft files quota limit timeleft /home/jhw204 0 50000 75000 0 0 0 /home/bdd3 20611 50000 75000 1518 0 0 tethys:~%Instead, with our current ZFS filesystem, every user has their own filesystem -- almost like having your own disk. The df -h . command will tell you how much disk space is on "your" disk, how much is in use, and how much is free. (A quick aside: if you ever fill up your home directory so that there are no bytes free, then you will be in trouble -- you might not be able to log in, and even if you can log in, you won't be able to use rm to remove a file and make room. Instead you'll need to use the "magic" command: cat /dev/null > bigfiletodelete. You already know what cat does; the /dev/null file is a special file that contains nothing. So this overwrites the file of your choice with nothing, which effectively frees the space that the file used to contain.) You can then use rm to delete this file that contains nothing.
The grep command
Sometimes it is useful to find the lines of a file that match some pattern. A very simple use is to find the lines in a program that call a function. grep is a great command to use for this. For example, I can find every line that contains the string "print" in mycat.c:
I could equivalently have typed grep print < mycat.c. When used this way, we can think of grep as a filter. It is reading from standard input and filtering the content in some way and sending the remaining text to standard output.
tethys:~% cd mycat/ tethys:~/mycat% grep print mycat.c printf("We need an argument.\n"); printf("File is not found.\n"); /* print content to the screen */ printf("Output failure.\n"); tethys:~/mycat%Writing a filter
For the last task of this lab, let's write our own very simple filter. This program will be somewhat similar to our mycat program. Your task is to write a simple character filter to collapse extra white space in text files. The filter should change each tab character ('\t') to a space and collapse a series of two or more spaces into a single space. As a Unix filter, it should read its input from stdin and write its output to stdout. Do not use character arrays or strings for this program.
As an example usage using input redirection, assume the contents of the sample input file are as shown.
and once you have successfully written your collapse.c program, you can compile and run it as:
...% cat infile Where the Lehigh's rocky rapids Rush from out the West, Mid a grove of spreading chestnuts Walls in ivy dressed. On the breast of old South Mountain Reared against the sky, Stands our noble Alma Mater, Stands our dear Lehigh.
...% gcc -o collapse collapse.c ...% collapse < infile Where the Lehigh's rocky rapids Rush from out the West, Mid a grove of spreading chestnuts Walls in ivy dressed. On the breast of old South Mountain Reared against the sky, Stands our noble Alma Mater, Stands our dear Lehigh.