Summary

This is a lecture on operating systems, focusing on the topic of concurrency. The lecture covers concepts like multiprogramming, multiprocessing, and distributed processing. It explores issues like the design and management concerns raised by concurrency, including sharing resources and process synchronization.

Full Transcript

Operating Systems: Chapter 5 Internals Concurrency: and Design Principles Mutual Exclusion and Synchronization Eighth Edition By William Stallings Operating System design is concerned...

Operating Systems: Chapter 5 Internals Concurrency: and Design Principles Mutual Exclusion and Synchronization Eighth Edition By William Stallings Operating System design is concerned with the management of processes and threads: Multiprogramming Multiprocessing Distributed Processing Multiprogramming allows multiple programs to reside in the main memory simultaneously to maximize utilization.. Multiprocessing involves using two or more processors within a single computer system. It increases the system’s processing power and reliability by distributing tasks across multiple CPUs. Distributed processing involves multiple computers (nodes) working together over a network to complete a task. Large applications are Multiple divided into smaller. Applications Structured Applications Operating invented to allow System processing time to Structure be shared among extension of active applications modular design and structured programming OS themselves to manage complexity implemented as a and improve set of processes maintainability. or threads Table 5.1 Some Key Terms Related to Concurrency Interleaving and overlapping can be viewed as examples of concurrent processing both present the same problems Uniprocessor – the relative speed of execution of processes cannot be predicted depends on activities of other processes the way the OS handles interrupts scheduling policies of the OS Sharing of global resources Difficult for the OS to manage the allocation of resources optimally Difficultto locate programming errors as results are not deterministic and reproducible Occurs when multiple processes or threads read and write data items Thefinal result depends on the order of execution the “loser” of the race is the process that updates last and will determine the final value of the variable Operating System Concerns Design and management issues raised by the existence of concurrency: The OS must: be able to keep track of various processes allocate and de-allocate resources for each active process protect the data and physical resources of each process against interference by other processes ensure that the processes and outputs are independent of the processing speed Degree of Awareness Relationship I nfluence that One Potential Control Process Has on the Problems Other Processes unaware of Competition Results of one Mutual exclusion Table 5.2 each other process independent of the action of Deadlock (renewable others resource) Process Timing of process may be affected Starvation Interaction Processes indirectly Cooperation by Results of one Mutual exclusion aware of each other sharing process may depend (e.g., shared object) on information Deadlock (renewable obtained from others resource) Timing of process Starvation may be affected Data coherence Processes directly Cooperation by Results of one Deadlock aware of each other communication process may depend (consumable (have communication on information resource) primitives available to obtained from others them) Starvation Timing of process may be affected Resource Competition  Concurrent processes come into conflict when they are competing for use of the same resource  for example: I/O devices, memory, processor time, clock In the case of competing processes three control problems must be faced: the need for mutual exclusion deadlock starvation Figure 5.1 Illustration of Mutual Exclusion PROCESS 1 * / / * PROCESS 2 * / / * PROCESS n * / voi d P1 voi d P2 voi d Pn { { { whi l e (true) { whi l e (true) { whi l e (true) { ; ; ; entercritical (Ra); entercritical (Ra); entercritical (Ra); ; ; ; exitcritical (Ra); exitcritical (Ra); exitcritical (Ra); ; ; ; } } } } } } Must be enforced A process that halts must do so without interfering with other processes No deadlock or starvation A process must not be denied access to a critical section when there is no other process using it No assumptions are made about relative process speeds or number of processes A process remains inside its critical section for a finite time only  Interrupt Disabling  Disadvantages:  uniprocessor system  the efficiency of execution could be  disabling interrupts noticeably degraded guarantees mutual exclusion  this approach will not work in a multiprocessor architecture Compare&Swap Instruction also called a “compare and exchange instruction” a compare is made between a memory value and a test value if the values are the same a swap occurs carried out atomically Figure 5.2 Hardware Support for Mutual Exclusion / * pr ogr am mutualexclusion */ / * pr ogr am mutualexclusion */ const i nt n = ; i nt const n = ; i nt bolt; i nt bolt; voi d P(i nt i) voi d P(i nt i) { { whi l e (true) { whi l e ( true) { whi l e (compare_and_swap(&bolt, 0, 1) == 1) i nt keyi = 1; ; do exchange (&keyi, &bolt) ; whi l e (keyi != 0); bolt = 0; ; ; bolt = 0; } ; } } voi d mai n( ) } { voi d mai n( ) bolt = 0; { par begi n ( P(1), P(2),... ,P(n)); bolt = 0; par begi n ( P(1), P(2),..., P(n)); } } (a) Compare and swap instruction (b) Exchange instruction Applicable to any number of processes on either a single processor or multiple processors sharing main memory Simple and easy to verify It can be used to support multiple critical sections; each critical section can be defined by its own variable Special Machine Instruction: Disadvantages Busy-waiting is employed, thus while a process is waiting for access to a critical section it continues to consume processor time Starvation is possible when a process leaves a critical section and more than one process is waiting Deadlock is possible Semaphor e An integer value used for signaling among processes. Only three operations may be performed on a semaphore, all of which are atomic: initialize, decrement, and increment. The decrement operation may result in the blocking of a process, and the increment operation may result in the unblocking of a process. Also known as a counting semaphore or a general semaphore Binary Semaphore M utex A semaphore that takes on only the values 0 and 1. Similar to a binary semaphore. A key difference between the two is Table 5.3 that the process that locks the mutex (sets the value to zero) must be the one to unlock it (sets the value to 1). Condition Variable A data type that is used to block a process or thread until a particular condition is true. Common M onitor A programming language construct that encapsulates variables, access procedures and initialization code within an abstract data type. The monitor's variable may only be accessed via its access procedures and only one process may be actively accessing the monitor at any one time. The access procedures are critical sections. Concurrency A monitor may have a queue of processes that are waiting to access it. Event Flags A memory word used as a synchronization mechanism. Application code may associate a different event with each bit in a flag. A thread can wait for either a single event or a combination of events by Mechanisms checking one or multiple bits in the corresponding flag. The thread is blocked until all of the required bits are set (AND) or until at least one of the bits is set (OR). M ailboxes/M essages A means for two processes to exchange information and that may be used for synchronization. Spinlocks Mutual exclusion mechanism in which a process executes in an infinite loop waiting for the value of a lock variable to indicate availability. Semaphore A variable that has an integer value There is no way to inspect or upon which only manipulate semaphores other three operations are than these three operations defined: 1) May be initialized to a nonnegative integer value 2) The semWait operation decrements the value 3) The semSignal operation increments the value Consequences There is no way to There is no way to know which process You don’t know know before a will continue whether another process decrements immediately on a process is waiting so a semaphore uniprocessor system the number of whether it will when two processes unblocked processes block or not are running may be zero or one concurrently Figure 5.3 A Definition of Semaphore Primitives st r uct binary_semaphore { enum {zero, one} value; queueType queue; }; voi d semWaitB(binary_semaphore s) { i f ( s.value == one) s.value = zero; el se { ; ; } } voi d semSignalB(semaphore s) { i f (s.queue is empty()) s.value = one; el se { ; ; } } Figure 5.4 A Definition of Binary Semaphore Primitives  A queue is used to hold processes waiting on the semaphore Strong Semaphores the process that has been blocked the longest is released from the queue first (FIFO) Weak Semaphores the order in which processes are removed from the queue is not specified A issues semWait, later times out 1 Processor C D B A D BA C Ready queue Processor Ready queue s= 1 s= 0 5 C issues semWait Blocked queue Blocked queue Processor A CD B Ready queue s= 0 2 B issues semWait Blocked queue Processor Processor AC D D Ready queue D issues semSignal Ready queue D issues semSignal s = –1 3 s = –3 6 B C A B Blocked queue Blocked queue D issues semSignal, later times out 4 Processor B AC D C D Ready queue Processor Ready queue D issues semSignal s= 0 s = –2 7 A B Blocked queue Blocked queue Figure 5.5 Example of Semaphor e M echanism Figure 5.6 Mutual Exclusion Using Semaphores Queue for Value of semaphore lock semaphore lock A B C Critical 1 region Normal semWait(lock) execution 0 Blocked on semWait(lock) semaphore B –1 lock semWait(lock) C B –2 semSignal(lock) C –1 semSignal(lock) 0 semSignal(lock) 1 Note that normal execution can proceed in parallel but that critical regions are serialized. Figure 5.7 Processes Accessing Shared Data Protected by a Semaphor e

Use Quizgecko on...
Browser
Browser