Threads
- What is a thread, anyway?
- A process has several resources.
- Memory region, including a stack.
- Registers, including the the PC.
- System resources: open files, network connections, etc.
- Divide the spoils.
- A process holds the memory region and system resources.
- A thread resides in a process.
- A thread has a stack and registers, including the PC.
- A process may contain one or more threads.
- Multiprogramming is among the threads.
- Terrible terminology.
- The first one is a traditional process, the second is a
threaded process or modern process.
- A traditional process is a modern process with exactly one thread.
- Some authors (not Tanenbaum) use task for our
modern process.
- What is a thread good for?
- GUIs: kb, screen and disk.
- Servers: Listening and multiple clients.
- Threads v. Processes
- Can use either.
- Threads are cheaper to create, destroy and switch.
- Threads can communicate more efficiently.
- Threads can clobber each other (same memory space).
- Shared variables.
- When multiple threads share data, any can update at any time.
- Final results may depend on how the threads happen to be
scheduled. race condition
- Subject of the next section.
- Implementing threads.
- User space (library).
- Kernel does not know about the threads.
- Kernel schedules processes.
- When a process runs, the library schedules the threads.
- Kernel.
- Kernel knows about the threads.
- Kernel schedules threads, not processes.
- Compare.
- Easier to write a library instead of modify the kernel.
- Library is more portable.
- Thread switching faster w/o the kernel.
- Library can be tailored to the application.
- Kernel scheduling can be fairer.
Consider processes with
different numbers of threads.
- When a thread of the library process blocks, all the threads block.
- Possible to re-implement I/O calls using non-blocking I/O.
- But then you have to re-implement all the basic I/O calls to
support threading.
- One thread per process can be running, even under SMP.
- Hybrid.
- Kernel supports threads.
- Kernel threads are divided in user space.
- One kernel thread per CPU can be a reasonable model.
- Scheduler Activations.
- Kernel creates threads, but lets the library schedule execution
on them.
- Kernel notifies the library when a thread must block. Library
can schedule another.
- This requires an up-call: Sort of a system call in reverse, where
the O/S calls a specified function in the scheduler.
- The kernel loads a state onto the CPU that starts the
library monitor from a known PC value.
- The monitor can then check if another thread is ready and
start it.
- Pop-up thread
- System creates a thread upon the arrival of a networking message.
- Useful way to serve web requests.
- Must pre-arrange the context in which the thread will run.
- Simple examples of threaded programs.
- C/Pthreads.
- C++ (2011 Standard).
- Java.
- Re-writing a program to use threads can be difficult.
- Static shared variables create race conditions.
- Traditional libraries often use these.
- Most threading systems support thread-local storage.
- A block of storage attached to the thread.
- Global to the function invocations within the thread, but local
to the thread.
- Can be used for these globals, but requires coding changes.
- Usually no direct support in languages.
- Support.
- Unix and friends.
- Designed with traditional processes.
- Threads tacked on. Posix threads.
- Windows: Designed with threading in mind.