3. Topics Concept of processes Concurrent processes Threads Overview of classical synchronization problems Monitors Communicating Sequential processes MIT CS704D Advanced OS Class of 2011 3
5. Denning’s DefinitionofOperating Systems(1983) OS is a computer software that assists hardware to perform following process management functions Creating and destroying processes Controlling progress towards completion Acting on arithmetic and other exceptions Allocating hardware resources Providing communications by messages and signals among processes MIT CS704D Advanced OS Class of 2011 5
6. Implicit & Explicit Tasking Implicit tasking (system defined) Where the OS creates the processes to manage its own work Explicit tasking(programmer defined) Defined by programmer to create concurrent processes to make the execution of the program faster Driving I/O that have latency User convenience Multiprocessing Distributed computing MIT CS704D Advanced OS Class of 2011 6
7. Process Relationship Competition Competes for resources for completion Careful resource allocation and protection Controlled sharing of data and exchange of sync signals Cooperation Shares resources, attributes; interacts Family processes, parent-child processes MIT CS704D Advanced OS Class of 2011 7
8. Process Control Block Process id Priority State (ready, running, suspended) Hardware state (processor registers, flags) Scheduling information, usage statistics Memory management information (registers, tables) I/O status (allocated devices, pending operations) File management information (open files, access rights) Accounting information MIT CS704D Advanced OS Class of 2011 8
9. Process Lists Linked lists of PCBs One list for ready processes One for suspended One of running- trivial in case of uniprocessor MIT CS704D Advanced OS Class of 2011 9
10. Process Switch Mode switch Record state of current process; PC, SP,PSW, and all program accessible registers Update PCB data; state, reason for preemption or suspension, measure processor usage Invoke scheduler to start a relevant process Get PCB data to prepare running of the process Restore hardware state, calculate interrupt mask Establish pointers to active files, resource lists Update memory management registers MIT CS704D Advanced OS Class of 2011 10
11. Process Switching Efficiency Hardware assists Have hardware set of registers identical in structure to PCB and Threads or lightweight processes MIT CS704D Advanced OS Class of 2011 11
12. Threads A lightweight process with reduced state A group of related threads can share resources such as memory and files Separate flow of control with its own stack and hardware state Programmer driven concurrency Switching between threads from different processes costs the same as switching processes MIT CS704D Advanced OS Class of 2011 12
13. OS ServicesforProcess Management Create (processID, attributes) Delete (processID) Fork/Join Abort (processID) Suspend (processID) Resume (processID) Delay (processID, time) Get_Attributes (processID, attribute_set) Change_Priority (processID, new_priority) MIT CS704D Advanced OS Class of 2011 13
14. Schedulers MIT CS704D Advanced OS Class of 2011 14 Suspended & Swapped out queue Medium term scheduler Short term scheduler Long term scheduler Batch jobs CPU Batch Queue Ready Queue Interactive programs Suspended queue
15. Scheduling performance Criteria Processor utilization Throughput Turnaround time Waiting time Response time MIT CS704D Advanced OS Class of 2011 15
16. Scheduling Algorithms Preemptive, Non preemptive First come first served (FCFS) Shortest remaining time next (SRTN) Time slice or Round Robin or RR Priority based event driven preemptive, (event driven, ED) Multiple level Queues, MLQ MLQ with feedback MIT CS704D Advanced OS Class of 2011 16
18. Inter-process Synchronization Cooperating processes need to synchronize with each other when resources need to be shared-shared data structures or physical device. Following interactions needed Inter-process synchronization For serially reusable resources Inter-process signaling Exchange of signals to help synchronization Inter-process communication Exchanging data, reporting progress, accumulating collective result MIT CS704D Advanced OS Class of 2011 18
19. Problem Example {Process Keyboard} Echo= echo+1 ----------------------------------- {Process Display}} Echo=echo-1 ------------------------------------ Load R, echo Dec R Store Echo, R MIT CS704D Advanced OS Class of 2011 19
20. Desirable features ofMutual Exclusion Mutual exclusion between processes accessing protected shared resource Relative speeds and priorities of contending processes can never be assumed Crashes outside the critical section should not prevent other processes from accessing the shared resource When contention arises access must be granted to one in finite time MIT CS704D Advanced OS Class of 2011 20
21. Basic Protocol Following sequence must be used (negotiation protocol)---winner goes ahead (Critical section)--Only one process uses the resource (Release protocol)—Ownership released MIT CS704D Advanced OS Class of 2011 21
22. Algorithm #1 Code as in Milan Milenkovic, p 93 Problems Identity of adversary is known There is a strict sequence (P1, P2) If one of them crashes, the other will have to keep waiting For multiple contending processes this could get complex MIT CS704D Advanced OS Class of 2011 22
23. Algorithm #2 Code as in Milan Milenkovic, p 96 Improvement No turn taking is forced Problems More than one process may be in the critical section MIT CS704D Advanced OS Class of 2011 23
24. Algorithm #3 Code as in Milan Milenkovic, p 98 Improvement No two processes can be in the CS at the same time Problem Both can get into an indefinite loop MIT CS704D Advanced OS Class of 2011 24
25. Semaphores Wait; decrement value of semaphore if it is non negative. Wait operation is indivisible Signal; increment the value of semaphore, operation is indivisible -------------------------------- Binary semaphores and general or counting semaphore MIT CS704D Advanced OS Class of 2011 25
26. Wait & signal operations wait(s): while not (s>0) do (keeptesting) s:=s-1 --------------------------------- signal(s): s:=s+1 Implementation Code in Milan Milenkovic, p 100 Advantages Meets all the criteria for mutual exclusion MIT CS704D Advanced OS Class of 2011 26
27. Properties & Characteristics Processes need not even be aware of number of competitors Modification of code in one process does not need changes in another Semaphores can be in Programming language or as service calls in OS Create_Semaphore, Attach_To_Semaphore, Wait, Signal, Close_Semaphore are some calls that can be implemented MIT CS704D Advanced OS Class of 2011 27
28. Service Discipline Required to avoid indefinite postponement or livelock or starvation Discipline policy has to avoid such starvation “A request to enter a critical section must be granted in finite time” must be enforced FCFS policy ensure everyone gets a turn MIT CS704D Advanced OS Class of 2011 28
29. Semaphore Granularity Serialization of processes by semaphore can have an impact on performance Granularity of individual semaphores then is an important aspect Finest granularity ensure there is no concurrency timing problems Problems: potentially large storage overhead for storing a large number of semaphores, processing overhead of too many wait & signals Coarse granularity (controlling a set of resources within one Semaphore) Problem: reduces benefits of parallelism MIT CS704D Advanced OS Class of 2011 29
30. Hardware SupportforConcurrency Control Semaphore implementation needs some hardware support Pessimistic & Optimistic concurrency control Disable/enable interrupts Test and set instruction Compare an Swap instruction MIT CS704D Advanced OS Class of 2011 30
31. Pessimistic & OptimisticConcurrency control Pessimistic solution Block everything Update global variable Unblock the part of the system blocked in first step --------------------------------------------------- Optimistic solution Read the value of the global variable, prepare tentative local update based on the value (global variable remains accessible to others) Compare current value of global variable to the value of local update. If the global variable is unchanged, apply local update. If not discard local update. Go to earlier step MIT CS704D Advanced OS Class of 2011 31
32. Tools for Implementationof Semaphores #1 Disable/enable interrupts (but is pessimistic) DI Critical section EI --------------------------- If users have access, it can cause chaos Does not work in multiple processors case ban use by programmers, only system should use for implementing the wait and signal methods MIT CS704D Advanced OS Class of 2011 32
33. Tools for Implementationof Semaphores #2 Test and Set instruction Wait: TS S BNF Wait Return ----------------------------- Wait: Mov AL, 1 XCHG AL,S CMP AL, 0 JNZ Wait Ret MIT CS704D Advanced OS Class of 2011 33