OS03 Slide Deck 2020
OS03 Slide Deck 2020
Threads (03)
Introduction to threads
Interconnected
Resource-aware
Intelligent Systems
Process parallelism
Geoffrey Nelissen 2
Process parallelism:
disadvantages
• Creation overhead (e.g., memory space creation/copy)
• Inter-Process Communication overhead: OS involvement
• IPC through message passing involve system calls
• IPC possible through shared memory in the kernel memory space.
Geoffrey Nelissen 3
Process parallelism:
disadvantages
• Creation overhead (e.g., memory space creation/copy)
• Inter-Process Communication overhead: OS involvement
• IPC through message passing involve system calls
• IPC possible through shared memory in the kernel memory space.
• Process switching overhead: mode + context switch
• for each IPC on a single-processor system
• save/restore state
(mode)
(mode)
B
Geoffrey Nelissen 4
Process vs Thread
• Process
• unit of work
• defines a memory space
• defines ownership on other resources
• has at least one associated thread
of execution
• Thread
• unit of concurrency and scheduling
− though in case of one thread per process,
the process is often said to be the unit of scheduling
• all threads of a process can operate in the process’ address space
− i.e., in a process; several threads may share the same variables
• has an associated execution state (PCB is extended with thread info)
− program counter, stack image, return addresses of function calls, values of processor
registers
Geoffrey Nelissen 5
Structuring by threads
• This program must deal with
A single
three input sources.
process
• Simpler structure
• start three threads
• each thread takes care of one source
(and they use interrupts)
Geoffrey Nelissen 6
Example: multithreaded server
• Dispatcher/worker model
Geoffrey Nelissen 7
Reasons to introduce threads
Geoffrey Nelissen 8
Multithreaded processes
• Process address space and resources (global data, files etc) are shared.
Geoffrey Nelissen 9
Concurrent execution:
single-core vs multi-core system
multi-core
• Considerations
• Dividing the program into activities that can be executed in parallel
• Load balancing
• Interference issues (solved by synchronization)
Geoffrey Nelissen 10
Operating Systems (2INC0)
Threads (03)
Multithreading models
Interconnected
Resource-aware
Intelligent Systems
Threads summary
• Concurrency on memory address space of the process
• inexpensive communication between threads
• no protection against other threads (faults, memory sharing)
Geoffrey Nelissen 16
User and kernel threads
Geoffrey Nelissen 17
Multithreading mapping models
• Many-to-one
• One-to-one
• Many-to-many
• plus a variation: Two-level model
Geoffrey Nelissen 18
Many-to-one model
Geoffrey Nelissen 19
One-to-one model
Geoffrey Nelissen 20
Many-to-many model
• Advantages:
• concurrency
• bounded performance cost for the kernel
• Disadvantage: more complex to implement
• Examples
• Windows NT/2000 with the ThreadFiber package
• Solaris 8 and earlier
Geoffrey Nelissen 21
Two-level model
• Similar to many-to-many,
except that it allows a user
thread to be bound to a
kernel thread
• Examples
• IRIX, HP-UX, Tru64 UNIX
• Solaris 8 and earlier
Geoffrey Nelissen 22
Thread execution
Geoffrey Nelissen 23
Thread switching
Geoffrey Nelissen 24
Thread pools
• Advantages:
• Usually slightly faster to service a request with an existing thread than
creating a new thread
• Allows the number of user threads in the application(s) to be bounded
by the size of the pool
• Example
• ThreadPoolExecutor (Java) : assigns app tasks to a thread pool
Geoffrey Nelissen 25
POSIX pthread interface, in C
pthread_t thread_id;
Geoffrey Nelissen 26
Example
#include <stdio.h>
#include <pthread.h>
int x;
void Count_100 ()
{
int i, s;
for (i = 0; i < 100; i ++) {
x = x+1;
}}
void main ()
{
pthread_t thread_id;
x = 0;
pthread_create (&thread_id, NULL, Count_100, NULL); Question 1: How many threads
Count_100 (); are executing here?
pthread_join (thread_id, NULL);
printf ("x = %d\n", x); Question 2: What is the value
} of x here?
Geoffrey Nelissen 27