CS 537: Introduction to Operating Systems
Project 0
Executive summary: In this project, you will write a short C program and familiarize yourself with the development environment available in the royal and emperor labs.
Assigned: June 16
Due: June 21 at 5:00 PM
If you have any questions, e-mail them to the class list. Then everyone will benefit from your question!
There are three parts to this assignment, which will be explained in greater detail below.:
- Read the C environment tutorial. Then enter, compile, and run the "Hello, world!" program given.
- Write a short C program that uses fork().
- Use the on-line documentation available by the man command to determine what the appropriate library functions or system calls are to accomplish certain tasks.
C-alesthenics
First, read Prof. Remzi Arpaci-Dusseau's guide to the C/UNIX environment. This will provide you with a good introduction to the tools, libraries, and documentation that systems programmers use every day. Be sure to read the whole thing.
Then, enter the program described as hw.c. (Note that you'll need to ensure that the string "Hello, world!" is enclosed in ordinary double quotes, like a string would be in Java. Due to a formatting error in the document, it may appear as two single quotes.) You can use emacs or whatever editor you're most comfortable with.
Finally, you'll want to compile and link the program so you can run and test it. Use gcc, as described in the guide. Save your hw.c file; you'll be turning it in. You're done with part 1 of the project; there are two to go!
Let's go on a scavenger hunt, man !
Prof. Arpaci-Dusseau's guide showed you how to use man, the on-line manual reader, and man -k, which searches the manual by keyword. (If you prefer to use polysyllabic commands, you can also use the apropos command -- it is functionally identical to man -k.) You probably won't always remember which library function or system call does what you want, so man will become a close friend as you develop C programs. (man is also very helpful if you've forgotten how many arguments a function takes or what its return value means!)
In this part of the assignment, you will use man and man -k to figure out how to accomplish various tasks in C. Write short answers to each of the following questions; you'll be including them in your cover letter. (Note that, in Linux, system calls are in section 2 of the manual and library functions are in section 3.)
- What system call(s) might a process invoke in order to block until a particular child process terminates? How about until any child process terminates?
- What system call would I use to open a file? What arguments would I use to this call if I wanted to create a new file called foobar?
- What library function converts an integer to a string?
- What library function searches for a substring of a string?
- What system call returns the process ID of the current process? How about the process ID of the current process' parent?
Raiding the silverware drawer
You are now going to write a short program that uses the C language and the fork() system call. Your program will first fork a child process.
- The child process will print the string Are we there yet?, followed by a newline, and then exit.
- The parent process will print the string Don't make me come back there!, followed by a newline, and then exit. You can use hw.c as a starting point, of course.
Call your program vacation.c. Compile, run, and test it. Once you've determined that your program meets the requirements of the assignment, answer the following questions in your cover letter:
- Is this program deterministic? That is, can we be sure that this program will always produce the same output?
- If you answered “yes” to the preceding question, explain why. If you answered “no”, please explain why and describe how you would change the program to make it deterministic.
For those of you who are new to C, this little program should be a gentle introduction to the language. Here are some pointers to C tutorials:
- http://www.strath.ac.uk/IT/Docs/Ccourse/
- http://www.howstuffworks.com/c.htm
- http://www.le.ac.uk/cc/tutorials/c/
- http://www.cs.washington.edu/education/courses/451/04au/section/c-tutorial.pdf
Now create a copy of your program. Name the copy vacation2.c. Using information gleaned in the “scavenger hunt” portion of the project, make one or two changes to vacation2.c:
- First, modify the printf statements to also print the current and parent process IDs. An example output, then, might look like this:
For a hint, click here.pid: 5432; parent pid: 5428 Are we there yet?
pid: 5428; parent pid: 154 Don't make me come back there! - Finally, if you believe that this program is not deterministic, make any changes that you deem necessary in order to make it deterministic.
What to turn in
You will turn this assignment in by e-mail. E-mail me the following items:
- Your cover letter (in plain text, NOT .doc format!). Your cover letter will include your answers to the scavenger hunt questions and your answers to the questions about vacation.c.
- the three .c files: hw.c, vacation.c, and vacation2.c
- You do not have to submit a Makefile for this project, but you will for later projects.
printf tips
To print out numbers and strings, you'll need to use a format string. printf is a special function which takes a variable number of arguments; the first argument is a format string, and the remaining arguments are inserted into the output at appropriate places based on the format. Let's say I have some int variable called x. I could print the value of x with the following line of code:
printf("%u marks the spot!\n", x);
The %u is a format specifier, and it means "format an argument to printf as an unsigned integer." (The format specifier in position n formats the argument in position n + 1.) For more information on printf, read the man page! (Note that there is often a man page for printf in section 1; you'll want to read the one in section 3.)
