TDDD36 Secure Mobile Systems Systems Software Lecture 1: Processes and shared

advertisement
Course overview
TDDD36
Secure Mobile Systems
• Selected topics from core computer systems
curriculum that are
Systems Software
– Essential and you only see in this course
– Or slightly overlap with later courses but you benefit
from while designing and implementing the project
Lecture 1: Processes and shared
resources
Simin Nadjm-Tehrani
Real-time Systems Laboratory
Department of Computer and Information Science
Linköping University
Project course TDDD36
Systems Software module
48 pages
Autumn 2012
People & organisation
• Lecturer and examiner:
•
•
•
•
Only
l this
h course
Concurrency, Processes, and Resource sharing
Distributed system models
Dependable systems & Availability
Adaptive Network resource allocation
Project course TDDD36
Systems Software module
From last year’s evaluations
We had two points to act on:
• Code examples: some students asked for more
example codes in the lectures.
– Simin Nadjm-Tehrani
– Takes care of the resource session in the
schedule too
Will do!
• Lessons:
– Laurent Delosières (2 scheduled occasions)
• Expert consultant in the project:
• Some students were asking for answers to
exercises.
Will be available but
handed out after you
have attempted!
– Laurent Delosières (until 15th October)
– Mikael Asplund (after 15th October)
– Note: different role compared to the theory
part of the course!
Project course TDDD36
Systems Software module
2 of 48
Autumn 2012
3 of 48
Autumn 2012
Project course TDDD36
Systems Software module
4 of 48
Autumn 2012
Literature & handouts
• Detailed sources and instructions on the
web, see also e-book with Java syntax
• Please ask me if you are unsure!
Questions?
• Note: lecture slide handouts are from
last year until one day before the
lecture!
Project course TDDD36
Systems Software module
5 of 48
Autumn 2012
Project course TDDD36
Systems Software module
6 of 48
Autumn 2012
1
This lecture
• Processes, threads, and their
representation
• Concurrency and execution
• Communication
• Synchronisation
The notion of Process
• An abstraction in computer science used
for describing program execution and
potential parallelism
• What other abstractions do you know?
– Functions
– Classes, Objects, Methods
• Solutions to mutual exclusion problem
• Processes emphasise the run-time
behaviour
Project course TDDD36
Systems Software module
7 of 48
Autumn 2012
Project course TDDD36
Systems Software module
Concurrent Programs
8 of 48
Autumn 2012
Concurrency
• A mathematical term for modelling
computational processes that could in
principle be run in parallel
Pseudopa allel
parallel
• Three possibilities:
A sequential program has a
single thread of control.
A concurrent program has
multiple threads of control
allowing it perform multiple
computations “in parallel” and
to control multiple external
activities which occur at the
same time.
– Single processor, shared memory
– Multi-processor, shared memory
– Multiple processors not sharing memory
[Magee and Kramer 2006]
Project course TDDD36
Systems Software module
9 of 48
Autumn 2012
Project course TDDD36
Systems Software module
Related terms
Concurrent programs
Define actions that may be
performed simultaneously
Parallel programs
A concurrent program that is
designed for execution on parallel
hardware
Distributed programs
Parallel programs designed to run
on network of autonomous
processors that do not share
memory
Project course TDDD36
Systems Software module
11 of 48
Autumn 2012
10 of 48
Autumn 2012
Processes on a single CPU
• A concurrent program consists of a set
of autonomous computation processes
(logically) running in parallel
• A process has its own thread of control
and its own address space
• Operating system may arbitrarily switch
among processes and give control of the
CPU to some process
Theory on processes in this
course covers single CPU
Project course TDDD36
Systems Software module
12 of 48
Autumn 2012
2
Run-time behaviour
Logical parallelism
Running processes on a single CPU:
C
B
P1:
P2:
{while true do
think;
talk
}
{while true do
sleep;
listen
}
A
Which potential execution sequences (traces)?
Time
Project course TDDD36
Systems Software module
Project course TDDD36
Systems Software module
13 of 48
Autumn 2012
14 of 48
Autumn 2012
Concurrent on single CPU
Historical data…
Response tim
me in seconds
• To fully utilise the processor
• More efficient use of the processor
• Increased responsiveness, e.g. user
interaction can be given priority
10
1
10
Project course TDDD36
Systems Software module
15 of 48
Autumn 2012
2
10
0
10
-1
10
-2
10
-3
10
-4
10
-5
10
-6
10
-7
10
10
human
tape
floppy
CD
memory
-8
-9
processor
Project course TDDD36
Systems Software module
16 of 48
Autumn 2012
Each process
• Is managed by the OS and has
– A process ID
– A record of its run-time data kept in the OS
“process control block”  PCB
• Is allocated segments in the memory:
– Code: the machine instructions it will run
– Data: global variables and memory allocated
at run-time
– Stack: local variables and records of
functions activated
Project course TDDD36
Systems Software module
17 of 48
Autumn 2012
[Wellings 2004]
Process life cycle
Non-existing
Created
Terminated
Initialising
Ready
Ready
Waiting
Executing
Executing
Project course TDDD36
Systems Software module
18 of 48
Autumn 2012
3
Keeping track of execution
• Each process in its PCB has:
–
–
–
–
–
Process state (which stage in life cycle)
Program counter (where in its running code)
Value of internal variables
Th allocated
The
ll
t d memory areas
Open files and other resources
Project course TDDD36
Systems Software module
Program/process structure
• Static/Dynamic
• Nested/Flat
Not part of
this course
• Substantial differences between various
languages wrt syntax, execution model,
and termination semantics for nested
processes
Project course TDDD36
Systems Software module
19 of 48
Autumn 2012
20 of 48
Autumn 2012
Threads (& tasks)
• Some modern operating systems allow a
process to create (spawn) a number of
parallel threads of control – in the same
address space
• Each one has its own stack
• Processes that run with a shared
address space (code, data) are called
light-weight processes or threads
• Here we consider processes with one
thread of control and use tasks
interchangeably with processes
Context Switch
• Consider a program that consists of
processes P1,…, P4
• An execution of the concurrent program
may look like:
P2
P1
P4
P3
Context switch
time
Project course TDDD36
Systems Software module
21 of 48
Autumn 2012
Project course TDDD36
Systems Software module
This lecture
22 of 48
Autumn 2012
Processes interact
To share data:
• Output for one process may be input for
another process
• Processes, threads, and their
representation
• Concurrency and execution
• Communication
• Synchronisation
– Compute distance, and then compute incremental
acceleration
To influence flow of control s nch onisation
synchronisation:
• One process may only start after
another/others finished
– Update display only after all sensor values are
Processes must
received
• Solutions to mutual exclusion problem
• Sharing common resources
notify each
other
– No more than one process at a time may send a
packet on a shared channel
Project course TDDD36
Systems Software module
23 of 48
Autumn 2012
Project course TDDD36
Systems Software module
24 of 48
Autumn 2012
4
Communication
Mutual exclusion
[Garg 2005]
• Two modes:
• Consider the two processes using a
shared variable x initialised at 0:
– Shared variables
– Message passing
Process A
Process A
Shared
Process B
P0
{x= x+1
}
Kernel
• What is the outcome of running them
both to completion?
It depends...
Process B
Kernel
Project course TDDD36
Systems Software module
25 of 48
Autumn 2012
Project course TDDD36
Systems Software module
Machine instructions
LD R, x // load register R from x
INC R // increment register R
ST R, x // store register R to x
• The
h program may then
h
be
b compiled
l d into
many different interleavings
P1
{x= x+1
}
26 of 48
Autumn 2012
Non-atomic operations
P0:
P0:
P1:
P1:
P0:
P1:
LD R, x
INC R
LD R, x
INC R
ST R, x
ST R, x
What is the value of x after this trace?
Project course TDDD36
Systems Software module
27 of 48
Autumn 2012
Project course TDDD36
Systems Software module
Basic communication operation
Problems?
• No! Since hardware memory can
support atomic memory update, so that
only one writes at a time
• But…
• Creates problems for more complex
shared data structures
• Communication using shared variables
P2
P1
28 of 48
Autumn 2012
– Update date, time and stock value
Writes to X
Reads from X
• One process may write at a faster rate
than the other process reads
Variable X
Project course TDDD36
Systems Software module
29 of 48
Autumn 2012
Project course TDDD36
Systems Software module
30 of 48
Autumn 2012
5
Decoupling from process rates
• Finite buffers
P1
• Must check that buffer is not full when
writing to it
• Must check that buffer is not empty
when reading from it
• Must ensure that two processes do not
write on one buffer position at the same
time
P2
Writes to buffer
Issues
Need mechanisms for
synchronisation and
sharing data
Reads from buffer
Finite buffer
Project course TDDD36
Systems Software module
31 of 48
Autumn 2012
Project course TDDD36
Systems Software module
General problems
Race condition
• Consider two processes P1 and P2 that are
allowed to write to a shared data structure
• Conditional action
– Examples:
• Compute the interest when all transactions have
been processed
• Book a flight seat only if seats are available
• Mutual exclusion
– Example:
• Two customers shall not be booked on the same
seat
Project course TDDD36
Systems Software module
33 of 48
Autumn 2012
• If the order of updating the data structure by
respective processes can affect the outcome of
the computation,
computation and this is unintended,
unintended then
the system suffers from a race condition
• Process synchronisation can be used to avoid
race conditions
– Analogy: Deposit 5000kr in the account, calculate
accrued interest
Project course TDDD36
Systems Software module
Recap
• Programmer wants to write code for processes
that run independently as far as possible
• This makes running on alternative
architectures a possibility (recall process as a
mathematical abstraction with interleaved
traces)
• Process dependencies are unavoidable (sharing
values, waiting for conditions to hold)
• But you want to enforce a sequence (wait for
conditions) only when it is part of
requirements, i.e. avoid race conditions
Project course TDDD36
Systems Software module
32 of 48
Autumn 2012
35 of 48
Autumn 2012
34 of 48
Autumn 2012
This lecture
• Processes, threads, and their
representation
• Concurrency and execution
• Communication
• Synchronisation
• Solutions to mutual exclusion problem
Project course TDDD36
Systems Software module
36 of 48
Autumn 2012
6
Atomic update
• To ensure that a single process
updates a shared data structure at any
one time the application needs to
manage the mutual exclusion problem!
Project course TDDD36
Systems Software module
37 of 48
Autumn 2012
Mutual exclusion
• Consider n processes that need to exclude
concurrent execution of some parts of their
code
Process Pi
{
entry-protocol
critical-section
exit-protocol
non-critical-section
}
• Fundamental problem to design entry and exit
protocols for critical sections
Project course TDDD36
Systems Software module
Critical section: example
Process A
While true do
{
…
X = X+1;
Z = X^2;
…
}
Process B
While true do
{
…
if ((X mod 2)==0){
Project course TDDD36
Systems Software module
print(X)
}
print(Z);
…
}
First attempt
process P1
loop
flag1 = up
while flag2 == up do
nothing
(* b
busy waiting
iti
*)
critical-section
flag1 = down
non-critical-section
end
Project course TDDD36
Systems Software module
process P2
loop
flag2 = up
while flag1 == up do
nothing
(* b
busy waiting
iti
*)
critical-section
flag2 = down
non-critical-section
end
[Dijkstra 1965]
39 of 48
Autumn 2012
Project course TDDD36
Systems Software module
Second attempt
process P1
loop
while flag2 == up do
nothing
(* busy waiting *)
fl 1 = up
flag1
critical-section
flag1 = down
non-critical-section
end
38 of 48
Autumn 2012
process P2
loop
while flag1 == up do
nothing
(* busy waiting *)
fl 2 = up
flag2
critical-section
flag2 = down
non-critical-section
end
41 of 48
Autumn 2012
40 of 48
Autumn 2012
Third attempt
process P1
loop
while turn == 2 do
nothing
(*busy waiting*)
critical-section
turn = 2
non-criticalsection
end
Project course TDDD36
Systems Software module
process P2
loop
while turn == 1 do
nothing
(*busy waiting*)
critical-section
turn = 1
non-criticalsection
end
42 of 48
Autumn 2012
7
Peterson’s algorithm
• Implementing synchronisation and concurrency
with shared variables, using only sequential
programming constructs, is difficult and error
prone
• Some programming languages have explicit
support:
process P1
loop
flag1 = up
turn = 2
while flag2 == up and turn == 2 do
nothing
end
critical-section
flag1 = down
non-critical-section
end
Project course TDDD36
Systems Software module
Programming language support
– Java: Early versions used Suspend/Resume
constructs that led to race condition!
– Ada: built-in run-time support with explicit task
synchronisation entry points (Rendezvous)
43 of 48
Autumn 2012
Project course TDDD36
Systems Software module
Ada tasks: example
procedure Morning is
task Get_Breakfast;
task Take_Shower;
task body
Get_Breakfast is
begin
Make_Coffee;
Make_Toast;
Boil_Egg;
Eat_Breakfast;
end Get_Breakfast;
Java threads: Example
public class Prepare Extends Thread
{
private int items;
public Prepare(int: Breakfast_items)
{
items = Breakfast_items
}
public void run()
{
while(true)
{
Cooking.Make(items);
}
}
}
task body
Take_Shower is
begin
Use_Shower;
Dry_Hair;
end Take_Shower;
begin -- Morning
null;
end Morning;
Project course TDDD36
Systems Software module
44 of 48
Autumn 2012
45 of 48
Autumn 2012
Project course TDDD36
Systems Software module
46 of 48
Autumn 2012
Java threads: Example
final int eggs = 1;
final int toast = 2;
final int coffee = 1;
Prepare P1 = new Prepare(eggs);
Prepare P2 = new Prepare(toast);
Prepare P3 = new Prepare(coffee);
. . .
P1.start();
P2.start();
P3.start();
Project course TDDD36
Systems Software module
Questions?
47 of 48
Autumn 2012
Project course TDDD36
Systems Software module
48 of 48
Autumn 2012
8
Download