Lecture 10: Threads-2
Operating Systems
Created by:
Dr. Salem Alqahtani
Dr. Aymen Trigui
Dr. Mudassir Rafi
1
Mapping: User → Kernel Threads
• Many-to-one
• Many user-level threads → one kernel thread.
• Thread management is done by the thread library in user space, so it is
efficient.
• The entire process will block if a thread makes a blocking system call.
• Because only one thread can access the kernel at a time, multiple threads
are unable to run in parallel on multiprocessors.
• One-to-One
• One user activity → one kernel thread; Limited number of kernel threads
• Provide more conurrency than the many-to-one model by allowing another
thread to run when a thread makes a blocking system call.
• Because the overhead of creating kernel threads can burden the
performance of an application.
Mapping: User → Kernel Threads
• Many-to-Many
• Many user level threads → many kernel threads
• Multiplexer many user threads to a smaller or equal number of
kernel threads.
• The number of kernel threads may be specific to either a
particular application or particular machine.
• Aslo, when a thread performs a blocking system call, the kernel
can schedule another thread for execution
Light-Weight Process (LWP)
• Two-level threading approach
• Kernel + user
• Lightweight process (LWP): intermediate structure
• Virtual processor: can execute user-level threads
• Each LWP attaches to a kernel thread
• Multiple user-level threads → a single LWP
• Normally from the same process
• A process may be assigned multiple LWPs
• Operating System schedules kernel threads (hence, LWPs) on the CPU
LWP in Solaris (from Dave, UK)
Scheduler Activations
• Process/thread → LWPs → kernel threads → CPUs
• Based on priority, resource requirements etc.
• Upcall: kernel calls thread package (in user space)
• Events to invoke upcall
• A thread make a blocking system calls
• A blocking system call complete returns
• To ask user-level thread scheduler (runtime systems) to select the next
runnable thread
• Process: maintain appropriate number kernel threads
Thread Libraries
• Provide programmers with API for creating and managing threads
• Two primary ways of implementing
• User-level library
• Entirely in user space
• Everything is done using function calls (no system calls)
• Kernel-level library supported by the OS
• Code and data structures for threads are in kernel space
• Function calls result in system calls to kernel
• Examples:
• POSIX Threads Pthread (either user or kernel)
• Java threads (JVM uses host system threads)
Case Study: POSIX Thread
• POSIX
• Portable Operating System Interface [for Unix]
• Standardized programming interface
• Pthreads
• Thread implementations adhering to POSIX standard
• APIs specify behavior of the thread library: defined as a set of C types and procedure calls
• Common in UNIX OS (Solaris, Linux, Mac OS X)
• Utilizes one-to-one mapping
• Support for thread creation and synchronization
https://computing.llnl.gov/tutorials/pthreads/
Pthreads: Thread/Synchronization APIs