Threads
Threads
Operating Systems
Created by:
Dr. Salem Alqahtani
Dr. Aymen Trigui
Dr. Mudassir Rafi
1
Processes
● Recall that …
• A process consists of:
• An address space ( defining all the code and data pages )
• The code for the running program
• The data for the running program
• A set of OS resources
• Open files, network connections ..
• Execution state ( PC, SP, regs, etc)
• Processes are completely isolated from each other
Processes
Some Issues with the Processes
● Creating a new process is costly because of new address space and data
structures that must be allocated and initialized
● Process
• Multiple threads
• For parallelism and efficient close cooperation
Kernel has
NO
knowledge
of threads!
User-Level Threads
● Each process needs its own private thread table to keep track of the threads in
that process.
● The thread-table keeps track of the per-thread items (program counter, stack
pointer, register, state..)
● When a thread does something that may cause it to become blocked locally
(e.g. wait for another thread), it calls a run-time system procedure.
● If the thread must be put into blocked state, the procedure performs thread
switching
User-Level Threads
● Advantages
• Fast thread switching: no kernel activity involved
• Customized/flexible thread scheduling algorithm
• Application portability: different machines with library
● Problems
• OS kernel knows only process
• Blocking system calls: one thread blocks all activities
• All threads share one CPU → cannot use multi-proc./core
● Example thread libraries
• POSIX Pthreads
• Java threads
Kernel-Level Threads
● Supported directly by OS: kernel performs thread creation, scheduling &
management in kernel space
Kernel-Level Threads
● Scheduling entities: threads
• When a thread blocks, the kernel may choose another thread from the
same or different process
● Thread operations
• System calls → cost