COMP3000 Operating Systems W22: Tutorial 2: Difference between revisions
Lianyingzhao (talk | contribs) mNo edit summary |
Lianyingzhao (talk | contribs) mNo edit summary |
||
Line 32: | Line 32: | ||
#:How does the size of hello.dynamic compare with that of hello.static? <u>Why?</u> | #:How does the size of hello.dynamic compare with that of hello.static? <u>Why?</u> | ||
# Download and compile another flavor of hello.c: [https://people.scs.carleton.ca/~lianyingzhao/comp3000/w22/tut2/syscall-hello.c syscall-hello.c]. Build one static version and one dynamic version as well. | # Download and compile another flavor of hello.c: [https://people.scs.carleton.ca/~lianyingzhao/comp3000/w22/tut2/syscall-hello.c syscall-hello.c]. Build one static version and one dynamic version as well. | ||
#: Now considering hello.dynamic, hello.static, syscall-hello.dynamic and syscall-hello.static, see what system calls each program produces by running <tt>strace -o sys-somename.log ./somename.static</tt> (or <tt>./somename.dynamic</tt>). Which version generates more system calls? <u>Why?</u> Note: system calls are saved in the log file <tt>sys-somename.log</tt>. Feel free to save them in a different file for each version. | #: Now considering hello.dynamic, hello.static, syscall-hello.dynamic and syscall-hello.static, see what system calls each program produces by running <tt>strace -o sys-somename.log ./somename.static</tt> (or <tt>./somename.dynamic</tt>). Which version generates more system calls? <u>Why?</u> | ||
#: Note: system calls are saved in the log file <tt>sys-somename.log</tt>. Feel free to save them in a different file for each version. | |||
# See what library calls each program produces by running <tt>ltrace -o lib-somename.log ./somename.dynamic</tt> (or <tt>./somename.static</tt>). Which version generates more library calls? <u>Why?</u> | # See what library calls each program produces by running <tt>ltrace -o lib-somename.log ./somename.dynamic</tt> (or <tt>./somename.static</tt>). Which version generates more library calls? <u>Why?</u> | ||
# Remember when building hello in Tutorial 1 you did not use <tt>-z lazy</tt>. Comparing <tt>hello</tt> with <tt>hello.dynamic</tt>, any difference? <u>Why this difference?</u> | |||
# Using ldd, what dynamic library dependencies does the | # Using ldd, what dynamic library dependencies does the | ||
===Part C=== | |||
Compile and run [https://people.scs.carleton.ca/~lianyingzhao/comp3000/w22/tut2/3000memview.c | 3000memview.c], then consider the following questions (if they turn out to be difficult, document your exploration and get some thinking). | Compile and run [https://people.scs.carleton.ca/~lianyingzhao/comp3000/w22/tut2/3000memview.c | 3000memview.c], then consider the following questions (if they turn out to be difficult, document your exploration and get some thinking). | ||
Revision as of 05:44, 14 January 2022
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
- 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
- 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
- Then compile a dynamically linked version:
- gcc -O2 -z lazy hello.c -o hello.dynamic
- How does the size of hello.dynamic compare with that of hello.static? Why?
- Download and compile another flavor of hello.c: syscall-hello.c. Build one static version and one dynamic version as well.
- Now considering hello.dynamic, hello.static, syscall-hello.dynamic and syscall-hello.static, see what system calls each program produces by running strace -o sys-somename.log ./somename.static (or ./somename.dynamic). Which version generates more system calls? Why?
- Note: system calls are saved in the log file sys-somename.log. Feel free to save them in a different file for each version.
- See what library calls each program produces by running ltrace -o lib-somename.log ./somename.dynamic (or ./somename.static). Which version generates more library calls? Why?
- Remember when building hello in Tutorial 1 you did not use -z lazy. Comparing hello with hello.dynamic, any difference? Why this difference?
- Using ldd, what dynamic library dependencies does the
Part C
Compile and run | 3000memview.c, then consider the following questions (if they turn out to be difficult, document your exploration and get some thinking).
- Why are the addresses inconsistent between runs?
- 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.)
- Change each malloc() call to allocate more than 128K. What happens to the values of sbrk? Why? (Hint: use strace)
- 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?