COMP3000 Operating Systems W22: Tutorial 2

From Soma-notes
Revision as of 00:15, 14 January 2022 by Lianyingzhao (talk | contribs)
Jump to navigation Jump to search

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

Part A

  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?

Part B

  1. By default, gcc-produced binaries are dynamically linked (so at runtime they will require dynamic libraries to be present on the system). To compile a binary that is statically linked (so it has no external runtime library dependencies), instead do this:
    gcc -O2 -static hello.c -o hello.static
    Compile hello.c, statically and dynamically. How does the size compare with the one linked without -static? Why?
  2. See what system calls the static version produces by running strace -o syscalls-static.log ./hello.static (or ./syscall-hello.static). Do the same with the dynamic version. Which version generates more system calls? Note: system calls are saved in the log file syscalls-static.log. Feel free to save them in a different file.
  3. See what library calls the dynamic version of hello and syscall-hello produces by running ltrace -o library-static.log ./hello.dynamic (or ./syscall-hello.dynamic). Do the same for the static version. Which version generates more library calls? (If ltrace is not installed, run sudo apt-get install ltrace)
  4. Similar to $PATH for program binaries, the operating system should also know where to look for dynamic libraries when a dynamically linked program needs them at run-time. Th counterpart is $LD_LIBRARY_PATH. The prebuilt binary hello-main depends on libhello.so (you can check with ldd). Let’s build it:

gcc -shared -o libhello.so -fPIC hello-lib.c -shared tells the linker to produce a dynamic library and -fPIC means position independent code as is required to be a library. Now, how do you make hello-main run?

  1. Using ldd, what dynamic library dependencies does the

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?