<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2020W_Lecture_11</id>
	<title>Operating Systems 2020W Lecture 11 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2020W_Lecture_11"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2020W_Lecture_11&amp;action=history"/>
	<updated>2026-06-02T22:26:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2020W_Lecture_11&amp;diff=22576&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  Video from the lecture given on February 12, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec11-20200212.m4v is now available]....&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2020W_Lecture_11&amp;diff=22576&amp;oldid=prev"/>
		<updated>2020-03-20T02:34:20Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  Video from the lecture given on February 12, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec11-20200212.m4v is now available]....&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
Video from the lecture given on February 12, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec11-20200212.m4v is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 11&lt;br /&gt;
----------&lt;br /&gt;
* Assignment 2&lt;br /&gt;
* busy mounts&lt;br /&gt;
* node.js&lt;br /&gt;
* mmap&lt;br /&gt;
&lt;br /&gt;
why do the functions we call in node have a &amp;#039;Sync&amp;#039; at the end?&lt;br /&gt;
 - because by default I/O is async in JavaScript/node&lt;br /&gt;
   (upon completion a function is called, and until then node does&lt;br /&gt;
    other things)&lt;br /&gt;
 - don&amp;#039;t worry about it for now, just use the Sync versions of functions&lt;br /&gt;
&lt;br /&gt;
To exit node, type Ctrl-D&lt;br /&gt;
 - this makes the terminal send an end of file to the process&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To solve #17 on the assignment, you want to erase an inode for a directory&lt;br /&gt;
 - the contents of the directory will be orphaned, thus landing in&lt;br /&gt;
   lost+found after filesystem repair&lt;br /&gt;
 - note you may have to force a filesystem check after erasing part of the&lt;br /&gt;
   &amp;quot;disk&amp;quot;&lt;br /&gt;
 - you can figure out where precisely an inode is using dumpe2fs and the info&lt;br /&gt;
   here (and the inode number):  &lt;br /&gt;
 &lt;br /&gt;
   https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Finding_an_Inode &lt;br /&gt;
&lt;br /&gt;
   if you are fuzzy on concepts, look in the textbook (in filesystem&lt;br /&gt;
   implemenation)!&lt;br /&gt;
&lt;br /&gt;
 - can filesystems be corrupted if an unmount is forced?  YES&lt;br /&gt;
    - because not all changes may have been written to disk&lt;br /&gt;
    - unmount -&amp;gt; removing the filesystem (i.e., pulling a USB stick)&lt;br /&gt;
    - so filesystem may be in an inconsistent state, with data missing&lt;br /&gt;
      or only partial changes made (which may make no sense on their own)&lt;br /&gt;
    - normally with USB disks data is flushed to them frequently&lt;br /&gt;
      so just pulling them normally doesn&amp;#039;t result in corruption.&lt;br /&gt;
      *normally*, not always (better to manually unmount before removing&lt;br /&gt;
      a device)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 - a corrupted filesystem means the metadata of the filesystem has been messed&lt;br /&gt;
   up, thus endangering all the data stored on the system&lt;br /&gt;
     - what if a block is marked as free but is actually being used to store&lt;br /&gt;
       file data?  This is a type of inconsistency that can result through&lt;br /&gt;
       filesystem corruption&lt;br /&gt;
 - in other words, filesystems are data structures.  data structures have&lt;br /&gt;
   invariants.  A corrupted filesystem is one where its invariants no longer&lt;br /&gt;
   hold.&lt;br /&gt;
&lt;br /&gt;
 - messing with the root filesystem is very dangerous; others can be, but&lt;br /&gt;
   it depends on how it is being used.&lt;br /&gt;
&lt;br /&gt;
 - RAID is a separate concept from filesystems (RAID is at the block level)&lt;br /&gt;
&lt;br /&gt;
 - If you are curious what happens, mess with your VM!  (Or make a new&lt;br /&gt;
   one to destroy.)  Discovery requires experimentation, after all.&lt;br /&gt;
&lt;br /&gt;
mmap&lt;br /&gt;
 - not on midterm, but will be covered later, is in tutorial 5&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Key idea of mmap is that we can associate files with the memory of a process&lt;br /&gt;
 - interacting with a range of addresses in memory &amp;lt;-&amp;gt; read/write to file on&lt;br /&gt;
   disk&lt;br /&gt;
 - &amp;quot;memory-mapped I/O&amp;quot;&lt;br /&gt;
&lt;br /&gt;
 - in 3000test, reading from data[] is actually reading from the file fn&lt;br /&gt;
   on disk&lt;br /&gt;
     - the kernel automatically takes care of reading the file and filling&lt;br /&gt;
       in memory as needed&lt;br /&gt;
