A10_concurrency - Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion Processes Threads Concurrency Traditional processes are

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Processes, Threads, Concurrency Traditional processes are sequential: one instruction at a time is executed. Multithreaded processes may have several sequential threads that can execute concurrently. Processes (threads) are concurrent if their executions overlap – start time of one occurs before finish time of another.
Background image of page 2
Concurrent Execution On a uniprocessor, concurrency occurs when the CPU is switched from one process to another, so the instructions of several threads are interleaved (alternate) On a multiprocessor, execution of instructions in concurrent threads may be overlapped (occur at same time) if the threads are running on separate processors.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Concurrent Execution An interrupt, followed by a context switch, can take place between any two instructions. Hence the pattern of instruction overlapping and interleaving is unpredictable. Processes and threads execute asynchronously – we cannot predict if event a in process i will occur before event b in process j .
Background image of page 4
Sharing and Concurrency System resources (files, devices, even memory) are shared by processes, threads, the OS. Uncontrolled access to shared entities can cause data integrity problems – Example: Suppose two threads (1 and 2) have access to a shared (global) variable “balance”, which represents a bank account. Each thread has its own private (local) variable “withdrawal i ”, where i is the thread number
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example • Let balance = 100, withdrawal 1 =50, and withdrawal 2 = 75. • Thread i will execute the following algorithm: if (balance >= withdrawal i ) balance = balance – withdrawal i else // print “Can’t overdraw account!” If thread1 executes first, balance will be 50 and thread2 can’t withdraw funds. If thread2 executes first, balance will be 25 and thread1 can’t withdraw funds.
Background image of page 6
But --- what if the two threads execute concurrently instead of sequentially? Break down into machine-level operations: if (balance >= withdrawal i ) balance = balance – withdrawal i move balance to a register compare register to withdrawal i branch if less-than register = register – withdrawal i store register contents in balance
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example-Multiprocessor (A possible instruction sequence showing interleaved execution) Thread 1 (2) Move balance to register 1 (register = 100) (4) compare register 1 to withdraw 1 (5)register 1 = register 1 withdraw 1 (100-50) (7) store register 1 in balance (balance = 50) Thread 2 (1) Move balance to register 2 (register = 100) (3) compare register 2 to withdraw 2 (6) register 2 = register 2 – withdraw 2 (100 – 75) (8) store register 2 in balance (balance = 25 )
Background image of page 8
Example – Uniprocessor (A possible instruction sequence showing interleaved execution) Thread 1 Move balance to register (Reg. = 100) P 1 ’s time slice expires – its state is saved P 1 is re-scheduled; its state is restored
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/14/2011 for the course CS 690 taught by Professor Staff during the Fall '11 term at University of Alabama - Huntsville.

Page1 / 37

A10_concurrency - Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion Processes Threads Concurrency Traditional processes are

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online