- Multiple activities going on at once with the ability to share an address space
- Lighter weight than processes, easy to create and destroy.
- Overlap activities with substantial I/O to speeding up the application.
- Useful on systems with multiple CPUs.
Three ways to construct a server
- Thread. Parallelism, blocking system calls.
- Single-threaded process. No parallelism, blocking system calls.
- Finite-state machine. Parallelism, nonblocking system calles, use interrupts to simulate thread.
Classical Thread Model
Processes are used to group resources together; threads are the entities scheduled for execution on the CPU. There are no protection betwwen threads because (1) it’s impossible, and (2) it should not be nessessary.
While threads share one memory space, it takes fewer space to maintain a thread, including Program Counter, Registers, Stack and State.
Pthread_create: create a new thread.
Pthread_exit: terminate the calling thread.
Pthread_join: wait for a specific thread to exit.
Pthread_yield: release the CPU.
Pthread_attr_init: create and initialize a thread’s attribute structure.
Pthread_attr_destroy: remove a thread’s attribute structure.
Implementing Threads in User Space
- useful on an OS that doesn’t support threads
- switching is faster than trapping to the kernal
- allow process have its own scheduling algorithm
Implementation of blocking sys-calls.
These calles intended to block the process (all threads in it) since the kernal know nothing about threads. This problem could be solved by adding wrappers to all blocking sys-calls.
The same as above.
- A running thread never voluntarily gives up the CPU since no clock interrupts in user space.
- Substantial sys-calles are needed generally in threads. It’s hardly any more work for the kernel to switch threads.
Implementing Threads in the Kernel
Due to the relatively greater cost of creating and destroying threads in the kernel, some systems take an environmentally correct approach and recycle their threads.
- When a multithreades process forks
- Signals sent to a multithread process
Programmers can determine how many kernel threads to use and how many user-level threads to multiplex on each one.
The kernel notifies the process’ run-time system to switch thread, thus avoiding unnecessary transitions between user and kernel space.
This implementation violates the structure inherent in any layered system.
On arrival of a message, the system creates a new thread to handle it. Since a pop-up thread has no history, it’s quicker to create than swap.
Making Single-Threaded Code Multithread
Problems should be solved
Private global variables. A new library to create, set, read these variables is needed.
many library procedures are not reentrant
A jacket for each of these procedures is needed.
overflows could not be awared.
本文采用 知识共享署名 4.0 国际许可协议（CC-BY 4.0）进行许可。转载请注明来源： https://harttle.land/2013/11/21/morden-os-threads.html 欢迎对文中引用进行考证，欢迎指出任何不准确和模糊之处。可以在下面评论区评论，也可以邮件至 firstname.lastname@example.org。