1 / 35

Lecture #4

Lecture #4. 쓰레드( Thread). “프로세스” 란?(1). 실행중인 프로그램 실행하기 위해 메모리에 적재되어거나 준비중인 프로그램 사용자가 프로그램을 실행시키면 “ process” 가 생성 프로세스 상태( process states) 5-state process model: ready, running, blocked, new, stop 7-state process model: 5-state process model + suspended state

tom
Download Presentation

Lecture #4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture #4 쓰레드(Thread) 신라대학교 컴퓨터공학과 - 운영체제

  2. “프로세스” 란?(1) • 실행중인 프로그램 • 실행하기 위해 메모리에 적재되어거나 준비중인 프로그램 • 사용자가 프로그램을 실행시키면 “process”가 생성 • 프로세스 상태(process states) • 5-state process model: ready, running, blocked, new, stop • 7-state process model: 5-state process model + suspended state • 프로세스 이미지(process image) • program, data, stack • PCB(process control block) 신라대학교 컴퓨터공학과 - 운영체제

  3. “프로세스” 란?(2) • Process Switching • 프로세스의 상태 변환 • Process context switching • 병렬 실행(Concurrent processing) on SMP Machine • 프로세스간의 통신(Interprocess communication) • 프로세스간의 데이터 전송 • Signal, Pipe, Shared Memory, Message Queue 등 • Kernel에 의해 수행 신라대학교 컴퓨터공학과 - 운영체제

  4. 프로세스 특성(1) • 자원 할당의 단위(Unit of resource ownership) • 프로세스가 실행에 필요한 자원이 프로세스 단위로 할당된다 • 프로세스 이미지(process image)를 가지는 메모리 공간 • 다양한 자원(files, I/O devices...)에 대한 제어 • 프로세서실행의 단위(Unit of dispatching) • 프로세스는 프로그램에서 하나의 실행 패스(path) 이다 • 프로세서는 여러 개의 프로세스에 의해 공유된다 • 각각의 프로세스는 실행 정보와 실행 우선순위를 갖는다 신라대학교 컴퓨터공학과 - 운영체제

  5. 프로세스 특성(2) • 최근 위의 두 가지 특성이 독립적으로 정의되고 있다 • 자원 할당 단위와 프로그램 실행 단위를 분리 • 프로세서 실행 단위(unit of dispatching)  athread or a LWP(lightweight process) • 자원 할당 단위 (unit of resource ownership)  a process or a task 신라대학교 컴퓨터공학과 - 운영체제

  6. Multithreading vs. Single threading • Multithreading: when the OS supports multiple threads of execution within a single process • Single threading: when the OS does not recognize the concept of thread • MS-DOS support a single user process and a single thread • UNIX supports multiple user processes but only supports one thread per process • Solaris supports multiple threads 신라대학교 컴퓨터공학과 - 운영체제

  7. Threads and Processes 신라대학교 컴퓨터공학과 - 운영체제

  8. Threads(1) • 프로그램 실행 단위 또는 프로그램 실행 패스(path) • 하나의 프로세스는 여러 개의 thread로 구성 • Thread는 실행 상태를 갖는다(running, ready, blocked etc.) • 실행 상태가 변할 때에 thread context switching이 수행된다 • Thread 별로 프로그램 실행 스택(execution stack)과 지역 변수를 위한 메모리를 갖는다 신라대학교 컴퓨터공학과 - 운영체제

  9. Threads(2) • 프로세스에 할당된 메모리 영역과 자원을 접근한다 • 하나의 프로세스를 구성하는 모든 쓰레드는 프로세스의 자원을 공유한다 • 하나의 쓰레드가 메모리 내용을 변경하면 다른 모든 쓰레드는 그 메모리 내용을 접근할 수 있다 • 하나의 쓰레드가 파일을 열면 다른 쓰레드가 그 파일을 접근할 수 있다 신라대학교 컴퓨터공학과 - 운영체제

  10. Single Threaded and Multithreaded Process Models Thread Control Block contains a register image, thread priority and thread state information 신라대학교 컴퓨터공학과 - 운영체제

  11. Threads States • Three key states: running, ready, blocked • 동일한 프로세스을 구성하는 쓰레드는 메모리 공간을 공유하므로 중단 상태(suspend state)을 갖지 않는다 • 하나의 쓰레드를 suspending (i.e: swapping) 하는 것은 동일한 프로세스의 모든 쓰레드를 suspending 하게 된다 • 프로세스 종료는 프로세스 내의 모든 쓰레드를 종료한다 신라대학교 컴퓨터공학과 - 운영체제

  12. Thread 응용(1) • 응용 #1: a file server on a LAN • 짧은 기간에 여러 개의 파일 접근 요청을 처리하여야 한다 • 각각의 파일 접근 요청에 대해 하나의 thread를 생성하는 것이 훨씬 효과적이다 • SMP machine의 경우: 여러 개의 쓰레드가 각각 다른 프로세서에서 동시에 실행할 수 있다 • Web server 등 신라대학교 컴퓨터공학과 - 운영체제

  13. Thread 응용(2) • 응용 #2: User Interface thread on Windows • 하나의 쓰레드는 메뉴를 생성하여 사용자 입력을 받고 다른 쓰레드는 사용자 명령을 처리한다 신라대학교 컴퓨터공학과 - 운영체제

  14. Thread 응용(3) • 응용 #3: 순서대로 실행할 필요가 없는 여러 개의 독립적인 부분으로 구성되어 있는 응용 • 각각의 독립적인 부분을 쓰레드로 구현한다 • 하나의 쓰레드가 입력 동작에 대한 대기 상태가 되면 다른 쓰레드가 실행 상태로 전환된다 (thread switching instead of switching to another process) 신라대학교 컴퓨터공학과 - 운영체제

  15. Thread 장점(1) • 프로세스 보다 새로운 쓰레드를 생성하는데 시간이 덜 걸린다 • 프로세스 보다 쓰레드를 종료하는데 시간이 덜 걸린다 • 동일한 프로세스에 있는 두 개의 쓰레드 사이의 전환이 더 적은 시간을 소요한다 신라대학교 컴퓨터공학과 - 운영체제

  16. Thread 장점(2) • 동일한 프로세스에 있는 쓰레드는 메모리 및 파일 등의 자원을 공유하므로 커널(kernel)의 도움없이 상호간에 통신할 수 있다 • 따라서, 공유되는 메모리나 파일에 대해 일관성 있는 접근을 지원하기 위해 쓰레드 간의 동기화가 필수적으로 요구된다 신라대학교 컴퓨터공학과 - 운영체제

  17. 일관성 없는 데이터 접근의 예 • 3 variables: A, B, C which are shared by thread T1 and thread T2 • T1 computes C = A+B • T2 transfers amount X from A to B • T2 must do: A = A -X and B = B+X (so that A+B is unchanged) • But if T1 computes A+B after T2 has done A = A-X but before B = B+X then T1 will not obtain the correct result for C = A + B • The resource sharing requires the synchronization between threads 신라대학교 컴퓨터공학과 - 운영체제

  18. Process와 Thread 비교(1) • 하드 디스크로 부터 필요한 데이터를 읽어 오는 producer 모듈과 읽어온 데이터를 처리 하는 consumer 모듈로 구성된 프로그램 • Producer 모듈과 consumer 모듈은 병행 수행(concurrent execution)이 가능하다 • 프로그램 실행 모델 • Single process execution • Multiple process execution • Multithreading execution 신라대학교 컴퓨터공학과 - 운영체제

  19. Producer Hard Disk Main memory (shared memory) Consumer Process와 Thread 비교(2) • Single process execution : • Sequential execution Process 신라대학교 컴퓨터공학과 - 운영체제

  20. Main memory (Shared Memory) Producer process Consumer process Hard Disk Process와 Thread 비교(3) • Multiple process execution : • Concurrent execution with high communication overhead Kernel 신라대학교 컴퓨터공학과 - 운영체제

  21. Producer Thread Hard Disk Local memory Consumer Thread Kernel Process와 Thread 비교(4) • Multithreading execution : • Concurrent execution with low communication overhead Process 신라대학교 컴퓨터공학과 - 운영체제

  22. User-Level Threads (ULT) • 커널은 쓰레드 존재를 알지 못하며 프로세스을 관리 한다 • 응용 프로그램이 thread library을 이용하여 모든 쓰레드를 관리한다 • Thread switching은 kernel mode privileges을 요구하지 않는다 (no mode switch) • 쓰레드 간의 실행 스케줄링은 응용 프로그램에 의해 정의된다 신라대학교 컴퓨터공학과 - 운영체제

  23. Threads library • 다음 작업을 수행하는 코드를 지원한다: • 쓰레드 생성과 종료 • 쓰레드 간의 메시지와 데이터 전달 • 쓰레드 실행 스케줄링 • 쓰레드 문맥 전환(thread context switching) 신라대학교 컴퓨터공학과 - 운영체제

  24. ULT에 대한 커널 동작 • 커널은 쓰레드 존재 여부를 알지 못하며 프로세스 동작 만을 관리한다 • 쓰레드가 시스템 콜을 요구하면 프로세스 전체가 대기 상태가 된다 • Thread library에 대해서는 프로세스가 대기 상태에 있더라도 현재의 쓰레드를 실행 상태로 인식한다 • 쓰레드 상태는 프로세스 상태와 독립적이다 신라대학교 컴퓨터공학과 - 운영체제

  25. 장 점: Thread switching이 커널과 상관없이 이루어진다: no mode switching Thread scheduling이 응용 프로그램에 의해 정의 할 수 있다 응용 프로그램에 가장 적합한 스케줄링 알고리즘을 선택할 수 있다 ULT은 어떠한 운영체제에서도 실행할 수 있다 단지 thread library가 필요하다 ULT 장점 신라대학교 컴퓨터공학과 - 운영체제

  26. 문제점: 대부분의 시스템 콜에 대해 커널은 프로세스를 대기 상태로 전환시킨다 프로세스 내의 모든 쓰레드가 대기 상태가 된다 커널은 프로세스를 프로세서(CPU)에 할당한다 프로세스 내의 두 개의 쓰레드는 다중 프로세서가 있더라도 동시에 실행할 수 없다 ULT 문제점 신라대학교 컴퓨터공학과 - 운영체제

  27. Kernel-Level Threads (KLT) • 커널이 모든 쓰레드 관리를 수행한다 • thread library가 없으며 커널 쓰레드 기능에 대한 API가 지원된다 • 커널은 프로세스와 쓰레드에 대한 문맥 정보(context information) 을 유지한다 • 커널이 쓰레드 간의 전환을 수행한다 • 커널이 쓰레드 스케줄링을 수행한다 • 예: Windows NT and OS/2 신라대학교 컴퓨터공학과 - 운영체제

  28. 장 점: 커널은 쓰레드 수준에서 프로세서 스케줄링을 수행한다 커널은 하나의 프로세스 내의 쓰레드가 다중 프로세서에서 동시에 실행될 수 있도록 스케줄링 한다 쓰레드 수준에서 blocking이 수행된다 하나의 쓰레드가 시스템 콜로 대기 상태가 되더라도 프로세스 내의 다른 쓰레드는 실행 상태를 유지한다 커널 루틴 자체도 multithread로 구성할 수 있다 KLT 장점 신라대학교 컴퓨터공학과 - 운영체제

  29. 문제점 프로세스 내의 쓰레드 전환(thread switching)이 커널에 의해 수행된다 쓰레드 전환 오버헤드가 크다 프로그램 실행 속도가 떨어진다 KLT 문제점 신라대학교 컴퓨터공학과 - 운영체제

  30. Combined ULT/KLT Approaches • 쓰레드 생성은 응용 프로그램에 의해 수행 • 쓰레드 스케줄링과 동기화가 사용자 공간에서 수행 • 사용자는 KLTs 개수를 조정한다 • 예: Solaris 신라대학교 컴퓨터공학과 - 운영체제

  31. Solaris • Process includes the user’s address space, stack, and process control block • User-level threads (threads library) • invisible to the OS • are the interface for application parallelism • Kernel threads • the unit that can be dispatched on a processor • it’s structures are maintainedby the kernel • Lightweight processes (LWP) • each LWP supports one or more ULTs and maps to exactly one KLT • each LWP is visible to the application 신라대학교 컴퓨터공학과 - 운영체제

  32. Process 2 is equivalent to a pure ULT approach Process 4 is equivalent to a pure KLT approach We can specify a different degree of parallelism (process 3 and 5) 신라대학교 컴퓨터공학과 - 운영체제

  33. Solaris: versatility • We can use ULTs when logical parallelism does not need to be supported by hardware parallelism (we save mode switching) • Ex: Multiple windows, but only one is active at any one time • If threads may block then we can specify two or more LWPs to avoid blocking the whole application 신라대학교 컴퓨터공학과 - 운영체제

  34. Solaris: user-level thread states (attached to a LWP) 신라대학교 컴퓨터공학과 - 운영체제

  35. Solaris: Lightweight Process States LWP states are independent of ULT states (except for bound ULTs) 신라대학교 컴퓨터공학과 - 운영체제

More Related