Docsity

Multi Threads vs Singles Threads - Operating Systems Design | CMPSC 473, Study notes of Computer Science

Partial preview of the text

Download Multi Threads vs Singles Threads - Operating Systems Design | CMPSC 473 and more Study notes Computer Science in PDF only on Docsity!

Operating SystemsOperating Systems

CMPSC 473 CMPSC 473

Operating Systems Structure Operating Systems Structure

February 5, 2008 - Lecture February 5, 2008 - Lecture 66

Instructor: Trent Jaeger Instructor: Trent Jaeger

  • Last class:
    • Processes
  • Today:
    • Threads

Advantages of Threads

• Improve Responsiveness

  • Ideally, a thread is always ready

• Resource Sharing

  • All the stuff is easily accessible

• Economy of Resources

  • Thread resources are cheaper than process resources

• Utilization of Multiprocessors

  • Get all of them running

Multi-Threaded vs. Single-

Threaded

What’s a Thread?

• Thread of Execution on CPU

  • Program counter
  • Registers

• Memory

  • Address space (process)
  • Stack -- per thread

• I/O

  • Share files, sockets, etc. (process)

Working with Threads

• In a C program

  • main () procedure defines the first thread
  • C programs always start at main

• Create a second thread

  • Allocate resources to maintain a second execution

context in same address space

  • Think about what process fields will be necessary for a thread
  • Supply a procedure name to start the new thread’s

execution

Which is Cheaper?

• Create new process or create new thread (in

existing process)

• Context switch between processes or threads

• Interprocess or inter-thread communication

• Sharing memory between processes or threads

• Terminate a process or terminate a thread (not last

one)

Threading Models

User-Space Threads

  • Thread management support in user-space library
    • Sets of functions for creating, invoking, and switching among threads
  • Linked into your program
    • Thread libraries
  • Examples
    • POSIX Threads (PThreads)
    • Win32 Threads
    • Java Threads

Kernel Threads

  • Thread management support in kernel
    • Sets of system calls for creating, invoking, and switching among threads
  • Supported and managed directly by the OS
    • Thread objects in the kernel
  • Nearly all OS support a notion of threads
    • Linux -- thread and process abstractions are mixed
    • Solaris
    • Mac OS X
    • Windows XP
    • …

One-to-one Thread Model

  • One user-level thread per kernel thread
    • A kernel thread is allocated for every user-level thread
    • Must get the kernel to allocate resources for each new user-level thread
  • How does it work?
    • Create new thread, including system call to kernel
    • Upon yield , switch to another thread in system
      • Kernel is aware
    • Upon wait , another thread in the process may run
      • Only the single kernel thread is blocked
      • Kernel is aware there are other options in this process

Many-to-many Thread Model

  • A pool of user-level threads maps to a pool of kernel threads
    • Pool sizes can be different (kernel pool is no larger)
    • A kernel thread is pool is allocated for every user-level thread
    • No need for the kernel to allocate resources for each new user-level thread
  • How does it work?
    • Create new thread (may map to kernel thread dynamically)
    • Upon yield , switch to another thread in system
      • Kernel is aware
    • Upon wait , another thread in the process may run
      • If a kernel thread is available to be scheduled to that process
      • Kernel is aware of the mapping between process threads and kernel threads
  • Next time: More Threads