Lab Assignment--Process Memory
To get the files for this exercise, please use the following commands:
cd ~
mkdir -p cs330/lab1
cd cs330/lab1
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:
- if necessary create your cs330/lab1 directory and start the script command there
(hint: script commands.txt will start the script and save it to commands.txt)
- create and change into a testscript directory
- create three new directories named
foo, bar, and baz
- display the contents of the current directory
- change into your foo directory
- without using a text editor, create three files called red, green, and blue
- check the timestamps with ls -l
- create a directory named backup
- copy the file blue to the directory backup and rename it blue.bak (try to do it in one statement)
- change working directory to backup
- check the timestamps and permissions with ls -l
- move up one directory
- copy the contents of the backup directory to the baz directory created in Step 6
(hint: you might have to use "..")
- without changing the directory, list the contents of the baz directory
- remove the original backup directory and its contents
- list the .c files in the lab1 directory to show the .c files from Lab1.zip are there
- 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.
- Compile and run the program: memory_segments.c.
Several addresses are output from the program including:
- etext
- edata
- end
- functions
- global variables
- static local variables
- heap variables
- 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
-
As variables are added to the stack, do the addresses get smaller or larger?
-
Do variables stored on the stack ever have the same address as other variables? Why or why not?
-
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.
-
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
- Script of the Linux/Unix Commands from Part 1 (named with .txt extension)
- The code in lab1.c with the new recursive factorial function
Remember to include:
- Answers to questions 3, 4, and 5 (as comments at the top of your code file)
- The memory diagram (as comments immediately below the answers in your code file)
- Script (named with a .txt extension) of two sample runs of the recursive factorial function showing: