Lecture 11: Threads-3
Operating Systems
Created by:
Dr. Salem Alqahtani
1
Dr. Aymen Trigui
Dr. Mudassir Rafi
Thread Creation
pthread_t threadID;
pthread_create (&threadID, *attr, methodName, *para);
• 1st argument is the ID of the new thread
• 2nd argument is a pointer to pthread_attr_t
• 3rd argument is thread function/method
• 4th argument is a pointer to the arguments for the thread’s
method/function
Thread joins and exits
• Joining a non-detached thread by using
• int pthread_join ( pthread_t thread, void **retval)
(All threads are created non-detached by default, so they are “joinable” by
default)
• Exit from threads:
• If threads use exit( ), the process terminates.
• A thread (main, or another thread ) can exit by calling pthread_exit( ), this
does not terminate the whole process.
• More information about Pthread programming
• [Link]
Case Study: Java Threads
To create a thread in Java, do one of the following:
• Create a class that extends Thread
(which implements Runnable, but has all of the infrastructure for running
as a separate thread)
• Create a class that implements Runnable (and then create a Thread to
run its object)
• You must explicitly call start to run a thread.
[Link]
Thread Life-times
A thread object exists when it is constructed, but it doesn't start running until the start
method is called. A thread completes (or dies) when its run method finishes or when it
throws an exception. The object representing this thread can still be accessed.
Issues: Process Creation in a Thread
• What will happen if one thread in a process call fork( ) to create a
new process?
• Does fork() duplicate only the calling thread or all threads?
• How many threads in the new process?
• Duplicate only the invoking thread
• exec(): will load another program
• Everything will be replaced anyway
• Duplicate all threads
• If exec() is not the next step after forking
Issues: Thread Cancellation
Examples
• Threads search in parallel of database:
• one finds → other stop
• Stop fetching web contents (images)
• Asynchronous cancellation
• One thread forcefully terminate target thread immediately
• Thread resources and data consistency
• Deferred cancellation
• Wait for self cleanup → cancellation safety points
Signal Handling
Signals
• Indicator of particular events → delivered to a process
• E.g., ready of I/O or forceful termination (Ctrl+C)
• Synchronously vs. asynchronous
• Which threads to notify?
• All threads
• Single thread to which the signal applies (illegal memory)
• Subset of threads: thread set what it wants (mask)
• Thread handler: kernel default or user-defined
Multithreaded Programs
Use multiple threads to improve performance
Thread Pool
• Recall web server example,
• We created a thread for every request
• This is better than creating a process, but still time consuming
• No limit is put on the number of threads
• Pool of threads
• Create some number of treads at the startup
• These threads will wait to work and put back into pool
• Advantages:
• Usually slightly faster to service a request with an existing thread than create a new
thread
• Allows the number of threads in the application(s) to be bound to the size of the pool
• Adjust thread number in pool
• According to usage pattern and system load
Performance of Threaded Programs
• Suppose that each request
• Takes X seconds for computation; and
• Takes Y seconds for reading data from I/O disk
• For single-thread program/process
• A single CPU & single disk system
• What is the maximum throughput (i.e., the number of requests can
be processed per second)?
• Example: suppose that each request takes
• 2ms for computation
• 8ms to read data from disk
1000/10ms = 100
Performance of Threaded Programs (cont)
• Multi-thread performance improvement
• Single CPU & single disk system
• How many threads should be used?
• What is the maximum throughput (i.e., the number of requests can
be processed per second)?
Example: suppose that each request takes
2ms for computation
8ms to read data from disk
1000/8ms = 125
Assuming that we have 8 cores and 1 disk
Performance of Threaded Programs (cont)
• What about m-CPU and n-disk system
• Maximum throughput and # of threads? (X: computation time, Y: IO time for each task)
• Throughput → 1 / max(X/m, Y/n)
• # of Threads →
• if (X/m < Y/n) , # = n + m’
IO Is the bottleneck
where m’ = min{k| X/k <= Y/n, 1<=k<=m};
• Similarly, if (X/m > Y/n), # = m + n’ CPU Is the bottleneck
where n’ = min{k| X/m>= Y/k, 1<=k<=n};
• Other issues:
• When I/O disk is bottleneck, adding more CPUs will NOT help improve the throughput
• What about heterogeneous disks and CPUs?!