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

advertisement
TDDD82
Secure Mobile Systems
Systems Software
Lecture 1: Processes and shared
resources
Mikael Asplund
Real-time Systems Laboratory
Department of Computer and Information Science
Linköping University
Based on slides by Simin Nadjm-Tehrani as well as the Silberschatz, Galvin
and Gagne book 2014
Project course TDDD82
Systems Software module
42 pages
Spring 2015
People & organisation
• Lecturer:
– Mikael Asplund
– Takes care of the last resource session
• Lessons:
– Chih-Yuan (Sana) Lin (2 scheduled
occasions)
Project course TDDD82
Systems Software module
2 of 42
Spring 2015
What is this module about?
“Systemprogramvara” – Systems software
1. Concurrent programming which is
normally taught as part of an operating
systems (OS) course, dealing with
application code that is run in a
pseudo-parallel manner on one CPU
– Distinguishing management of concurrency
in the application itself and supporting it
with generic code (parts of operating
system)
Project course TDDD82
Systems Software module
3 of 42
Spring 2015
What is this module about?
2. Applications that are realised on
multiple CPUs in distributed systems
use generic code used by many
applications – typically complex
–
–
The generic patterns and causes of the
complexities are usually taken up in a
Distributed systems course
One of the complexities arises from how
organisation of distributed code affects
dependability and how faults can lead to
failures
Project course TDDD82
Systems Software module
4 of 42
Spring 2015
What is this module about?
3. Performance is another aspect in a
distributed system that is complex to
characterise and assure
– How to characterise quality of service in a
networked application is a topic normally
taken up in a network systems course
Project course TDDD82
Systems Software module
5 of 42
Spring 2015
Module overview
• In this module (with 3 credit points) we
scratch the surface of these three important
topics
• We have placed the focus of the selected topics
on what you need for your project work
– Distributed systems introduction and
introduction to dependability
• We also take up what every CS educated
person needs to know on theory of concurrent
computational processes
– Processes, resource sharing, deadlocks
Project course TDDD82
Systems Software module
6 of 42
Spring 2015
Literature & handouts
• Detailed sources and instructions on the
web, see also e-book with Java syntax
• Please ask me if you are unsure!
Project course TDDD82
Systems Software module
7 of 42
Spring 2015
Overview
• Lecture 1-3:Processes
– All concurrency related topics, including
synchronisation, mutual exclusion,
deadlocks
• Lecture 4: Distributed systems (intro)
• Lecture 5: Dependability
• Lecture 6: Quality of Service
Project course TDDD82
Systems Software module
8 of 42
Spring 2015
Lecture 1-3 Topics
•
•
•
•
•
•
•
Processes, threads, and their representation
Concurrency and execution
Communication
Synchronisation
Solutions to mutual exclusion problem
Support in operating system: semaphores
Support in programming environment (runtime
system): monitors
• Deadlock and related issues
Project course TDDD82
Systems Software module
9 of 42
Spring 2015
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
• Processes emphasise the run-time
behaviour
Project course TDDD82
Systems Software module
10 of 42
Spring 2015
Concurrent Programs
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.
[Magee and Kramer 2006]
Project course TDDD82
Systems Software module
11 of 42
Spring 2015
Concurrency
• A mathematical term for modelling
computational processes that could in
principle be run in parallel
Pseudoparallel
• Three possibilities:
– Single processor, shared memory
– Multi-processor, shared memory
– Multiple processors not sharing memory
Project course TDDD82
Systems Software module
12 of 42
Spring 2015
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 TDDD82
Systems Software module
13 of 42
Spring 2015
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
Theory on processes in this
course covers single CPU
Project course TDDD82
Systems Software module
14 of 42
Spring 2015
Run-time behaviour
Running processes on a single CPU:
app1
app2
app3
Time
Project course TDDD82
Systems Software module
15 of 42
Spring 2015
Logical parallelism
P1:
P2:
{while true do
think;
talk
}
{while true do
sleep;
listen
}
Which potential execution sequences (traces)?
Project course TDDD82
Systems Software module
16 of 42
Spring 2015
Concurrent on single CPU
• More efficient use of the processor
• Increased responsiveness, e.g. user
interaction can be given priority
Project course TDDD82
Systems Software module
17 of 42
Spring 2015
Uniprogramming
Running
0
Waiting
10
Running
110
120
Waiting
220
Process execution time:
CPU:
10 + 10 time units
I/O:
100 + 100 time units
I.e., I/O intensive (200/220 = 90.9%), CPU utilization 9.1%
Project course TDDD82
Systems Software module
18 of 42
Spring 2015
Storage media
Project course TDDD82
Systems Software module
19 of 42
Spring 2015
Multiprogramming
A
Running
B
Waiting (printer)
Running
C
Waiting (disk)
Running
Waiting
Running
Running Waiting (network)
Waiting
Running
Waiting
Running Running Running Waiting Running Running Running Waiting
combined
Project course TDDD82
Systems Software module
20 of 42
Spring 2015
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
Project course TDDD82
Systems Software module
21 of 42
Spring 2015
Process in Memory
Project course TDDD82
Systems Software module
22 of 42
Spring 2015
Diagram of Process State
Project course TDDD82
Systems Software module
23 of 42
Spring 2015
Process Creation
• Address space
– Child duplicate of parent
– Child has a program loaded into it
• UNIX examples
– fork() system call creates new process
– exec() system call used after a fork()
to replace the process’ memory space
with a new program
Project course TDDD82
Systems Software module
24 of 42
Spring 2015
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
The allocated memory areas
Open files and other resources
Project course TDDD82
Systems Software module
25 of 42
Spring 2015
Program/process structure
• Static/Dynamic
• Nested/Flat
– Nested: More complex life cycle graph!
Not part of this
course
• Substantial differences between various
languages wrt syntax, execution model,
and termination semantics for nested
processes
Project course TDDD82
Systems Software module
26 of 42
Spring 2015
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
Project course TDDD82
Systems Software module
27 of 42
Spring 2015
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 TDDD82
Systems Software module
28 of 42
Spring 2015
Exercise
Before next lecture: Prepare and bring with
you!
How many possible traces exist if each of these
processes runs exactly once? Motivate!
Process P1
X = 3;
Y = 0;
If Y > 1
X = X
else
X = X
}
{
then
- 1;
+ 2
Project course TDDD82
Systems Software module
Process
X = 3;
Y = 2;
If Y <
X =
else
X =
}
P2 {
1 then
X + 1
X - 2
29 of 42
Spring 2015
Lecture 1-3 Topics
•
•
•
•
•
•
•
•
Processes, threads, and their representation
Concurrency and execution
Communication
Synchronisation
Solutions to mutual exclusion problem
Support in operating system: semaphores
Support in runtime system: monitors
Deadlock and related issues
Project course TDDD82
Systems Software module
30 of 42
Spring 2015
Processes interact
To share data:
• Output for one process may be input for
another process
– Cruise control: compute distance by one process,
compute incremental acceleration by another
To influence flow of control, also called
synchronisation:
• One process may only start after
another/others finished
– Update display only after all sensor values are
received
• Sharing common resources
– No more than one process at a time may send a
packet on a shared channel
Project course TDDD82
Systems Software module
31 of 42
Spring 2015
Communication
• Two modes:
– Shared variables
– Message passing
Process A
Process A
Shared
Process B
Process B
Kernel
Project course TDDD82
Systems Software module
Kernel
32 of 42
Spring 2015
Sharing variables
[Garg 2005]
• Often requires atomicity
• Consider the two processes using a
shared variable x initialised at 0:
P0
{x= x+1
}
P1
{x= x+1
}
It depends...
• What is the outcome of running them
both to completion?
Project course TDDD82
Systems Software module
33 of 42
Spring 2015
Machine instructions
LD R, x // load register R from x
INC R // increment register R
ST R, x // store register R to x
• The program may then be compiled into
many different interleavings
Project course TDDD82
Systems Software module
34 of 42
Spring 2015
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 TDDD82
Systems Software module
35 of 42
Spring 2015
Basic operation
• Communication using shared variables
P2
P1
Writes to X
Reads from X
Variable X
Project course TDDD82
Systems Software module
36 of 42
Spring 2015
Problems?
• No! Since hardware can support atomic
memory update, so that only one writes
at a time
• But…
• Creates problems for more complex
shared data structures
– Update date, time and stock value
• Also if one process may write at a faster
rate than the other process reads
Project course TDDD82
Systems Software module
37 of 42
Spring 2015
Decoupling from process rates
• Finite buffers
P1
P2
Writes to buffer
Reads from buffer
Finite buffer
Project course TDDD82
Systems Software module
38 of 42
Spring 2015
Issues
• 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
Need mechanisms for
shared access to data
structures
Project course TDDD82
Systems Software module
39 of 42
Spring 2015
Race condition
• Consider two processes P1 and P2 that are
allowed to write to a shared data structure
• If the order of updating the data structure by
respective processes can affect the outcome of
the computation, and if this is 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 TDDD82
Systems Software module
40 of 42
Spring 2015
General problems
• 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 TDDD82
Systems Software module
41 of 42
Spring 2015
Summary
• Programmer wants to write code for processes
that run independently as far as possible
• This makes running on alternative
architectures a possibility (single CPU, multiple
CPUs)
• 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 TDDD82
Systems Software module
42 of 42
Spring 2015
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;
Project course TDDD82
Systems Software module
task body
Take_Shower is
begin
Use_Shower;
Dry_Hair;
end Take_Shower;
begin -- Morning
null;
end Morning;
43 of 42
Spring 2015
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);
}
}
}
Project course TDDD82
Systems Software module
44 of 42
Spring 2015
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 TDDD82
Systems Software module
45 of 42
Spring 2015
Download