1 / 60

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

gabrielle
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) Operating System • 실행중인 프로그램 • 실행하기 위해 메모리에 적재되어거나 실행 준비중인 프로그램 • 사용자가 프로그램을 실행시키면 “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에 의해 수행 Operating System

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

  5. 프로세스 특성(2) • 최근 위의 두 가지 특성이 독립적으로 정의되고 있다 • 자원 할당 단위와 프로그램 실행 단위를 분리 • 프로세서 실행 단위(unit of dispatching)  athread or a LWP(lightweight process) • 자원 할당 단위 (unit of resource ownership)  a process or a task Operating System

  6. Threads(1) • 프로그램 실행 단위 또는 프로그램 실행 패스(path) • 하나의 프로세스는 여러 개의 thread로 구성 가능 • Thread는 실행 상태를 갖는다(running, ready, blocked etc.) • 실행 상태가 변할 때에 thread context switching이 수행된다 • Thread 별로 프로그램 실행 스택(execution stack)과 지역 변수를 위한 메모리를 갖는다 Operating System

  7. Threads(2) • 프로세스에 할당된 메모리 영역과 자원을 접근한다 • 하나의 프로세스를 구성하는 모든 쓰레드는 프로세스의 자원을 공유한다 • 하나의 쓰레드가 메모리 내용을 변경하면 다른 모든 쓰레드는 그 메모리 내용을 접근할 수 있다 • 하나의 쓰레드가 파일을 열면 다른 쓰레드가 그 파일을 접근할 수 있다 Operating System

  8. 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 Operating System

  9. Threads and Processes Operating System

  10. Single Threaded and Multithreaded Process Models Thread Control Block contains a register image, thread priority and thread state information Operating System

  11. Threads States • Three key states: running, ready, blocked • 동일한 프로세스을 구성하는 쓰레드는 메모리 공간을 공유하므로 중단 상태(suspend state)을 갖지 않는다 • 하나의 쓰레드를 suspending (i.e: swapping) 하는 것은 동일한 프로세스의 모든 쓰레드를 suspending 하게 된다 • 프로세스 종료는 프로세스 내의 모든 쓰레드를 종료한다 Operating System

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

  13. Thread 응용(2) • 응용 #2: User Interface thread on Windows • 하나의 쓰레드는 메뉴를 생성하여 사용자 입력을 받고 다른 쓰레드는 사용자 명령을 처리한다 Operating System

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

  15. Thread 장점(1) • 프로세스 보다 새로운 쓰레드를 생성하는데 시간이 덜 걸린다 • 프로세스 보다 쓰레드를 종료하는데 시간이 덜 걸린다 • 동일한 프로세스에 있는 두 개의 쓰레드 사이의 전환이 더 적은 시간을 소요한다 Operating System

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

  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 Operating System

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

  19. Producer Hard Disk Main memory (shared memory) Consumer Process와 Thread 비교(2) • Single process execution : • Sequential execution Process Operating System

  20. Main memory (Shared Memory) Producer process Consumer process Hard Disk Process와 Thread 비교(3) • Multiple process execution : • Concurrent execution with high communication overhead Kernel Operating System

  21. Producer Thread Hard Disk Local memory Consumer Thread Kernel Process와 Thread 비교(4) • Multithreading execution : • Concurrent execution with low communication overhead Process Operating System

  22. User-Level Threads (ULT) Operating System 커널은 쓰레드 존재를 알지 못하며 프로세스을 관리 한다 응용 프로그램이 thread library을 이용하여 모든 쓰레드를 관리한다 Thread switching은 kernel mode privileges을 요구하지 않는다 (no mode switch) 쓰레드 간의 실행 스케줄링은 응용 프로그램에 의해 정의된다

  23. Operating System

  24. Threads library • 다음 작업을 수행하는 코드를 지원한다: • 쓰레드 생성과 종료 • 쓰레드 간의 메시지와 데이터 전달 • 쓰레드 실행 스케줄링 • 쓰레드 문맥 전환(thread context switching) Operating System

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

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

  27. 문제점: 대부분의 시스템 콜에 대해 커널은 프로세스를 대기 상태로 전환시킨다 프로세스 내의 모든 쓰레드가 대기 상태가 된다 커널은 프로세스를 프로세서(CPU)에 할당한다 프로세스 내의 두 개의 쓰레드는 다중 프로세서가 있더라도 동시에 실행할 수 없다 ULT 문제점 Operating System

  28. Kernel-Level Threads (KLT) Operating System 커널이 모든 쓰레드 관리를 수행한다 thread library가 없으며 커널 쓰레드 기능에 대한 API가 지원된다 커널은 프로세스와 쓰레드에 대한 문맥 정보(context information) 을 유지한다 커널이 쓰레드 간의 전환을 수행한다 커널이 쓰레드 스케줄링을 수행한다 예: Windows NT and OS/2

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

  30. 문제점 프로세스 내의 쓰레드 전환(thread switching)이 커널에 의해 수행된다 쓰레드 전환 오버헤드가 크다 프로그램 실행 속도가 떨어진다 KLT 문제점 Operating System

  31. Combined ULT/KLT Approaches Operating System 쓰레드 생성은 응용 프로그램에 의해 수행 쓰레드 스케줄링과 동기화가 사용자 공간에서 수행 사용자는 KLTs 개수를 조정한다 예: Solaris

  32. Relationship Between Threads and Processes Operating System

  33. Categories of Computer Systems • Single Instruction Single Data (SISD) stream • Single processor executes a single instruction stream to operate on data stored in a single memory • Single Instruction Multiple Data (SIMD) stream • Each instruction is executed on a different set of data by the different processors Operating System

  34. Categories of Computer Systems • Multiple Instruction Single Data (MISD) stream • A sequence of data is transmitted to a set of processors, each of which executes a different instruction sequence. Never implemented • Multiple Instruction Multiple Data (MIMD) • A set of processors simultaneously execute different instruction sequences on different data sets Operating System

  35. Operating System

  36. Symmetric Multiprocessing • Kernel can execute on any processor • Typically each processor does self-scheduling form the pool of available process or threads Operating System

  37. Operating System

  38. Multiprocessor Operating System Design Considerations • Simultaneous concurrent processes or threads • Scheduling • Synchronization • Memory management • Reliability and fault tolerance Operating System

  39. Microkernels • Small operating system core • Contains only essential core operating systems functions • Many services traditionally included in the operating system are now external subsystems • Device drivers • File systems • Virtual memory manager • Windowing system • Security services Operating System

  40. Operating System

  41. Benefits of a Microkernel Organization • Uniform interface on request made by a process • Don’t distinguish between kernel-level and user-level services • All services are provided by means of message passing • Extensibility • Allows the addition of new services • Flexibility • New features added • Existing features can be subtracted Operating System

  42. Benefits of a Microkernel Organization • Portability • Changes needed to port the system to a new processor is changed in the microkernel - not in the other services • Reliability • Modular design • Small microkernel can be rigorously tested Operating System

  43. Benefits of a Microkernel Organization • Distributed system support • Message are sent without knowing what the target machine is • Object-oriented operating system • Components are objects with clearly defined interfaces that can be interconnected to form software Operating System

  44. Low-level memory management Mapping each virtual page to a physical page frame Microkernel Design Operating System

  45. Microkernel Design • Interprocess communication • I/O and interrupt management Operating System

  46. Windows Processes • Implemented as objects • An executable process may contain one or more threads • Both processes and thread objects have built-in synchronization capabilities Operating System

  47. Operating System

  48. Windows Process Object Operating System

  49. Windows Thread Object Operating System

  50. Windows 2000 - Thread States • Ready • Standby • Running • Waiting • Transition • Terminated Operating System

More Related