1. Definition

  • Idea: allow multiple thread of execution within the same process environment, to a large degree independent of each other.
  • Multiple threads running in parallel in one process is analogous to having multiple processes running in parallel in one computer.
  • Multiple threads within a process will share 
    • the address space
    • open files
    • other resources
  • Potential for efficient and close cooperation
2. Multithreading
  • when a multithread process is run on a single CPU system, the threads take turns running
  • All threads in the process have exactly the same address space
  • each thread can be in any one of the several states, just like processes
  • Each thread has its own stack
3. Benefits
  • Responsiveness
    • multithreading an interactive application may allow a program to continue running even if part of it is blocked or performing a lengthy operation
  • Resource sharing
    • sharing the address space and other resources may result in high degree of cooperation
  • Economy
    • creating/managing processes is much more time consuming than managing threads
  • Better utilization of multiprocessor architectures
4. Example: a multi-threaded web server
5. Implementing threads
  • processes usually start with a single thread
  • usually, library procedures are invoked to manage threads
    • thread_create: typically specifies the name of the procedure for the new thread to run
    • thread_exit
    • thread_join: blocks the calling thread until another (specific) thread has exited
    • thread_yield: voluntarily gives up the CPU to let another thread run
  • threads may be implemented in the user space or in the kernel space
6. User-level thread
  • user threads are supported above the kernel and are implemented by a thread library at the user level
  • the library (or run-time system) provides support for thread creation, scheduling and management with no support from the kernel
  • when threads are managed in user space, each process needs its own private thread table to keep track of the threads in that process
  •  the thread-table keeps track only of the per-thread items (program counter, stack pointer, register, state…)
  • when a thread does something that may cause it to be 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 switching
7. User-level advantage
  • the operating system does not need to support multi-threading
  • since the kernel is not involved, thread switching may be very fast
  • each process may have its own customized thread scheduling algorithm
  • thread scheduler may be implemented in the user space very efficiently
8. User-level threads: problems
  • the implementation of blocking system calls is highly problematic (e.g., read from the keyboard). All the threads in the process risk being blocked
  • Possible solutions
    • change all system-call to non-blocking
    • sometimes it may be possible to tell in advance if a call will block (e.g., select system call in some version of unix) 
      • jacket code round system calls
9. Kernel-level threads
  • kernel threads are supported directly by the OS
    • the kernel provide threat creation, scheduling and management in the kernel space
  • the kernel has a thread table that keeps track of all threads in the system
  • all calls that might block a thread are implemented as system call (at greater cost)
  • when a thread blocks, the kernel may choose another thread from the same process, or a thread from a different process
10. Hybrid Implementation
  • An alternative solution is to user kernel-level threads, and then multiplex user-level threads onto some or all of the kernel threads
  • A kernel-level thread has some set of user-level threads that take turns using it

11. Windows XP threads
  • windows XP support kernel-level threads
  • the primary data structures of a thread are 
    • ETHREAD (executive thread blocks)
      • thread start address
      • pointer to parent process
      • pointer to corresponding KTHREAD
    • KTHREAD (kernel thread block)
      • scheduling and synchronizing information
      • kernel stack (used when the thread is running in kernel mode)
      • pointer to TEB
    • TEB (thread environment block)
      • thread identifier
      • user-mode stack
      • thread-local storage
12. Linux Threads
  • In addition to fork() system call, Linux provides the clone() system call, which may be used to create threads
  • Linux users the term task (rather than process or thread) when referring to a flow of control
  • A set of flags, passed as arguments to the clone() system call determine how much sharing is involved (e.g., open files, memory space, etc).

Leave a Reply