2INC0 - Operating Systems
Disadvantages of Process
- Creation overhead (e.g., memory space creation/copy) - Complex inter-process communication - Process switching overhead: mode + context switch ( for each inter-process communication (IPC) on a single-processor system, save/restore state)
Process Creation
- Parent process creates children processes, which, in turn creates other processes, forming a tree of processes - Generally, process identified and managed via a process identifier (pid) - When a process creates children, it can share its memory or resources (such as files) with them. Resource sharing options: • Parent and children share all resources • Children share subset of parent's resources • Parent and child share no resources Execution options • Parent and children execute concurrently • Parent waits until children terminate Address space options • The child is a copy of the parent (e.g., default in Linux) • The child has a new program loaded into its address space (e.g., Windows)
Preventing deadlock
-Always let critical sections terminate: always call unlock(m) after lock(m) -Avoid cyclic waiting • Try to avoid P or lock operations that may block indefinitely between lock(m) and unlock(m) • Use a fixed order when calling P-operations on semaphores or mutexes− P(m);P(n); .... in one task may deadlock with P(n);P(m);... in another task • Look for the "dining philosopher problem" for a good example - Avoid greedy consumers: P(a)k should be an indivisible atomic operation when tasks compete for limited resources
Requirements on synchronization solutions
1. Functional correctness: satisfy the given specification (e.g., mutual exclusion) - an assertion that is needed for correctness must not be disturbed (by another process). 2. Minimal waiting: ("make progress"): waiting only when correctness is in danger. 3. Absence of deadlock: don't manoeuvre (part of) the system into a state such that progress is no longer possible. 4. Absence of livelock: ensure convergence towards a decision in a synchronization protocol 5. Fairness in competition: absence of fairness leads to starvation of processes.
What does an operating system provide?
1. Process management (Creation and deletion, Scheduling, Synchronization) 2. Memory management ( Allocate and deallocate memory space as requested) 3. I/O management (A buffer-caching system) 4. File management (Manipulation of files and directories) 5. Error detection (Debugging facilities) 6. Protection (concurrent processes should not interfere with each other (memory)) 7. Security (Against other processes and outsiders) 8. Accounting (Using timers)
Deadlock state
A deadlock state is a system state in which a set of tasks is blocked indefinitely: • each task is blocked on another task in the same set We typically prove the absence of deadlock by contradiction 1. assume a deadlock occurs 2. investigate all task sets that can be blocked at the same time (often: just 1) 3. show a contradiction for all possible combinations of blocking actions of those tasks
Monitor
A monitor is an object that contains a combination of data and operations on those data • Data record the state of the monitor • Procedures of a monitor are executed under mutual exclusion. (At most one task can be "inside the monitor") • Synchronization is implemented using condition variables We use a monitor to synchronize accesses to the read and write actions.
Priority scheduling
A priority number (integer) is associated with each task • explicitly assigned by the programmer or • computed, through a function that depends on task properties e.g., memory use, timing: duration (estimated), deadline, period
Deadlocked set
A set of task Dis deadlocked if 1. all tasks in Dare blocked or terminated (normally or abnormally), 2. there is at least one non-terminated task in D, and 3. for each non-terminated task T in D, any task that might unblock T is also in D.
Single reference rule
A statement (expression) may be regarded as atomic if it makes at most one reference to a shared variable.
A system supporting POSIX provides:
A system supporting POSIX provides: • a host language and compiler (often: C) • programming interface definition files (e.g., C-header files) • programming interface implementation binary or code (e.g., C-libraries) • a run-time system (a platform: OS or the like)
Blocked Task
A task is blocked if it is waiting on a blocking synchronization action
Trap/Exception
A trap or exception is a software generated interrupt • caused either by a software error (e.g. division by zero, floating pt error) • or by a system call
Advantages/Disadvantages of an API to system calls
Advantage of an API to system calls + API works at a higher abstraction closer to programs need➔ easier to work with + API calls may combine many system calls to implement higher level tasks (e.g., copy a file) ➔ less calls needed + a program written using an API can compile/run on any system supporting the API ➔ portable Disadvantage of an API to system calls - Adds one more layer ➔ increased overhead
Advantage of Priority scheduling
Ageing - increase the priority of the process over time.
Many-to-many Multithreading model
Allows OS to create a (limited) number of kernel threads (#kernel_threads ≤ #user_threads) Advantages: • concurrency (Concurrency level is limited by the number of kernel threads) • bounded performance cost for the kernel • Disadvantage: more complex to implement
Invariant
An assertion that holds at all control points of a program
Interrupt
An interrupt is a signal to the CPU that transfers control to an interrupt service routine • caused by external device (e.g. a result of clock tick, ready for i/o, i/o completion) • interrupt vector contains address of the service routine • OS preserves CPU state by storing registers and program counter (address of the interrupted instruction is saved)
Topology invariant
An invariant derived directly from the program code• Examples: • number of times an operation is executed in comparison to another action. • Value of a variable based on the number of times a set of actions was executed
Operating System (OS) + Advantages
An operating system is a piece of software that acts as an intermediary between users/applications and computer hardware. Advantages: - It provides a level of abstraction that hides the gory details of the HW architecture from applications. - It organizes sharing of HW resources among applications and users. - It optimizes performance through resource management.
Limitations of mutexes or semaphores
At any given time, we can have several readers but no writer, or one writer and no readers. (readers-writers problem)
Peterson's algorithm
Combine the ideas of the last two solutions: • take turns in crowded circumstances (use a variable t) • don't wait if there is no need (look at the boolean bY or bX) Limitations of Peterson's algorithm: - Can synchronize only two tasks (extension for more tasks exists but more complex) - Busy-wait (wastes CPU cycles, does not work for single core if the scheduling policy is non-preemptive)
Context switch:
Context switch: Saving the state of a process whose execution is to be suspended, and reloading this state when the process is to be resumed.
Scheduling
Decide what (executes/is loaded/has access to) where and when: Examples: • Processor scheduling: decide which task (thread/process) executes on each core at every given time. • Memory scheduling: decide which memory page of a process is loaded in each page frame of the main memory at every given time. Decision mode defines when scheduling decisions are taken: • Preemptive vs non-preemptive • Periodic/time-based vs event-based
Earliest Deadline First (EDF)
Decision mode: preemptive Priority function: least time remaining until absolute deadline → higher priority Arbitration rule: chronological or random ordering
Rate Monotonic (RM) Scheduling
Decision mode: preemptive Priority function: shorter period T → higher priority Arbitration rule: chronological or random ordering
Deadline Monotonic (DM) Scheduling
Decision mode: preemptive Priority function: shorter relative deadline D→ higher priority Arbitration rule: chronological or random ordering
Disadvantages of threads
Downside: no protection against other threads (faults, memory sharing)
OS: Dual-mode operation
Dual-mode allows the OS to protect itself and other system components. Different privileges required for different types of code • Most instructions (user code) can be executed in user mode − In user mode CPU cannot access memory locations reserved for OS • Some "privileged" instructions are only executable in kernel mode Mode bit provided by hardware − Provides ability to distinguish when system is running user code or kernel code. − A system call changes the execution mode to kernel mode, returning from the system call resets the mode to user mode.
Round Robin (RR) scheduling
Each task gets a small unit of CPU time (time quantum of length q) (At the end of a time quantum, the task is preempted and is added to the end of the ready queue.) • Decision mode: if a task runs q continuous time units, it is preempted • Priority function: the earliest it is added to the ready queue, the higher the priority • Arbitration rule: random ordering Typically, higher average turnaround than SJF, but better response time
One-to-one Multithreading model
Each user-level thread maps to a kernel thread Advantage: allows for concurrency between thread • Disadvantage: the kernel must manage all threads (state, schedule, ...) • Low performance in case of many user threads
Condition synchronization
Explicit communication (signalling) between tasks when just counting is not enough to solve a synchronization problem. Two principles: >Where a condition may be violated: check and block >Where a condition may have become true: signal waiters
Transparency
Hide details with respect to a given issue. Examples: • processor architecture (Instruction Set Architecture - ISA) − mechanism: use compiler • physical memory size − mechanism: virtual memory i.e., present linear memory model that is larger than physical • Location of programme and data in physical memory − mechanism: indirection using logical memory address
Advantages of threads
Increase concurrency level (less costly than by using processes): • Better performance − hide latency (while waiting CPU can do something useful in another thread) − increase responsiveness (divide work) − exploit platform parallelism (multiple processors → multiple threads) Use "natural" concurrency within a program : • natural organization, structure, e.g. − one thread per event − one thread per task − one thread per resource − one thread to handle each user interface
Atomic Action
Indivisible step in the evolution of an executed program
Detection (during execution)
Invoke detection algorithm periodically to check if deadlock occurs You need • an algorithm to examine the state upon execution of a blocking action • an algorithm to recover from a deadlock Repeatedly monitoring and potentially recovering causes large overheads
lightweight process (LWP)
Lightweight process (LWP) as a user-level representation of a kernel thread • LWP an intermediate data structure • like a virtual processor for user the threads and thread library User thread library schedules user threads on LWPs and Kernel schedules LWPs.
Direct Memory Access (DMA) + Advantages
Main memory access by a controller over the bus. Advantages: • Less operations executed by the CPU • Less interrupts to treat • CPU can execute other useful code in parallel
Semaphores
Non-negative integer s with initial value s0 and atomic operations P(s) and V(s). P(s): < await(s>0); s := s-1 > →the task sleeps until 's>0' holds, decrement s V(s): < s := s+1 > →increment s Note: the notations P(s) and V(s), and lock(s) and unlock(s) interchangeably when s is a mutex (i.e., a binary semaphore). Semaphores can be used to implement mutual exclusion.
Process v.s. Thread
Process: • defines a memory space • defines ownership on other resources • has at least one associated thread of execution • Context of execution saved in a PCB Thread: • all threads of a process can operate in their process' address space (several threads may share the same variables) • has an associated execution state (PCB is extended with thread info) - program counter, stack image, return addresses of function calls, values of processor registers
Deadlock Conditions
Program behaviors that may lead to deadlock • mutual exclusion • greediness: hold and wait − incrementally reserving some resources while waiting for other resources to become available (e.g., dining philosophers). • absence of preemption mechanism • circular waiting− e.g., tasks tA, tB and tC waiting on each other:
Virtualization
Provides a simple, abstract, logical model of the system • virtual memory, virtual CPU, virtual disk. • Program can be developed as if they were the sole user of the HW resources. Current systems tend to virtualize the entire hardware • i.e., it yields a virtual machine Virtualization can also help support several guest OS's on top of one host OS or on top of a given platform • Implemented on a run-time virtualization layer
Busy-waiting
Repeated testing without going to sleep Acceptable only when: • waiting is guaranteed to be short or • there is nothing else to do (e.g., when the task executes on dedicated hardware) Busy-waiting using await() works only if • the tests using await and locking the mutex are executed atomically(usually not possible) Alternative: use semaphores for actions synchronization
Resource dependency graph (Reusable resources and action synchronizations)
Resource dependency graph: • bipartite graph with two classes of nodes = tasks and resources • three types of edges: − Type1: task has requested and now waits for the resource − Type2: resource acquired (held) by task − Type3: task may request the resource • A resource dependency graph represents a particular state of the system
Reusable resources
Resources are given back after use (➔number of resources is fixed) • Typically, readers/writers type of problems or mutual exclusion (critical section) • examples: processor, memory blocks, physical entities, shared variables, buffer spaces, ...
Consumable resources
Resources is taken away upon use (➔number of resources varies) • Typical producer / consumer problems • examples: sensor data, characters typed using a keyboard, blocks of data received from the network, ...
Shortest-Job-First (SJF) scheduling
Schedule the task with the shortest next CPU burst. • Decision mode: non-preemptive • Priority function: equal to -L where L is the length of the next CPU burst • Arbitration rule: chronological or random ordering + SJF is optimal. • gives minimum average waiting time for a given set of tasks... if you know their execution time - Problem • needs prediction of the next CPU burst length e.g. by using exponential weighted average of bursts in the past (see in the textbook)
First-Come, First-Served (FCFS) scheduling
Schedule the tasks in order of their arrival. • Decision mode: non-preemptive • Priority function: the earlier the arrival, the higher the priority • Arbitration rule: random choice among processes that arrive at the same time
Shared variables/Private variables/Atomic action/Concurrent execution/Race condition
Shared variables: accessible to several tasks (processes/threads) Private variables: accessible only to a single task Atomic action: finest grain of detail, indivisible • typically, assignments and tests in a program • sufficient at program level: single reference to shared variable in statement− ignoring possible optimization and reordering by compiler/processor Concurrent execution: interleaving of atomic actions• Interference: disturbing others' assumptions about the state• usually, caused by "bad" interleavings • particularly with shared variables Race condition: situation in which correctness depends on the execution order of concurrent activities ("bad interference")
Two-level Multithreading model
Similar to many-to-many, except that it allows a user thread to be bound to a kernel thread
Disadvantage of Priority scheduling
Starvation - low priority processes may never execute.
Switching between threads
Switching between threads: two possibilities • switching as a kernel activity − kernel maintains execution state of thread − similar to process switching • switching handled by user -level thread package (library) − the package maintains execution state of threads (kernel knows kernel threads only) − the package must obtain control in order to switch threads − responsibility of programmer to call package - 'cooperative multithreading'
Process control block (PCB)
The PCB records information associated with a process context of execution: • Process state - running, waiting, etc. • Program counter - location of next instruction to execute • CPU registers - contents of all process-centric registers • CPU scheduling information- priorities, scheduling queue pointers • Memory-management information - memory allocated to the process • Accounting information - CPU used, clock time elapsed since start, time limits • I/O status information - I/O devices allocated to process, list of open files T
Wait-for graph (Consumable resources and condition synchronizations)
The graph captures a possible dynamic situation (reachable system state) • We must prove the possibility of existence of the graph − e.g., if a is always negative, there is never a state with an arrow p2 → p3 • We label the edges with corresponding blocking conditions − i.e., information about the state that gives the specific blocking− e.g., at this state p3 is blocked due to x<0 and p4 is blocked due to ¬b
Reasons why threads operate faster than processes
Threads operate faster than processes due to the following reasons: • Thread creation and termination is much faster (no memory address space copy needed) • Context switching between threads of the same process is much faster (no need to switch the whole address space) • Communication between threads is faster and more at the programmer's hand than between processes (shared address space)
Approaches to dealing with deadlocks
Three active approaches to dealing with deadlocks: 1. Prevention (programmer side)− at design time 2. Avoidance (system side)− dynamicly checks to avoid entering risky states (can be expensive) 3. Detection and recovery− checks only whether we are in a deadlock state or not, and try to recover from it
Concurrent Trace
Trace made of an interleaving of atomic actions of two or more tasks
Types of system calls
Types of system calls: • Process management − create, destroy, communication, synchronization, ... • File management − open, close, read, write, ... • Memory management − allocation, free, virtual memory, ... • Device management − access control, open, attach, send/receive, .... • Communications − setup communications, exchange messages, .... • Miscellaneous − timers, inspect system resources, ...
Deadlock Avoidance (during execution)
Upon executing each potentially blocking action (e.g., P(m)): • check if an open execution path exists (system remains in a "safe" state) • otherwise, deny or postpone the action Existence of an open path = reduced dependency graph is empty Postponing works if the blocking actions refer to allocations of reusable resources and we can compute all possible future states Example of such algorithm: the banker's algorithm
Deadlock Prevention (at design time)
Use reduced dependency graphs and wait-for graphs at design time • make the reduced dependency graphs empty by construction • prevent cycles in the wait-for graphs Use synchronization tricks (see "actions synchronization") • Examples: − no circular wait, − ensure terminating critical sections − Acquire "all resources at once", i.e., avoids the "wait-and-hold" greediness • not always possible Allow preemption of resources when needed • Examples: add timeout on how long a task can hold a resource, or allow a task to steal a resource from another one • not always possible (e.g., an I/O device may be in an inconsistent state if preempted during an I/O operation)
Many to One Multithreading Model
User threads mapped to single kernel thread Advantage: Thread management by thread lib • no need for kernel involvement, ➔ fast ➔ easy to deploy Disadvantage: Only one user thread can access the kernel at a given time • multiple threads cannot run in parallel on different processors • a blocking system call from one user thread blocks all user threads of the process
Actions synchronization
Uses semaphores to synchronizes actions in different tasks to enforce new invariants (called synchronization invariants or synchronization conditions)
Advantages of Process
Using processes allows to use platform parallelism (Example: if the processor has more than one core, two or more different process can make simultaneous progress) + It also allows for concurrency (The same resource (e.g., a core or network driver) can be time shared ➔ improve responsiveness) + If a process is waiting (e.g., to get access to a resource, or to receive data from another process), then the kernel can schedule another process ➔ more efficient use of the system resources
Deadlock
When a set of tasks is blocked indefinitely and cannot make progress anymore
Thread
• A dispatchable unit of work within a process • Threads within a process share code and data segments (i.e., share memory address space)
Process
• It is a program in execution. • It has a context of execution • A process owns resources (has memory and can own other resources such as files, etc.) • Several processes can run the same program (they all have a different context of execution)
Competition →Mutual exclusion
• N concurrent processes competing for a resource • Tasks define critical sections (CS) • Introduce extra synchronization requirements to access the CS.
Cooperation →Synchronization
• N concurrent processes working for a common goal • Goal 1: avoid program traces that may violate a certain invariant • Goal 2: steer the execution to maintain some property− e.g., enforce an execution order • Typically, by blocking the task execution until an assertion becomes true
Signal vs. Sigall
• Requires a careful analysis to ensure both correctness and efficiency. • Sigall is always correct, but often inefficient.
Trace
• Sequence of atomic actions in the execution of a program • Typically, a sequence of assignments and tests
Portability
• a program written using an OS or an API can compile/run on any system supporting that OS or API • Protect investments in application software - Reduces cost to support several platforms - Reduces cost to upgrade/change the execution platform - Simplifies integration of several application components • OS gives a unified machine view to applications, effectively defining a virtual machine