Threads (Chapter 4) : References
Threads (Chapter 4) : References
Lecture 4
Threads
(chapter 4)
Dr. İbrahim Körpeoğlu
http://www.cs.bilkent.edu.tr/~korpe
References
• The slides here are adapted/modified from the textbook and its slides:
Operating System Concepts, Silberschatz et al., 7th & 8th editions,
Wiley.
REFERENCES
• Operating System Concepts, 7th and 8th editions, Silberschatz et al.
Wiley.
• Modern Operating Systems, Andrew S. Tanenbaum, 3rd edition, 2009.
1
Outline
• Overview • Reentrency
• Multithreading Models • Thread specific data
• Thread Libraries
• Threading Issues
• Operating System Examples
• Windows XP Threads
• Linux Threads
Objectives
2
Threads and Thread Usage
data data
CPU
blocks blocks
blocks
run enough
3
a multithreaded process’ execution flows:
threads
Instructions of the Program
main()
Thread0
Thread2
Thread1
time
Multithreading Concept
CPU
4
Multithreading Concept
Process Process
Schedulable Entities
We can select one of them and run
Multithreading Concept
thread1 thread2 thread3 thread4
function1(…)
{
….
{
function2(…)
{
….
{
main()
{ ….
thread_create (function1 ,…);
….
thread_create (function2, …);
….
thread_create (function1, …);
….
}
CS342 Operating Systems - Spring 2009 10 İbrahim Körpeoğlu, Bilkent University
5
Single and Multithreaded Processes
Benefits
• Responsiveness
– One thread blocks, another one runs.
– One thread may always wait for the user
• Resource Sharing
– Threads can easily share resources
• Economy
– Creating a thread is fast
– Context switching among threads may be faster
• Scalability
– Multiprocessors can be utilized better
6
Multicore Programming
7
Concurrent Execution on a Single-core System
8
Threading Support
Thread Library
9
Kernel Support of Threads
• Examples
– Windows XP/2000
– Solaris
– Linux
– Tru64 UNIX
– Mac OS X
Multithreading Models
• How the threads created by the process are actually run (mapped into
kernel threads)?
• What is the relationship between user level threads and kernel level
threads?
– Many-to-One
– One-to-One
– Many-to-Many
10
Many-to-One Model
Many-to-One Model
Implementing Threads in User Space
• Kernel does not have to be aware
Thread
of multithreading
Process A Process B
• Library is used to create and
manage, schedule threads.
11
One-to-one Model
One-to-one Model
Implementing Threads in Kernel
• Kernel is aware of threads. It is
managing threads, schedules
Process A Process B
threads.
• Need system calls to create threads
• Thread switching and creation is
more costly (-)
• Blocking system calls are not
problem anymore. (+)
• Kernel can stop a long running
thread and run another thread. No PCB A
need for explicit request from a PCB B
thread to be stopped. (+) Kernel process table
• Any thread function requires a
system call: (-) Thread table
– ex: thread_wait
12
Many-to-Many Model
Two-level Model
13
Thread Libraries
Pthreads Library
14
Pthreads Example
• The program will just create a new thread to do a simple computation. The
new thread will get a parameter, an integer value, and will sum all integers
from 1 up to that value. N
– sum = 1+2+…+parameter_value sum = ∑ i
i =1
• The main thread will wait until sum is computed into a global variable.
Pthreads Example
#include <pthread.h>
#include <stdio.h>
15
Pthreads Example
int main(int argc, char *argv[]){
pthread_t tid; /* id of the created thread */
pthread_attr_t attr; /* set of thread attributes */
if (argc != 2) {
fprintf (stderr, “usage: a.out <value>\n”);
return -1;
}
if (atoi(argv[1]) < 0) {
fprintf (stderr, “%d must be >= 0\n”, atoi(argv[1]);
return -1;
}
pthread_attr_init (&attr);
pthread_create (&tid, &attr, runner, argv[1]);
pthread_join (tid, NULL);
printf (“sum = %d\n”, sum);
}
CS342 Operating Systems - Spring 2009 31 İbrahim Körpeoğlu, Bilkent University
Pthreads Example
int upper;
upper = atoi(param);
sum = 0;
pthread_exit(0);
}
16
Pthreads Example
int main(…) thread1 thread2
{
…
….
pthread_create(&tid,…,runner,..);
wait
pthread_join(tid);
runner (…)
{
….
sum = …
pthread_exit();
{
CS342 Operating Systems - Spring 2009 33 İbrahim Körpeoğlu, Bilkent University
• You can put the above code into a .c file, say mysum.c
• In order to use the Pthreads functions, we need to include pthread.h header
file in our program (as shown in previous slides)
• We also need to link with the pthread library (the Pthreads API functions are
not implemented in the standard C library). The way to do that is using the –l
option of the C compiler. After –l you can provide a library name like pthread.
17
Java Threads
Threading Issues
18
Semantics of fork() and exec()
Thread Cancellation
19
Signal Handling
20
delivering signal (notifying)
CTRL-C
Keyboard
kill program
process id = 3405
Keyboard
21
Some Signals
Thread Pools
22
Scheduler Activations
• Kernel threads are good, but they are slower if we create short threads
too frequently, or threads wait for each other too frequently.
• Is there a middle way?
– Schedule Activation
• Goal is mimic kernel threads at user level with some more kernel
support. But kernel will not create another thread for each user thread
(M:1 or M:M model).
Process
Run-time
System upcall handler
(i.e. thread library) Thread schedules
table another thread
library registers a handler makes system call
(upcall handler)
when kernel runs the upcall handler
process/thread (i.e. makes an upcall; activates
is started Kernel the user level scheduler)
kernel detects that I/O is finished
Kernel initiates I/O
and blocks the thread kernel informs the library via upcall
23
From Singlethread to Multithreaded
func1(…) {
….
status = …
do_something_based_on(status);
}
func2(…) {
…
status = …
do_something_based_on(status);
}
main() {
….
func1 (…);
func2 (…);
}
CS342 Operating Systems - Spring 2009 48 İbrahim Körpeoğlu, Bilkent University
24
From Singlethread to Multithreaded
int status;
• We can have problem here.
func1(…) {
…. • Just after func1 of thread 1
status = … updated status, a thread
do_something_based_on(status); switch may occur and 2nd
} thread can run and update
status.
func2(…) {
…
status = … • Then thread 1 will run
do_something_based_on(status); again, but will work with a
} different status value.
main() { Wrong result!
….
thread_create(…, func1, …);
thread_create(…, func2, …);
}
CS342 Operating Systems - Spring 2009 49 İbrahim Körpeoğlu, Bilkent University
• Scope of variables:
– Normally we have: global, local
– With threads we want: global, local, thread-local
• Therefore thread API has special functions that can be used to create
such variables – data.
– This is called thread specific data.
25
Thread Specific Data
26
Operating System Examples
• Windows XP Threads
• Linux Thread
Windows XP Threads
27
Windows XP Threads
Linux Threads
• clone() allows a child task to share the address space of the parent
task (process)
28
Clone() and fork()
user program
sys_fork() sys_clone(){
{ ….
… }
kernel }
End of lecture
29