ICS 143: Midterm Review Operating System Concepts – 9th Edition. Silberschatz, Galvin and Gagne ©2013. Modified by Dmitri V. Kalashnikov and Nalini Venkatasubramanian. 2 3 What is an Operating System? OS is a program that acts as an intermediary between a user of a computer and the computer hardware Operating system goals: 1. Execute user programs and make solving user problems easier 2. Make the computer system convenient to use 3. Use the computer hardware in an efficient manner 4 Four Components of a Computer System 5 Computer System Organization Computer-system operation One or more CPUs and device controllers connect through a common bus providing access to shared memory I/O and CPU can execute concurrently 6 Operating System Views OS is a resource allocator Manages all resources Decides between conflicting requests for efficient and fair resource use OS is a control program Controls execution of programs to prevent errors and improper use of the computer 7 Operating System Definition No universally accepted definition of OS “Everything a vendor ships when you order an operating system” is a good approximation But varies wildly “The one program running at all times on the computer” is the kernel. Everything else is either a system program (ships with the operating system) , or an application program. 8 Operating System: Broad Spectrum Special purpose and embedded systems Real-time systems Batch Systems and Multiprogramming Timesharing Systems workstations, servers, minicomputers, timeframes Transaction systems software/hardware combination that supports transaction processing Parallel and Distributed Systems symmetric vs. asymmetric multiprocessing Real-time systems Hard vs. soft realtime 9 A View of Operating System Services An operating system provides an environment for the execution of programs. It provides certain services to programs and to the users of those programs. 10 Operating System Services Services that provide user-interfaces to OS Program Execution - load program into memory and run it I/O Operations - since users cannot execute I/O operations directly File System Manipulation - read, write, create, delete files Communication - interprocess and intersystem Error Detection - in hardware, I/O devices, user programs Services for providing efficient system operation Resource Allocation - for simultaneously executing jobs Accounting - for account billing and usage statistics Protection - ensure access to system resources is controlled 11 System Calls Systems calls -- programming interface to the services provided by the OS Typically written in a high-level language (C or C++) Mostly accessed by programs via a high-level Application Programming Interface (API) rather than direct system call use Three most common APIs are Win32 API for Windows, POSIX API for POSIX-based systems including virtually all versions of UNIX, Linux, and Mac OS X, Java API for the Java virtual machine (JVM) 12 System Call Implementation Typically, a number associated with each system call System-call interface maintains a table indexed according to these numbers The system call interface invokes the intended system call in OS kernel and returns status of the system call and any return values The caller just needs to obey API Does not need to know how the system call is implemented 13 API – System Call – OS Relationship 14 Operating System Structure General-purpose OS is a very large program How to implement and structure it? Can apply many ideas from software engineering Software engineering - a separate area in CS Studies design, development, and maintenance of software 15 Operating System Structure (Modules) A common approach is to partition OS into modules/components Each modules is responsible for one (or several) aspect of the desired functionality Each module has carefully defined interfaces Advantages: Traditional advantages of modular programming: Simplifying development and maintenance of computer programs, etc Modules can be developed independently from each other Disadvantages: Efficiency can decrease (vs monolithic approach) 16 Operating System Structure (contd) In general, various ways are used to structure OSes Many OS’es don’t have well-defined structures Not one pure model: Hybrid systems Combine multiple approaches to address performance, security, usability needs Examples of OS structures: Simple structure – MS-DOS More complex structure - UNIX Layered OS’es Microkernel OS’es OS’es with loadable kernel modules 17 Process Management Process - fundamental concept in OS Process is a program in execution Process needs resources - CPU time, memory, files/data and I/O devices OS is responsible for the following process management activities Process creation and deletion Process suspension and resumption Process synchronization and interprocess communication Process interactions - deadlock detection, avoidance and correction 18 Diagram of Process State As a process executes, it changes state new: The process is being created ready: The process is waiting to be assigned to a processor running: Instructions are being executed waiting: The process is waiting for some event to occur terminated: The process has finished execution 19 Process Control Block (PCB) Each process is represented in OS by PCB PCB - info associated with the process Also called task control block Process state – running, waiting, etc Program counter – location of instruction to next 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 20 CPU Switch From Process to Process 21 Process Scheduling System maintains scheduling queues of processes Job queue – set of all processes in the system Ready queue – set of all processes residing in main memory, ready and waiting to execute Device queues – set of processes waiting for an I/O device Processes migrate among the various queues Scheduler – component that decides how processes are selected from these queues for scheduling purposes 22 Schedulers Long-term scheduler (or job scheduler) selects which processes should be brought into the ready queue. invoked very infrequently (seconds, minutes); may be slow. controls the degree of multiprogramming Short-term scheduler (or CPU scheduler) selects which process should execute next and allocates CPU. invoked very frequently (milliseconds) - must be very fast Medium-term scheduler swaps out process temporarily balances load for better throughput 23 Process Creation Processes are created and deleted dynamically Process which creates another process is called a parent process; the created process is called a child process. Result is a tree of processes e.g. UNIX - processes have dependencies and form a hierarchy. Resources required when creating process CPU time, files, memory, I/O devices etc. 24 Process Termination Process executes last statement and asks the operating system to delete it (exit). Outputs data from child to parent (via wait). Process’ resources are deallocated by operating system. Parent may terminate execution of child processes Child has exceeded allocated resources Task assigned to child is no longer required Parent is exiting OS does not allow child to continue if parent terminates Cascading termination 25 Threads Processes do not share resources well high context switching overhead A thread (or lightweight process) basic unit of CPU utilization; it consists of: program counter, register set and stack space A thread shares the following with peer threads: – code section, data section and OS resources (e.g., open files) 26 26 Single and Multithreaded Processes 27 Producer-Consumer Problem Paradigm for cooperating processes; producer process produces information that is consumed by a consumer process. Uses a buffer of items that can be filled by producer and emptied by consumer Unbounded-buffer places no practical limit on the size of the buffer. Consumer may need to wait, producer never waits. Bounded-buffer assumes that there is a fixed buffer size. Consumer waits for new item, producer waits if buffer is full. Producer and Consumer must synchronize. 28 Interprocess Communication (IPC) Mechanism for processes to communicate and synchronize their actions Via Shared memory Via Messaging system - processes communicate without resorting to shared variables. Messaging system and shared memory not mutually exclusive can be used simultaneously within a single OS or a single process. Messaging IPC facility provides two operations. send(message) - message size can be fixed or variable receive(message) Direct vs. Indirect communication (mailbox). 29 CPU Scheduling Scheduling Objectives Scheduling Criteria Scheduling Algorithms Multiple Processor Scheduling Real-time Scheduling Algorithm Evaluation 30 Basic Concepts Maximum CPU utilization obtained with multiprogramming waiting for I/O is wasteful 1 thread will utilize only 1 core CPU–I/O Burst Cycle Process execution consists of: a cycle of CPU execution and I/O wait CPU burst followed by I/O burst CPU burst distribution is of main concern 31 CPU Scheduler Short-term scheduler Selects 1 process from the ready queue then allocates the CPU to it Queue may be ordered in various ways CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates Scheduling under 1 and 4 is called nonpreemptive (=cooperative) All other scheduling is called preemptive Process can be interrupted and must release the CPU 32 Scheduling Policies FCFS (First Come First Serve) Process that requests the CPU FIRST is allocated the CPU FIRST. SJF (Shortest Job First) Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time. Priority A priority value (integer) is associated with each process. CPU allocated to process with highest priority. Round Robin Each process gets a small unit of CPU time Multilevel Queue Ready queue partitioned into separate queues Variation: Multilevel Feedback queues. 33 Process Synchronization The Critical Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors 34 Background Processes can execute concurrently May be interrupted at any time, partially completing execution Might need to read and write some shared data Concurrent access to shared data may result in data inconsistency Reading and writing to shared data concurrently can result in unpredictable/unintended results! How to deal with this? Use synchronization mechanisms to ensure the orderly execution of cooperating processes 35 Diagram of Critical Section Problem Shared Data P0 • Each process Pi has its own critical section • Accesses to shared data only in CS • Lock – will consider later • In shared data, accessible by all • Often 1 lock for all n processes • Guards access to all critical sections • Ensures mutual exclusivity P1 Pn-1 ... 36 The Critical Section Problem Requirements to the critical section problem 1. Mutual Exclusion 2. Progress 3. Bounded Waiting Solutions to the critical section problems Software based Hardware based Petersen’s solution to the 2 process case Lamport’s Bakery Algorithm Solution to the n process case Before entering its critical section, process receives a number. Holder of the smallest number enters critical section. 37 Synchronization Hardware Modern machines provide special atomic hardware instructions for synchronization Atomic = non-interruptible test_and_set instruction test memory word and set value compare_and_swap instruction swap contents of two memory words All solutions below based on idea of locking Protecting critical regions via locks while (true) { acquire lock critical section release lock remainder section } 38 test_and_set Instruction Definition: boolean test_and_set (boolean *target) { boolean rv = *target; *target = true; return rv; } 1. Executed atomically – cannot be interrupted in the middle 2. Returns the original value of passed parameter 3. Set the new value of passed parameter to true. 39 Solution using test_and_set() Shared Boolean variable lock initialized lock = false unlocked initially Solution: while (true) { while (test_and_set(&lock)) ; // wait until lock becomes equal to false // here lock=true /* critical section */ lock = false; /* remainder section */ } Bounded waiting is not satisfied Other processes might keep acquiring the lock first 40 compare_and_swap Instruction Definition: int compare_and_swap(int *value, int expected, int new_value) { int temp = *value; if (*value == expected) *value = new_value; return temp; } 1. Executed atomically 2. Returns the original value of passed parameter “value” 3. Set the variable value to new_value but only if value ==expected The swap takes place only under this condition 41 Solution using compare_and_swap Shared integer lock initialized to lock=0; // unlocked Solution: while (true) { while (compare_and_swap(&lock, 0, 1) != 0) ; // wait for lock to be 0 // here lock = 1 /* critical section */ lock = 0; /* remainder section */ } Bounded waiting is not satisfied 42 Mutex Locks OS designers build convenient software tools to solve critical section problem Usually implemented via hardware atomic instructions Simplest is mutex lock Protect a critical section by: first acquire() a lock then release() the lock Boolean variable indicating if lock is available or not Calls to acquire() and release() are atomic Implementations: Busy waiting -- such a lock called a spinlock No busy waiting 43 Semaphore Semaphore – a synchronization tool Provides more sophisticated ways (than Mutex locks) for processes to synchronize their activities. Semaphore S – integer variable Can only be accessed via two indivisible (atomic) operations wait() and signal() Originally called P() and V() Definition of the wait() operation wait(S) { while (S <= 0) ; // Notice, busy waits, spending CPU cycles, not (always) good S--; } Definition of the signal() operation signal(S) { S++; } 44 Semaphore Usage Counting semaphore – integer value can range over an unrestricted domain Can control access to resource that has a finite number of instances Initialized to the number of resources available Binary semaphore – integer value can range only between 0 and 1 Same as a mutex lock Can solve various synchronization problems Consider P1 and P2 that require S1 to happen before S2 Create a semaphore “synch” initialized to 0 (locked) P1: S1; signal(synch); P2: wait(synch); S2; 45 Classical Problems of Synchronization Classical problems used to test newly-proposed synchronization schemes Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem 46 The Readers-Writers Problem A data set is shared among a number of concurrent processes: Readers – only read the data set they do not perform any updates Writers – can both read and write Problem Allow multiple readers to read (shared data) at the same time Only one single writer can access shared data at the same time Readers cannot read during this time Other writers cannot write during this time 47 Readers-Writers Problem (Cont.) Shared Data: Data set semaphore rw_mutex = 1; // mutual exclusion for writers semaphore mutex = 1; // guards read_count int read_count = 0; // #processes currently reading the object The structure of a writer process while (true) { wait(rw_mutex); ... /* writing is performed */ ... signal(rw_mutex); } 48 Readers-Writers Problem (Cont.) The structure of a reader process while (true) { wait(mutex); read_count ++; if (read_count == 1) wait(rw_mutex); signal(mutex); ... /* reading is performed */ ... wait(mutex); read_count--; if (read_count == 0) signal(rw_mutex); signal(mutex); } Writers can starve in this solution 49 Deadlocks … just have studied them System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock 50 End of Midterm Overview Operating System Concepts – 9th Edition. Silberschatz, Galvin and Gagne ©2013. Modified by Dmitri V. Kalashnikov and Nalini Venkatasubramanian.
© Copyright 2024