SlideShare une entreprise Scribd logo
1  sur  28
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
Processes and
Concurrency in VW
S.Ducasse 2
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
S.Ducasse 3
• Processes in Smalltalk:
• Class Process, Process States, Process Scheduling and
Priorities
• Synchronization Mechanisms in Smalltalk:
• Semaphores, Mutual Exclusion Semaphores,
• SharedQueues
• Delays
• Promises
Processes & Concurrency inVW
S.Ducasse 4
• Smalltalk supports multiple independent processes.
• Each instance of class Process represents a sequence of
actions which can be executed by the virtual machine
concurrently with other processes.
• Processes share a common address space (object memory)
• Blocks are used as the basis for creating processes
• [ Transcript cr; show: 5 factorial printString ] fork
• The new process is added to the list of scheduled
processes.This process is runnable (i.e., scheduled for
execution) and will start executing as soon as the current
process releases the control of the processor.
Processes in Smalltalk: Process class
S.Ducasse 5
• We can create a new instance of class Process which
is not scheduled by sending the #newProcess message
to a block:
| aProcess |
aProcess := [ Transcript cr; show: 5 factorial printString ]
newProcess
• The actual process is not actually runnable until it
receives the #resume message.
Process class
S.Ducasse 6
• A process may be in one of the five states:
• suspended
• waiting
• runnable
• running, or
• terminated
Process states
S.Ducasse 7
• A process can be created with any number of
arguments:
aProcess := [ :n | Transcript cr; show: n factorial
printString ] newProcessWithArguments: #(5).
• A process can be temporarily stopped using a suspend
message.A suspended process can be restarted later
using the resume message.
• A process can be stopped definitely using a message
terminate. Once a process has received the terminate
message it cannot be restarted any more.
Process class
S.Ducasse 8
• Processes are scheduled by the unique instance of
class ProcessorScheduler called Processor.
• A runnable process can be created with an specific
priority using the forkAt: message:
• [ Transcript cr; show: 5 factorial printString ]
• forkAt: Processor userBackgroundPriority.
Process Scheduling and Priorities
S.Ducasse 9
• Process scheduling is based on priorities associated to
processes.
• Processes of high priority run before lower priority.
• Priority values go between 1 and 100.
• Eight priority values have assigned names.
Priority Name Purpose
100timingPriority Used by Processes that are dependent on real time.
98 highIOPriority Used by time-critical I/O
90 lowIOPriority Used by most I/O Processes
70 userInterruptPriority Used by user Processes desiring immediate service
50 userSchedulingPriority Used by processes governing normal user
interaction
30 userBackgroundPriority Used by user background processes
10 systemBackgroundPriority Used by system background processes
1 systemRockBottonPriorityThe lowest possible priority
Process Scheduling and Priorities (VW)
S.Ducasse 10
In Squeak
• SystemRockBottomPriority := 10.
• SystemBackgroundPriority := 20.
• UserBackgroundPriority := 30.
• UserSchedulingPriority := 40.
• UserInterruptPriority := 50.
• LowIOPriority := 60.
• HighIOPriority := 70.
• TimingPriority := 80.
S.Ducasse 11
• The priority of a process can be changed by using a
#priority: message
• | process1 process2 |
• Transcript clear.
• process1 := [Transcript show:‘first’] newProcess.
• process1 priority: Processor systemBackgroundPriority.
• process2 := [Transcript show:‘second’ ] newProcess.
• process2 priority: Processor highIOPriority.
• process1 resume.
• process2 resume.
• The default process priority is userSchedulingPriority
(50)
InVW
S.Ducasse 12
- The active process can
be identified by the
expression:
- Processor activeProcess
- The processor is given
to the process having
the highest priority.
- A process will run until
it is suspended,
terminated or pre-
empted by a higher
priority process, before
giving up the processor.
- When the highest
priority is held by
multiple processes, the
active process can give
up the processor by
using the message
#yield.
Process Scheduling Algorithm
S.Ducasse 13
Process Scheduling
S.Ducasse 14
• Concurrent processes typically have references to
some shared objects. Such objects may receive
messages from these processes in an arbitrary order,
which can lead to unpredictable results.
Synchronization mechanisms serve mainly to maintain
consistency of shared objects.
Synchronization Mechanisms
S.Ducasse 15
• We can calculate the sum of the first N natural
numbers:
• | n |
• n := 100000.
• [ | i temp |
• Transcript cr; show:‘P1 running’.
• i := 1. temp := 0.
• [ i <= n ] whileTrue: [ temp := temp + i. i := i + 1 ].
• Transcript cr; show:‘P1 sum = ‘; show: temp
printString ]
forkAt: 60.
P1 running
P1 sum is = 5000050000
N naturals
S.Ducasse 16
• What happens if at the same time another process
modifies the value of n?
• | n d |
• n := 100000.
• d := Delay forMilliseconds: 400.
• [ | i temp |
• Transcript cr; show:‘P1 running’.
• i := 1. temp := 0.
• [ i <= n ] whileTrue: [ temp := temp + i.
(i = 5000) ifTrue: [ d wait ].
i := i + 1 ].
• Transcript cr; show:‘P1 sum is = ‘; show: temp printString ] forkAt:
60.
• [ Transcript cr; show:‘P2 running’. n := 10 ] forkAt: 50.
• P1 running
Accessing a SharedVariable
S.Ducasse 17
• A semaphore is an object used to synchronize
multiple processes.A process waits for an event to
occur by sending the message #wait to the
semaphore.Another process then signals that the
event has occurred by sending the message #signal to
the semaphore.
• | sem |
• Transcript clear.
• sem := Semaphore new.
• [ Transcript show:‘The’] fork.
• [ Transcript show:‘quick’. sem wait.
• Transcript show:‘fox’. sem signal ] fork.
• [ Transcript show:‘brown’. sem signal.
• sem wait.Transcript show:‘jumps over the lazy dog’; cr ] fork
Synchronization using Semaphores
S.Ducasse 18
• If a semaphore receives a wait message for which no
corresponding signal has been sent, the process
sending the wait message is suspended.
• Each semaphore maintains a linked list of suspended
processes.
• If a semaphore receives a wait from two or more
processes, it resumes only one process for each signal
it receives
• A semaphore pays no attention to the priority of a
process. Processes are queued in the same order in
which they “waited” on the semaphore.
Semaphores
S.Ducasse 19
Semaphores (ii)
S.Ducasse 20
• A semaphore for mutual exclusion must start with one
extra #signal, otherwise the critical section will never
be entered.A special instance creation method is
provided:
• Semaphore forMutualExclusion.
• Semaphores are frequently used to provide mutual
exclusion for a “critical section”.This is supported by
the instance method critical:.The block argument is
only executed when no other critical blocks sharing
the same semaphore are evaluating.
Semaphores for Mutual Exclusion
S.Ducasse 21
| n d sem |
n := 100000.
d := Delay forMilliseconds: 400.
[ | i temp |
Transcript cr; show:‘P1 running’.
i := 1. temp := 0.
sem critical: [ [ i <= n ] whileTrue: [ temp := temp + i.
(i = 5000) ifTrue: [ d
wait ].i := i + 1 ]. ].
Transcript cr; show:‘P1 sum is = ‘; show: temp printString ]
forkAt: 60.
[Transcript cr; show:‘P2 running’. sem critical: [ n := 10 ]]
forkAt: 50.
Example
S.Ducasse 22
• A SharedQueue enables synchronized communication
between processes. It works like a normal queue, with the
main difference being that aSharedQueue protects itself
against possible concurrent accesses (multiple writes
and/or multiple reads).
• Processes add objects to the shared queue by using the
message #nextPut: (1) and read objects from the shared
queue by sending the message #next (3).
• | aSharedQueue d |
• d := Delay forMilliseconds: 400.
• aSharedQueue := SharedQueue new.
• [ 1 to: 5 do:[:i | aSharedQueue nextPut: i ] ] fork.
• [ 6 to: 10 do:[:i | aSharedQueue nextPut: i. d wait ] ] forkAt: 60.
• [ 1 to: 5 do:[:i |Transcript cr; show:aSharedQueue next printString] ]
forkAt: 60.
Synchronization with a SharedQueue
S.Ducasse 23
• If no object is available in the shared queue when the
messsage next is received, the process is suspended.
• We can query whether the shared queue is empty or
not with the message isEmpty
Synchronization with a SharedQueue
S.Ducasse 24
• Instances of class Delay are used to delay the execution of
a process.
• An instance of class Delay responds to the message wait by
suspending the active process for a certain amount of time.
• The time at which to resume is specified when the delay
instance is created.Time can be specified relative to the
current time with the messages forMilliseconds: and
forSeconds:.
• | minuteWait |
• minuteWait := Delay forSeconds: 60.
• minuteWait wait.
Delays
S.Ducasse 25
untilMilliseconds:...
• The resumption time can also be specified at an absolute
time with respect to the system’s millisecond clock with
the message untilMilliseconds:. Delays created in this way
can be sent the message wait at most once.
S.Ducasse 26
• Class Promise provides a means to evaluate a block
within a concurrent process.
• An instance of Promise can be created by sending the
message promise to a block:
• [ 5 factorial ] promise
• The message promiseAt: can be used to specify the
priority of the process created.
Promises
S.Ducasse 27
• The result of the block can be accessed by sending
the message value to the promise:
• | promise |
• promise := [ 5 factorial ] promise.
• Transcript cr; show: promise value
printString.
• If the block has not completed evaluation, then the
process that attempts to read the value of a promise
will wait until the process evaluating the block has
completed.
• A promise may be interrogated to discover if the
process has completed by sending the message
hasValue
Promises (ii)
S.Ducasse 28

