CSI3131 Module 3
CSI3131 Module 3
Goal:
▪ Understanding the concept of threads and
its relationship to the process
▪ Understand how the operating systems
manage and use the threads.
1
Topics
▪ Examples of threads
2
Characteristics of processes
▪ Resource ownership unit - a process has:
▪ an addressable virtual space containing the
process image
▪ other resources (files, I / O units ...)
▪ Execution unit (dispatching) - a process is
executed along a path among several
programs
▪ execution nested among the execution of
several processes
▪ the process has an execution state and a
priority for scheduling
3
Process
4
Process characteristics
5
Resource ownership unit
Process image
BCP
▪ Related to the following
Identification Battery
components of the Structures
process image data
▪ The part of the PCB which State of Battery
contains identification processor user
and resource structures Ordonnan.
▪ Memory containing the Memory
execution code user
▪ Memory containing global Coded Battery
data execution core
Data
global
6
Execution unit
Process image
BCP
▪ Related to the Identification Battery
following components Structures
of the process image data
State of Battery
▪ PCB processor user
▪ Processor state Ordonnan.
▪ Scheduling structure Memory
user
▪ Stack
Coded Battery
execution core
Data
global
7
Topics
▪ Examples of threads
8
Threads = lightweight processes
9
Example
10
Single and multi-threaded processes
11
Threads and processes [Stallings]
12
Thread
13
Why threads
14
Switching between threads is less
expensive than switching between
processes
▪ A process has memory, files, other
resources
▪ Changing from one process to another
involves saving and restoring the state of it
all.
▪ Switch from one thread to another in the
same process is much simpler, involves
saving CPU registers, stack, and little else
15
Communication is also less expensive
between threads than between
processes.
16
Creation is less expensive
17
Kernel and user threads
18
User and kernel threads (kernel)
19
Mixed solutions: user and kernel threads
20
Multiple user threads for a kernel thread:
the user controls the threads
Thread noyau
23
Multithreads and monothreads
24
Topics
▪ The execution thread in the processes
▪ Examples of threads
25
Threading challenges
26
Fork () and exec () semantics
27
Cancellation of thread
28
Wire groupings (Thread Pools)
29
thread-specific data
30
Topics
▪ The execution thread in the processes
▪ Examples of threads
31
Examples of thread libraries
▪ Pthreads
▪ Win32
▪ Java threads
32
Pthreads
33
34
Programming exercise with threads
Goal: Write a matrix multiplication program with several threads, to
take advantage of several CPUs.
35
Matrix multiplication with multi-threads
Idea:
• creation of 6 threads
• each thread solves 1/6 of matrix C
• wait for the end of the 6 threads
• matrix C can now be used
Thread 0
Thread 1
Thread 2
Thread 3
Thread 4
Thread 5
36
Let's go!
pthread_t tid [6];
pthread_attr_t attr;
int i;
37
Let's go!
void *worker (void * param)
{
int i, j, k;
int id = * ((int *) param); / * interpret the param
as pointer to an integer * /
int low = id * n / 6;
int high = (id + 1) * n / 6;
38
Let's go!
Does it work?
• Do we need to pass A, B, C and n as a parameter?
• no, they are in shared memory, we are good
• Have the IDs been passed?
• not really, pointers all receive the same address.
int id[6];
.
.
for (i = 0; i <6; i ++) / * create the working
threads * / {
id [i] = i;
pthread_create (& tid [i], & attr, worker, & id
[i]);
}
Now does it work?
• should, ...
39
Win32 thread API
// create a thread
ThreadHandle = CreateThread (
NULL, // default security attributes
0, // default stack size
Summation, // function to execute
& Param, // parameter to thread function
0, // default creation flags
& ThreadId); // returns the thread ID
if (ThreadHandle! = NULL) {
WaitForSingleObject (ThreadHandle, INFINITE);
CloseHandle (ThreadHandle);
printf ("sum =% d \ n", Sum);
}
40
Java Threads
▪ Java threads are created with a call to
the start () method of a class that
▪ Extends the Thread class, or
▪ Uses the Runnable interface:
public interface Runnable
{
public abstract void run ();
}
▪ Java threads are an important part of
the Java language which offers a
feature rich API.
41
Extend the Thread class
class Worker1 extends Thread
{
public void run () {
System.out.println ("I Am a Worker Thread");
}
}
43
Attach Threads
class JoinableWorker implements Runnable
{
public void run () {
System.out.println ("Worker working");
}
}
try{task.join (); }
catch (InterruptedException ie) {}
...
// now interrupt it
Thrd.interrupt ();
45
Examples of I/O Thread Implementation
▪ Windows XP
▪ Linux
▪ Java
46
Windows XP Threads
▪ One to one model
▪ Each thread contains
▪ A thread identifier (id)
▪ A set of registers
▪ Different user and kernel stacks
▪ Private data memory
47
Windows XP Threads
48
Java threads
Java threads are managed by the JVM
49
The producer - consumer pb
50
Communication buffers
Prod Prod
Cons Cons
b[6] b[3] or
in: 1st pos. out: 1st
b[5] b[4] free pos. full
out: 1st pos.
full
producerThread.start ();
consumerThread.start ();
}
while (true) {
SleepUtilities.nap ();
message = new Dated();
System.out.println ("Producer produced" + message);
while (true) {
SleepUtilities.nap ();
// consume an item from the buffer
System.out.println ("Consumer wants to consume.");
متشکرم