Operating Systems

Operating doggedly...

Refreshing C


For Thursday, you will be

  1. Refreshing C. We'll do some C programming (not on PintOS) to remember how to write code in C, set up a simple Makefile, and so on.

I recommend you start on this right away.

Refreshing C

First, some resources.

Now, we'll write some C programs this weekend for the purpose of getting into the language.

Set Up an Environment

You can either use cs.berea.edu, or you can set up a virtual machine. If you choose to set up a virtual machine, I recommend you download Virtual Box, a recent version of Ubuntu, and go from there. There are many tutorials on this subject (here's one that looks pretty good).

You'll need to do an

sudo apt-get install build-essential

from the command line before you get started programming.

Using the VM is probably the quickest path to getting started.

Hello, World

First, write a program called hello.c that prints out "Hello, Operating Systems!" and exits.

Version Control

This program should be in a directory called hello-os, and should be under version control (eg. Bitbucket). This should be a private repository, and should be shared with the Bitbucket user jadudm.

Makefile

Next, create a Makefile that will compile the program

Put this text in a file called Makefile. Now, from the command line, you should be able to type

make

and your program will compile automatically.

Run it!

You should be able to type

./hello-os

and run your program. (The Makefile names the output of the compilation process hello-os. If you run gcc from the command line, it might be called a.out.)

Walking a List

A lot of work in PintOS will use structures and lists. In particular, the PintOS implementation provides a doubly-linked list implementation that you should become familiar with.

You can check out a Github repository that contains some starter code that will help get you moving.

git clone https://github.com/jadudm/pintos-linked-list-example.git

You won't be able to commit back to that repository, but you can change the remote to a repository of your own if you want to use this as a starter.

Suggested Steps

You're welcome to make changes here where it makes sense; these are intended to make things simple for you in terms of helping you organize your thoughts. The goal is to get used to working with the linked list code.

  1. Write a program that has a header and code file (.h and .c). The header file should define functions and structures; your code should implement them.

  2. Define a structure called "birthdate." It should have three fields: a year, a month, and a day, all of which are integers. The students should be born between 1980 and 2000.

  3. Define a structure called "student." It should have fields for a name (char[20]), an age (int), and a birthdate (pointer to struct birthdate).

  4. Write a C function that allocates a birthdate structure, fills it with random data, and returns a pointer to that structure.

  5. Write a function that allocates a student structure, fills it with random data, and returns a pointer to that structure. (Names can just be 20 random ASCII characters.) You will probably want to investigate the C standard library (stdlib.h) for functions that generate random integers; for example, writing a function that returns a random ASCII character in the range 'A' to 'Z' might be useful.

  6. Write a function that takes a doubly-linked list and a student structure. Your function should add the student to the list. Because we're adding to a list for which we have the pointer, we don't need to return anything---the return type can be void.

  7. Write a function that declares an empty list, adds ten random students to an that list, and returns the list with 10 students in it.

  8. Write a function that prints out the names and ages of the 10 students in the list by walking the list forwards. (Check debug.h for functions that might be of use to you---this is the debug package provided by PintOS.)

  9. Write a function that prints out the names and ages of the 10 students in the list by walking the list backwards.

  10. Write a function that walks the list and removes any students from the list who were born before 1990.

  11. Write a driver (or main) that 1) generates a random list of 10 students, 2) prints them in forward list order, 3) prints them in backward list order, 4) removes all the students born before 1990, and then 5) prints that list in forward order.

  12. BONUS. Finally, write a function that deallocates a doubly-linked list of structures. It should walk a list and free all of the structures and nodes in the list.

Learning Objectives

This program will force you to:

  1. Learn to use pointers.
  2. Learn to use C structures.
  3. Learn to allocate space for structures using malloc.
  4. Learn to traverse a doubly-linked list in a forward direction.
  5. Learn to traverse a doubly-linked list in a reverse direction.
  6. Learn to print the contents of structures for debugging purposes.
  7. BONUS. Learn to free memory allocated for data structures.

These are all essential things that you will need to do in PintOS.

Your completed program should have a Makefile that builds the code and names the output of the compilation linked-list-practice. You are encouraged to collaborate on this assignment, but you must credit each-other, and include (in your submission) a Markdown file that describes the process that you used in working on this. Divide-and-conquer is NOT recommended. Collaborative, pair programming is. And, you both MUST understand the material when you are done---otherwise, you will struggle mightily as we move into PintOS.

Submission

Please share a private Bitbucket repository with me (jadudm). It should contain your code for the linked list exploration.



This website is provided under a CC BY-SA license by the The Berea CS Department.
Fall 2013 offering of taught by Matt Jadud