CS330 · Annotated SlidesChapter 4 — Threads and Concurrency
Chapter 4 · Annotated slides

Threads and Concurrency

Why threads, single vs. multithreaded processes, benefits, multicore programming and its challenges, concurrency vs. parallelism, user and kernel threads, and the many-to-one, one-to-one, many-to-many and two-level models.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up
Slide 1: Chapter 4: Threads & Concurrency
Exam weight Low

    Outline of this Topic

    In this Topic, we will cover the following:

    • Overview
    • Multicore Programming
    • Multithreading Models
    • Thread Libraries
    • Implicit Threading
    • Operating System Examples
    Outline of this Topic
    2
    Exam weight Low

      Objectives

      • Identify the basic components of a thread, and contrast threads and processes
      • Describe the benefits and challenges of designing multithreaded applications
      • Describe how the Windows and Linux operating systems represent threads
      • Describe multithreaded applications using the Pthreads, Java, and Windows threading APIs
      Objectives
      3
      Exam weight Low

        Motivation

        • Most modern applications are multithreaded
        • Threads run within application
        • Multiple tasks with the application can be implemented by separate threads
          • Update display
          • Fetch data
          • Spell checking
          • Answer a network request
        • Can simplify code, increase efficiency
        • Kernels are generally multithreaded
        Motivation
        4
        Exam weight Low

          Threads

          • Heavyweight process: Traditional processes that can have only one thread
          • Multi-threaded processes: A process that includes multiple threads that share the resources allocated to the process
          • lightweight process: another name for A thread, is a basic unit of CPU utilization; it is comprised of:
            • A thread ID
            • program counter
            • register set
            • stack space
          • A thread shares with other threads its:
            • code section
            • data section
            • O/S resources
          Threads
          5
          Exam weight High
          1. A thread is a basic unit of CPU use: thread ID, program counter, registers, stack. Threads of one process SHARE code, data and open files.
          🎯 How this can come up · 1 question
          MCQ

          Which is NOT shared between threads of the same process?

          Slide 6: Single and Multithreaded Processes
          Exam weight Low

            Examples of Using Threads

            • Threads are important for any application with multiple tasks that can be run with separate threads of control.
            • A web server could produce a thread for each client
              • Serve clients with multiple threads concurrently.
              • less overhead to use multiple threads than to use multiple processes.
            • A Word processor may have separate threads for:
              • Reading user input
              • Display graphics
              • Performing Spell & grammar check
            Examples of Using Threads
            7
            Exam weight Low
              Slide 8: Multithreaded Server Architecture
              Exam weight Low

                Benefits

                • Benefits of threads are:
                  • Responsiveness:
                    • Threads allow a program to continue running even if part of it is blocked/ performing a lengthy operation. e.g: a multithreaded web browser can allow user interaction in 1 thread while loading an image in another thread.
                  • Resource Sharing:
                    • threads share memory and resources of the process they belong to.
                  • Economy:
                    • Allocating memory and resources to a process is costly- threads share resources of the process .
                    • Threads are faster to create and to switch between them than processes .(i.e. in Solaris, creating P is 30 times slower than creating thread).
                  • Scalability- Utilization of Multiprocessor architectures:
                    • Multiple threads can run in parallel on different CPUs - A single threaded process can run only on 1 CPU no matter how many are available.
                • Thread management could be done at either
                  • User level
                  • Kernel level
                Benefits
                9
                Exam weight High
                1. Benefits: responsiveness, resource sharing, economy (cheaper than processes), scalability on multicore.
                🎯 How this can come up · 1 question
                MCQ

                Which is a benefit of multithreading?

                Multicore Programming

                • Multicore or multiprocessor systems putting pressure on programmers, challenges include:
                  • Dividing activities- area of the application that can be divided into separate tasks.
                  • Balance- ensure that tasks perform equal work or equal value
                  • Data splitting- data accessed by the tasks must be divided to run on separate cores
                  • Data dependency- 1 task depend on data from another, the execution synchronized
                  • Testing and debugging- When a program is running in parallel on multiple cores, many different execution paths are possible. Testing and debugging such concurrent programs is inherently more difficult than testing and debugging single-threaded applications
                Multicore Programming
                10
                Exam weight Low
                1. Multicore challenges: identifying tasks, balance, data splitting, data dependency, testing and debugging.
                Slide 11: cont. Multicore Programming
                Exam weight Low
                  Slide 12: Concurrency vs. Parallelism
                  Exam weight High
                  1. Concurrency: more than one task making progress (possible on one core by interleaving). Parallelism: tasks running at the same time (needs multiple cores).
                  Memory trickParallelism ⇒ concurrency, but concurrency does not need parallelism.
                  🎯 How this can come up · 2 questions
                  True / False

                  Concurrency is possible on a single-core system.

                  True / False

                  Parallelism is possible on a single-core system.

                  User Threads

                  • A thread is managed by an application (by user-level threads library) not in the kernel
                  • Scheduled by the thread library
                  • Fast to create and manage: all thread creation and scheduling are done in user space without the need for kernel intervention
                  • Any user-level thread performing a blocking system call will cause the entire process to block if the kernel is single threaded.
                  • Examples of three primary thread libraries - POSIX Pthreads - Java threads - Win32 threads
                  User Threads
                  13
                  Exam weight Low
                  1. User threads: managed by a user-level library (POSIX Pthreads, Java, Windows threads APIs).

                  Kernel Threads

                  • All thread management is done by the kernel itself
                  • Generally slower to create and manage than user threads
                  • If a thread is performing a blocking system, the kernel can schedule another thread in an application to be executed
                  • Kernel can schedule threads on different processors
                  • Kernel does creation, scheduling and management of threads.
                  • Examples – virtually all general purpose operating systems, including:
                    • Windows
                    • Linux
                    • Mac OS X
                    • iOS
                    • Android
                  Kernel Threads
                  14
                  Exam weight Low
                  1. Kernel threads: managed by the OS kernel.
                  Slide 15: User and Kernel Threads
                  Exam weight Low

                    Multithreading Models

                    What is the relationship between the user & kernel threads:

                    • Many-to-One
                    • One-to-One
                    • Many-to-Many
                    Multithreading Models
                    16
                    Exam weight Low
                      Slide 17: Many-to-One
                      Exam weight High
                      1. Many-to-one: many user threads → one kernel thread. One blocking call blocks all; no parallelism on multicore.
                      🎯 How this can come up · 1 question
                      MCQ

                      In the many-to-one model, if one thread makes a blocking system call:

                      Slide 18: 18
                      Exam weight Low
                      1. One-to-one: each user thread gets a kernel thread (Windows, Linux). More concurrency, but many kernel threads may hurt performance.
                      Slide 19: Many-to-Many
                      Exam weight Low
                      1. Many-to-many: many user threads multiplexed on a smaller or equal number of kernel threads.
                      Slide 20: Two-level Model
                      Exam weight Low
                      1. Two-level: many-to-many that also lets a user thread be bound to a kernel thread.

                      End of Chapter 4

                      21
                      Exam weight Low