5.Threads
5.Threads
5.Threads
Process Concept
• Classically, processes are executed programs that have ...
• Resource Ownership
• Scheduling/Execution
2
Control Blocks
• Information associated with each process: Process Control
Block
• Accounting information
• Program counter
• CPU registers
3
Control Blocks
Process ID (PID)
Parent PID
…
Program Counter
…
Registers
0
Single & Multithreading
•Process
Each thread has
• An execution stack
6 PT / FF 2014
Why Multithreading
• Advantages
• Disadvantages
• Coordinated
termination
• New thread gets its own register context and stack space
• Finish: When a thread completes, its register context and stacks are
deallocated.
Thread
Dispatchi
ng Thread T1
Interrupt or system call Thread T2
executing
Save state into TCB1 ready or
waiting
Reload state from TCB2
Examples
◦ Windows XP/2000
◦ Solaris
◦ Linux
◦ Tru64 UNIX
◦ Mac OS X
User vs. Kernel Thread
Multithreading Models
User Thread – to - Kernel Thread
Many-to-One
One-to-One
Many-to-Many
Many-to-One
Many user-level threads
mapped to single kernel
thread
multiprocessors.
drawback is, creating a user thread requires creating the
attr Is the thread attribute object specifying the attributes for the
thread that is being created. If attr is NULL, the thread is created
with default attributes.
start Is the main function for the thread; the thread begins
executing user code at this address.
pthread_t pthread_self()
/* Main block now waits for both threads to terminate, before it exits
If main block exits, both threads exit, even if the threads have not
finished their work */
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
Example code but not complete
exit(0);