Laurea Specialistica in Ingegneriadell'Automazione – A.A. 2006-2007 Sistemi in Tempo Reale
Giuseppe Lipari
Introduzione alla concorrenza - I
Laurea Specialistica in Ingegneriadell'Automazione – A.A. 2006-2007 Sistemi in Tempo Reale
Giuseppe Lipari
Introduzione alla concorrenza - I
Processes
Process
The fundamental concept in any operating system is the “process”
A process is an executing program
An OS can execute many processes at the same time (concurrency)
Example: running a Text Editor and a Web Browser at the same time in the PC
Processes have separate memory spaces
Each process is assigned a private memory space
One process is not allowed to read or write in the memory space of another process
If a process tries to access a memory location not in its space, an exception is raised (Segmentation fault), and the process is terminated
Two processes cannot directly share variables
Process Control Block
PID PPID UID Page table File Table Handles State Statistics ... Process Table It contains all the data concerning one process
All PCBs are stored in the Process Table
The role of PCB
Virtually every routine in the OS will access the PCBs
The scheduler
The Virtual memory
The Virtual File System
Interrupt handlers (I/O devices)
...
It can only be accessed by the OS!
The user can access some of the information in the PCB by using appropriate system calls
The PCB is a critical point of any OS!
Memory layout of a Process
Text Initialized Data BSS Stack Heap Other data Contains the process code
(machine code) Global variables
(initialized) Global variables
(non initialized) Stack
(variable size) Dynamically allocated
memory
(variable size)
Memory protection
Text Initialized Data BSS Stack Heap Other data Initialized Data Stack Heap Other data Every process has its own memory space
Part of it is “private to the process”
Part of it can be shared with other processes
For examples: two processes that are instances of the same program will probably share the TEXT part
If two processes want to communicate by shared memory, they can share a portion of the data segment
Memory Protection
Text Initialized Data BSS Stack Heap Other data Any reference to this
memory results in a
segmentation fault By default, two processes cannot share their memory
If one process tries to access a memory location outside its space, a processor exception is raised (trap) and the process is terminated
The famous “Segmentation Fault” error!!
Processes and Threads
Processes
We can distinguish two aspects in a process
Resource Ownership
A process includes a virtual address space, a process image (code + data)
It is allocated a set of resources, like file descriptors, I/O channels, etc
Scheduling/Execution
The execution of a process follows an ececution path, and generates a trace (sequence of internal states)
It has a state (ready, Running, etc.)
And scheduling parameters (priority, time left in the round, etc.)
Multi-threading
Many OS separate these aspects, by providing the concept of thread
The process is the “resource owner”
The thread is the “scheduling entity”
One process can consists of one or more threads
Threads are sometime called (improperly) lightweight processes
Therefore, on process can have many different (and concurrent) traces of execution!
Single threaded Process Model
In the single-threaded process model one process has only one thread
One address space
One stack
One PCB only
Multi-threaded process model
In the multi-threaded process model each process can have many threads
One address space
One PCB
Many stacks
Many TCB (Thread Control blocks)
The threads are scheduled directly by the global scheduler
Threads
Generally, processes do not share memory
To communicate between process, it is necessary to user OS primitives
Process switch is more complex because we have to change address space
Two threads in the same process share the same address space
They can access the same variables in memory
Communication between threads is simpler
Thread switch has less overhead
Processes vs. Threads
Processes are mainly used to compete for some resource
For example, two different users run two separate applications that need to print a file
The printer is a shared resource, the two processes compete for the printer
Threads are mainly used to collaborate to some goal
For example, one complex calculation can be split in two parallel phases, each thread does one phase
In a multi-processor machine the two threads go in parallel and the calculation becomes faster
Example - I
1. wait 2. update 3. format 4. syntax 5. Other
events 1. wait Consider a Word Processor application
Main cycle
Wait for input from the keyboard
Update the document
Format the document
Check for syntax errors
Check for other events (i.e. temporary save)
Return to 1
One single process would be a waste of time!
Example - II
Input
Process Format
Process Syntax
Process Graphic
Process Problems
Most of the time, the program waits for input
Idea, while waiting we could perform some other task
Activities 3 and 4 (formatting and syntax checking) are very time consuming
Idea: let’s do them while waiting for input
Solution with multiple processes
One process waits for input
Another process periodically formats the document
A third process periodically performs a syntax checking
A fourth process visualize the document
Example - III
Input
Process Format
Process Syntax
Process Graphic
Process Data
Server Problem with multiple processes
All processes needs to access the same data structure, the document
Which process holds the data structure?
Solution 1: message passing
A dedicated process holds the data, all the others communicate with it to read/update the data
Very inefficient!
Example - IV
Another solution...
Solution 2: shared memory
One process holds the data and makes that part of its memory shareable with the others
Still not very efficient:
We have a lot of process switches
Memory handling becomes very complex
Why using threads
Input
Thread Format
Thread Syntax
Thread Graphic
Thread Document Process Speed of creation
Creating a thread takes far less time than creating a process
Speed of switching
Thread switch is faster than process switch
Shared memory
Threads of the same process run in the same memory space
They can naturally access the same data!
Comments