0% found this document useful (0 votes)
5 views

Parallel Distributed Computing

Chapter 1

Uploaded by

reyan.24n67e
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Parallel Distributed Computing

Chapter 1

Uploaded by

reyan.24n67e
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Distributed Systems

(4th edition, version 01)

Chapter 03: Processes


Processes Threads

Introduction to threads
Basic idea
We build virtual processors in software, on top of physical processors:
Processor: Provides a set of instructions along with the capability of
automatically executing a series of those instructions.
Thread: A minimal software processor in whose context a series of
instructions can be executed. Saving a thread context implies
stopping the current execution and saving all the data needed
to continue the execution at a later stage.
Process: A software processor in whose context one or more threads
may be executed. Executing a thread, means executing a
series of instructions in the context of that thread.

Introduction to threads
Processes Threads

Context switching
Contexts
• Processor context: The minimal collection of values stored in the
registers of a processor used for the execution of a series of instructions
(e.g., stack pointer, addressing registers, program counter).

Introduction to threads
Processes Threads

Context switching
Contexts
• Processor context: The minimal collection of values stored in the
registers of a processor used for the execution of a series of instructions
(e.g., stack pointer, addressing registers, program counter).
• Thread context: The minimal collection of values stored in registers and
memory, used for the execution of a series of instructions (i.e., processor
context, state).

Introduction to threads
Processes Threads

Context switching
Contexts
• Processor context: The minimal collection of values stored in the
registers of a processor used for the execution of a series of instructions
(e.g., stack pointer, addressing registers, program counter).
• Thread context: The minimal collection of values stored in registers and
memory, used for the execution of a series of instructions (i.e., processor
context, state).
• Process context: The minimal collection of values stored in registers and
memory, used for the execution of a thread (i.e., thread context, but now
also at least MMU register values).

Introduction to threads
Processes Threads

Context switching
Observations
1. Threads share the same address space. Thread context switching can be
done entirely independent of the operating system.
2. Process switching is generally (somewhat) more expensive as it involves
getting the OS in the loop, i.e., trapping to the kernel.
3. Creating and destroying threads is much cheaper than doing so for
processes.

Introduction to threads
Processes Threads

Why use threads


Some simple reasons
• Avoid needless blocking: a single-threaded process will block when doing
I/O; in a multithreaded process, the operating system can switch the CPU
to another thread in that process.
• Exploit parallelism: the threads in a multithreaded process can be
scheduled to run in parallel on a multiprocessor or multicore processor.
• Avoid process switching: structure large applications not as a collection of
processes, but through multiple threads.

Introduction to threads
Processes Threads

Avoid process switching


Avoid expensive context switching

Trade-offs
• Threads use the same address space: more prone to errors
• No support from OS/HW to protect threads using each other’s memory
• Thread context switching may be faster than process context switching

Introduction to threads
Processes Threads

The cost of a context switch


Consider a simple clock-interrupt handler
• direct costs: actual switch and executing code of the handler
• indirect costs: other costs, notably caused by messing up the cache

What a context switch may cause: indirect costs

(a) before the context switch


(b) after the context switch
(c) after accessing block D.

(a) (b) (c)

Introduction to threads
Processes Threads

A simple example in Python

1 from multiprocessing import Process


2 from time import *
3 from random import *
4
5 def sleeper(name):
6 t = gmtime()
7 s = randint(1,20 )
8 t x t = str(t.tm_min)+ ’ :’ +str(t.tm_sec)+ ’ ’+name+’ i s going t o s l e e p f o r ’ + s tr (s )+ ’ seconds’
9 pr i nt(txt)
10 s le e p (s )
11 t = gmtime()
12 t x t = str(t.tm_min)+ ’:’+str(t.tm_sec)+ ’ ’+name+’ has woken up’
13 p r i nt(txt)
14
15 if name == ’ main ’ :
16 p = Process(ta rge t=s lee pe r, args=(’eve’ ,))
17 q = Process(ta rge t=s lee pe r, args=(’bob’ ,))
18 p.start( ); q.start()
19 p . j o i n ( ) ; q . j o in ( )

40:23 eve i s going t o sleep f o r 14 seconds


40:23 bob i s going t o sleep f o r 4 seconds
40:27 bob has woken up
40:37 eve has woken up

Introduction to threads
Processes Threads

A simple example in Python


1 from multiprocessing import Process
2 from thread in g import Thread
3
4 shared_x = randint(10,99)
5
6 def sleeping(name):
7 global shared_x
8 t = gmtime(); s = randint(1,20)
9 t x t = str(t.tm_min)+’ :’+str(t.tm_sec)+ ’ ’+name+’ i s going t o s le e p f o r ’ + s tr (s )+ ’ seconds’
10 pr int (txt)
11 s le e p (s )
12 t = gmtime(); shared_x = shared_x + 1
13 t x t = str(t.tm_min)+ ’ :’ +str(t.tm_sec)+ ’ ’+name+’ has woken up , seeing shared x being ’
14 print(txt+str(shared_x) )
15
16 def sleeper(name):
17 sleeplis t = l i s t ( )
18 print(name, ’ s e e s shared x be ing’ , shared_x)
19 f o r i i n range(3):
20 subsleeper = Thread(target=sleeping, args=(name+’ ’ + s t r ( i ) , ) )
21 sleeplist.append(subsleeper)
22
23 for s i n s l e e p l i s t : s . s t a r t ( ) ; for s i n s l e e p l i s t : s.join( )
24 print(name, ’ s e e s shared x b e in g’ , shared_x)
25
26 if name == ’ main ’ :
27 p = Process(ta rge t=s lee pe r, args=(’eve’,))
28 q = Process(ta rge t=s lee pe r, args=(’bob’ ,))
29 p.start( ); q.start()
30 p . j o i n ( ) ; q . j o in ( )
Introduction to threads
Processes Threads

A simple example in Python


eve sees shared x being 71
53:21 eve 0 i s going t o sleep f o r 20 seconds
bob sees shared x being 84
53:21 eve 1 i s going t o sleep f o r 15 seconds
53:21 eve 2 i s going t o sleep f o r 3 seconds
53:21 bob 0 i s going t o sleep f o r 8 seconds
53:21 bob 1 i s going t o sleep f o r 16 seconds
53:21 bob 2 i s going t o sleep f o r 8 seconds
53:24 eve 2 has woken u p , seeing shared x being 72
53:29 bob 0 has woken u p , seeing shared x being 85
53:29 bob 2 has woken u p , seeing shared x being 86
53:36 eve 1 has woken u p , seeing shared x being 73
53:37 bob 1 has woken u p , seeing shared x being 87
bob sees shared x being 87
53:41 eve 0 has woken u p , seeing shared x being 74
eve sees shared x being 74

Introduction to threads

You might also like