Operating Systems 2019F Lecture 11: Difference between revisions
Created page with "==Topics== * UNIX pipes * Assembly language, try 2 ** CPU registers: general, stack, base, condition ** CPU stack, stack allocation ** function calling conventions" |
No edit summary |
||
Line 1: | Line 1: | ||
==Video== | |||
The video from the lecture given on October 9, 2019 [https://homeostasis.scs.carleton.ca/~soma/os-2019f/lectures/comp3000-2019f-lec11-20191009.m4v is now available]. | |||
==Topics== | ==Topics== | ||
Line 6: | Line 10: | ||
** CPU stack, stack allocation | ** CPU stack, stack allocation | ||
** function calling conventions | ** function calling conventions | ||
==Notes== | |||
<pre> | |||
Lecture 11 | |||
---------- | |||
Assembly language | |||
In UNIX, a process is one or more execution contexts plus an address space | |||
Address space: range of memory, parts that may or may not be allocated | |||
execution context: set of CPU registers, state of a core | |||
- instruction pointer | |||
- stack pointer | |||
- general registers | |||
- condition register | |||
modern systems multiplex the CPU between processes | |||
- saving and restoring CPU registers, switching address spaces | |||
the stack is important because that's how we do the cheap memory | |||
management associated with functions | |||
To call a function, you | |||
- gather arguments <--- pushed onto the stack or put in registers | |||
- save where you are <--- save instruction pointer (IP) to the stack (push) | |||
- run the function <--- call | |||
- continue on <--- function call ret, popping IP from the stack | |||
When a function runs, you | |||
- save registers <--- the caller may care about state of registers, so | |||
push them on stack | |||
- allocate local variables <--- change stack pointer | |||
- do work | |||
- restore registers <--- pop them | |||
- return <--- pop return address (IP of caller) | |||
Make sure you understand push, pop, call, ret | |||
Midterm review, part 1 | |||
---------------------- | |||
How does a shell work? | |||
Key system calls for managing processes | |||
* fork | |||
* execve | |||
* wait (+ exit) | |||
* sigaction | |||
* kill | |||
File I/O | |||
* open/openat | |||
* read | |||
* write | |||
* lseek | |||
* mmap | |||
* close | |||
Directory I/O | |||
* opendir/open | |||
* readdir/getdents64 | |||
* closedir/close | |||
Signals | |||
- messages your process can receive at any time | |||
- when your process has a signal, the signal handler *will be called* | |||
- the instruction pointer will be changed to the signal handler | |||
mmap | |||
- normal file I/O is read/write | |||
- mmap allows us to map file data into a process's address space | |||
- accessing memory is equivalent to doing read/write to file | |||
Couple of key variations | |||
- read-only mmap of file, normaly with PROT_EXEC | |||
* load code (executable, libraries) | |||
* read-only data (no PROT_EXEC) | |||
- read-write mmap, shared, of file | |||
* modifying file on disk | |||
- read-write mmap, private, of file | |||
* copy file to memory, modify memory | |||
- read-write mmap, anonymous, shared | |||
* allocate memory shared between processes | |||
- read-write mmap, anonymous, private | |||
* allocate memory private to process | |||
File permissions, hard links, symbolic links | |||
* file data versus metadata | |||
inodes contain | |||
- file owner, group, perms | |||
- size of file | |||
- modification times | |||
- data or references to other data blocks | |||
hard link is a filename that refers to an inode | |||
lseek | |||
- associated with every open file is a file position | |||
- lseek lets you change it | |||
- lseek past the end of a file and then write - create a "hole" | |||
- all zeros are filled in | |||
- space in between not allocated | |||
</pre> |
Latest revision as of 22:05, 9 October 2019
Video
The video from the lecture given on October 9, 2019 is now available.
Topics
- UNIX pipes
- Assembly language, try 2
- CPU registers: general, stack, base, condition
- CPU stack, stack allocation
- function calling conventions
Notes
Lecture 11 ---------- Assembly language In UNIX, a process is one or more execution contexts plus an address space Address space: range of memory, parts that may or may not be allocated execution context: set of CPU registers, state of a core - instruction pointer - stack pointer - general registers - condition register modern systems multiplex the CPU between processes - saving and restoring CPU registers, switching address spaces the stack is important because that's how we do the cheap memory management associated with functions To call a function, you - gather arguments <--- pushed onto the stack or put in registers - save where you are <--- save instruction pointer (IP) to the stack (push) - run the function <--- call - continue on <--- function call ret, popping IP from the stack When a function runs, you - save registers <--- the caller may care about state of registers, so push them on stack - allocate local variables <--- change stack pointer - do work - restore registers <--- pop them - return <--- pop return address (IP of caller) Make sure you understand push, pop, call, ret Midterm review, part 1 ---------------------- How does a shell work? Key system calls for managing processes * fork * execve * wait (+ exit) * sigaction * kill File I/O * open/openat * read * write * lseek * mmap * close Directory I/O * opendir/open * readdir/getdents64 * closedir/close Signals - messages your process can receive at any time - when your process has a signal, the signal handler *will be called* - the instruction pointer will be changed to the signal handler mmap - normal file I/O is read/write - mmap allows us to map file data into a process's address space - accessing memory is equivalent to doing read/write to file Couple of key variations - read-only mmap of file, normaly with PROT_EXEC * load code (executable, libraries) * read-only data (no PROT_EXEC) - read-write mmap, shared, of file * modifying file on disk - read-write mmap, private, of file * copy file to memory, modify memory - read-write mmap, anonymous, shared * allocate memory shared between processes - read-write mmap, anonymous, private * allocate memory private to process File permissions, hard links, symbolic links * file data versus metadata inodes contain - file owner, group, perms - size of file - modification times - data or references to other data blocks hard link is a filename that refers to an inode lseek - associated with every open file is a file position - lseek lets you change it - lseek past the end of a file and then write - create a "hole" - all zeros are filled in - space in between not allocated