Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Reading:
Silberschatz
chapter 5
Additional Reading:
Stallings
chapter 4
Understanding Linux/Unix Programming, Bruce Molay, Prentice-Hall, 2003.
EEL 358 1
Outline
¾ Process and Threads
¾ Multithreading
¾ Motivation
¾ Advantages
¾ RPC using Thread(s)
¾ User-Level Threads
¾ Kernel-Level Threads
¾ Combined Approaches
¾ Phtreads
¾ Threading Issues
System Call Semantics
Thread Cancellation
Signal Handling
Thread Pools
Thread Specific Data
¾ Introduction: Linux, Win32, Solaris and Java Threads
EEL 358 2
Process and Threads
¾ Process Management
Resource Ownership
Memory, I/O channel, I/O devices and file
Scheduling/Execution
Execution state and priority
¾ Independent treatment by OS
¾ Unit of dispatching
Thread
Lightweight Process (LWP, even KLT)
¾ Unit of resource ownership
Process
Task
EEL 358 3
Multithreading
Multiple threads of execution within single process
¾ Thread
Basic unit of CPU utilization, consisting of
PC
Register set
Stack
EEL 358 4
Multithreading
EEL 358 5
Examples - Motivation
¾ Web Browser
One thread to display images
Other thread retrieves data from network
¾ Word Processor
One thread for responding to keystrokes
Other thread for spelling and grammar checking
Other thread for displaying graphics
EEL 358 7
Thread
¾ An execution state (running, ready, blocked)
¾ Saved thread context/state when not running
¾ Has an execution stack
¾ Some per-thread static storage for local variables
¾ Access to the memory and resources of its
process
Shared by all threads of the process
EEL 358 8
Multithreading
• All of the threads of a process share the state and resources of process
• They reside in same address space and have access to same data
EEL 358 9
Advantage Threads!
¾ Takes far less time to create a new thread in
existing process than a new process; Factor 10
EEL 358 10
Functionality
¾Key States – RRB but no suspend
¾Operations
Spawn
Spawn another thread
Block
Unblock
Finish
EEL 358 11
RPC Using Single Thread
EEL 358 12
RPC Using Threads
EEL 358 13
Example - Multithreading
EEL 358 14
User-Level Threads
¾ Thread Management → Application
¾ New threads within the same process
¾ Thread Library
¾ Control, Library ↔ Thread
¾ Context → user reg, pc & sp
EEL 358 15
User-Level Threads
EEL 358 16
ULT Vs KLT
¾ Advantages of ULTs
Thread switching → Kernel mode privileges, less overhead
Scheduling can be application specific
Thread libraries → application utilities, Can run on any OS
¾ Disadvantages of ULTs
High blocking, OS → many system calls are blocking, all
threads in process are blocked
Pure ULT strategy → cannot take advantage of multiprocessing
¾ Solutions?
Jacketing
Writing application as multiple processes rather than threads
EEL 358 17
Kernel-Level Threads
¾ Thread Management → Kernel
¾ API to the kernel thread facility
EEL 358 18
Kernel-Level Threads
EEL 358 19
Combined Approaches
EEL 358 21
Threading Issues
¾ Semantics of System Calls
fork()
exec()
¾ Thread Cancellation
Asynchronous Cancellation
Deferred Cancellation
EEL 358 22
Threading Issues
¾ Signal Handling
To thread to which its applicable
To every thread
To certain threads
To a specific thread assigned
¾ Thread Pools
Sit & Wait
Work, Return to pool
Faster than waiting to create a thread
Limits # that can exists at any point of time
EEL 358 23
Pthreads
¾ A POSIX standard API for thread creation and sync
EEL 358 24
Example: Running 2 functions simultaneously
Single-threaded version
Duration: 10 seconds
Source code:
/* hello_single.c - a single threaded hello world program */
#include <stdio.h>
#define NUM 5
main()
{
void print_msg(char *);
print_msg("hello");
print_msg("world\n");
}
Screen output:
void print_msg(char *m) [ajaykr@lib ~]$ ./hello_single
{ hellohellohellohellohelloworld
int i;
for(i=0 ; i<NUM ; i++){ world
printf("%s", m); world
fflush(stdout); world
sleep(1);
} world
}
EEL 358 25
Example: Running 2 functions simultaneously
Multi-threaded version
Duration: 5 seconds
Source code:
/* hello_multi.c - a multi-threaded hello world program */
#include <stdio.h>
#include <pthread.h>
#define NUM 5
main()
{
pthread_t t1, t2; /* two threads */
EEL 358 26
Linux Threads
¾ Linux refers to them as tasks rather than threads
EEL 358 27
Win32 Threads
¾ Primarily API for Win XP, NT, 2000, 98, 95
¾ windows.h
¾ Thread creation → CreateThread(), …
¾ Windows XP application → Separate Process
¾ Components of thread
- a thread id
- register set
- separate user and kernel stacks
- private data storage area
¾ 1:1 Mapping, fiber library
EEL 358 28
Solaris 2 Threads
EEL 358 29
Solaris Process
EEL 358 30
Java Threads
¾ Java → language level support
¾ Thread Creation
Extending Thread class, new class → thread class
Implementing the Runnable interface
EEL 358 31
Java Thread States
EEL 358 32
Questions
Provide examples where multithreading does not provide
better performance than single-threaded solutions