03 Process Part2
03 Process Part2
Amir H. Payberah
[email protected]
Nov. 7, 2023
Threads
1 / 42
Thread
A basic unit of CPU utilization.
https://tinyurl.com/e8crhtne
2 / 42
Threads (1/2)
3 / 42
Threads (1/2)
3 / 42
Threads (1/2)
3 / 42
Threads (2/2)
• Update display
• Fetch data
• Spell checking
• Answer a network request
4 / 42
Threads - Example
5 / 42
Threads Benefits
6 / 42
Threads Benefits
6 / 42
Threads Benefits
6 / 42
Threads Benefits
6 / 42
Multi-core Programming
7 / 42
Multi-core Systems
8 / 42
Multi-core Systems
8 / 42
Multi-core Systems
I Multi-threaded programming
• Improves concurrency and more efficient use of multiple cores.
8 / 42
Concurrency vs. Parallelism (1/2)
I Concurrency: supporting more than one task by allowing all the tasks to
make progress.
9 / 42
Concurrency vs. Parallelism (1/2)
I Concurrency: supporting more than one task by allowing all the tasks to
make progress.
• A scheduler providing concurrency.
9 / 42
Concurrency vs. Parallelism (1/2)
I Concurrency: supporting more than one task by allowing all the tasks to
make progress.
• A scheduler providing concurrency.
9 / 42
Concurrency vs. Parallelism (2/2)
10 / 42
Concurrency vs. Parallelism (2/2)
10 / 42
Types of Parallelism
I Data parallelism
• Distributes subsets of the same data across multiple cores, same operation
on each.
11 / 42
Types of Parallelism
I Data parallelism
• Distributes subsets of the same data across multiple cores, same operation
on each.
I Task parallelism
• Distributes threads across cores, each thread performing unique operation.
11 / 42
Multi-threading Models
12 / 42
User Threads and Kernel Threads
13 / 42
User Threads and Kernel Threads
13 / 42
Multi-Threading Models
I Many-to-One
I One-to-One
I Many-to-Many
14 / 42
Many-to-One Model
15 / 42
Many-to-One Model
15 / 42
Many-to-One Model
15 / 42
Many-to-One Model
15 / 42
One-to-One Model
16 / 42
One-to-One Model
16 / 42
One-to-One Model
16 / 42
One-to-One Model
16 / 42
One-to-One Model
16 / 42
Many-to-Many Model
17 / 42
Many-to-Many Model
17 / 42
Many-to-Many Model
17 / 42
Thread Libraries
18 / 42
Thread Libraries (1/2)
I Thread library provides programmer with API for creating and managing
threads.
19 / 42
Thread Libraries (1/2)
I Thread library provides programmer with API for creating and managing
threads.
19 / 42
Thread Libraries (2/2)
I Pthread
• Either a user-level or a kernel-level library.
20 / 42
Thread Libraries (2/2)
I Pthread
• Either a user-level or a kernel-level library.
I Windows thread
• Kernel-level library.
20 / 42
Thread Libraries (2/2)
I Pthread
• Either a user-level or a kernel-level library.
I Windows thread
• Kernel-level library.
I Java thread
• Uses a thread library available on the host system.
20 / 42
21 / 42
Pthreads
22 / 42
Pthreads
22 / 42
Pthreads
22 / 42
Pthreads
22 / 42
Thread ID
23 / 42
Thread ID
I The PID is assigned by the Linux kernel, and TID is assigned in the
Pthread library.
23 / 42
Thread ID
I The PID is assigned by the Linux kernel, and TID is assigned in the
Pthread library.
I Represented by pthread t.
23 / 42
Thread ID
I The PID is assigned by the Linux kernel, and TID is assigned in the
Pthread library.
I Represented by pthread t.
#include <pthread.h>
pthread_t pthread_self(void);
23 / 42
Creating Threads
#include <pthread.h>
24 / 42
Creating Threads
#include <pthread.h>
24 / 42
Terminating Threads
#include <pthread.h>
25 / 42
Terminating Threads
#include <pthread.h>
#include <pthread.h>
25 / 42
Joining and Detaching Threads
I Joining allows one thread to block while waiting for the termination
of another.
#include <pthread.h>
[https://computing.llnl.gov/tutorials/pthreads/#Joining]
26 / 42
Joining and Detaching Threads
I Joining allows one thread to block while waiting for the termination
of another.
I You use join if you care about what value the thread returns when
it is done, and use detach if you do not.
#include <pthread.h>
[https://computing.llnl.gov/tutorials/pthreads/#Joining]
26 / 42
A Threading Example
void *thread_func(void *message) {
printf("%s\n", (const char *)message);
return message;
}
int main(void) {
pthread_t thread1, thread2;
const char *message1 = "Thread 1";
const char *message2 = "Thread 2";
return 0;
}
27 / 42
Implicit Threading
28 / 42
Implicit Threading
29 / 42
Implicit Threading
29 / 42
Implicit Threading
29 / 42
Thread Pools
30 / 42
Thread Pools
30 / 42
Thread Pools
30 / 42
Fork-Join (1/2)
31 / 42
Fork-Join (2/2)
32 / 42
Fork-Join (2/2)
32 / 42
OpenMP (1/2)
33 / 42
OpenMP (1/2)
33 / 42
OpenMP (1/2)
33 / 42
OpenMP (2/2)
#include <omp.h>
#include <stdio.h>
/* sequential code */
/* sequential code */
return 0;
}
34 / 42
Thread Cancellation
35 / 42
Thread Cancellation (1/4)
36 / 42
Thread Cancellation (1/4)
36 / 42
Thread Cancellation (1/4)
36 / 42
Thread Cancellation (1/4)
36 / 42
Thread Cancellation (1/4)
36 / 42
Thread Cancellation (2/4)
int counter = 0;
pthread_t tmp_thread;
int main() {
pthread_t thread1, thread2;
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
}
37 / 42
Thread Cancellation (3/4)
while (1) {
printf("thread number two\n");
sleep(1); // sleep 1 second
}
}
38 / 42
Thread Cancellation (4/4)
if (counter == 2) {
pthread_cancel(tmp_thread);
pthread_exit(NULL);
}
}
}
39 / 42
Summary
40 / 42
Summary
41 / 42
Summary
41 / 42
Summary
41 / 42
Summary
41 / 42
Summary
I Implicit threading
41 / 42
Summary
I Implicit threading
I Thread cancellation
41 / 42
Questions?
Acknowledgements
Some slides were derived from Avi Silberschatz slides.
42 / 42