1 / 24

Chapter 4: Threads

Chapter 4: Threads. Single and Multithreaded Processes. Benefits. Responsiveness One part of a program can continue running even if another part is blocked Resource Sharing Threads of the same process share the same memory space and resources. Economy

atara
Download Presentation

Chapter 4: Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: Threads

  2. Single and Multithreaded Processes

  3. Benefits • Responsiveness • One part of a program can continue running even if another part is blocked • Resource Sharing • Threads of the same process share the same memory space and resources. • Economy • Much less time consuming to create and manage threads than processes • Solaris 2: creating a process is 30 times slower than creating a thread, context switching is 5 times slower. • Scalability • Each thread can run in parallel on a different processor

  4. Example of Use • Multithreaded Server (for instance a Web Server)

  5. Multicore Programming & Multithreading • Multicore systems putting pressure on programmers, challenges include • Dividing activities • Balance • Data splitting • Data dependency • Testing and debugging

  6. Concurrent Execution on a Single-core System

  7. Parallel Execution on a Multicore System

  8. User Threads • User threads supported above the kernel and managed without kernel support • Thread management done by user-level threads library • Three primary thread libraries: • POSIX Pthreads • Win32 threads • Java threads

  9. Kernel Threads • Supported by the Kernel • Examples • Windows XP/2000 • Solaris • Linux • Tru64 UNIX • Mac OS X

  10. Multithreading Models • Many-to-One • One-to-One • Many-to-Many

  11. Many-to-One • Many user-level threads mapped to single kernel thread • Entire process blocks with a thread blocking system call • Examples: • Solaris Green Threads • GNU Portable Threads

  12. One-to-One • Each user-level thread maps to kernel thread • Creation of a user thread requires creation of a kernel thread • Examples • Windows NT/XP/2000 • Linux • Solaris 9 and later

  13. Many-to-Many Model • Allows many user level threads to be mapped to many kernel threads (smaller or equal number) • Allows the operating system to create a sufficient number of kernel threads • Solaris prior to version 9 • Windows NT/2000 with the ThreadFiber package

  14. Many-to-Many Model

  15. Two-level Model • Similar to M:M, except that it also allows a user thread to be bound to kernel thread • Examples • IRIX • HP-UX • Tru64 UNIX • Solaris 8 and earlier

  16. Two-level Model

  17. Thread Libraries • Thread library provides programmer with API for creating and managing threads • Two primary ways of implementing • Library entirely in user space • Kernel-level library supported by the OS

  18. Pthreads • Refers to the POSIX standard (IEEE 1003.1c) API for thread creation and synchronization • May be provided either as user-level or kernel-level • POSIX standard specifies behavior of the thread library; Implementation is up to development of the library • Common in UNIX operating systems (Solaris, Linux, Mac OS X)

  19. Win32 Threads • Also known as Windows API or WinAPI • Win32 is a kernel-level library • Available on Windows systems (Windows 95, 98, NT, 2000 and XP)

  20. Windows XP Threads • Implements the one-to-one mapping, kernel-level • Each thread contains • A thread id • Register set • user stack or kernel stack • Private data storage area • The register set, stacks, and private storage area are known as the context of the threads • The primary data structures of a thread include: • ETHREAD (executive thread block) • KTHREAD (kernel thread block) • TEB (thread environment block)

  21. Windows XP Threads

  22. Linux Threads • Linux refers to them as tasks rather than threads • Thread creation is done through clone() system call • Flags, arguments of clone() specify sharing details

  23. Linux Threads • If CLONE_FS, CLONE_VM, CLONE_SIGHAND, and CLONE_FILES are passed to clone(), parent and child tasks share every thing • If none of these flags are passed to clone(), no sharing takes place, resulting in functionality similar to that of fork()

  24. End of Chapter 4

More Related