Home CS439

CS439: Principles of Computer Systems

Homework 1, Part 1

Due: 8:45a Friday, January 24, 2014

Part 1 of the Homeworks must be submitted electronically. Please refer to the homework turnin instructions.

  1. Write a C program that expects an integer command line argument, x, and then uses recursion to print, Hello, World! x times. Paste the code into your homework.
  2. Compile your program from 1) on the CS Linux machines using gcc. Use command line options to:
    1. Name the executable file hello.
    2. Set the optimization to level 0.
    3. Turn on the debugging information.

    You may find the overview of gcc on the course homepage helpful.

    What command did you use?

  3. Run your program from within gdb with an argument of 5.
    1. Set a break point at the entrance to your recursive function.
    2. Print the value of the argument to the function.
    3. Step through each line of the function until Hello, World! is printed.
    4. Continue the execution until the next breakpoint is reached. Check the value of the argument, and, if it is not the last recursive call, continue the program.
    5. On the last recursive call, print a backtrace of the call stack. Identify the line number of the call to your recursive function in main().

    What commands did you use in gdb? What is the line number of your recursive call?

  4. Return to your program and add an if statement in main after the recursive call that reads:
    if(x == x)
       printf("Goodbye\n");
    
    where x is the name of the parameter to the recursive call.

    Recompile your program with the optimizations set to level 4.

    Run your program using gdb, but set a breakpoint in main for after the recursive call but before the print statement. What happens?

  5. System Calls vs. Procedure Calls: How much more expensive is a system call than a procedure call? Write a simple test program to compare the cost of a simple procedure call to a simple system call ("getuid()" is a good candidate on UNIX; see the man page.) (Note: be careful to prevent the optimizing compiler from "optimizing out" your procedure calls. Do not compile with optimization on.)
    • Explain the difference (if any) between the time required by your simple procedure call and simple system call by discussing what work each call must do (be specific). [Note: Do not provide the source code for your program, just the results].

    Hint: You should use system calls such as gethrtime() or gettimeofday() for time measurements. Design your code such that the measurement overhead is negligible. Also, be aware that timer values in some systems have limited resolution (e.g., millisecond resolution).