Computer Science 322
Operating Systems

Mount Holyoke College
Spring 2010


Lab 1: C Practice
Due: 2:40 PM, Monday, February 8, 2010


This lab assignment is designed to help you become more proficient at C programming in a Unix environment. There are several programs here, but most introduce only one or two new ideas. As such, they are not intended to take you very long. Given this, please make every effort to work on these slowly over the course of the week so you have time to ask questions.


Setting Up

It's good practice to make a directory for each lab assignment, so that would be a good place to start. lab1 would be a good choice. And since you'll be developing a number of programs for this lab, I recommend creating a new subdirectory of lab1 for each program.

I encourage you to work in the Unix environment (Linux, FreeBSD, or Mac OS X) on these, but you may use your own computers if you wish. Be sure that you test your programs on either the Linux systems in Clapp 202, or on the FreeBSD system, mogul.


Hello

We begin with a simple "Hello, World!" type of program. You may print whatever you wish in place of "Hello, World!" (within reasonable bounds of decorum and decency). All we are going for here is the basic structure of a very simple C program.

Some things you will need to know:

Call your program hello.c and place it in a subdirectory hello.

To compile, you can use the gcc compiler:

        gcc -o hello hello.c

This will create an executable program named hello (rather than the default a.out that we would get if we didn't include the -o hello). If everything went well, you should see no output from the gcc command. If you see warnings or errors, you should investigate and fix these before continuing.

To run your program, you specify its name:

        ./hello

Without the ./, the Unix shell would look for a program named hello in your search path, and might not find the one you just compiled in your current directory (.).

Include a few comments in your program (enclosed in /* */ bounds). Most importantly, include your name! Sure, it's just a "Hello, World!" program, but claim credit for your work.


Argument Adder

You have likely seen Java applications that take command-line parameters (the String args[] parameter to main). Write a C program that takes an arbitrary number of command-line parameters, each of which should represent an integer value. Print out the sum of the values provided. Call your C program argadder.c and include a Makefile that guides a simple compilation of your program into an executable argadder. Include these in a subdirectory argadder in your submission.

Some things to know:


Input Adder

Next, we will change the "adder" program to take its input from the keyboard rather than from the command line. Your program should read in integer values one by one, accumulating a sum as you go, until you encounter an invalid (non-integer) or the end of the input (someone types Ctrl-d). At that point, print out the sum and exit.

Our main "new thing" here is keyboard input. There are several ways to read in values from the input, but we will use scanf. Some details about scanf may be found in the class example that computes greatest common denominators.

Call your C program inputadder.c and include a Makefile that guides a simple compilation of your program into an executable inputadder. Include these in a subdirectory inputadder in your submission.


Extended Input Adder

For one more bit of practice before getting to the somewhat more interesting tasks, extend your input adder program to remember all the values read in by storing them in a dynamically-allocated array. At the end, the program should print the addition problem and its solution in a standard format. For example, given the input values 9, 4, and 5, the program would print

9+4+5=18

Your task here is to be able to create the array and continue to make it larger as needed. Start with a reasonably-sized array (perhaps 10 slots). If the user inputs 10 or fewer values, you are all set. But if an 11th value is input, you will need to resize the array to make space. Use the same strategy as Java's Vector class - double the size of the array each time it fills.

Some things to consider:

Call your C program inputadder2.c and include a Makefile that guides a simple compilation of your program into an executable inputadder2. Include these in a subdirectory inputadder2 in your submission.


Fibonacci Sequence

Finally, you will implement a program specified at the end of Chapter 3 in SG&G.

First, write the program described in Programming Problem 3.15 on p. 143-144. Call your C program forkfib.c and include a Makefile that guides a simple compilation of your program into an executable forkfib. Include these in a subdirectory forkfib in your submission.

Note: we will discuss the fork system call during class. Until then, you can get started on the computing the Fibonacci sequence and add in the fork system call after we have covered it.


Submission and Grading

Each program will be graded based on design, documentation, style, and correctness. The correctness portion of the lab will be worth 25 points (3 points for the first program, 5 for each of the others, plus 2 points for a correct directory structure in your submission), and the design, documentation, and style will be worth a total of 10 points.

To submit this lab, change into your directory that contains all of the subdirectories that you are to turn in (likely lab1) and and create a "tar file" to submit.

tar cvf c.tar *

This will create a file c.tar in your directory. Send this tar file as an attachment to jteresco AT mtholyoke.edu by 2:40 PM, Monday, February 8, 2010.

Please do not include any additional files, such as emacs backup files, object files, or executable programs.