Contenu connexe

Tendances

Distribute Key Value Store
Distribute Key Value StoreDistribute Key Value Store
Distribute Key Value Store
Santal Li
 
Tcp congestion control
Tcp congestion controlTcp congestion control
Tcp congestion control
Abdo sayed
 
Supporting Time-Sensitive Applications on a Commodity OS
Supporting Time-Sensitive Applications on a Commodity OSSupporting Time-Sensitive Applications on a Commodity OS
Supporting Time-Sensitive Applications on a Commodity OS
NamHyuk Ahn
 
Teoria efectului defectului hardware: GoogleFS
Teoria efectului defectului hardware: GoogleFSTeoria efectului defectului hardware: GoogleFS
Teoria efectului defectului hardware: GoogleFS
Asociatia ProLinux
 

Tendances (20)

NATS: Simple, Secure and Scalable Messaging For the Cloud Native Era
NATS: Simple, Secure and Scalable Messaging For the Cloud Native EraNATS: Simple, Secure and Scalable Messaging For the Cloud Native Era
NATS: Simple, Secure and Scalable Messaging For the Cloud Native Era
 
Distribute Key Value Store
Distribute Key Value StoreDistribute Key Value Store
Distribute Key Value Store
 
Docker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platformsDocker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platforms
 
A Deep Dive into Apache Cassandra for .NET Developers
A Deep Dive into Apache Cassandra for .NET DevelopersA Deep Dive into Apache Cassandra for .NET Developers
A Deep Dive into Apache Cassandra for .NET Developers
 
Tcp congestion control
Tcp congestion controlTcp congestion control
Tcp congestion control
 
Lect9 (1)
Lect9 (1)Lect9 (1)
Lect9 (1)
 
TCP Westwood
TCP WestwoodTCP Westwood
TCP Westwood
 
Message passing in Distributed Computing Systems
Message passing in Distributed Computing SystemsMessage passing in Distributed Computing Systems
Message passing in Distributed Computing Systems
 
Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)
 
TCP Congestion Control By Owais Jara
TCP Congestion Control By Owais JaraTCP Congestion Control By Owais Jara
TCP Congestion Control By Owais Jara
 
Distributed Transactions(flat and nested) and Atomic Commit Protocols
Distributed Transactions(flat and nested) and Atomic Commit ProtocolsDistributed Transactions(flat and nested) and Atomic Commit Protocols
Distributed Transactions(flat and nested) and Atomic Commit Protocols
 
Supporting Time-Sensitive Applications on a Commodity OS
Supporting Time-Sensitive Applications on a Commodity OSSupporting Time-Sensitive Applications on a Commodity OS
Supporting Time-Sensitive Applications on a Commodity OS
 
Clock Synchronization in Distributed Systems
Clock Synchronization in Distributed SystemsClock Synchronization in Distributed Systems
Clock Synchronization in Distributed Systems
 
Kubernetes networking - basics
Kubernetes networking - basicsKubernetes networking - basics
Kubernetes networking - basics
 
Chap3 clientsrvr
Chap3 clientsrvrChap3 clientsrvr
Chap3 clientsrvr
 
NTP Server - How it works?
NTP Server - How it works?NTP Server - How it works?
NTP Server - How it works?
 
Docker swarm workshop
Docker swarm workshopDocker swarm workshop
Docker swarm workshop
 
File replication
File replicationFile replication
File replication
 
Teoria efectului defectului hardware: GoogleFS
Teoria efectului defectului hardware: GoogleFSTeoria efectului defectului hardware: GoogleFS
Teoria efectului defectului hardware: GoogleFS
 
An Overview of Distributed Debugging
An Overview of Distributed DebuggingAn Overview of Distributed Debugging
An Overview of Distributed Debugging
 

En vedette

En vedette (20)

Concurrency in Smalltalk -- Beyond Threads
Concurrency in Smalltalk -- Beyond ThreadsConcurrency in Smalltalk -- Beyond Threads
Concurrency in Smalltalk -- Beyond Threads
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop 430-design patternsintro
Stoop 430-design patternsintroStoop 430-design patternsintro
Stoop 430-design patternsintro
 
Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop 434-composite
Stoop 434-compositeStoop 434-composite
Stoop 434-composite
 
Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 
Stoop 305-reflective programming5
Stoop 305-reflective programming5Stoop 305-reflective programming5
Stoop 305-reflective programming5
 
15 - Streams
15 - Streams15 - Streams
15 - Streams
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
Stoop 433-chain
Stoop 433-chainStoop 433-chain
Stoop 433-chain
 
Stoop 400-metaclass only
Stoop 400-metaclass onlyStoop 400-metaclass only
Stoop 400-metaclass only
 

Similaire à 15 - Concurrency in VisualWorks

Tupperware: Containerized Deployment at FB
Tupperware: Containerized Deployment at FBTupperware: Containerized Deployment at FB
Tupperware: Containerized Deployment at FB
Docker, Inc.
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
The World of Smalltalk
 

Similaire à 15 - Concurrency in VisualWorks (20)

SYNCHRONIZATION IN MULTIPROCESSING
SYNCHRONIZATION IN MULTIPROCESSINGSYNCHRONIZATION IN MULTIPROCESSING
SYNCHRONIZATION IN MULTIPROCESSING
 
Tupperware: Containerized Deployment at FB
Tupperware: Containerized Deployment at FBTupperware: Containerized Deployment at FB
Tupperware: Containerized Deployment at FB
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
 
Distributed Performance testing by funkload
Distributed Performance testing by funkloadDistributed Performance testing by funkload
Distributed Performance testing by funkload
 
MULTITHREADING CONCEPT
MULTITHREADING CONCEPTMULTITHREADING CONCEPT
MULTITHREADING CONCEPT
 
slides8 SharedMemory.ppt
slides8 SharedMemory.pptslides8 SharedMemory.ppt
slides8 SharedMemory.ppt
 
Reactive Design Patterns — J on the Beach
Reactive Design Patterns — J on the BeachReactive Design Patterns — J on the Beach
Reactive Design Patterns — J on the Beach
 
