Upload
dmitry-zagorulkin
View
82
Download
1
Embed Size (px)
Citation preview
Computational models in software engineering
Dmitry Zagorulkin ([email protected])
Concurrency in practice:
• - Distributed systems;
• - Fault tolerant systems;
• - Mobile systems(mobile phones network, vertices may connect and disconnect);
• - Parallel systems(running on one node)
Concurrency communication types:
• Shared memory – usually required the application of some form of locking. (e.g. mutexes, semaphores, monitors).
Java/C# • Message passing – concurrent components
communicate by exchanging messages. Can be done in synchronous/asynchronous way. Mostly used in functional programming languages.
Erlang/Scala(Akka)
Shared memory example
• Execution # • Synchronized • Not synchronized
1 Incr: 100000000Decr: 0
Decr: 67356100Incr: 67356100
2 Incr: 100000000Decr: 0
Decr: -88564700Incr: 1407800
3 Incr: 100000000Decr: 0
Decr: -86920200Incr: -1667800
Shared memory
• Advantages:
• easy to write such code
• deterministic behaviour
• Disadvantages:
• wrong waste of computer resources
• locking on resource
• hard to scale such code
Message passing
• Advantages:
• scalability out of the box
• possible to use in real time systems
• Disadvantages:
• hard to debug
• hard write code in such style
Models of computation
• λ (lambda) calculus by Church and Kleene (1941);
• π (pi) calculus by Milner et. al. (1992);
• Actor model (1973) by Hewitt et. al;
• Join calculus (1996) by Fournet and Conthier;
• Mobile ambients (2000) by Cardelli and Gordon.
λ (lambda) calculus
• Used as a foundation for sequential computation;
• Heart of functional programming languages;
• Turing-complete; any computable function can be expressed and evaluated using the calculus;
• In the λ calculus, all functions may only have one variable;
λ (lambda) calculus
• Function: λx.x2 ( the same f (x) = x2)• Function application: (λx.x2 2) ⇒ 22 ⇒ 4. ( the same f (2) = 4)
• Currying and High order functions:h(x,y) = x + y, h: (Z x Z) -> Zf : Z -> ( Z -> Z) and gx : Z -> ZExample f(2) = g2, where g2(y) = 2 + y.f(2)(3) = g2(3) = 2 + 3 = 5in lambda calculus this function with currying by: λx.λy.x + y. (λx.λy.x+y 2) 3) ⇒ (λy.2+y 3) ⇒ 2+3 ⇒ 5.
• Functions that operate on functions - ‘first class values’, so function may be used as the inputs, or be returned as outputs from other functions.
π (pi) calculus
• Pi calculus is a formal model for specification, analysis, and verification of systems composed of communicating concurrent processes.
• Concurrent computation is modeled in the Pi calculus as processes communicating over share channels.
• Calculus consist of processes {P,Q,…R}, channel names {a,b…c}, interaction (reading, writing) is denoted by prefix α.
• A simple interaction between two processes over a channel a can be modeled as follows in the π calculus: a(x).P | āb.Q
Actor model
• Asynchronous communication between nodes
• Every actor has it’s own identity ( used for communication)
• In response to message, an actor a may perform following:
1. send a message to a friend(acquaintance), an actor whose identity is known to the actor a
2. create a new actor a’, with a given behaviour b
3. become ready to receive a new message with new behaviour b.
Problems
• Reference cell problem (a cell contains mutable state. A simple cell can be created, updated, and queried by other concurrent computations) – illustrates nondeterminism problem;
• Mutual exclusion (refers to the need of two or more concurrent computations to coordinate and avoid executing at the same some critical region of code) – A useful abstraction for guaranteeing mutual exclusion is the notion of semaphores;
Problems
• Dining Philosophers – A typical example of complexities of concurrent computation is the famous dining philosophers scenario. Consider n philosophiers, Ph1, … , Phn -1, dining at a round table containing n chopsticks ch1, … , chn-1 where each chopstick is shared by two consecutive philosophers. – illustrates deadlock problem.
Dining philosophers algorithm
1. Pick left chopstick (if available)
2. Pick right chopstick (if available)
3. Eat
4. Release both chopsticks
5. Think
6. Go to 1
Alan Kay about OOP
• “I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages (so messaging came at the very beginning -- it took a while to see how to do messaging in a programming language efficiently enough to be useful)” – Alan Kay.