CSE333 Homework 0

Out: Wednesday September 25th, 2013
Due: Friday Sepember 27th, 2013 by 11:15am.

[ summary | part a | part b | part c | part d | how to submit | grading ]

Summary.

For homework #0, you will fetch a source distribution that we have prepared. Next, you will trivially modify a simple hello world C application, use "make" to compile it, and run it to learn the magic code. You will also run a tool that checks your code for memory leaks. Finally, you will package up and submit your code.

As you can probably tell, the real goal of this homework is to make sure that all of the course infrastructure is working for you.
Part A -- Fetch the code

Overview

For this quarter, you'll fetch code from the CSE file system using scp. The code is packages as a "gzip'ed tar archive" so you'll need to un-gzip and un-tar it to produce your working directory. Once you've done that, you'll have the homework files and can edit them locally until ready to turn them in. We'll use the course drop box for turn-in.

You may want to use some source control system to help manage your files, and to provide a crude form of backup for them. Popular options are git and subversion.


Fetch the code

In the directory where you want to create a directory named hw0 containing your code:

bash% # note the period at the end of the command line
bash% # replace username with your CSE login name
bash% scp -r username@attu.cs.washington.edu:/cse/courses/cse333/13au/hw0.tar.gz .


Expand the tar file

First, have a look at what is about to happen:

bash% tar tzf hw0.tar.gz
hw0/
hw0/hello_world.c
hw0/.gitignore
hw0/Makefile
LICENSE.TXT
bash%
The files listed will be created when we expand the tar file. The paths are relative to the current working directory. So, file LICENSE.TXT will be created in the current working directory, as well as subdirectory hw0 and the files it contains.

bash% # expand the tar file
bash% tar xzf hw0.tar.gz
bash% cd hw0

Part B -- edit, compile, and run hello_world

In the hw0 directory, type make. This command will use the instructions in file Makefile to compile the hello_world application using the gcc compiler. Then run hello_world by typing the command ./hello_world. You should see one line of output from running the program:

bash% make
gcc -g -Wall -O0 -c -std=c99 hello_world.c
gcc -g -Wall -O0 -o hello_world hello_world.o
bash% ./hello_world
The magic code is: <xxxxx>
bash%

for some <xxxxx> we're not telling you here.

Now, edit hello_world.c using your favorite Unix-based editor (mine is emacs), and change the printf so that it instead says:

bash% ./hello_world
The magic word is: <xxxxx>
bash% 

Execute make to rebuild, and then re-run hello_world to make sure it does what you expect.

Part C -- experience the gdb debugger

You're unlikely to have any run time bugs in this homework, but let's use the debugger so that you know what it can do. Here is a session that launches the debugger, sets a breakpoint on source line 27 of hello_world.c, prints the values of a couple of variables and an expression, prints a "backtrace" of the stack (showing the sequence of procedure calls that led to executing line 27 of hello_world.c), and then continues execution. Type the italicized lines.

bash$ gdb ./hello_world 
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/zahorjan/cse333/hw0/hello_world...done.
(gdb) b 27
Breakpoint 1 at 0x400511: file hello_world.c, line 27.
(gdb) r
Starting program: /home/zahorjan/cse333/hw0/hello_world 

Breakpoint 1, main (argc=1, argv=0x7fffffffe0c8) at hello_world.c:27
27  printf("The magic code is: %X\n", a + b);
(gdb) p argv[0]
$1 = 0x7fffffffe394 "/home/zahorjan/cse333/hw0/hello_world"
(gdb) p a
$1 = -889262067
(gdb) p a+b
$3 = -559038737
(gdb) bt
#0  main (argc=1, argv=0x7fffffffe0c8) at hello_world.c:27
(gdb) c
Continuing.
The magic code is: <xxxxxxxx>
[Inferior 1 (process 6760) exited normally]
(gdb) q
bash$
Note: For gdb to be useful, you must give the -g switch to gcc when compiling. The make file we distributed does that.
Part D -- verify you have no leaks

Throughout the quarter, we'll also be testing whether your code has any memory leaks. We'll be using Valgrind to do this. Try out Valgrind for yourself, to make sure you can run it:

bash% valgrind --leak-check=full ./hello_world # there are two hypens before leak-check

Note that Valgrind prints out that no memory leaks were found.

Turn in

When you're ready to turn in your assignment, do the following:

  1. Create a README.TXT file in directory hw0 that contains your name, student number, and UW email address. As well, write a sentence that reveals the magic word you learned from Part B.

  2. Execute the following commands:
    bash$ ls
    hello_world  hello_world.c  hello_world.o  Makefile README.TXT
    bash$ make clean
    /bin/rm -f *.o *~ hello_world
    bash$ ls
    hello_world.c  Makefile README.TXT
    bash$ cd ..
    bash$ tar czf hw0_<username>.tar.gz hw0  # substitute your CSE user name
    bash$
    

  3. Verify that what you're turning in is what you expect to be turning in:
    $ tar tzf hw0_<username>.tar.gz 
    hw0/
    hw0/hello_world.c
    hw0/README.TXT
    hw0/.gitignore
    hw0/Makefile
    
  4. Turn in file hw0_<username>.tar.gz using the course dropbox.

  5. Fill out the following survey to give us feedback on your background, which will help us tune future assignments:
    https://catalyst.uw.edu/webq/survey/zahorjan/213189

Grading

For hw0, we'll give you 1 point if you learn the magic code, correctly submit your code, and submit the survey.