Reactive Design Patterns by Dr.Roland Kuhn
Reactive Design Patterns by Dr.Roland KuhnReactive Design Patterns by Dr.Roland Kuhn
Reactive Design Patterns by Dr.Roland Kuhn
 
Reactive Design Patterns: a talk by Typesafe's Dr. Roland Kuhn
Reactive Design Patterns: a talk by Typesafe's Dr. Roland KuhnReactive Design Patterns: a talk by Typesafe's Dr. Roland Kuhn
Reactive Design Patterns: a talk by Typesafe's Dr. Roland Kuhn
 
Give your little scripts big wings: Using cron in the cloud with Amazon Simp...
Give your little scripts big wings:  Using cron in the cloud with Amazon Simp...Give your little scripts big wings:  Using cron in the cloud with Amazon Simp...
Give your little scripts big wings: Using cron in the cloud with Amazon Simp...
 
Insider operating system
Insider   operating systemInsider   operating system
Insider operating system
 
Fluentd v1.0 in a nutshell
Fluentd v1.0 in a nutshellFluentd v1.0 in a nutshell
Fluentd v1.0 in a nutshell
 
Troubleshooting and Best Practices with WSO2 Enterprise Integrator
Troubleshooting and Best Practices with WSO2 Enterprise IntegratorTroubleshooting and Best Practices with WSO2 Enterprise Integrator
Troubleshooting and Best Practices with WSO2 Enterprise Integrator
 
Fault tolerance
Fault toleranceFault tolerance
Fault tolerance
 
Lec18 pipeline
Lec18 pipelineLec18 pipeline
Lec18 pipeline
 
OS_module2. .pptx
OS_module2.                          .pptxOS_module2.                          .pptx
OS_module2. .pptx
 
Ch5 process synchronization
Ch5   process synchronizationCh5   process synchronization
Ch5 process synchronization
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
Chicago DevOps Meetup Nov2019
Chicago DevOps Meetup Nov2019Chicago DevOps Meetup Nov2019
Chicago DevOps Meetup Nov2019
 
Troubleshooting and Best Practices with WSO2 Enterprise Integrator
Troubleshooting and Best Practices with WSO2 Enterprise IntegratorTroubleshooting and Best Practices with WSO2 Enterprise Integrator
Troubleshooting and Best Practices with WSO2 Enterprise Integrator
 

Plus de The World of Smalltalk (20)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
10 reflection
10 reflection10 reflection
10 reflection
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
02 basics
02 basics02 basics
02 basics
 
01 intro
01 intro01 intro
01 intro
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 

Dernier

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 

