Lab Assignment--Process Memory


To get the files for this exercise, please use the following commands:

cd ~
mkdir cs330
cd cs330
wget www.labs.cs.uregina.ca/330/ProcessMem/Lab1.zip
unzip Lab1.zip

Part 1 - Linux/Unix Commands

Before you get started, you need to know the script command.

You can think of the script as a "recording" of your Linux/Unix commands.

A script is started by typing: script filename.
You end the script by typing exit.
Follow all of the steps:
  1. start a script in your cs330 directory
  2. (hint: script commands.txt will start the script and save it to commands.txt)
  3. (create) and change into a lab1 directory
  4. create and change into a testscript directory
  5. create three new directories named foo, bar, and baz
  6. display the contents of the current directory
  7. change into your foo directory
  8. without using a text editor, create three files called red, green, and blue
  9. check the timestamps with ls -l
  10. create a directory named backup
  11. copy the file blue to the directory backup and rename it blue.bak (try to do it in one statement)
  12. change working directory to backup
  13. check the timestamps and permissions with ls -l
  14. move up one directory
  15. copy the contents of the backup directory to the baz directory created in Step 6 (hint: you might have to use "..")
  16. without changing the directory, list the contents of the baz directory
  17. remove the original backup directory and its contents
  18. move the sample .c files from Lab1.zip into the lab1 directory
  19. exit from the script (hint: exit)

Part 2 - Process Memory

The purpose of this part is to explore the memory associated with the user process: text, data, and stack.
  1. Compile and run the program: memory_segments.c.
  2. Several addresses are output from the program including:

  3. Your task is to fill in addresses of the variables, and functions in lab1.c.
    You will change the addresses to match what you are seeing when you run memory_segments.c.

    Please remember to include the "etext", "edata", and "end" addresses

  4. As variables are added to the stack, do the addresses get smaller or larger?

  5. Do variables stored on the stack ever have the same address as other variables? Why or why not?

  6. Where would you expect variables (or arguments) in recursive functions to be stored (stack, heap, or other data segment)? When you are finished step 6 below, comment on whether your expectations were correct or not.

  7. Test your expectation by creating a recursive factorial function in lab1.c . For instance, the factorial of 5 is represented by 5! and is calculated as 5x4x3x2x1=120.

    In the factorial function, you will print the address of the factorial function and the address of the argument passed to it.

    In main, you will prompt the user for what factorial they will want to calculate and send that input as an argument to the function. In main, you will also print the value returned from the factorial function.

    Note: you will be expected to use scanf and printf

    The idea behind the recursive factorial function is the following:
    Fn = {
    1 if n = 0 (base case)
    n*Fn-1       if n >= 1 (recursive step)

Deliverables:

Submit 3 files to URCourses

  1. Script of the Linux/Unix Commands from Part 1 (named with .txt extension)
  2. The code in lab1.c with the new recursive factorial function
    Remember to include:
  3. Script (named with a .txt extension) of two sample runs of the recursive factorial function showing: