Operating Systems - Unit2

Download Operating Systems - Unit2

Post on 30-May-2018




0 download

Embed Size (px)


<ul><li><p>8/14/2019 Operating Systems - Unit2</p><p> 1/94</p><p>UNIT - 2</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 2/94</p><p>Operating Systems 2</p><p>Threads</p><p> Overview Multithreading Models</p><p>Threading Issues</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 3/94</p><p>Operating Systems 3</p><p>Single vs. MultithreadedProcesses</p><p>Process: everything weve seen up to now</p><p>Thread: like a process, only shares memory, global variables, files, PID</p><p>with other threads within the same process</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 4/94</p><p>Operating Systems 4</p><p>Should you use threads orprocesses?</p><p> Why use threads instead of processes?</p><p> Faster context switch (why?)</p><p> Easier to share data</p><p> Uses less memory and resources Thread creation faster than process creation (30xfaster in Solaris)</p><p> Less things to set-up</p><p> Why use processes instead of threads?</p><p> Different code Running on different machines</p><p> Different owners</p><p> Little communication</p><p> Sharing memory can lead to obscure bugs</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 5/94</p><p>Operating Systems 5</p><p>User-level threads</p><p> Threads can be provided at the user or kernellevel</p><p> User level: kernel knows nothing about threads</p><p> Implemented in a library by somebody withouttouching the kernel</p><p> User library handles Thread creation</p><p> Thread deletion</p><p> Thread scheduling</p><p> Benefits: Faster creation and scheduling (why?)</p><p> Drawbacks:</p><p> One thread blocking during I/O blocks allthreads in process (even ready-to-run</p><p>threads)</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 6/94</p><p>Operating Systems 6</p><p>User-level threads(contd)</p><p>Three primary threadlibraries: POSIX Pthreads</p><p> Win32 threads</p><p> Java threads</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 7/94</p><p>Operating Systems 7</p><p>Kernel-level threads</p><p> Kernel knows about threads Kernel handles thread creation, deletion,scheduling</p><p> Benefits:</p><p> Kernel can schedule another thread if current</p><p>one does blocking I/O Kernel can schedule multiple threads ondifferent CPUs on SMP multiprocessor</p><p> Drawbacks:</p><p> Slower to schedule, create, delete than user-level</p><p> Most modern OSes support kernel-level threads</p><p> Windows XP/2000</p><p> Solaris</p><p> Linux</p><p> Tru64 UNIX Mac OS X </p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 8/94</p><p>Operating Systems 8</p><p>Multithreading Models</p><p> How to map kernel-levelthreads to user-levelthreads?</p><p> Many-to-One</p><p> One-to-One</p><p> Many-to-Many</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 9/94</p><p>Operating Systems 9</p><p>Many-to-One Model</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 10/94</p><p>Operating Systems 10</p><p>Many-to-One</p><p> Many user-level threads mapped tosingle kernel thread</p><p> Examples:</p><p> Solaris Green Threads</p><p> GNU Portable Threads</p><p> Any disadvantages? All block when one blocks</p><p> All run on 1 CPU</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 11/94</p><p>Operating Systems 11</p><p>One-to-one Model</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 12/94</p><p>Operating Systems 12</p><p>One-to-One</p><p> Each user-level thread maps to a kernelthread</p><p> Examples:</p><p> Windows NT/XP/2000</p><p> Linux</p><p> Solaris 9 and later</p><p> Any disadvantages?</p><p> Overhead for creating threads</p><p> Many operating systems limit number</p><p>of threads </p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 13/94</p><p>Operating Systems 13</p><p>Many-to-Many Model</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 14/94</p><p>Operating Systems 14</p><p>Many-to-Many Model</p><p> Allows many user levelthreads to be mapped tosmaller or equal number of</p><p>kernel threads</p><p> Allows the flexibility ofchoosing the number of kernel</p><p>threads allocated to a process Best of both worlds</p><p> Solaris prior to version 9</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 15/94</p><p>Operating Systems 15</p><p>Two-level Model</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 16/94</p><p>Operating Systems 16</p><p>Two-level Model</p><p> Similar to many-to-many,but allows a user thread tobe bound to kernel thread</p><p> Examples IRIX</p><p> HP-UX</p><p>Tru64 UNIX Solaris 8 and earlier</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 17/94</p><p>Operating Systems 17</p><p>Threading Issues</p><p> Semantics offork() andexec() system calls</p><p>Thread cancellation Signal handling</p><p>Thread pools</p><p>Thread-specific data</p><p>i f d</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 18/94</p><p>Operating Systems 18</p><p>Semantics offork() andexec()</p><p> Does fork() duplicate only the calling thread or all threads?</p><p> Some UNIX systems have two versions offork()</p><p> exec() usually replaces all threads with new program</p><p>fork1()</p><p>fork2()</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 19/94</p><p>Operating Systems 19</p><p>Thread Cancellation</p><p> Terminating a thread before it has finished E.g., two cooperating threads, one</p><p>discovers an error</p><p> Two general approaches:</p><p> Asynchronous cancellationterminates the target threadimmediately</p><p> Deferred cancellation allows thetarget thread to periodically check if itshould be cancelled</p><p> Why is this an issue?</p><p> What if a thread is in the middle of</p><p> Allocating resources</p><p> Performing I/O</p><p> Updating a shared data structure </p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 20/94</p><p>Operating Systems 20</p><p>Thread Cancellation(contd)</p><p> Essentially, deferredcancellation = target, pleasecancel yourself</p><p> Occurs when target checks forcancellation signal</p><p> Allows cancellation at safepoints Called cancellation points in</p><p>Pthreads </p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 21/94</p><p>Operating Systems 21</p><p>Signal Handling</p><p> Signals are used in UNIX to notify a process that aparticular event has occurred</p><p> A signal handler is used to process signals</p><p>1. Signal is generated by a particular event</p><p>2. Signal is delivered to a process</p><p>3. Signal is handled (or ignored/blocked) Options:</p><p> Deliver the signal to the thread to which thesignal applies</p><p> Applicable with synchronous signals e.g.,</p><p>illegal memory access</p><p> Deliver the signal to every thread in the process</p><p> Deliver the signal to certain threads in theprocess</p><p> Assign a specific threat to receive all signals forthe process </p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 22/94</p><p>Operating Systems 22</p><p>Thread Pools</p><p> Motivating example: a web server running on anSMP machine</p><p> To handle each connection:</p><p>1. Create a new process to handle it</p><p> too slow, inefficient</p><p>2. Create a new thread to handle it Option 2 better but still has some problems:</p><p> Some overhead for thread creation/deletion</p><p> Thread will only be used for this connection</p><p> Unbounded number of threads might crashweb server</p><p> Better solution: use a thread pool of (usually) fixedsize</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 23/94</p><p>Operating Systems 23</p><p>Thread Pools (contd)</p><p> Threads in pool sit idle</p><p> Request comes in:</p><p> Wake up a thread in pool</p><p> Assign it the request</p><p> When it completes, return it to pool If no threads in pool available, wait</p><p> Advantages:</p><p> Usually slightly faster to wake up anexisting thread than create a new one</p><p> Allows the number of threads in theapplication to be limited by the size of thepool</p><p> More sophisticated systems dynamicallyadjust pool size</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 24/94</p><p>Operating Systems 24</p><p>Thread-specific Data</p><p> Allows each thread to have its own copy of data</p><p> Useful for implementing protection</p><p> For example, user connects to banksdatabase server</p><p> Server process responds, has access to allaccounts</p><p> Multiple people may be accessing theiraccounts at the same time</p><p> Thread assigned to manipulate or view users</p><p>bank account Using thread-specific data limits (unintentional</p><p>or erroneous) access by other threads</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 25/94</p><p>Operating Systems 25</p><p>CPU Scheduling</p><p> Basic Concepts</p><p> Scheduling Criteria</p><p> Scheduling Algorithms Multiple-ProcessorScheduling</p><p> Real-Time Scheduling</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 26/94</p><p>Operating Systems 26</p><p>Basic Concepts</p><p> Maximum CPU utilizationobtained withmultiprogramming</p><p> CPUI/O Burst Cycle Processexecution consists of a cycleof CPU execution and I/O wait</p><p> CPU burst distribution</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 27/94</p><p>Operating Systems 27</p><p>Alternating Sequence of CPU And I/OBursts</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 28/94</p><p>Operating Systems 28</p><p>Histogram of CPU-burstTimes</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 29/94</p><p>Operating Systems 29</p><p>CPU Scheduler</p><p> Selects from among the processes inmemory that are ready to execute, andallocates the CPU to one of them</p><p> CPU scheduling decisions may take placewhen a process:</p><p>1. Switches from running to waiting state</p><p>2. Switches from running to ready state</p><p>3. Switches from waiting to ready</p><p>4.Terminates</p><p> Scheduling under 1 and 4 isnonpreemptive</p><p> All other scheduling ispreemptive</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 30/94</p><p>Operating Systems 30</p><p>Dispatcher</p><p> Dispatcher module gives control of the CPUto the process selected by the short-termscheduler; this involves:</p><p> switching context</p><p> switching to user modejumping to the proper location in the user</p><p>program to restart that program</p><p> Dispatch latency time it takes for the</p><p>dispatcher to stop one process and startanother running</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 31/94</p><p>Operating Systems 31</p><p>Scheduling Criteria</p><p> CPU utilization keep the CPU as busy aspossible</p><p> Throughput # of processes that completetheir execution per time unit</p><p> Turnaround time amount of time to executea particular process</p><p> Waiting time amount of time a process hasbeen waiting in the ready queue</p><p> Response time amount of time it takes from</p><p>when a request was submitted until the firstresponse is produced, not output (for time-sharing environment)</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 32/94</p><p>Operating Systems 32</p><p>Optimization Criteria</p><p> Max CPU utilization Max throughput</p><p> Min turnaround time</p><p> Min waiting time</p><p> Min response time</p><p>Fi t C Fi t S d (FCFS) </p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 33/94</p><p>Operating Systems 33</p><p>First-Come, First-Served (FCFS)Scheduling</p><p>Process Burst TimeP1 24</p><p>P2 3</p><p>P3 3 Suppose that the processes arrive in the order: P1 , P2</p><p>, P3The Gantt Chart for the schedule is:</p><p> Waiting time for P1 = 0; P2 = 24; P3 = 27</p><p> Average waiting time: (0 + 24 + 27)/3 = 17</p><p>P1 P2 P3</p><p>24 27 300</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 34/94</p><p>Operating Systems 34</p><p>FCFS Scheduling (Cont.)</p><p>Suppose that the processes arrive in the order</p><p>P2 , P3 , P1</p><p> The Gantt chart for the schedule is:</p><p> Waiting time for P1 = 6;P2 = 0; P3 = 3</p><p> Average waiting time: (6 + 0 + 3)/3 = 3</p><p>Much better than previous case Convoy effectshort process behind longprocess</p><p>P1P3P2</p><p>63 300</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 35/94</p><p>Operating Systems 35</p><p>Shortest-Job-First (SJR)Scheduling</p><p> Associate with each process the length of itsnext CPU burst. Use these lengths toschedule the process with the shortest time</p><p> Two schemes:</p><p> nonpreemptive once CPU given to theprocess it cannot be preempted untilcompletes its CPU burst</p><p> preemptive if a new process arrives withCPU burst length less than remaining timeof current executing process, preempt.</p><p>This scheme is know as theShortest-Remaining-Time-First (SRTF)</p><p> SJF is optimal gives minimum averagewaiting time for a given set of processes</p><p>l f</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 36/94</p><p>Operating Systems 36</p><p>Process Arrival Time Burst TimeP1 0.0 7</p><p>P2 2.0 4</p><p>P3 4.0 1</p><p>P4 5.0 4 SJF (non-preemptive)</p><p> Average waiting time = (0 + 6 + 3 + 7)/4 = 4</p><p>Example of Non-Preemptive SJF</p><p>P1 P3 P2</p><p>73 160</p><p>P4</p><p>8 12</p><p>l f i</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 37/94</p><p>Operating Systems 37</p><p>Example of PreemptiveSJF</p><p>Process Arrival Time Burst Time</p><p>P1 0.0 7</p><p>P2 2.0 4</p><p>P3 4.0 1</p><p>P4 5.0 4</p><p> SJF (preemptive)</p><p> Average waiting time = (9 + 1 + 0 +2)/4 = 3</p><p>P1 P3P2</p><p>42110</p><p>P4</p><p>5 7</p><p>P2 P1</p><p>16</p><p>Determining Length of Next</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 38/94</p><p>Operating Systems 38</p><p>Determining Length of NextCPU Burst</p><p> Can only estimate the length</p><p> Can be done by using the length ofprevious CPU bursts, using exponential</p><p>averaging</p><p>:Define4.</p><p>10,3.</p><p>burstCPUnexttheforvaluepredicted2.</p><p>burstCPUoflenghtactual1.</p><p>1</p><p>=</p><p>=</p><p>+</p><p>n</p><p>th</p><p>nnt ( ) .1</p><p>1 nnnt +=</p><p>=</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 39/94</p><p>Operating Systems 39</p><p>Prediction of the Length of the NextCPU Burst</p><p>E l f E ti l</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 40/94</p><p>Operating Systems 40</p><p>Examples of ExponentialAveraging</p><p> =0 n+1 = n Recent history does not count</p><p> =1</p><p> n+1</p><p>= tn</p><p> Only the actual last CPU burst counts</p><p> If we expand the formula, we get:</p><p>n+1 = tn+(1 - )tn-1+ </p><p> +(1 - )jtn-j+ </p><p> +(1 - )n +1 0</p><p> Since both and (1 - ) are less than orequal to 1, each successive term has lessweight than its predecessor</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 41/94</p><p>Operating Systems 41</p><p>Priority Scheduling</p><p> A priority number (integer) is associatedwith each process</p><p> The CPU is allocated to the process withthe highest priority (smallest integer highest priority)</p><p> Preemptive</p><p> nonpreemptive</p><p> SJF is a priority scheduling where priorityis the predicted next CPU burst time</p><p> Problem Starvation low priorityprocesses may never execute</p><p> Solution Aging as time progressesincrease the priority of the process</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 42/94</p><p>Operating Systems 42</p><p>Round Robin (RR)</p><p> Each process gets a small unit of CPU time (timequantum), usually 10-100 milliseconds. After thistime has elapsed, the process is preempted andadded to the end of the ready queue.</p><p> If there are n processes in the ready queue and</p><p>the time quantum is q, then each process gets 1/nof the CPU time in chunks of at most q time unitsat once. No process waits more than (n-1)q timeunits.</p><p> Performance</p><p> q large FIFO q small q must be large with respect to</p><p>context switch, otherwise overhead is too high</p><p>Example of RR with Time Quantum</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 43/94</p><p>Operating Systems 43</p><p>Example of RR with Time Quantum= 20</p><p>Process Burst TimeP1 53</p><p>P2 17</p><p>P3 68</p><p>P</p><p>4 24 The Gantt chart is:</p><p> Typically, higher average turnaround than SJF,but better response</p><p>P1 P2 P3 P4 P1 P3 P4 P1 P3 P3</p><p>0 20 37 57 77 97 117 121 134 154 162</p><p>Ti Q t d C t t</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 44/94</p><p>Operating Systems 44</p><p>Time Quantum and ContextSwitch Time</p><p>T d Ti V i With Th</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 45/94</p><p>Operating Systems 45</p><p>Turnaround Time Varies With TheTime Quantum</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 46/94</p><p>Operating Systems 46</p><p>Multilevel Queue</p><p> Ready queue is partitioned into separate queues:foreground (interactive)background (batch)</p><p> Each queue has its own scheduling algorithm</p><p> foreground RR</p><p> background FCFS</p><p> Scheduling must be done between the queues</p><p> Fixed priority scheduling; (i.e., serve all fromforeground then from background).</p><p>Possibility of starvation. Time slice each queue gets a certain</p><p>amount of CPU time which it can scheduleamongst its processes; i.e., 80% toforeground in RR</p><p> 20% to background in FCFS </p><p>Multilevel Queue</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 47/94</p><p>Operating Systems 47</p><p>Multilevel QueueScheduling</p><p>Multilevel Feedback</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 48/94</p><p>Operating Systems 48</p><p>Multilevel FeedbackQueue</p><p> A process can move between the various queues;aging can be implemented this way</p><p> Multilevel-feedback-queue scheduler defined by thefollowing parameters:</p><p> number of queues</p><p> scheduling algorithms for each queue</p><p> method used to determine when to upgrade aprocess</p><p> method used to determine when to demote aprocess</p><p> method used to determine which queue aprocess will enter when that process needsservice</p><p>Example of Multilevel Feedback</p></li><li><p>8/14/2019 Operating Systems - Unit2</p><p> 49/94</p><p>Operating Systems 49</p><p>Example of Multilevel FeedbackQueue</p><p> Three queues:</p><p> Q0 RR with time quantum 8 milliseconds</p><p> Q1 RR time quantum 16 milliseconds</p><p> Q2 FCFS</p><p> Scheduling</p><p> A new job enters queue Q0which is...</p></li></ul>


View more >