15 - Concurrency in VisualWorks

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.listic.univ-savoie.fr/~ducasse/ Processes and Concurrency in VW
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/2.0/
  • 3. S.Ducasse 3 • Processes in Smalltalk: • Class Process, Process States, Process Scheduling and Priorities • Synchronization Mechanisms in Smalltalk: • Semaphores, Mutual Exclusion Semaphores, • SharedQueues • Delays • Promises Processes & Concurrency inVW
  • 4. S.Ducasse 4 • Smalltalk supports multiple independent processes. • Each instance of class Process represents a sequence of actions which can be executed by the virtual machine concurrently with other processes. • Processes share a common address space (object memory) • Blocks are used as the basis for creating processes • [ Transcript cr; show: 5 factorial printString ] fork • The new process is added to the list of scheduled processes.This process is runnable (i.e., scheduled for execution) and will start executing as soon as the current process releases the control of the processor. Processes in Smalltalk: Process class
  • 5. S.Ducasse 5 • We can create a new instance of class Process which is not scheduled by sending the #newProcess message to a block: | aProcess | aProcess := [ Transcript cr; show: 5 factorial printString ] newProcess • The actual process is not actually runnable until it receives the #resume message. Process class
  • 6. S.Ducasse 6 • A process may be in one of the five states: • suspended • waiting • runnable • running, or • terminated Process states
  • 7. S.Ducasse 7 • A process can be created with any number of arguments: aProcess := [ :n | Transcript cr; show: n factorial printString ] newProcessWithArguments: #(5). • A process can be temporarily stopped using a suspend message.A suspended process can be restarted later using the resume message. • A process can be stopped definitely using a message terminate. Once a process has received the terminate message it cannot be restarted any more. Process class
  • 8. S.Ducasse 8 • Processes are scheduled by the unique instance of class ProcessorScheduler called Processor. • A runnable process can be created with an specific priority using the forkAt: message: • [ Transcript cr; show: 5 factorial printString ] • forkAt: Processor userBackgroundPriority. Process Scheduling and Priorities
  • 9. S.Ducasse 9 • Process scheduling is based on priorities associated to processes. • Processes of high priority run before lower priority. • Priority values go between 1 and 100. • Eight priority values have assigned names. Priority Name Purpose 100timingPriority Used by Processes that are dependent on real time. 98 highIOPriority Used by time-critical I/O 90 lowIOPriority Used by most I/O Processes 70 userInterruptPriority Used by user Processes desiring immediate service 50 userSchedulingPriority Used by processes governing normal user interaction 30 userBackgroundPriority Used by user background processes 10 systemBackgroundPriority Used by system background processes 1 systemRockBottonPriorityThe lowest possible priority Process Scheduling and Priorities (VW)
  • 10. S.Ducasse 10 In Squeak • SystemRockBottomPriority := 10. • SystemBackgroundPriority := 20. • UserBackgroundPriority := 30. • UserSchedulingPriority := 40. • UserInterruptPriority := 50. • LowIOPriority := 60. • HighIOPriority := 70. • TimingPriority := 80.
  • 11. S.Ducasse 11 • The priority of a process can be changed by using a #priority: message • | process1 process2 | • Transcript clear. • process1 := [Transcript show:‘first’] newProcess. • process1 priority: Processor systemBackgroundPriority. • process2 := [Transcript show:‘second’ ] newProcess. • process2 priority: Processor highIOPriority. • process1 resume. • process2 resume. • The default process priority is userSchedulingPriority (50) InVW
  • 12. S.Ducasse 12 - The active process can be identified by the expression: - Processor activeProcess - The processor is given to the process having the highest priority. - A process will run until it is suspended, terminated or pre- empted by a higher priority process, before giving up the processor. - When the highest priority is held by multiple processes, the active process can give up the processor by using the message #yield. Process Scheduling Algorithm
  • 14. S.Ducasse 14 • Concurrent processes typically have references to some shared objects. Such objects may receive messages from these processes in an arbitrary order, which can lead to unpredictable results. Synchronization mechanisms serve mainly to maintain consistency of shared objects. Synchronization Mechanisms
  • 15. S.Ducasse 15 • We can calculate the sum of the first N natural numbers: • | n | • n := 100000. • [ | i temp | • Transcript cr; show:‘P1 running’. • i := 1. temp := 0. • [ i <= n ] whileTrue: [ temp := temp + i. i := i + 1 ]. • Transcript cr; show:‘P1 sum = ‘; show: temp printString ] forkAt: 60. P1 running P1 sum is = 5000050000 N naturals
  • 16. S.Ducasse 16 • What happens if at the same time another process modifies the value of n? • | n d | • n := 100000. • d := Delay forMilliseconds: 400. • [ | i temp | • Transcript cr; show:‘P1 running’. • i := 1. temp := 0. • [ i <= n ] whileTrue: [ temp := temp + i. (i = 5000) ifTrue: [ d wait ]. i := i + 1 ]. • Transcript cr; show:‘P1 sum is = ‘; show: temp printString ] forkAt: 60. • [ Transcript cr; show:‘P2 running’. n := 10 ] forkAt: 50. • P1 running Accessing a SharedVariable
  • 17. S.Ducasse 17 • A semaphore is an object used to synchronize multiple processes.A process waits for an event to occur by sending the message #wait to the semaphore.Another process then signals that the event has occurred by sending the message #signal to the semaphore. • | sem | • Transcript clear. • sem := Semaphore new. • [ Transcript show:‘The’] fork. • [ Transcript show:‘quick’. sem wait. • Transcript show:‘fox’. sem signal ] fork. • [ Transcript show:‘brown’. sem signal. • sem wait.Transcript show:‘jumps over the lazy dog’; cr ] fork Synchronization using Semaphores
  • 18. S.Ducasse 18 • If a semaphore receives a wait message for which no corresponding signal has been sent, the process sending the wait message is suspended. • Each semaphore maintains a linked list of suspended processes. • If a semaphore receives a wait from two or more processes, it resumes only one process for each signal it receives • A semaphore pays no attention to the priority of a process. Processes are queued in the same order in which they “waited” on the semaphore. Semaphores
  • 20. S.Ducasse 20 • A semaphore for mutual exclusion must start with one extra #signal, otherwise the critical section will never be entered.A special instance creation method is provided: • Semaphore forMutualExclusion. • Semaphores are frequently used to provide mutual exclusion for a “critical section”.This is supported by the instance method critical:.The block argument is only executed when no other critical blocks sharing the same semaphore are evaluating. Semaphores for Mutual Exclusion
  • 21. S.Ducasse 21 | n d sem | n := 100000. d := Delay forMilliseconds: 400. [ | i temp | Transcript cr; show:‘P1 running’. i := 1. temp := 0. sem critical: [ [ i <= n ] whileTrue: [ temp := temp + i. (i = 5000) ifTrue: [ d wait ].i := i + 1 ]. ]. Transcript cr; show:‘P1 sum is = ‘; show: temp printString ] forkAt: 60. [Transcript cr; show:‘P2 running’. sem critical: [ n := 10 ]] forkAt: 50. Example
  • 22. S.Ducasse 22 • A SharedQueue enables synchronized communication between processes. It works like a normal queue, with the main difference being that aSharedQueue protects itself against possible concurrent accesses (multiple writes and/or multiple reads). • Processes add objects to the shared queue by using the message #nextPut: (1) and read objects from the shared queue by sending the message #next (3). • | aSharedQueue d | • d := Delay forMilliseconds: 400. • aSharedQueue := SharedQueue new. • [ 1 to: 5 do:[:i | aSharedQueue nextPut: i ] ] fork. • [ 6 to: 10 do:[:i | aSharedQueue nextPut: i. d wait ] ] forkAt: 60. • [ 1 to: 5 do:[:i |Transcript cr; show:aSharedQueue next printString] ] forkAt: 60. Synchronization with a SharedQueue
  • 23. S.Ducasse 23 • If no object is available in the shared queue when the messsage next is received, the process is suspended. • We can query whether the shared queue is empty or not with the message isEmpty Synchronization with a SharedQueue
  • 24. S.Ducasse 24 • Instances of class Delay are used to delay the execution of a process. • An instance of class Delay responds to the message wait by suspending the active process for a certain amount of time. • The time at which to resume is specified when the delay instance is created.Time can be specified relative to the current time with the messages forMilliseconds: and forSeconds:. • | minuteWait | • minuteWait := Delay forSeconds: 60. • minuteWait wait. Delays
  • 25. S.Ducasse 25 untilMilliseconds:... • The resumption time can also be specified at an absolute time with respect to the system’s millisecond clock with the message untilMilliseconds:. Delays created in this way can be sent the message wait at most once.
  • 26. S.Ducasse 26 • Class Promise provides a means to evaluate a block within a concurrent process. • An instance of Promise can be created by sending the message promise to a block: • [ 5 factorial ] promise • The message promiseAt: can be used to specify the priority of the process created. Promises
  • 27. S.Ducasse 27 • The result of the block can be accessed by sending the message value to the promise: • | promise | • promise := [ 5 factorial ] promise. • Transcript cr; show: promise value printString. • If the block has not completed evaluation, then the process that attempts to read the value of a promise will wait until the process evaluating the block has completed. • A promise may be interrogated to discover if the process has completed by sending the message hasValue Promises (ii)