Alistair
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # COMP3301 Exam Notes # Definitions Trap : An interrupt generated by software # Multi-Processing # Processes ## Process Control Block - Process State - New - Ready - Running - Waiting - Terminated - Process number - Program Counter - CPU Registers - Memory limits (allocated main memory) - I/O Status info (open files, devices etc) ## Context Switch From process 0 to process 1. 1. Switch initiated by interrupt or system call 1. Save state into PCB0 (cpu idle) 2. Reload state from PCB1 (idle) 3. Execute process 1 4. Save state into PCB1 (idle) 5. Reload state from PCB0 (idle) 6. Continue running process 0 ## Process Cooperation - Pros: - Information sharing - Computation speedup (cpu utilisation) - Modularity - Multi-Tasking (by users), convenience ## Interprocess Communication IPC - Shared Memory - Risks memory corruption - Message Passing - higher performance cost - can use buffering and queues - Easier to synchonise - blocking send, blocking receive - direct - Processes must know about eachother explicitly, - unix sockets - pipes - requires parent-child relationship - pipes also need an agreed serialisation scheme - unidirectional - cant be used over a network - named pipes - bidirectional - addressed (don't need parent child relationship) - indirect - processes have ports which can receive messages - processes can communicate if they share a mailbox # Threads Multicore / Multiprocessor : A system that run multiple programs simultaneously using multiple hardware threads. Data Parallelism : Distributing the same data across multiple cores to be worked on simultaneously Task Parallelism : Distributing tasks across multiple cores each doing something different - Fork and Exec have different semantics - does `fork` duplicate all the threads or just the caller? - `exec` replaces ALL threads. - Signals - go to all threads, only the relevant thread, a specific signal-handling thread? Thread-local storage : Storage tied to a specific thread, good when using a thread pool and you dont have control over thread creation. TLS is visible across all functions in the thread, similar to static data, unique to each thread. ## Thread Pools - Faster thread dispatch - Puts a bound on the number of threads in an application using the size of the thread pool. ## OpenMP - Threading library - Identifies parallel regions - Cross platform and easy to use, widely supported # Synchronisation - Method depends on whether the kernel is preemptive. Deadlock : Two processes waiting indefinintely for a resource - circular dependency Starvation : Infinite blocking (a process is never removed from a) ## Deadlock Avoidance Safe State : No deadlocks can occur Unsafe State : deadlocks are possible Deadlock Avoidance : Ensure that the system doesnt enter an unsafe state Deadlock Recovery : Recover system from a deadlocked state ### Safe State For all processes in a system Pi needing resources, and Pj holding resources. 1. If Pi's needed resources are not immediately available then it can wait until all Pj have finished using them. 2. When Pj is finished Pi can obtain resources, execute and terminate. 3. When Pi terminates Pi+1 can obtain needed resources and so on. - Resources can safely be passed around all processes in the system. - Avoidance Algorithms - Banker's Algorithm - Single instance of a resource type, use a **resource allocation graph** - Access only allowed if adding the resource request edge to the resource graph doesn't create a cycle. ## Critical section problem - in hardware - disable interrupts (on uniprocessors) --- obviously not scalable or performant - locking - atomic instructions (test memory and set value, compare and swap two memory values) - mutex locks (spinlocks) - `acquire()` and `release()` - Requires busywaiting to enter - good for short waits on a multiprocerssor system - semaphores - Can put the process to sleep when waiting - give it to other processes or power saving states - counting - read mode, write mode, can read many write one and so on with greater control - binary - like a mutex - an abstracted solution to the critical section problem - Has two operations - `block` add process to the waiting queue until it can be served by the critical section - `wakeup` (when a process leaves the crit section) move the next process from the waiting queue to the ready queue # CPU Scheduling Long Term Scheduler : Selects which processes should move to the ready queue. Balanced for the degree of multiprogramming used so that processes share time fairly depending on their priority and importance. Invoked with a seconds to minutes period. Short Term Scheduler : Selects what process to run next. Executed every number of milliseconds. It has a large performance impact. Medium Term Scheduler : is sometimes used to reduce the degree of multiprogramming if it is too high for efficient operation. (Swapping processes to disk and back). - CPU utilisation - Throughput - Turnaround time (time until another process can be executing) - Waiting time (in ready queue) - Response time ## Types - First come first served - Shortest Job First - based on next burst time - not preemptive - Shortest Remaining Time First - preemptive - time to live and priority ageing prevent starvation - Priority - preemptive - By priority, then process time - starvation mitigated by ageing (increasing priority over time) - Roundrobin - not preemptive - Each process runs for a unit time $q$ in a round robin. - $q$ close to process time is ideal, else starvation or dispath latency - Good for frequent bursts of periodic tasks - multilevel queue - multiple queues with different priorities and algorithms - promotion and demotion, ageing - scheduling must be done between queues - i.e. do all foreground, then do all background - time slices, i.e. 80% to foreground 20% to background (this can use any scheudling algorithm) ### Real Time Hard Realtime : Guarantees processes will be serviced by their deadlines Soft Realtime : Prioritises realtime processes, but does not guarantee Throughput : Number of taks / total time CPU Utilisation : Fraction of cpu time spent doing anything (not waiting) - Rate Monotonic - Only for **Periodic** processes that need to be updated every $p$ amount of time. - $p =$ time to deadline - Smaller $p$ is higher priority - Priority assigned based on the inverse of the period - Can miss deadlines - Earliest Deadline First - Non-periodic, non-constant burst time. - Theoretically optimal (minus context switch) 100% cpu and all deadlines met. - Smaller deadline is higher priority - Proportional Share - $T$ shares are allocated, each program receives $N$ of those. # Multiprogramming & Synchronisation (TODO) Classical problems - Bounded buffer - Readers and writers - Dining philosophers - Producer COnsumer # Deadlocks (TODO) ## Deadlock Characterisation 1. Mutual Exclusion - Resource held in a nonsharable mode 2. Hold and wait - A process is holding a nonsharable resource and waiting for another resource that is being held 3. No preemption - Resources cannot be preempted: released only voluntarily 4. Circular Wait - A waits for B waits for C waits for ... waits for A Priority Inversion : When a lower priority process is holding a resource that is being waited on by a higher priority process. - deadlock recovery - Abort all deadlocked processes - Abort processes one at a time until deadlock cycle is gone, based on - priority - process accounting stuff (time ran, resources used, etc..) - interactive or batch - deadlock avoidance - Check for cycles in resource allocation graph Random terms - starvation: in the mutex queue for a long time - priority inversion: When low priority tasks hold a lock needed by a higher priority process # Virtual Memory ## Page Faults + Demand Paging First reference to a page will trap to OS 1. Check validity of address 2. Find a free frame 3. Swap page into fram in via scheduled operation (takes a long time) 4. Restart the process ## Page Replacement 1. Find the page 2. Find a free frame (or victim frame determined by page replacement algo.) 3. Move the page into the free frame and update tables 4. Restart process Frame Allocation Algorithm : how to initially allocate memory to processes ## Page Replacement Algorithms Want the lowest page-fault rate on first access and reaccess - FIFO Algorithm - replace the oldest frame - Suffers belady's anomaly (more frames can mean more page faults) - Optimal Algorithm - Replace the page that won't be used for the longest time. - (you need to know what frames will be needed in the future) - Least Recently Used (LRU) - Earliest last-use time gets replaced - Associate a last use clock counter value with each page - Slow/complex without hardware support - Hardware to approximate LRU - LFU, MFU (least / most frequently used) - keep a counter of each page access - LFU: smaller count $\implies$ not used frequently, replace - MFU: smaller count $\implies$ new and yet to be used, replaced highest count ## Other memory - kernel memory - usually a free-memory pool - Memory mapped I/O - written when a pager scans for dirty pages, periorically, or at `close()` - processes can share the memory mapping - higher performance than read() and write() # Main memory? ## Swapping - Backing store - lage, fast disk that accommodates copies of all memory images for all users; provides direct access - Roll out, roll in - used for priority based scheduling algorithms; lower priority process i sswapped out so higher priority process # External I/O ## Magnetic Disks Average I/O time : average access time + (amount to transfer / transfer rate) + controller overhead Bandwidth : bytes transferred / total time of request Rotational Latency (ms) : $\left(\frac{rpm} { 60000}\right)^{-1}$ - Sector 0 is first on the outermost cylinder ### Disk Scheduling Minimise seek time $\approx$ seek distance - FCFS (first come first served) - pro: fair (no starvation) - con: not fast - SSTF (Shortest Seek Time First) - go to linearly closest cylinder next - pro: fast - con: may cause starvation, and make some requests wait longer - optimal if the disk isnt under heavy load - SCAN (elevator algorithm) - Service all cylinders $\le$ current position in descending order, then all cylinders $\ge$ position in ascending order. - going all the way to the end of the disk and turn around. - Is fair and has better performance than FTFS - C-SCAN - Only service cylinders when moving in one direction, them jump to the other end. - more uniform wait time - LOOK and C-LOOK - SCAN and C-SCAN but the arm only goes as far as the last request then turns around - better performance than SCAN ### RAID [Reference](http://www.raid-calculator.com/raid-types-reference.aspx) - Striping = combine multiple disks into one unit - Duplication = failure tolerance and performance RAID 1 : Mirroring (keeps a suplicate of each disk) RAID 1+0 and 0+1 : Striped mirrors and mirrored stripes are high performance and higgh reliability RAID 4,5,6 : Block interleaved parity combine multiple disks with some performance and reliability benefit without using as much redundancy. # File Systems External Fragmentation : When there are lots of small non-contigouous memory blocks which cannot be useful. Internal Fragmentation : Memory allocated is too big for what it is to be used for and there is some left over that cannot be used by another process, due to the page size. ### Directories Implementations - Linear List - A list of file names with a pointer to data - Slow to traverse, linear search time (improved using a B+ tree) - Hash Table - much faster to find a file in a directory - expensive to resize the hash table - Only good if the number of entries has a fixed maximum - even a linked list collision resolution is better than a linear list. ## File Allocation Methods - Contiguous allocation - Contiguous blocks are placed next to eachother on disk - Mapping from logical to physical - good for random access - suffers from external fragmentation - Linked - Files are linked lists of blocks - pros - No external fragmentation - Good for sequential access - cons - Locating a block can take many disk seeks and I/Os. - Slow random access - Generally slower than indexed - FAT variation := the beggining of the volume has a table indexed by block number that can be cached and is faster, but still a linked list of blocks, stored separately to the blocks themselves. - Indexed - Every file has an index block containing all the block pointers in that file - Good for random access without causing external fragmentation. - blocks are in linked chains, not contiguous areas, but you have a big table to them that is stored in memory. - Index block is a significant overhead for many small files, is generally greater than that of normal linked allocation. - Fast - UNIX - Linked index table ## Free Space Management - Contiguous Bitmap - easy to get contiguous files - Size on disk is, with disk size of $2^d$ bytes 8 blocks per byte of the bitmap, $n = t^d / 8$ bytes. - Linked - no waste of space - cannot get contiguous space easy ## File System Performance - Buffer cache - Synchronous writes: prevents buffering and caching - Async writes that are queued are faster - Free-behind and read ahead optimise sequentnial access - Reads slower than writes # I/O Systems ## Hardware - Some data tx,rx registers, control, status as bytes or a fifo buffer. - Addressed via direct I/O instructions - MMIO. ## Polling - use status bits to busy-wait until both are ready - good if device is fast but bad if the device is slow. - can lose data if the CPU wasn't ready to copy when the device was - Excessive CPU time demand from the OS when the device doesnt have anything for it to do - Good for very frequent periodical events that are low latency (would require frequent interrupts) - Not really bufferable ## Interrupts - maskable interrupt handler, activated by an interrupt vector that watches the CPU interrupt request lines. - Requires a context switch which has significant performance impact if it is too frequent. - can have a performance and power use impact - Good for slow devices that only require the OS to wake up occasionally - or use an interrupt to wake up into polling mode ## Direct Memory Access - Does not require the CPU be active to transfer the data to memory (moves data concurrently) - Avoid programmed I/O single-byte at a time - Interrupts to signal completion - Steals *some* cycles from CPU - Requires more complicated hardware - the I/O bus needs to talk to both the CPU and DMA controller to handle I/O, balancing, routing - DMA is good for long bursts (eg a hard drive) - DMA is also good for fixed length bursts - Depends on device support - via DMA controller, that can write into main memory 1. Driver asks disk for data 2. Disk sends data to DMA 3. DMA sends data to the memory buffer 4. DMA sends interrupt - DVMA: variant understanding virtual addresses ## Device Modes - Direct manipulation setting device data registers - on unix using `ioctl()` - Block Devices - `read()` `write()` `seek()` - Raw, direct I/O - Can use Memory-mapped I/O through demand-paging - Can use DMA - Character Devices - `get()` `put()` characters - line editing libraries and so on - Network Devices - Socket interface/ structure/abstraction of the underlying device - Separates network protocol from network operation - Can activate callbacks/intterrupts when packets are ready - Implemented using pipes, FIFOs, streams, queues and many ways. - Clocks and Timers - facility for current time, elapsed time - Programmanble interval timer provided - on unix covered by the `ioctl()` system calls ## Non-Blocking and Asynchronous I/O Blocking : the requesting process is suspended until I/O is completed Nonblocking : the I/O call returns as much data as is immediately available. - used for UI, buffered data copy I/O - implemented with multithreading - Returns quickly with the amount of bytes that are read - use `select()` to know whether data is ready, and `read()` or `write()` to transfer. Asyncrhonous : Process runs while I/O executes - I/O subsystem signals process when I/O is completed - Difficult to use, need to manage concurrency and signal handling Vectored I/O : Unix `readve()`, batching multiple I/O operations, decreases system call overhead, context switches. ## Kernel Device Management Buffering : store data between transfers to cope with speed mismatch. Caching : Keep copies of athe data Spooling : Queue requests if a device can only process one at a time. (eg printers) Device reservation : Restrict exclusive access of device to one process ## Improving Device Performacne - reduce context switches - reduce data copying - reduce interrupts by using large transfers, smart controllers, polling - use DMA - use smarter hardware devices, controlelrs - Balance CPU memory, bus, IO performance for highest throughput - move user-mode processes / daemons to kernel threads. # Virtual Machines Host : the hardware system being used VMM / Hypervisor : Software that creates and runs machines pretending to be a physical host Guest : Operating system running on virtual machine ## Types - Emulation - Any guest CPU can be emulated on any host CPU - terrible performance - Type 0 Hypervisor - hypervisor is in firmware - Guests have dedicated resources (cpus, memory) - Type 1 Hypervisor - Is a specialised OS - Implements device drivers for host HW - Does CPU and memory management - Can share resources and overcommit - provides snapshots and cloning - Type 2 hypervisor - A eneral purpose os that privides VMM functionality - Linux + KVM, Windows + Hyper-V - Treats guests as just another process - special handling to run guest code natively on cpus (for performance benefit over emulating the CPU) - Guest kernel runs in user mode, trap and emulate used when a guest tries to do something privileged and control is given back in virtual kernel mode. - kernel code is slower, but user code is the same speed - Containers - Not virtualisation - Docker, LXC, Solaris Zones - Only one kernel (the host) running - Little to no performance loss - Separation of processes. # Protection (TODO) - Access matrix - columns (objects) = access lists pertaining to an object - Each object has an access list of tuples `<domain, rights>` - rows (domains) = capability lists for each domain - Use the capability list to restrict access to modifying the capability list itself Implementations of access matrix - Global Table - Store tuples `<domain, object, rights set>` - Really big and slow to access - Access list for objects - per object list of <domain, rights-set> - Slow (every access to an object must be checked, and there are a lot of objects) - Capability list for domains - per domain list of <objects, rights> - Hard to revoke rights to an object, because the permissions are stored with the domain - Lock/key - Comrpomise between access and capability list - Objects have locks (bit patterns) - Domains have keys (bit patterns) - Can only access if keys match locks - Can be passed freely between domains; simple to revoke # Security - Detecting intrusions - repeated access denials - odd location, ip address change - non-human-like behaviour - Password Security - Hash passwords, or use one half of an asymmetric cipher, restrict user permissions to passwd file, third party authentication, 2nd factor authentication

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully