[OS] Concurrency, Thread, Multithreading

Ko Hyejung·2021년 10월 12일
0

Operating Systems

목록 보기
12/26

Concurrency in Process Model


Use of concurrency within an application has several advantages.

응용 프로그램 내에서 동시성을 사용하면 몇 가지 이점이 있습니다.

Concurrency can be implemented by structuring an application as a set of concurrent processes (e.g., using fork()).

애플리케이션을 동시 프로세스 집합으로 구성하여 구현 가능

Overhead in Process Model


Each process is completely independent (need IPC via kernel).

각 프로세스는 완전히 독립적입니다(커널을 통한 IPC 필요).

Too heavy for process creation, context switching.

What is Thread ?

+) 스레드는 프로세스 내에서의 작업 단위로서 시스템의 여러 자원을 할당받아 실행하는 프로그램의 단위

+) https://www.tutorialspoint.com/operating_system/os_multi_threading.htm

A flow of control within a process that consists of a PC, a register set and a stack space (similar to process – sub-process).
An individual thread has at least its own register state, and usually its own stack.

PC, 레지스터 세트 및 스택 공간으로 구성된 프로세스 내의 제어 흐름입니다(프로세스 – 하위 프로세스와 유사).
개별 스레드는 적어도 자체 레지스터 상태를 가지고 있으며 보통 자체 스택을 가지고 있다.

A basic unit of CPU scheduling.
CPU 스케줄링의 기본 단위.

Multiple threads can be created within a same process and share code, address space, and operating resources for the process.

프로세스에 대해 동일한 프로세스공유 코드, 주소 공간운영 리소스 내에 여러 스레드를 생성할 수 있습니다.

Thread makes creation and switching inexpensive (use small context).

The environment in which a thread executes is called task, and a traditional (heavyweight) process is equal to a task with one thread.

스레드가 실행되는 환경을 task라고 하며, 기존의(heavyweight) 프로세스는 스레드가 하나 있는 작업과 동일

Single and Multithreaded Processes

Multithreading (Multiple Cores or Processors)


💛 Achieve parallelism

  • Two tasks are parallel if they are performed at the same time.
    두 작업이 동시에 수행되는 경우 두 작업은 병렬
  • Threads of a process can even execute in parallel on different processors or cores (multicore).
    프로세스의 스레드는 서로 다른 프로세서 또는 코어에서 병렬로 실행될 수도
  • Parallelism = no. of CPUs (cores)

Multithreading (Single Core or Processor)


💛 Achieve concurrency (illusion of parallelism)

  • Two tasks are concurrent if there is an illusion that they are being performed in parallel, whereas, in reality, only one of them may be performed at any time.
    두 개의 작업이 동시에 수행된다는 착각이 있는 경우 두 개의 작업이 동시에 수행되지만, 실제로는 두 작업 중 하나만 수행될 수도
  • Provide computation speed-up even on a single processor system because if an I/O bound thread blocks, the kernel can switch to another thread of the same process.

I/O 바인딩된 스레드가 차단되면 커널이 동일한 프로세스의 다른 스레드로 전환할 수 있으므로 단일 프로세서 시스템에서도 계산 속도를 높일 수 있습니다.

Benefits

Scalability 확장성

  • Achieve concurrency and parallelism
    동시성 & 병렬성 달성
  • Throughput (overlap computation and I/O)
    처리량 (오버랩 계산 및 I/O)
  • Utilization of MP Architectures

  • Responsiveness
  • Resource Sharing
  • Economy

  • If all operations are CPU intensive do not go far on multithreading
  • Thread creation is very cheap, it is not free
    Thread that has only five lines of code would not be useful

Example 1 (Multithreaded Program-1)

Example 1 (Multithreaded Program-2)

Example 2 (Multithreaded Server)

Example 2 (Multithreaded Server-1)

Example 2 (Multithreaded Server-2)

Example 2 (Multithreaded Server-3)

Example 2 (Multithreaded Server-4)

0개의 댓글