멀티스레딩 프로그래밍

Bang!·2022년 1월 19일
0

OS(오퍼레이션)

목록 보기
4/7
post-thumbnail


Chapter 4: Multithreaded Programming

  • Operating system goals

What is a Thread

• is a flow of control within a process.
- single-threaded process, multi-threaded process.
• is a basic unit of CPU utilization, which comprise
- a thread ID, program counter, register set, stack.

• shares with other threads belonging to the same process its code section,
data section, and other OS resources (open files and signal)

• If a process has multiple threads of control, it can perform more than one task at a time.

Multi-threaded programs

• Many software packages that run on modern OS are multi-threaded.

• A web browser might have
- One thread displays images or text
- Another thread retrieves data from the network

• A word processor may have
- A thread for displaying graphics
- Another thread for responding to keystrokes form the user
- A third thread for performing spelling and grammar checking
Multi-threaded programs

• Web Server, for example
- Single-threaded web server: a client might have to wait for its request to be serviced.
- Multi-processes web server: used before threads become popular, much overhead in creating a new process.
- Multi-threaded web server: less overhead in thread creation, concurrent service to multiple client

• Many OS kernels are now multi-threaded
- Several threads operates in the kernel.
- Each thread performs a specific task, such as managing devices or interrupt handling.

Benefits of multi-threaded programming

• Responsiveness
- Multithreading an interactive application may allow a program to continue running even if part of it is blocked or doing a lengthy operation.
• Resource Sharing
- Threads share the memory and the resources of the process to which they belong.
• Economy
- Because threads in a process shares the resources, it is more economical to create and context-switch threads.
- Creating process is about thirty times slower than creating thread in Solaris.
• Utilization of Multi-Processor Architectures
- Threads may be running in parallel on different processors.

Two types of threads
• User Thread
• Kernel Thread

• User-level thread are threads that are visible to the programmer and are unknown to the kernel.
• OS kernel supports and manages kernel-level threads
• In general, user-level threads are faster to create and manage than are kernel threads, because no intervention from the kernel is required.

User Threads
• Thread management done by user-level threads library
- User thread are supported above the kernel and are managed without kernel support.

• Three primary thread libraries:
- POSIX Pthreads
- Win32 threads
- Java threads

Kernel Threads
• Supported by the Kernel
- The threads are supported and managed directly by the operating system.

• Examples
- Windows XP/2000
- Solaris
- Linux
- Tru64 UNIX
- Mac OS X

Multithreading Models
• A Relationship between user threads and kernel threads.
- Many-to-One
- One-to-One
- Many-to-Many

Many-to-One

  • Many user-level threads mapped to single kernel thread
    - Thread management is done by the thread library in user space
    - It is efficient. can create as many user threads as you wish.
    - The entire process will block when a thread makes a blocking system call.
    - Even on multiprocessors, threads are unable to run in parallel

• Examples:

  • Solaris Green Threads
  • GNU Portable Threads
    Many-to-One Model

One-to-One
• Each user-level thread maps to a kernel thread
- provides more concurrency than the many-to-one model
- allows another thread to run when a thread makes a blocking system call
- Creating a user thread requires creating the corresponding kernel thread. – overhead.
- The number of threads a process can create is smaller than many- to-one model. – be careful not to create too many thread.

• Examples
- Windows NT/XP/2000
- Linux
- Solaris 9 and later

Two-level Model
• One popular variation on many-to-many model
- Similar to many-to-many model,
- Many user-level threads are multiplexed to a smaller or equal number of kernel threads
- But it allows a user thread to be bound to a kernel thread

• Examples
- IRIX
- HP-UX
- Tru64 UNIX
- Solaris 8 and earlier

Threading Issues
• Semantics of fork() and exec() system calls
• Thread cancellation
• Signal handling
• Thread pools
• Thread specific data
• Scheduler activations

Semantics of fork() and exec()
• fork() system call is used to create a new separate, duplicate process.
• If one thread in a program calls fork() system call,
- does the new process duplicate all threads?
- or is the new process single-threaded?
• Some UNIX systems provides two versions of fork()

Thread Cancellation
• Terminating a thread before it has finished by other threads
- Ex, multiple threads search DB, one thread returns result. The remaining thread might be canceled.

• Two general approaches to cancel the target thread
- Asynchronous cancellation terminates the target thread immediately
- Deferred cancellation allows the target thread to periodically check if it should be cancelled
• Asynchronous cancellation
- The difficulty with cancellation occurs in situation
- where resources have been allocated to a canceled thread, or
- where a thread is canceled while in the midst of updating data it is sharing with other threads
• Deferred cancellation
- One thread indicates that target thread is to be canceled.
- Cancellation occurs only after the target thread has checked a flag to determine if it should be canceled or not

Signal Handling
• Signals are used in UNIX systems to notify a process that a particular event has occurred

• Two types of signals
- Synchronous signals
○ illegal memory access, division by 0
- Asynchronous signals
○ Specific keystrokes (Ctrl-C), timer expire

• What happen when a signal generated?
1. Signal is generated by particular event
2. Signal is delivered from kernel to a process
3. Signal is handled

Thread Pools
• Pool of threads(Procceses) where they await work
- A process creates a number of threads at start up and place them into a pool
- When receiving a request, server awakens a thread from the pool and passes the request to service
- Ones the thread completes its service, it returns to the pool and await more work.
- If the pool contains no available thread, the server waits until one becomes free.

• Advantages:
- Usually slightly faster to service a request with an existing thread than create a new thread
- Allows the number of threads in the application(s) to be bound to the size of the pool

Thread Specific Data

• Threads belong to a process share the data of the process.
- In some circumstances, each thread may need it’s own data

• Thread Specific Data
- allows each thread to have its own copy of data
- is useful when you do not have control over the thread creation process (i.e., when using a thread pool)

• Most thread libraries (Win32, Pthread, Java) provide some form of support for thread-specific data.

profile
pro한 프로그래머가 되자!

0개의 댓글