COMP3000 Operating Systems W22: Tutorial 2

From Soma-notes
Revision as of 03:58, 14 January 2022 by Lianyingzhao (talk | contribs)

In this tutorial, you will revisit the lifecyle of a program, from source code, to an executable (binary image), and further to being loaded into the address space. Then from a different angle, you can see when in execution, how the program (now a process) makes different types of calls to function, and how its memory is laid out.

Tutorials are graded based on participation and effort (so no need to try to have the “correct” answers — what matters is the process), but you should still turn in your work. Submit your answers on Brightspace as a single text file named "<username>-comp3000-t2.txt" (where username is your MyCarletonOne username). The first four lines of this file should be "COMP 3000 Tutorial 2", your name, student number, and the date of submission.

The deadline is usually four days after the tutorial date (see the actual deadline on the submission entry). Note that the submission entry is enforced by the system, so you may fail to get the effort marks even if it is one minute past the deadline.

You should also check in with your assigned TA online (by responding to the poll in the Teams channel tutorials-public or the private channel). Your TA will be your first point of contact when you have questions or encounter any issues during the tutorial session.

You get 1.5 marks for submitting answers that shows your effort and 0.5 for checking in, making this tutorial worth 2 points total.

Building Your Program

Assuming you use a compiled programming language like C, you will involve the following steps (implicitly) to build your program:

  • Compile source (C) code into assembly code (.s files).
    You can see it using gcc -S -O2 hello.c.
  • Assemble assembly code into machine code placed in object code files (.o files).
    To avoid the .o file being deleted automatically, you can do the compilation alone without linking by: gcc -c -O2 hello.c.
  • Link object code files together to create a runnable binary image (ELF files, no extension name).
    This is the way you did it in Tutorial 1.

Tasks/Questions

  1. Looking at the .s file produced from gcc -S -O2 hello.c, do you see anything familiar and discussed in last week's lecture?

Compile and run | 3000memview.c, then consider the following questions (if they turn out to be difficult, document your exploration and get some thinking).

  1. Why are the addresses inconsistent between runs?
  2. Roughly where does the stack seem to be? The heap? Code? Global variables? (hints: recall the memory image layout of a process discussed in the lecture and you can search for a more detailed one somewhere; local variables go to the stack; initialized data and global variables go to the data segment, data allocated at runtime go to the heap.)
  3. Change each malloc() call to allocate more than 128K. What happens to the values of sbrk? Why? (Hint: use strace)
  4. Add more code and data to the program, and add more printf's to see where things are. Are things where you expect them to be?