Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 {...

21
Concurrent Systems Exercise 02 – Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016) 1 – 10

Transcript of Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 {...

Page 1: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Concurrent SystemsExercise 02 – Processes, Threads, Coroutines

Stefan Reif

November 07, 2016

sr cs-ex2 (November 07, 2016) 1 – 10

Page 2: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Agenda

Control Flows

Coroutines

Threads

Assignment 2

sr cs-ex2 (November 07, 2016) 2 – 10

Page 3: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Review: Executor Service

Executor Service (⇒ Assignment 1)

Jobs have run-to-completion semanticsNo inter-job coordinationNo inter-job dependencies

Example

sr cs-ex2 (November 07, 2016) Control Flows 3 – 10

Page 4: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Review: Executor Service

Executor Service (⇒ Assignment 1)

Jobs have run-to-completion semanticsNo inter-job coordinationNo inter-job dependencies

Example

sr cs-ex2 (November 07, 2016) Control Flows 3 – 10

Page 5: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Control Flows

Examples⇒ Instruction sequence, function call, interrupts, coroutines, threads, ...

Overlapping patterns⇒ Sequential, stack-like, pseudo-parallel, arbitrary, ...

Associated resources⇒ Stack space, address space, file descriptors, ...

Synchronization

Manage concurrent control flowsConsider application dependencies and overlapping patterns

sr cs-ex2 (November 07, 2016) Control Flows 4 – 10

Page 6: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Coroutines

Voluntarily release the processor

create(), resume(), destroy()Switch to another coroutine explicitly

Symmetric relation

Example

C1 C2

resume(C1)

wait(...)

C3

resume(C1)

resume(C2)

sr cs-ex2 (November 07, 2016) Coroutines 5 – 10

Page 7: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Coroutines

Voluntarily release the processor

create(), resume(), destroy()Switch to another coroutine explicitly

Symmetric relation

Example

C1 C2

resume(C1)

wait(...)

C3

resume(C1)

resume(C2)

sr cs-ex2 (November 07, 2016) Coroutines 5 – 10

Page 8: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Coroutines

Voluntarily release the processor

create(), resume(), destroy()Switch to another coroutine explicitly

Symmetric relation

Example

C1 C2

resume(C1)

wait(...)

C3

resume(C1)

resume(C2)

sr cs-ex2 (November 07, 2016) Coroutines 5 – 10

Page 9: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Coroutines

Voluntarily release the processor

create(), resume(), destroy()Switch to another coroutine explicitly

Symmetric relation

Example

C1 C2

resume(C1)

wait(...)

C3

resume(C1)

resume(C2)

sr cs-ex2 (November 07, 2016) Coroutines 5 – 10

Page 10: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Coroutines

Voluntarily release the processor

create(), resume(), destroy()Switch to another coroutine explicitly

Symmetric relation

Example

C1 C2

resume(C1)

wait(...)

C3

resume(C1)

resume(C2)

sr cs-ex2 (November 07, 2016) Coroutines 5 – 10

Page 11: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Continuations

Context Switches

How can a processor switch between coroutines?How can a coroutine be continued?

Language Considerations

Typical high-level programming languages cannot implement resumeSome languages offer “coroutines” to programmers

→ e.g. Python yield

Context switches need assembler language support

Continuation

Data structure for the Context of a coroutineStopped control flow can proceed laterStores at least a code addressTypically associated to an individual stack

sr cs-ex2 (November 07, 2016) Coroutines 6 – 10

Page 12: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Continuations

Context Switches

How can a processor switch between coroutines?How can a coroutine be continued?

Language Considerations

Typical high-level programming languages cannot implement resumeSome languages offer “coroutines” to programmers

→ e.g. Python yield

Context switches need assembler language support

Continuation

Data structure for the Context of a coroutineStopped control flow can proceed laterStores at least a code addressTypically associated to an individual stack

sr cs-ex2 (November 07, 2016) Coroutines 6 – 10

Page 13: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Continuations

Context Switches

How can a processor switch between coroutines?How can a coroutine be continued?

Language Considerations

Typical high-level programming languages cannot implement resumeSome languages offer “coroutines” to programmers

→ e.g. Python yield

Context switches need assembler language support

Continuation

Data structure for the Context of a coroutineStopped control flow can proceed laterStores at least a code addressTypically associated to an individual stack

sr cs-ex2 (November 07, 2016) Coroutines 6 – 10

Page 14: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Threads

Problems with Coroutines

Unstructured resume() callsHow to choose a successor?

Threads extend CoroutinesStructured synchronization primitives

→ Mutex, Condition Variable, Semaphor, Monitor, Signal, ...

Thread states

→ READY, RUNNING, BLOCKED, TERMINATED, ...

Scheduling

→ Manage control flows explicitly→ Implement a strategy for idle processors

sr cs-ex2 (November 07, 2016) Threads 7 – 10

Page 15: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Assignment 2

Implement a Light-weight Threading Library (LWT)

Extend jobs to coroutines/threadsImplement synchronization primitives

Scheduling

Cooperative, non-preemptiveOne shared ready list

Use kernel-level threads as back-end

Pthreads logically represent processor coresPthread synchronization mechanisms are available

Simplifications

No graceful terminationNo dynamic adaption of parallelismSimple scheduler

sr cs-ex2 (November 07, 2016) Assignment 2 8 – 10

Page 16: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Assignment 2 Waiting

Blocking synchronization

Set thread state to BLOCKED

Add thread to condition-specific waiting queue

Thread notification

Set thread state to READY

Add thread to ready list

READY

BLOCKED

Condition

sr cs-ex2 (November 07, 2016) Assignment 2 9 – 10

Page 17: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Assignment 2 Waiting

Blocking synchronization

Set thread state to BLOCKED

Add thread to condition-specific waiting queue

Thread notification

Set thread state to READY

Add thread to ready list

READY

BLOCKED

Condition

sr cs-ex2 (November 07, 2016) Assignment 2 9 – 10

Page 18: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Assignment 2 Waiting

Blocking synchronization

Set thread state to BLOCKED

Add thread to condition-specific waiting queue

Thread notification

Set thread state to READY

Add thread to ready list

READY

BLOCKED

Condition

sr cs-ex2 (November 07, 2016) Assignment 2 9 – 10

Page 19: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Assignment 2 Synchronization

Scheduler data structures need protectionWe can use a pthread mutexWe will use non-blocking synchronization later

What to do in idle state?We can use pthread condition variables to wait passively

Critical Section

T1 T2

Idle

lock()

resume(T2)

unlock()

resume(Idle)

sleep()

resume(T1)

unlock()

sr cs-ex2 (November 07, 2016) Assignment 2 10 – 10

Page 20: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Assignment 2 Synchronization

Scheduler data structures need protectionWe can use a pthread mutexWe will use non-blocking synchronization later

What to do in idle state?We can use pthread condition variables to wait passively

Critical Section

T1 T2

Idle

lock()

resume(T2)

unlock()

resume(Idle)

sleep()

resume(T1)

unlock()

sr cs-ex2 (November 07, 2016) Assignment 2 10 – 10

Page 21: Concurrent Systems - Exercise 02 Processes, Threads ... · Concurrent Systems Exercise 02 { Processes, Threads, Coroutines Stefan Reif November 07, 2016 sr cs-ex2 (November 07, 2016)

Assignment 2 Synchronization

Scheduler data structures need protectionWe can use a pthread mutexWe will use non-blocking synchronization later

What to do in idle state?We can use pthread condition variables to wait passively

Critical Section

T1

T2

Idle

lock()

resume(T2)

unlock()

resume(Idle)

sleep()

resume(T1)

unlock()

sr cs-ex2 (November 07, 2016) Assignment 2 10 – 10