&lt;br /&gt;
 - mostly, when you mmap a file into memory, you could have just done&lt;br /&gt;
   read/writes to the file&lt;br /&gt;
     - but it can be more convenient to directly interact with&lt;br /&gt;
       data in an mmap&amp;#039;d buffer rather than interacting with a temporary&lt;br /&gt;
       buffer that you then have to read and write (to/from the file)&lt;br /&gt;
 - but there are some other cool tricks&lt;br /&gt;
   - dynamic libraries are mmap&amp;#039;d read only from disk at program start&lt;br /&gt;
     - key benefit - RAM allocated to these libraries can be shared&lt;br /&gt;
       across processes&lt;br /&gt;
     - The &amp;quot;SHR&amp;quot; column in top is mostly due to libraries that have been&lt;br /&gt;
       mmap&amp;#039;d by multiple processes (mapped multiple times but only&lt;br /&gt;
       consuming RAM once)&lt;br /&gt;
     - how this is implemented will be discussed later&lt;br /&gt;
 - dynamic linking means library code is only loaded at runtime (generally&lt;br /&gt;
   via mmap).  static linking means library code is added to the binary at&lt;br /&gt;
   compile/link time&lt;br /&gt;
     - thus code for printf resides in the dynamic C libary that is mmap&amp;#039;d in&lt;br /&gt;
&lt;br /&gt;
mmap can take a file descriptor (to specify the file to be mapped into memory), but it doesn&amp;#039;t have to&lt;br /&gt;
 - mmap&amp;#039;s with a file desciptor of -1 are known as &amp;quot;anonymous mmaps&amp;quot;&lt;br /&gt;
 - (you should also set the MAP_ANONYMOUS flag)&lt;br /&gt;
&lt;br /&gt;
anonymous maps are used to allocate memory from the kernel&lt;br /&gt;
 - anonymous maps make no sense for read-only memory (there&amp;#039;s nothing there),&lt;br /&gt;
   so normally anonymous maps are read/write&lt;br /&gt;
&lt;br /&gt;
anonymous mmap is normally used for larger memory allocations, while sbrk is used for smaller ones&lt;br /&gt;
 - the &amp;quot;heap&amp;quot; really isn&amp;#039;t one thing&lt;br /&gt;
 - how this is all implemented depends on how malloc is written&lt;br /&gt;
   - there are actually many different kinds of malloc, and&lt;br /&gt;
     C apps often have custom memory allocators that may use sbrk and mmap&lt;br /&gt;
     themselves&lt;br /&gt;
&lt;br /&gt;
mmaps of files follow the same permissions as always (note you do it to&lt;br /&gt;
a file descriptor, so you have to get past the checks in open)&lt;br /&gt;
&lt;br /&gt;
When you look at /proc/&amp;lt;PID&amp;gt;/maps, the entries have permissions&lt;br /&gt;
 - each entry is a range of memory for that process&lt;br /&gt;
 - memory has permissions (readable, writable, executable)&lt;br /&gt;
   - permissions are for security and performance&lt;br /&gt;
   - read-only memory is easier to share&lt;br /&gt;
   - don&amp;#039;t want bytes in memory to be interpreted as code&lt;br /&gt;
     if we don&amp;#039;t want to, that&amp;#039;s a common form of attack&lt;br /&gt;
     (machine code injection, e.g. buffer overflow attacks)&lt;br /&gt;
   - if you try writing to read-only memory, your process will be&lt;br /&gt;
     sent a signal (SIGSEGV or something similar)&lt;br /&gt;
   - the memory map is how the kernel decides whether a memory access&lt;br /&gt;
     (pointer access) is valid or not&lt;br /&gt;
&lt;br /&gt;
mmap is not necessarily faster than read&lt;br /&gt;
 - main performance hit is accessing the disk normally&lt;br /&gt;
 - all depends on data access patterns&lt;br /&gt;
    - with read/write you have more explicit control&lt;br /&gt;
 &lt;br /&gt;
To really understand mmap, you have to understand how virtual memory is implemented&lt;br /&gt;
&lt;br /&gt;
Maps can be shared or private&lt;br /&gt;
 - shared maps mean changes to the memory -&amp;gt; change file on disk&lt;br /&gt;
 - with private maps, changes to memory don&amp;#039;t change files on disk&lt;br /&gt;
&lt;br /&gt;
Also, fork and mmap interact&lt;br /&gt;
 - with shared mmaps (files or anonymous), memory is shared&lt;br /&gt;
   between parent and child&lt;br /&gt;
 - with private mmaps, memory is copied from parent to child&lt;br /&gt;
&lt;br /&gt;
So you can actually control what parts of memory are copied and which&lt;br /&gt;
are shared based on flags to mmap&lt;br /&gt;
 - so can get shared memory benefits of multi-threaded processes&lt;br /&gt;
   without threads (can instead have processes that share some ranges of memory)&lt;br /&gt;
&lt;br /&gt;
midterm review is on Feb. 26th, midterm is on Feb 28th (in class)&lt;br /&gt;
 - I will be there for the midterm, review wil be online&lt;br /&gt;
&lt;br /&gt;
Remember that memory permissions and file permissions are different&lt;br /&gt;
 - same kinds of bits, but applying to completely different data&lt;br /&gt;
 - file permissions are for inodes&lt;br /&gt;
 - memory permissions are for ranges of memory, parts of a process&amp;#039;s address&lt;br /&gt;
   space&lt;br /&gt;
    - DON&amp;#039;T CONFUSE THEM!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>