ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Process vs Thread
    CS 지식/운영체제(OS) 2023. 4. 19. 10:56

    Process

    - Program in execution(수행 중인 프로그램). 메모리 상에서 실행 중인 프로그램.

    (Thread는 이 프로세스 안에서 실행되는 흐름 단위.)

    - Execution stream in the context of particular process state

    • Execution stream : 프로세스가 수행한 모든 명령어들의 순서
    • Process state / context
      • Memory context : code, data(program의 전역변수 저장), stack(local 변수들, 함수 호출을 위한 필요한 데이터들 저장)
      • Hardware context : CPU의 register values, I/O registers
      • System context : kernel context. process table, page table..

     

     

    Multiprogramming vs Multiprocessing

    - Multiprogramming : main memory에 여러 개의 active 한 process가 load 되어 있는 경우. Multiple processes in memory.

    - Multiprocessing : CPU 관점에서 CPU가 여러개의 process 왔다갔다 하면서 처리. CPU is multiplexed.
                                     Multiple processes are running together at the same time.

     

     

     

    PCB(Process Control Block)

    https://techaccess.in/2021/05/07/process-control-block/

     

     

     

    Process의 State Transition

    Process Life cycle

    - Ready Queue : ready 상태의 process들을 관리하기 위한 Queue 형태의 자료구조. 일반적으로 PCB들을 linked list로 연결하여 구현.

    - Waiting 상태의 process는 여러 개 있을 수 있음. OS는 waiting 이유에 따라 별도의 큐를 이용함. 

     

     

     

    Thread란

    - lightweight process (하나의 process에 여러 개 thread 존재)

     

     

    Multithreading이 생기게 된 배경

    웹 서버 구동 방식 2가지

    1) Iterative server architecture

    : 서버가 깨어나서 request queue에서 따와서 스스로 처리. 처리가 다 끝나면 request queue에 새로운 request가 있는지 확인해서 처리. 서버가 직접 처리

    장점 -  단순해서 programming 하거나 관리하기 쉬움.

     

    2) Concurrent server architecture (요즘 많이 사용)

    : messaging queue에서 request를 따오면 server가 직접 처리하는 것이 아니라 그 일을 처리해 줄 worker process를 하나 스폰해서 걔로 하여금 처리하게 시킴. 
    서버는 worker process가 일하는 동안 자유로워지니까 또 다른 request를 따와서 또 다른 worker process를 fork 해와서 처리하면 됨. 

    장점 - 사용자 입장에서는 response time이 줄어든다. 병렬성을 높일 수 있음.

    단점 - worker process를 사용해서 구현한 concurrent server는 처리할 요청의 수만큼 process를 생성해야 하기 때문에 시스템에 큰 부담이 생김.

    => Multithreading의 등장!!

     

     

     

    Multithreading의 목적

    : Concurrency는 높이면서 Execution Unit을 생성하거나 수행시키는데 드는 overhead를 줄임. 

    1) 적은 비용으로 concurrency를 얻어서 agility를 높이고 response time을 줄이기 위해

    2) Massive parallel scientific programming을 할 때 발생하는 overhead를 줄이기 위해

     

     

    Multithreading

    - Multithreading에서는 thread가 execution stream이 됨. 

    - thread마다 stack을 가지고 있어서 하나의 process가 여러개 stack을 가지게 됨

     

    https://slideplayer.com/slide/5219996/

    - 각각의 Thread는 stack, thread ID, Thread Control Block(TCB) 등을 가지고 있음.

    - thread들이 process에 할당된 resource들을 공유하니까 need to synchronize

     

    -> 지금은 웹 서버 process 안에 dispatcher thread가 있고 필요에 따라 그때그때 worker thread를 생산하고 수행시킴. 

    (thread가 하나였다면 download 하는 동안 저 web의 다른 어떤 버튼도 입력을 받아들일 수 없음.)

     

     

     

    Multithreading에서 Thread Implemention 3가지 방법

    1) User - Level Threads

    - Thread는 100% user-level entity이며 kernel은 thread의 존재를 모르는 상태.

    - Thread의 생성, 소멸, context를 저장하는 block 들에 대한 thread library는 user-level library로 구현 

    - processor allocation on a process basis

     

    장점

    - thread switching에 kernel이 개입하지 않아서 mode switching이 필요 없다. (구현 쉬움)

    - scheduling이 application specific 해서 best algorithm 선택 가능하다. (OS 안 고쳐도 됨)

    - thread library만 필요로 하기 때문에 어떤 OS에서도 동작한다. (병렬 연산에 잘 동작)

     

    단점

    - thread가 blocking system call을 호출하면 그 process 전체가 block 된다.(block anomaly)

    => reactive system, I/O interactive system에는 사용하기 어려움. user interactive 하지 X

    => interrupt가 필요한 preemptive scheduling에는 한계가 있음. (non-preemptive scheduling에는 문제 X )

     

    https://tutorialwing.com/user-level-thread-and-kernel-level-thread-with-example/

     

     

     

    2) Kernel - Level Threads

    - thread 생성과 종료, TCB 조정, thread scheduling 모두 kernel의 system call로 구현

    - User - Level Threads와 달리 interrupt forwarding 가능해짐, block anomaly 문제도 해결됨.

    - user-level thread와 kernel-level thread 1:1 mapping

    - processor allocation on a thread basis

     

    장점

    - blocking이 thread level에서 되고 한 process내의 여러 thread를 동시에 실행 가능

    - 반응성이 좋아져서 reactive control system에 좋음. 

     

    단점

    - 같은 process내의 thread switching에도 kernel이 개입하므로 thread switch마다 2번의 mode switch 발생 

    - massive parallel scientific programming 할 때 overhead 많이 증가

    - kernel단의 수행이 증가하면서 추가적인 overhead 발생 -> 결과 느려짐.

     

    https://www.javatpoint.com/threads-in-operating-system

     

     

     

    3) Combined User-Level / Kernel Level Threads 

    - preemptive 가능.

    - user-level의 threads library, kernel level의 System call API 모두 존재. 각각은 user-level, kernel level thread의 생성과 소멸 관장. 

    - user-level thread와 kernel-level thread는 n:m mapping

    - processor allocation on a thread basis

    https://quizlet.com/317209055/user-level-threads-and-kernel-level-threads-chapter-4-diagram/

     

     

    Multiprocessing vs Multithreading

    Multiprocessing

    - parallelism 수행

    - CPU intensive task(CPU bound tasks)에 사용하면 좋음 

    bottleneck이 time과 resource라서. ex) image, graphics processing

    I/O intensive task에 multiprocessing을 하면 상대적으로 waste of resource.

    중요한 연산을 할 수 있는 것이 input을 기다리기만 하니까.

     

    Multithreading

    - concurrency 수행

    - I/O intensive task(I/O bound tasks)에 사용하면 좋음. user interaction이 중요한 경우.
    process에 job을 할당하면 user가 더 많이 기다려야 할 테니까. speedup.

    bottleneck이 주로 reading or writing operation

     

     

     

    실습

     

     

    Reference

    'CS 지식 > 운영체제(OS)' 카테고리의 다른 글

    paging vs segmentation  (0) 2023.05.23
    Page 교체 알고리즘  (0) 2023.05.01
    Process 동기화(semaphore vs mutex vs monitor)  (0) 2023.04.26
    IPC(Interprocess Communication) protocol  (0) 2023.04.23
Designed by Tistory.