Concurrency Concurrency Definition : Several scripts are executing - - PowerPoint PPT Presentation

concurrency concurrency
SMART_READER_LITE
LIVE PREVIEW

Concurrency Concurrency Definition : Several scripts are executing - - PowerPoint PPT Presentation

Concurrency Concurrency Definition : Several scripts are executing simultaneously and potentially interacting with each other This is how we assign grades! Based on the Birkahni Theorem, we usually get the grades to average to a B+, though due


slide-1
SLIDE 1

Concurrency

slide-2
SLIDE 2

Concurrency

Definition: Several scripts are executing simultaneously and potentially interacting with each other

This is how we assign grades! Based on the Birkahni Theorem, we usually get the grades to average to a B+, though due to the size of the class this semester, the average will be a C+ (jk)

slide-3
SLIDE 3

Race Condition

Concurrency Issue

slide-4
SLIDE 4

Race Condition

Definition: when events of a program don't happen in the

  • rder that the programmer intended.
slide-5
SLIDE 5

Function Definitions

  • read value: reads in a value from user input
  • increments value: increments the value, but does not set it
  • sets value: sets the value to the incremented version of it.
slide-6
SLIDE 6

Race Condition Example

We have two programs, program 1 and program 2, and a global variable ‘num’. Ideally, we want the script in program 1 to run before the script in program 2, but this won’t always be the case. We’ll look at two scenarios, the first where they run in order (serial), and the second where they don’t (race condition).

slide-7
SLIDE 7

Serial - Example

(Global Integer Value)

‘num’ starts out with value 0

slide-8
SLIDE 8

Serial - Example

(Global Integer Value)

read value (reads the value of ‘num’, and sets ‘temp1’ to that value)

slide-9
SLIDE 9

Serial - Example

(Global Integer Value)

increments value (increases the value of ‘temp1’ by 1)

slide-10
SLIDE 10

Serial - Example

(Global Integer Value)

1

sets value (sets ‘num’ to the value of ‘temp1’, which is 1)

slide-11
SLIDE 11

Serial - Example

(Global Integer Value)

1 1

read value (reads the value of ‘num’, and sets ‘temp2’ to that value)

slide-12
SLIDE 12

Serial - Example

(Global Integer Value)

1 1 1

increments value (increases the value of ‘temp2’ by 2)

slide-13
SLIDE 13

Serial - Example

(Global Integer Value)

1 1 1 3

sets value (sets ‘num’ to the value of ‘temp2’, which is 3)

slide-14
SLIDE 14

Serial - Example

(Global Integer Value)

1 1 1 3

This is the expected output. We’re good here!

slide-15
SLIDE 15

What if we interleaved the commands?

slide-16
SLIDE 16

Race Condition - Example

(Global Integer Value)

‘num’ starts out with value 0

slide-17
SLIDE 17

Race Condition - Example

(Global Integer Value)

read value (reads the value of ‘num’, and sets ‘temp1’ to that value)

slide-18
SLIDE 18

Race Condition - Example

(Global Integer Value)

read value (reads the value of ‘num’, and sets ‘temp2’ to that value)

slide-19
SLIDE 19

Race Condition - Example

(Global Integer Value)

increments value (increases the value of ‘temp1’ by 1)

slide-20
SLIDE 20

Race Condition - Example

(Global Integer Value)

increments value (increases the value of ‘temp2’ by 2)

slide-21
SLIDE 21

Race Condition - Example

(Global Integer Value)

1

sets value (sets ‘num’ to the value of ‘temp1’, which is 1)

slide-22
SLIDE 22

Race Condition - Example

(Global Integer Value)

1 2

sets value (sets ‘num’ to the value of ‘temp2’, which is 2)

slide-23
SLIDE 23

Race Condition - Example

(Global Integer Value)

1 2

This is the NOT the expected output. ‘num’ is only 2!

slide-24
SLIDE 24

Takeaway

Concurrency is great because it allows for tasks to be broken up and completed almost simultaneously. However, you have to be careful how you break up the tasks so you don’t get erroneous behavior.

slide-25
SLIDE 25

Race Condition Example from Lecture

slide-26
SLIDE 26

Winky Face Problem

slide-27
SLIDE 27
slide-28
SLIDE 28
slide-29
SLIDE 29

Deadlock

Concurrency Issue

slide-30
SLIDE 30

Deadlock

Definition: a situation in which two or more competing actions are each waiting for the other(s) to finish, and thus no one ever finishes.

slide-31
SLIDE 31

Deadlock - Example

slide-32
SLIDE 32

Dining Philosopher Problem

slide-33
SLIDE 33
slide-34
SLIDE 34
slide-35
SLIDE 35

Recursion

Base Case(s): Recursive Case(s):

slide-36
SLIDE 36

Recursion

Base Case(s):

  • Simplest form of the problem

Recursive Case(s):

slide-37
SLIDE 37

Recursion

Base Case(s):

  • Simplest form of the problem

Recursive Case(s):

  • Divide problem into smaller instances
slide-38
SLIDE 38

Recursion

Base Case(s):

  • Simplest form of the problem

Recursive Case(s):

  • Divide problem into smaller instances
  • Invoke function (recursively)
slide-39
SLIDE 39

Recursion

Base Case(s):

  • Simplest form of the problem

Recursive Case(s):

  • Divide problem into smaller instances
  • Invoke function (recursively)
  • Work towards base case
slide-40
SLIDE 40

Ladder Fractal

slide-41
SLIDE 41

Ladder Fractal Solution

slide-42
SLIDE 42

Rectangle Fractal

slide-43
SLIDE 43

Rectangle Fractal Solution