______ Threads (CS 273 (OS), Fall 2020)
Home
>>    




Threads

CS 273 (OS), Fall 2020

Basic ideas

  • Process: an execution of a program
    • Example: System call fork().
      • The example program forkeg.c uses fork() to create a second process (executing the same code).

  • Thread: a current execution path within a process
    • The example program trap_omp.c uses the OpenMP library to create multiple threads for executing for loop. Each thread adds values for a different collection of indices i, insuring that all indices i are included.

      • This would likely speed up the computation...

    • The example program pthreads.c uses the POSIX threads (pthreads) library to estimate the value of π.

  • Multi-threaded processes
    • Per thread: PC, stack, register, state (i.e., running, runnable, blocked or terminated). One thread can block without preventing another thread from running.
    • Per process: Address space (shared memory), fd-table, signals, semaphores
  • Note that threads for the same process typically share certain resources (e.g., memory) leading to efficient handling of those resources (e.g., two threads may simply share a data structure rather than having to communicate its values somehow).
  • Lightweight process (vs heavyweight process)
  • Example applications: file server, web server, database server, client (I/O vs graphics); convenience/performance of blocking calls

Some Parallel Design Patterns that can use threads

  • Dispatcher/worker model: one thread (dispatcher) for receiving requests, other threads (workers) for carrying out the work and replying

  • Team model: any thread can receive request; that thread carries out the work and replies

  • Pipeline model.

  • Threads computational models:

    • Multi-threaded process
    • Single threaded process
    • Finite-state machine (T. vs. theory)
    • Issues: parallelism and performance vs. ease of programming and design

Design and implementation of thread systems

  • Static vs. dynamic threads
  • Global variables per process vs. per thread (vs no globals)
  • Threads implemented at user level vs. within kernel

    • User-level implementation

      • Runtime system layer.
      • Advantages: Runs on unmodified OS; high performance; user-controlled scheduling
      • Disadvantages: Requires non-blocking OS calls, a major reason for multi-threaded design; timesharing among threads within a process?
      • Remedies: Jackets? Rewrite the C library (which affects existing code)?? select()
    • Kernel implementation

      • Advantages: True blocking, true timesharing
      • Disadvantages: Heavier (system calls require much more computation than user-level function calls), less user-level control (e.g., scheduling)

  • Signals?

Example threads package: POSIX threads

  • POSIX is IEEE-standard API (Application Programming Interface) that provides a uniform interface over variants of UNIX. "Portable Operating System Interface for uniX"

  • Thread management:
    • Pthread_create, (dynamically) create a new thread.
    • Pthread_join (cf. UNIX wait)
    • Pthread_exit
    • Pthread_yield, release CPU to another thread.
    • Pthread_attr_init, create a data structure of attributes for a thread with default values, such as priority.
    • Pthread_attr_destroy, deallocates the attribute structure (thread continues to exist)

Example threads package: OSI Distributed Computing Environment

  • Thread management:
    • Create (dynamic)
    • Join (cf. UNIX wait)
    • Detach (called by parent thread; child's resources reclaimed without a parental Join)
    • Exit
  • Templates of parameters for thread creation, e.g., priority, stack size
  • Mutex feature, built-in "binary quasi-semaphores" (except wakeup saving is implementation dependent...).
    • Mutex_init, Mutex_destroy, Mutex_lock, Mutex_trylock, Mutex_unlock.
    • "Friendly mutexes" (idempotent) vs "Fast mutexes")
  • Monitor-like condition variables.
  • Access to per-thread global variables (add-on?)
  • Cancel (cf kill()), Setcancel (cf signal())
  • User-selectable scheduling algorithm for threads within a process, as well as get/set priority

Java Thread class

  • Dynamic allocation via constructor call (of a user-defined subclass of Thread). start() (calls run()), stop(), join() (with optional timeout), suspend(), resume() methods for parent; sleep(), return for child.
  • Trivial default run().
  • Shared data passed as argument to constructor. Interfaces to facilitate sharing of memory and methods, e.g., bouncing ball example.
  • Only synchronized predefined for IPC
  • Scheduling control: get/set priority, yield() method.
  • Examples in reference page (java.lang.Thread)