Why Multithreaded Server ? (1)
Consider the maximum server throughput (single processor computer)
- 2ms for processing
- 8ms for input-output delay (there is no caching)
Why Multithreaded Server ? (2)
Server throughput per second
In case of single thread
One request : 2+8 = 10ms
Then, server can 100 client requests per second
In case of two threads without caching
One request : 8ms
Then, maximum throughput : 1000/8 = 125 requests per second
In case of two threads with caching (75% hit ratio)
One request : I/O time is (0.75 x 0 + 0.25 x 8) = 2ms
Then, maximum throughput : 1000/2 = 500 requests per second
If processing time is increased to 2.5ms as a result of caching,
Then, maximum throughput : 1000/2.5 = 400 requests per second
User-Level vs. Kernel-Level Thread
❤️ User-level threads vs Kernel-level thread
User-level threads run at the user level and are managed via thread library.
User-level threads는 사용자 수준에서 실행되며 스레드 라이브러리를 통해 관리
Kernel-level threads run at the kernel level and are managed directly by kernel (This kernel is called multithreaded kernel).
Kernel-level threads는 커널 레벨에서 실행되며 커널에서 직접 관리
- Most contemporary operating systems (e.g., Windows XP, Linux, Solaris, Mac OS X, etc.) are multithreaded kernels.
Mapping between user-level threads and kernel-level threads.
- In non-multithreaded kernels, (e.g., MS-DOS etc.) user-level threads run on a processor like a regular process
(no mapping: kernel doesn’t aware threads).
- In multithreaded kernels, there must exists a relationship between user and kernel threads.
Multithreading Models (Mapping Model) (1)
Multithreading Models (Mapping Model) (2)
Map many user-level threads to a smaller or equal number of kernel-level thread.
여러사용자 수준 스레드를 더 작거나 같은 수의 커널 수준 스레드에 매핑
The number of kernel-level threads may be specific to either a particular application or a particular machine.
커널 수준 스레드의 수는 특정 응용 프로그램 또는 특정 시스템에 한정될 수 있다.
No blocking problem and can utilize multi-processors.
차단 문제가 없으며 멀티 프로세서를 활용할 수 있습니다.
Effect on concurrency and parallelism
- Many to one model
Creates as many threads as possible for concurrency but no parallelism
병렬은 아니지만 동시성을 위해 가능한 많은 스레드를 만듭니다.
- One to one model
Allows concurrency and parallelism but should be careful not to create so many threads within an application.
-> Some systems limit the number of threads.
동시성과 병렬성을 허용하지만 응용프로그램 내에 너무 많은 스레드가 생성되지 않도록 주의해야 합니다.
-> 일부 시스템은 스레드 수를 제한합니다.
- Many to many model
Allows concurrency and parallelism. Developers can create as many user threads as possible and kernel threads can run in parallel.
동시성과 병렬성을 허용합니다. 개발자들은 가능한 한 많은 사용자 스레드를 만들 수 있으며 커널 스레드는 병렬로 실행될 수 있다.
Most flexible but hard to implement.
-> Most operating systems prefer one to one model.
Thread Mapping and Scheduling
Parallel Execution due to
- Concurrency of threads on Virtual Processors 가상 프로세서에서 스레드의 동시성
- Concurrency of threads on Physical Cores 물리적 코어의 스레드 동시성
True Parallelism
- Single Thread : Single Core = 1:1
Thread Libraries
Thread library provides the programmer with an API for managing threads.
스레드 라이브러리는 프로그래머에게 스레드 관리를 위한 API를 제공합니다.
Two ways to implement a thread library.
User-level thread library
- Provides a library entirely in user space with no kernel support (i.e., no mapping).
커널 지원 없이 전체 사용자 공간에 라이브러리를 제공
- All code and data structures for the library exist in user space.
라이브러리에 대한 모든 코드와 데이터 구조가 사용자 공간에 존재
- Usually used in a non-multithreaded kernel.
일반적으로 멀티스레드되지 않은 커널에서 사용
- However, we can use it over a multithreaded kernel.
-> In this case, the outcome will be the same as running over a non-multithreaded kernel.
멀티스레드 커널에서 사용할 수 있습니다.
-> 이 경우 결과는 비 멀티스레드 커널에서 실행되는 것과 같습니다.
Kernel-level thread library
- Provides a library supported directly by the kernel (i.e., mapping).
커널에서 직접 지원하는 라이브러리(예: 매핑)를 제공
- Code and data structures for the library exist in kernel space.
커널 공간에 라이브러리의 코드와 데이터 구조가 존재
- Invoking an API function typically results in a system call to the kernel.
API 함수를 호출하면 일반적으로 커널에 대한 시스템 호출이 발생
- Multithreading model (mapping model) depends on the OS types.
멀티스레딩 모델(매핑 모델)은 OS 유형에 따라 다릅니다.
Three main thread libraries : POSIX Pthreads, Windows and Java threads
- POSIX Pthreads may be provided as either a user-level or a kernel-level library.
사용자 레벨 또는 커널 레벨 라이브러리로 제공
- Windows provides a kernel-level library.
커널 수준 라이브러리를 제공
- Java threads are generally implemented using a thread library available on the host system.
일반적으로 호스트 시스템에서 사용 가능한 스레드 라이브러리를 사용하여 구현
Process and Threaded Models
Threading Issues
- Semantics of fork() and exec() system calls
- Signal handling
- Thread cancellation
- Thread pools
- Thread Local Storage (TLS)
Semantics of fork() and exec()
Does fork() duplicate only the calling thread or all threads?
If exec() is called immediately after forking, duplicating all threads is unnecessary (since exec() replaces the entire process including all threads).
If not, the process should duplicate all threads.
Some UNIX systems have chosen to have two versions of fork().