Computer Science 330
Operating Systems
Spring 2012, Siena College
The focus of the first lab is on developing, compiling, and running C programs in our Unix environment.
You are to work individually on most of this lab assignment, but you may work with a partner on the program at the end.
Logging into the Linux Environment
We will be using Unix-based systems within the Computer Science department. We will begin on the Linux systems in Roger Bacon 306. The same environment is available on the PCs near the back of the room in Roger Bacon 350. You will also get accounts today on the FreeBSD computer in my office. Later in the semester, we will likely use some other Unix-based systems.
If your PC in Roger Bacon 306 is booted into Windows, reboot it. You will see a boot menu provided by a program called Grub which allows multiple operating systems to be installed on the same computer. Choose the first Linux option. After a few minutes of boot messages, you should be prompted for your username and password. These are the same as the ones you use to log into the Windows system or into the remote access machine olsen.cs.siena.edu.
Once you are in, the system will present your desktop for a Graphical User Interface (GUI) called Gnome. The desktop you are presented with should be reasonably intuitive to use. Experiment with the menus to find useful and familiar applications like the Firefox browser and a Terminal window.
Logging into the FreeBSD Environment
An account will be created for each of you on the FreeBSD computer in my office, which you can access at winterstorm.teresco.org. For now, it is useful mainly as a place to browse the FreeBSD source code, but we will later use it as a secondary development environment. The same class examples that are in the class shared area on Linux will be placed in /home/cs330 on winterstorm.
For today, make sure you can log in and transfer files back and forth between winterstorm and your SoS account.
Unix Refresher
Each of you has some previous experience with Linux. If you are not a regular user, look back at the CSIS 220 Unix intro lab and make sure you remember how to use the Unix command line, how to navigate the directory structure, how to use the Emacs editor, and how to use some of the most common Unix commands. From this point forward, it is assumed that you can find your way around the Unix environment, so ask if any of it doesn't make sense.
Before moving on, create a directory in your account for your work for this course, and a subdirectory within that for your work for this lab.
Open a new file answers.txt in your directory for this lab using Emacs and answer the questions here and throughout the rest of the lab assignment in that file.
Recall that placing the |
character (the "pipe") between two
Unix commands at the command line causes the output of the first (the
"standard output" or stdout, which usually goes to the screen) to be
used as input for the second (the "standard input" or stdin,
which usually comes from the keyboard).
Issue this piped command:
grep ^q /usr/share/dict/words | wc -l
Running a Simple C Program
Copy the C program here that computes the late penalties for this course to your account. Compile and run it, redirecting your output to a file late.txt.
Assuming you have copied the program into a file late.c, you can do this with:
gcc late.c -lm ./a.out > late.txt
C Refresher
Each of you has some previous experience with C. Unless you are completely confident in your C programming abilities, look back at the examples from the CSIS 220 C intro lab and the CSIS 220 MIPS decoding lab. Make sure you remember how to compile and run C programs. Also be sure you are familiar with using printf to produce output, scanf to read input, using header files, command-line parameters (argv and argc), dealing with strings in C, the basics of memory allocation, structures, and pointers, and multi-file compilation and linking.
See Example:
~jteresco/shared/cs330/examples/addingone
Compile and run the above example.
Writing a Simple C Program
To get you back into C programming, write a C program that takes an arbitrary number of command-line parameters, each of which should represent an integer value and prints out the sum of the values provided. Call your C program adder.c.
When that works, enhance your program so that if no numbers are found on the command line, it prompts for numbers, adding them to a running sum until an input is encountered that is not a valid number or no input remains. At the end, print out the sum.
Next, enhance your program so it can take the name of a file as a command-line parameter and open that file for reading, reading numbers, one per line and again maintaining a running sum. When the end of the input is reached, close the file and print the sum. You will find the functions fopen(3), fscanf(3), and fclose(3) to be useful.
Using the make Utility
Any non-trivial software development involves many iterations of editing, compiling, linking, and running your programs. The code will be spread across multiple files. The most common mechanism for managing this process when programming in C in a Unix environment is the make utility. The actions of make are specified by rules in a Makefile. Copy the following example to your account:
See Example:
~jteresco/shared/cs330/examples/make-example
You should find a small C program that demonstrates the use of multiple source files and a Makefile. Compile the program with make. In a plain text file called make.txt, paste the output produced when you run make. Now, look at the rules and the description in the Makefile. Briefly describe in your make.txt file how make uses the rules in the Makefile to produce the executable main. Be sure to include the series of targets, their dependencies, and the commands used to satisfy those dependencies for each target.
C Programming Task
You may work alone or with a partner on this task.
Write a text-based Hangman program in C. It should read a dictionary file, then enter a loop where on each iteration, a word is randomly selected, and a hangman game is played interactively. The specific behavior of your program is pretty open-ended, but it should be playable (and should draw an ASCII-art Hangman as incorrect guesses mount). It should have at least similar functionality to the example solution executable available in the shared areas on the Linux systems and on winterstorm.
There are many details of C to deal with here. Please don't hesitate to ask if you run into trouble! You will likely find the string manipulation functions like strcmp(3), strlen(3) and strcpy(3) to be useful. You can use the srand(3) and rand(3) functions to generate random number. You may wish to seed the random number generator with something like:
void randomize() { struct timeval tm; gettimeofday(&tm, 0); srand(tm.tv_sec); }
after which calls to rand() will return a pseudorandom sequence
of int values, which, combined with the %
operator, will
allow you to select random words.
Call your program file hangman.c and include a Makefile that compiles up your program to an executable named hangman.
Submission and Evaluation
This lab is graded out of 40 points.
To submit this lab, place all of the files that you are to turn in (and nothing else) into a directory, change to that directory, and create a "tar file" to submit using a command similar to:
tar cvf intro.tar *.txt *.c Makefile
This will create a file intro.tar in your directory. Send this tar file as an attachment to jteresco AT siena.edu by 9:20 AM, Friday, January 27, 2012.
If you worked with a partner on the Hangman program, only one group member need include the Hangman program in the submission.
Please include a meaningful subject line (something like "CS330 Lab 1 Submission") and use the exact filenames specified (for this lab and all semester) to make my job easier when gathering your submissions together for grading. You don't want to annoy your grader with misnamed or missing files just before he grades your assignment. Please do not include any additional files, such as emacs backup files, object files, or executable programs.
Grading Breakdown | |
answers.txt responses | 5 points |
late.txt file | 1 point |
make.txt description | 3 points |
adder.c correctness | 5 points |
adder.c efficiency, style, and elegance | 2 points |
adder.c documentation | 2 points |
hangman.c correctness | 15 points |
Makefile to build hangman executable | 1 point |
hangman.c efficiency, style and elegance | 3 points |
hangman.c documentation | 3 points |