Computer Science 432/563
Operating Systems

Spring 2016, The College of Saint Rose

Lab 1: C and Unix Refresher
Due: 11:59 PM, Friday, January 29, 2016

The focus of the first lab is on developing, compiling, and running C programs in our Unix environments.

You are to work individually on most of this lab assignment, but you may work with a partner on the program at the end.

Preliminaries

Before you begin work on this lab, you should make sure you can log into the Macs in Science Center 469A (these should accept your regular username and password) and that you have accounts on and remember your password to the two virtual Unix servers in the Saint Rose cloud that we will be using: mogul.strose.edu runs Linux (Red Hat Enterprise Linux 5, specifically), and ascg.strose.edu runs FreeBSD (Release 10.2). These are separate accounts, each of which has its own usernames, passwords, and file systems. So you will need accounts on each if you don't already have them.

Also, read over the description of the types of items you will encounter in our labs on the course home page.

Your Own Linux VM

The SG&G text uses a custom Linux virtual machine that it will be helpful for you to have access to. If you have a computer that will support it (most current desktops and laptops should), please install this according to the instructions at http://people.westminstercollege.edu/faculty/ggagne/osc/vm/

For those unable to do this, it might soon be possible to run the VM on college computers.

Unix Refresher

If you are not a regular Unix user, look at the labs from CSC 381 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 on each of mogul and ascg 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 on one of those systems using Emacs and answer the questions here and throughout the rest of the lab assignment in that file.

Question 1: What is the full path to your directory for this lab on each of those systems? (1/2 point)

Question 2: What are the Emacs keystrokes to save the file you are editing, quit Emacs, open a file, move the cursor to the end of the document, move the cursor to the beginning of the document, move the cursor to the beginning of the line, and move the cursor to the end of the line? (1 point)

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 on mogul:

grep ^q /usr/share/dict/words | wc -l

Question 3: What is the output? What does each command and parameter do and what does the answer tell us? (1 point)

Running a Simple C Program

Copy the C program here that computes the late penalties for this course to your account on each of the systems. 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

Next, rename each output file so it has the hostname where it was generated (e.g., late-mogul.txt).

Output Capture: late-XXXX.txt for 1 point(s)

C Refresher

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.

Question 4: How would you change the gcc command that compiles late.c above to name your executable late instead of a.out? (1/2 point)

Question 5: Suppose you have two int variables in your program named hour and minute. Write a printf statement that would print the time represented in HH:MM format (including the leading zero, when needed). (1/2 point)

See Example:
/home/cs432/examples/matmult

Question 6: You have used constants to make your Java programs more readable and easier to modify. In the matrix-matrix multiply example above, there is an example of how to define a constant in C. What line does this? (1/2 point)

See Example:
/home/cs432/examples/addingone

Compile and run the above example.

Question 7: What does the program print and why? (1 point)

Writing a Simple C Program

Practice Program: Write a program adder.c that works as described below. (10 points)

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:
/home/cs432/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.

Output Capture: make.txt for 1 point(s)

Now, look at the rules and the description in the Makefile.

Question 8: 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. (2 points)

Programming Assignment

You may work alone or with a partner on this task.

Write a text-based Hangman program in C, as described below. (20 points)

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 mogul and on ascg.

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

Please submit all required files as email attachments to terescoj AT strose.edu by 11:59 PM, Friday, January 29, 2016. Be sure to check that you have used the correct file names and that your submission matches all of the submission guidelines listed on the course home page. Please do not include any additional files, such as emacs backup files, object files, or executable programs. In order to email your files, you will need to transfer them to the computer from which you wish to send the email. There are a number of options, including the sftp command from the Mac command line and WinSCP from Windows.

If you worked with a partner on the Hangman program, only one group member need include the Hangman program in the submission. However, the submission must include all group member names in a comment in the hangman.c file.

Grading

This lab is graded out of 40 points.

Grading Breakdown

answers.txt responses 5 points
late-XXXX.txt files 1 point
make.txt description 2 points
adder.c correctness 10 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