0% found this document useful (0 votes)
40 views23 pages

OS03 Slide Deck 2020

Uploaded by

Niek Rongen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views23 pages

OS03 Slide Deck 2020

Uploaded by

Niek Rongen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Operating Systems (2INC0)

Threads (03)
Introduction to threads

Dr. Geoffrey Nelissen


Courtesy of Prof. Dr. Johan Lukkien and
Dr. Tanir Ozcelebi

Interconnected
Resource-aware
Intelligent Systems
Process parallelism

• Using processes allows to use platform parallelism:


• Example: if the processor has more than one core, two or more different process
can make simultaneous progress

• It also allows for concurrency:


• The same resource (e.g., a core or network driver) can be time shared
➔ improve responsiveness
• If a process is waiting (e.g., to get access to a resource, or to receive data from
another process), then the kernel can schedule another process
➔ more efficient use of the system resources

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

• Inefficient program structure (busy-waiting):


• while ... do
check keyboard;
check network;
check mouse;
wait a while
od

• 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

• Increase concurrency level (less costly)


• performance
− hide latency (while waiting CPU can do something useful in another thread)
− increase responsiveness
− exploit platform concurrency (multiple processors → multiple threads)
• discriminate importance levels in activities
− e.g. interrupt routines
process process process process
1 2 3 4
• “Natural” concurrency Apache client editor
...
master
• natural organization, structure, e.g.
− thread per event t t t t t
− thread per task
− thread per resource
− thread per (active) OS
external interaction sequence
− e.g. in user interfaces
P P P P Memory

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

Dr. Geoffrey Nelissen


Courtesy of Prof. Dr. Johan Lukkien and
Dr. Tanir Ozcelebi

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)

• State per thread


• Thread context:
CPU registers, stack image
• … but code, data and files
are shared between the threads
of the same process

• Generally, little overhead for switching


• depending on underlying thread execution model

Geoffrey Nelissen 16
User and kernel threads

• Threads are supported either at user level or by kernel


• user threads
• kernel threads

• User threads supported • Kernel threads supported by all


above the kernel modern OS (kernel support)
(no kernel support) – Windows
– via thread libraries – Solaris
– Linux, Unix (some versions), Android
• Three primary thread – Mac OS X
libraries:
– POSIX Pthreads Thread libraries can also be used on top of OS’s
– Win32 threads supporting kernel threads.
– Java threads
Needed: A mapping model between user threads
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

• User threads mapped to single kernel thread

• Advantage: Thread management by thread lib


• no need for kernel involvement,
➔ fast
➔ easy to deploy

• Disadvantage: Only one user thread


can access kernel at a given time
• multiple threads cannot run concurrently on different
processors
• a blocking system call from one user thread blocks all user
threads of the process

• Examples: Solaris Green Threads Lib., GNU Portable Threads Lib.

Geoffrey Nelissen 19
One-to-one model

• Each user-level thread maps to a kernel thread

• Advantage: allows for concurrency between thread

• Disadvantage: the kernel must manage all threads (state, schedule, …)


• Low performance in case of many user threads

• Examples: Windows, Linux (also Android), Solaris 9 and later

Geoffrey Nelissen 20
Many-to-many model

• Allows OS to create a (limited) number of kernel threads


• #kernel_threads ≤ #user_threads

• 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

Concurrency level is limited by the number of kernel threads.

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

• Issue: mapping between kernel and user threads

• Lightweight process (LWP) as a user-level


representation of a kernel thread
• LWP an intermediate data structure
• like a virtual processor for user threads and threadlib

• Kernel schedules LWPs


• user threads are scheduled onto an available LWP
• blocking calls in user space or call to thread library
package switch LWP to a new user thread
− a user thread can also be bound to a LWP

• Concurrency level: number of kernel threads

Geoffrey Nelissen 23
Thread switching

• Threads are executed in an interleaved way

• Switching between threads: two possibilities

• switching as a kernel activity


− kernel maintains execution state of thread
− similar to process switching, but without context

• switching handled by user level thread package (library)


− the package maintains execution state of threads (kernel knows kernel threads only)
− the package must obtain control in order to switch threads
− responsibility of programmer to call package – ‘cooperative multithreading’
− could use timer interrupt

Geoffrey Nelissen 24
Thread pools

• Not efficient to create many threads dynamically.

• Alternative: given an application, create a number of threads in a


pool where they wait for work.

• 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

• Start a function as a new thread


• setting attributes, e.g. stacksize;
• Extensive interface
• detach: clean up when thread terminates
• join: pick up terminated thread
• cancel: stop a thread

pthread_t thread_id;

status = pthread_create (&thread_id, attr, func, arg_of_func);


/* func(arg_of_func) is started as a new thread; when attr == NULL some
* internal defaults are chosen */

status = pthread_join (thread_id, &result);


/* wait for thread to terminate */

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

You might also like