* OS overview (quiz answers), cont. # What is a "context switch"? When switching to run another process, has to load CPU/MMU and other states for the new process. Resume new process from exactly where it last ran. "mode switch": switching b/t user mode and kernel mode. - user mode: protect from access to h/w directly, translate virt/phys mem, etc. - kernel mode: no protection or addr translation. # What is a "system call"? APIs that the kernel exports, inside the kernel. calls into libraries executing in user-mode. e.g., libc. either call looks the same: has a name, takes 0 or more args, may return a value, and have some side effects (memory allocated, file created, etc.) Kernel code checks for various conditions to protect itself, the system, and even one user from another. # What does the "two hand-clock" refer to? Hint: Caching, MMU... A page-reclamation algorithm, cache cleaning, cache flushing, eviction. Known property: if you accessed an object recently, you'll probably access it again in the near future. Solution: LRU, or Least Recently Used. LRU alg 1: - every time I insert a new item into cache, or reuse one, add a timestamp along with the item. - when I need to clean/evict: 1. sort all items by timestamp as "key" 2. remove oldest items until enough has been cleaned (e.g., threshold reached, enough room made, etc.) - Alg 1 is slow: we have to sort, best O(n*logn); also have to get and store a time (sampling clock register, e.g., TSC reg, can take dozens of instructions). - Alg 1 also consumes mem (to store timestamps), thus taking away PHYSICAL mem away from user processes. LRU alg 2: using the MMU - every time MMU successfully translates a virt to phys addr, it also turns on a single "recently used" bit. Only one bit (small amount of memory). Also fast: done at MMU (hardware) speeds. - define a time period, start time T0 and end time T1. - at T0: you reset all "recently used" bits to 0 (via MMU h/w) - wait until time T1. Meanwhile MMU sets any recently used mem/page to "1". - at T1: any page with recently==0 means it was NOT used in interval T1-T0. - so can evict those pages with recently bit set to 0. - T0 and T1 act as two hands of an (analog) clock. - T1-T0 interval controls how many pages may be marked as recent or not - most OSs allow you to set this interval, or control it dynamically. - If T1-T0 interval is too long, many pages are marked recent, and I may not be able to evict enough pages. - conversely, if interval is too short: we may inadvertently throw away pages that are needed shortly in time, and have to pay the penalty of getting that data again from its slow source (e.g., disk/network). - Note: dirty pages have to be flushed first (discuss in detail later in course). # What does the unlink(2) system call? UNIX man(ual) pages are broken into sections: 1. user programs 2. system calls 3. library calls ... 8. sysadmin programs Any system call is in section 2, e.g., stat(1) refers to the user program /usr/bin/stat stat(2) refers to the system call "stat" $ man stat $ man 1 stat $ man 2 stat unlink(2): used by /bin/rm -- to delete a file. called "unlink" and not "delete" b/c it only removes a name of a file from the namespace (file system). In unix, a file (inode) can have MULTIPLE names, anywhere in the file system. unlink removes just ONE name. if it happens to be the only or last name, then and only then does the actual inode+file get removed. # What does the ln -s command do? creates a symlink (symbolic link) to a file, even dangling symlinks that point to non-existent files. # What does this command do: gcc -W main.c -o bar -lssl $ gcc -W main.c -o bar -lssl gcc: compile a C program using the GNU C compiler -W: enable some warnings main.c: the program you want to compile -o: output the binary name (else a.out) bar: name of the output binary -lssl: link with libssl.so or libssl.a -Wall: enables a lot more warnings -Werror: turns any warning into a compile time error Important b/c - in userland, if you declare a variable and not initialize it, most likely it'll be 0 b/c kernel gives new pages to a process AFTER zeroing them out. - but in kernel (and any long running program), the values of pages may not be zero and uninitialized vars WILL have non-zero values. - so let gcc compiler catch such problems and FIX all of them.