Lecture 10-Concurrent Process Model
Lecture 10-Concurrent Process Model
- 3-
Introduction
- 4-
An example of trying to be precise in English
- 5-
Models and languages
- 6-
Models vs. languages
- 7-
Text versus Graphics
X = 1; X=1
Y = X + 1;
Y=X+1
- 8-
Introductory example: An elevator controller
up1 up/down
elevator to this up2 buttons
dn2 on each
requested floor up3
dn3
floor
- 9-
Elevator controller using a sequential program
model
System interface
Sequential program model
Partial English description
Inputs: int floor; bit b1..bN; up1..upN-1; dn2..dnN;
Unit up
Outputs: bit up, down, open;
Global variables: int req; “Move the elevator either up or down Control down
void UnitControl() void RequestResolver() to reach the requested floor. Once at open
{ { the requested floor, open the door for
up = down = 0; open = 1; while (1) floor
while (1) { ...
at least 10 seconds, and keep it open
until the requested floor changes. req
while (req == floor); req = ...
open = 0; ... Ensure the door is never open while Request
if (req > floor) { up = 1;} } Resolver
else {down = 1;}
moving. Don’t change directions b1 buttons
void main()
while (req != floor); unless there are no higher requests b2
inside
{ ... elevator
up = down = 0; Call concurrently: when moving up or no lower requests bN
open = 1; UnitControl() and when moving down…”
delay(10); RequestResolver() up1
} up/down
} up2 buttons
}
dn2 on each
up3 floor
dn3
You might have come up with something having ...
even more if statements. dnN
- 10 -
Finite-state machine (FSM) model
- 11 -
Finite-state machine (FSM) model
- 12 -
Formal definition
- 13 -
Finite-state machine with datapath model (FSMD)
FSMD extends FSM: complex data types and variables for storing data
FSMs use only Boolean data types and operations, no variables
FSMD: 7-tuple <S, I , O, V, F, H, s0>
S is a set of states {s0, s1, …, sl} We described UnitControl as an FSMD
I is a set of inputs {i0, i1, …, im} req > floor
O is a set of outputs {o0, o1, …, on}
u,d,o, t = 1,0,0,0 GoingUp !(req > floor)
V is a set of variables {v0, v1, …, vn} req > floor timer < 10
F is a next-state function (S x I x V → S) u,d,o,t = 0,0,1,0
Idle
!(timer < 10)
DoorOpen
H is an action function (S → O + V) req == floor
req < floor
u,d,o,t = 0,0,1,1
I,O,V may represent complex data types (i.e., integers, floating point, etc.)
F,H may include arithmetic operations
H is an action function, not just an output function
Describes variable updates as well as outputs
Complete system state now consists of current state, si, and values of all variables
- 14 -
Describing a system as a state machine
1. List all possible states 2. Declare all variables (none in this example)
3. For each state, list possible transitions, with conditions, to other states
4. For each state and/or transition, req > floor
list associated actions
5. For each state, ensure exclusive u,d,o, t = 1,0,0,0 GoingUp !(req > floor)
and complete exiting transition
conditions req > floor timer < 10
– Otherwise nondeterministic
state machine u,d,o,t = 0,1,0,0
!(req<floor)
- 15 -
State machine vs. sequential program model
- 16 -
Try Capturing Other Behaviors with an FSM
- 17 -
Capturing state machines in
sequential programming language
Despite benefits of state machine model, most popular development tools use
sequential programming language
C, C++, Java, Ada, VHDL, Verilog, etc.
Development tools are complex and expensive, therefore not easy to adapt or replace
• Must protect investment
Two approaches to capturing state machine model with sequential programming
language
Front-end tool approach
• Additional tool installed to support state machine language
• Graphical and/or textual state machine languages
• May support graphical simulation
• Automatically generate code in sequential programming language that is input to main development tool
• Drawback: must support additional tool (licensing costs, upgrades, training, etc.)
Language subset approach
• Most common approach...
- 18 -
Language subset approach
- 19 -
General template
#define S0 0
#define S1 1
...
#define SN N
void StateMachine() {
int state = S0; // or whatever is the initial state.
while (1) {
switch (state) {
S0:
// Insert S0’s actions here & Insert transitions Ti leaving S0:
if( T0’s condition is true ) {state = T0’s next state; /*actions*/ }
if( T1’s condition is true ) {state = T1’s next state; /*actions*/ }
...
if( Tm’s condition is true ) {state = Tm’s next state; /*actions*/ }
break;
S1:
// Insert S1’s actions here
// Insert transitions Ti leaving S1
break;
...
SN:
// Insert SN’s actions here
// Insert transitions Ti leaving SN
break;
}
}
}
- 20 -
HCFSM and the Statecharts language
B x y u v
• Return to saved substate of A when returning from B
C2 D2
instead of initial state
- 21 -
UnitControl with FireMode
req>floor UnitControl
- 22 -
Program-state machine model (PSM): HCFSM plus
sequential program model
- 23 -
Role of appropriate model and language
- 24 -
Concurrent process model
- 25 -
Dataflow model
When all of node’s input edges have at least one token, node t1 t2
may fire *
Z
Nodes with more complex
transformations
- 26 -
Synchronous dataflow
each firing mA mB mC mD
Can statically schedule nodes, so can easily use sequential
program model modulate convolve
• Don’t need real-time operating system and its overhead mt1 t1 t2 ct2
How would you map this model to a sequential programming tt1 tt2
appearance” schedules Z
Only one statement needed to call each node’s associated
procedure Synchronous dataflow
• Allows procedure inlining without code explosion, thus reducing
overhead even more
- 27 -
Concurrent processes and
real-time systems
Concurrent processes
- 29 -
Process
- 30 -
Communication among processes
- 31 -
Shared Memory
- 32 -
Message Passing
Message passing
Data explicitly sent from one process to void processA() {
while( 1 ) {
another produce(&data)
send(B, &data);
• Sending process performs special operation, /* region 1 */
receive(B, &data);
send }
consume(&data);
blocking }
- 33 -
Back to Shared Memory: Mutual Exclusion
- 34 -
Correct Shared Memory Solution to the Consumer-
Producer Problem
The primitive mutex is used to ensure critical sections 01: data_type buffer[N];
02: int count = 0;
are executed in mutual exclusion of each other 03: mutex count_mutex;
Following the same execution sequence as before: 04:
05:
void processA() {
int i;
A/B execute lock operation on count_mutex 06: while( 1 ) {
Either A or B will acquire lock 07: produce(&data);
• Say B acquires it 08: while( count == N );/*loop*/
09: buffer[i] = data;
• A will be put in blocked state 10: i = (i + 1) % N;
B loads count (count = 3) from memory into register R2 (R2 11: count_mutex.lock();
= 3) 12:
13:
count = count + 1;
count_mutex.unlock();
B decrements R2 (R2 = 2) 14: }
B stores R2 back to count in memory (count = 2) 15: }
B executes unlock operation 16:
17:
void processB() {
int i;
• A is placed in runnable state again 18: while( 1 ) {
A loads count (count = 2) from memory into register R1 (R1 19: while( count == 0 );/*loop*/
20: data = buffer[i];
= 2) 21: i = (i + 1) % N;
A increments R1 (R1 = 3) 22: count_mutex.lock();
A stores R1 back to count in memory (count = 3) 23: count = count - 1;
24: count_mutex.unlock();
Count now has correct value of 3 25: consume(&data);
26: }
27: }
28: void main() {
29: create_process(processA);
30: create_process(processB);
31: }
- 35 -
Process Communication
controller Unit up
up1 up/down
up2 buttons
dn2 on each
up3 floor
dn3
...
dnN
- 36 -
A Common Problem in Concurrent Programming:
Deadlock
- 37 -
Synchronization among processes
- 38 -
Condition variables
- 39 -
Condition variable example:
consumer-producer
- 40 -
Monitors
- 41 -
Monitor example: consumer-producer
- 42 -
Implementation
- 43 -
Concurrent process model:
implementation
Communication Bus
True multitasking (parallel processing) Process1
Processor B
General-purpose processors Process2
• Use programming language like C and compile to (a) Process3 Processor C
instructions of processor Process4
Processor D
• Expensive and in most cases not necessary
Custom single-purpose processors
• More common Process1
(b) One general-purpose processor running all Process2
General Purpose
processes (b) Process3 Processor
Process4
Most processes don’t use 100% of processor time
Can share processor time and still achieve necessary
execution rates
Processor A
Communication Bus
(c) Combination of (a) and (b)
Process1
Multiple processes run on one general-purpose Process2 General
processor while one or more processes run on own (c) Process3 Purpose
single_purpose processor Process4 Processor
- 44 -
Implementation:
multiple processes sharing single processor
- 45 -
Processes vs. threads
- 46 -
Implementation:
suspending, resuming, and joining
- 47 -
Implementation: process scheduling
- 48 -
Scheduling: priority
- 49 -
Priority assignment
Period of process
Repeating time interval the process must complete one execution within
• E.g., period = 100 ms
• Process must execute once every 100 ms Rate monotonic
Usually determined by the description of the system
• E.g., refresh rate of display is 27 times/sec Process Period Priority
• Period = 37 ms
A 25 ms 5
Execution deadline B 50 ms 3
Amount of time process must be completed by after it has started C 12 ms 6
• E.g., execution time = 5 ms, deadline = 20 ms, period = 100 ms D 100 ms 1
• Process must complete execution within 20 ms after it has begun regardless of its period E 40 ms 4
• F 75 ms 2
Process begins at start of period, runs for 4 ms then is preempted
• Process suspended for 14 ms, then runs for the remaining 1 ms
• Completed within 4 + 14 + 1 = 19 ms which meets deadline of 20 ms Deadline monotonic
• Without deadline process could be suspended for much longer
Rate monotonic scheduling Process Deadline Priority
Processes with shorter periods have higher priority G 17 ms 5
Typically used when execution deadline = period H 50 ms 2
I
Deadline monotonic scheduling J
32 ms
10 ms
3
6
Processes with shorter deadlines have higher priority K 140 ms 1
Typically used when execution deadline < period L 32 ms 4
- 50 -
Real-time systems
- 51 -
Real-time operating systems (RTOS)
Provide mechanisms, primitives, and guidelines for building real-time embedded systems
Windows CE
Built specifically for embedded systems and appliance market
Scalable real-time 32-bit platform
Supports Windows API
Perfect for systems designed to interface with Internet
Preemptive priority scheduling with 256 priority levels per process
Kernel is 400 Kbytes
QNX
Real-time microkernel surrounded by optional processes (resource managers) that provide POSIX
and UNIX compatibility
• Microkernels typically support only the most basic services
• Optional resource managers allow scalability from small ROM-based systems to huge multiprocessor systems
connected by various networking and communication technologies
Preemptive process scheduling using FIFO, round-robin, adaptive, or priority-driven scheduling
32 priority levels per process
Microkernel < 10 Kbytes and complies with POSIX real-time standard
- 52 -
Summary
- 53 -
Control Systems
Control System
- 55 -
Tracking
- 56 -
Open-Loop Control Systems
Plant
Physical system to be controlled
• Car, plane, disk, heater,…
Actuator
Device to control the plant
• Throttle, wing flap, disk motor,…
Controller
Designed product to control the plant
- 57 -
Open-Loop Control Systems
Output
The aspect of the physical system we are interested in
• Speed, disk location, temperature
Reference
The value we want to see at output
• Desired speed, desired location, desired temperature
Disturbance
Uncontrollable input to the plant imposed by environment
• Wind, bumping the disk drive, door opening
- 58 -
Other Characteristics of open loop
Feed-forward control
Delay in actual change of the output
Controller doesn’t know how well thing goes
Simple
Best use for predictable systems
- 59 -
Close Loop Control Systems
Sensor
Measure the plant output
Error detector
Detect Error
Feedback control systems
Minimize tracking error
- 60 -
Designing Open Loop Control System
- 61 -
Model of the Plant
- 62 -
Designing the Controller
- 63 -
Analyzing the Controller
- 64 -
Considering the Disturbance
- 65 -
Determining Performance
Vt+1=0.7*vt+0.5P*r0-w0
v1=0.7*v0+0.5P*r0-w0
v2=0.7*(0.7*v0+0.5P*r0-w0) +0.5P*r0-w0 =0.7*0.7*v0+(0.7+1.0)*0.5P*r0-
(0.7+1.0)w0
vt=0.7t*v0+(0.7t-1+0.7t-2+…+0.7+1.0)(0.5P*r0-w0)
Coefficient of vt determines rate of decay of v0
>1 or <-1, vt will grow without bound
<0, vt will oscillate
- 66 -
Designing Close Loop Control System
- 67 -
Stability
ut = P * (rt-vt)
vt+1 = 0.7vt+0.5ut-wt = 0.7vt+0.5P*(rt-vt)-w
=(0.7-0.5P)*vt+0.5P*rt-wt
vt=(0.7-0.5P)t*v0+((0.7-0.5P)t-1+(0.7-0.5P)t-2+…+0.7-0.5P+1.0)(0.5P*r0-
w 0)
- 68 -
Reducing effect of v0
ut = P * (rt-vt)
vt+1 = 0.7vt+0.5ut-wt = 0.7vt+0.5P*(rt-vt)-w
=(0.7-0.5P)*vt+0.5P*rt-wt
vt=(0.7-0.5P)t*v0+((0.7-0.5P)t-1+(0.7-0.5P)t-2+…+0.7-0.5P+1.0)(0.5P*r0-
w 0)
- 69 -
Avoid Oscillation
ut = P * (rt-vt)
vt+1 = 0.7vt+0.5ut-wt = 0.7vt+0.5P*(rt-vt)-w
=(0.7-0.5P)*vt+0.5P*rt-wt
vt=(0.7-0.5P)t*v0+((0.7-0.5P)t-1+(0.7-0.5P)t-2+…+0.7-0.5P+1.0)(0.5P*r0-
w 0)
To avoid oscillation
0.7-0.5P >=0
P<=1.4
- 70 -
Perfect Tracking
ut = P * (rt-vt)
vt+1 = 0.7vt+0.5ut-wt = 0.7vt+0.5P*(rt-vt)-w
=(0.7-0.5P)*vt+0.5P*rt-wt
vss=(0.7-0.5P)*vss+0.5P*r0-w0
(1-0.7+0.5P)vss=0.5P*r0-w0
vss=(0.5P/(0.3+0.5P)) * r0 - (1.0/(0.3+0.5P)) * wo
To make vss as close to r0 as possible
P should be as large as possible
- 71 -
Close-Loop Design
ut = P * (rt-vt)
Finally, setting P=3.3
Stable, track well, some oscillation
ut = 3.3 * (rt-vt)
- 72 -
Analyze the controller
- 73 -
Analyze the controller
- 74 -
Analyze the controller
- 75 -
Analyzing the Controller
- 76 -
Minimize the effect of disturbance
vt+1 = 0.7vt+0.5*3.3*(rt-
vt)-w
w=-5 or +5
39.74
Close to 42.31
Better than
• 33
• 66
Cost
SS error
oscillation
- 77 -
General Control System
Objective
Causing output to track a reference even in the presence of
• Measurement noise
• Model error
• Disturbances
Metrics
Stability
• Output remains bounded
Performance
• How well an output tracks the reference
Disturbance rejection
Robustness
• Ability to tolerate modeling error of the plant
- 78 -
Performance (generally speaking)
Rise time
Time it takes form
10% to 90%
Peak time
Overshoot
Percentage by which
Peak exceed final
value
Settling time
Time it takes to reach
1% of final value
- 79 -
Plant modeling is difficult
- 80 -
Controller Design: P
Proportional controller
A controller that multiplies the tracking error by a constant
• ut = P * (rt-vt)
Close loop model with a linear plant
• E.g. vt+1 = (0.7-0.5P)*vt+0.5P*rt-wt
P affects
Transient response
• Stability, oscillation
Steady state tacking
• As large as possible
Disturbance rejection
• As large as possible
- 81 -
Controller Design: PD
- 82 -
PD Controller
- 83 -
PD Control Example
- 84 -
PI Control
- 85 -
PID Controller
- 86 -
Software Coding
- 87 -
Software Coding (continue)
- 88 -
Computation
ut=P*et+I*(e0+e1+…+et)+D*(et-et-1)
- 89 -
PID tuning
- 90 -
Practical Issues with Computer-Based Control
Quantization
Overflow
Aliasing
Computation Delay
- 91 -
Quantization & Overflow
Quantization
Can’t store 0.36 as 4-bit fractional number
Can only store 0.75, 0.59, 0.25, 0.00, -0.25, -050,-0.75, -1.00
Choose 0.25
• Result in quantization error of 0.11
Sources of quantization error
Operations, e.g. 0.50*0.25=0.125
• Can use more bits until input/output to the environment/memory
A2D converters
Overflow
Can’t store 0.75+0.50 = 1.25 as 4-bit fractional number
Solutions:
Use fix-point representation/operations carefully
• Time-consuming
Use floating-point co-processor
• Costly
- 92 -
Aliasing
Quantization/overflow
Due to discrete nature of computer data
Aliasing
Due to discrete nature of sampling
- 93 -
Aliasing Example
- 94 -
Computation Delay
- 95 -
Benefit of Computer Control
Cost!!!
Expensive to make analog control immune to
• Age, temperature, manufacturing error
Computer control replace complex analog hardware with complex
code
Programmability!!!
Computer Control can be “upgraded”
• Change in control mode, gain, are easy to do
Computer Control can be adaptive to change in plant
• Due to age, temperature, …etc
“future-proof”
• Easily adapt to change in standards,..etc
- 96 -