Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.

Similar presentations


Presentation on theme: "ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH."— Presentation transcript:

1 ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

2 ITEC502 컴퓨터 시스템 및 실습 2 Contents 1.Threads vs. Process 2.Thread Model 3.Thread Usage 4.Implementing Threads 5.Scheduler Activations 6.Pop-up Threads 7.Making Single-Threaded Code Multithreaded

3 ITEC502 컴퓨터 시스템 및 실습 3 Threads vs. Processes  The process model is based on two concepts: ‘ resource grouping ’ + ‘ execution ’  Resource grouping of a process –an address space for program text and data –other resources: open files, child processes, etc.  Execution of a process –a single control sequence (i.e., program counter) –why not multiple control sequences (threads)?  multi-threading!!!

4 ITEC502 컴퓨터 시스템 및 실습 4 Thread Model (1)  A process can have one or more threads  A thread consists of: –its own program counter (PC) –its own set of registers –its own (run-time) stack a frame for each invoked proc. that has not yet been done  Each thread is subject to scheduling  Also called lightweight process

5 ITEC502 컴퓨터 시스템 및 실습 5 Thread Model (2) (a) Three processes each with one thread (b) One process with three threads

6 ITEC502 컴퓨터 시스템 및 실습 6 Thread Model (3)  Items shared by all threads in a process  Items private to each thread

7 ITEC502 컴퓨터 시스템 및 실습 7 Thread Model (4) Each thread has its own stack

8 ITEC502 컴퓨터 시스템 및 실습 8 Thread Model (5)  Thread-related library procedures –thread_create() –thread_exit() –thread_wait() –thread_yield()

9 ITEC502 컴퓨터 시스템 및 실습 9 Thread Model (6)  Multi-threading may introduce lots of difficulties/complications to programming –what if a parent process calls fork() ? Should the child inherit threads too? –what if a thread closes a file while another thread is reading from the same file?  The problems can be solved with some efforts, but careful thought and design should be used!

10 ITEC502 컴퓨터 시스템 및 실습 10 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, state  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

11 ITEC502 컴퓨터 시스템 및 실습 11 Single & Multithreaded Processes

12 ITEC502 컴퓨터 시스템 및 실습 12 Thread Usage (1) When threads are useful?  In many applications, several activities are going at once –multi-threading may provide a simpler programming model  Threads do not have any resources to attached to them –much easier and cheaper to create and destroy them  If there are substantial computing and, also, I/O, then they can be overlap by threads –there is no performance gain from CPU bound applications  Threads are useful on systems with multiple CPUs

13 ITEC502 컴퓨터 시스템 및 실습 13 Thread Usage (2): Multithreaded programs  Many software packages that run on modern OS are multi-threaded  A web browser might have –One thread display 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 from the user –A third thread for writing the contents of RAM to disk periodically

14 ITEC502 컴퓨터 시스템 및 실습 14 Thread Usage (3) A word processor with three threads

15 ITEC502 컴퓨터 시스템 및 실습 15 Thread Usage (4): Multithreaded 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

16 ITEC502 컴퓨터 시스템 및 실습 16 Thread Usage (5) A multithreaded Web server

17 ITEC502 컴퓨터 시스템 및 실습 17 Thread Usage (6)  Rough outline of code for previous slide (a) Dispatcher thread (b) Worker thread

18 ITEC502 컴퓨터 시스템 및 실습 18 Thread Usage (7) Three ways to construct a server

19 ITEC502 컴퓨터 시스템 및 실습 19 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

20 ITEC502 컴퓨터 시스템 및 실습 20 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

21 ITEC502 컴퓨터 시스템 및 실습 21 Implementing Threads in User Space A user-level threads package

22 ITEC502 컴퓨터 시스템 및 실습 22 Pros & Cons of User Threading  All thread-related activities are user procedures –no system calls or context switching –very fast  Each process can have a customized scheduler  BUT …  How blocking system calls are implemented? –blocking vs. non-blocking system calls –can use hints whether a system call will be blocking or not  jacket or wrapper  A single thread may run forever  Many applications (e.g., web servers) where threading is useful generally block often – if they block, often no more work is left to be done!

23 ITEC502 컴퓨터 시스템 및 실습 23 Implementing Threads in the Kernel A threads package managed by the kernel No run-time system, nor thread table in user space needed! all thread-related procedure are now system calls! the cost for creating and destroying threads can be high!  why not recycle threads!!!

24 ITEC502 컴퓨터 시스템 및 실습 24 Pros & Cons of Kernel Threading  No need for non-blocking system calls  Time-out is supported by OS  BUT …  System calls for thread management can be high  Less flexible –Custom scheduler not possible unless kernel is recompiled (and installed)

25 ITEC502 컴퓨터 시스템 및 실습 25 Hybrid Implementations Multiplexing user-level threads onto kernel- level threads

26 ITEC502 컴퓨터 시스템 및 실습 26 Scheduler Activations (1)  Goal – mimic functionality of kernel threads –gain performance of user space threads  Avoids unnecessary user/kernel transitions  Kernel assigns virtual processors to each process –lets runtime system allocate threads to processors  Basic mechanism: –when a thread makes a blocking system call, the kernel activates the user run-time system  upcall –when an interrupt occurs, the kernel also let the user run-time system decide which thread to run

27 ITEC502 컴퓨터 시스템 및 실습 27 Scheduler Activations (2)  Problem: –fundamental reliance on kernel (lower layer) –calling procedures in user space (higher layer)  violation of a fundamental principle!

28 ITEC502 컴퓨터 시스템 및 실습 28 Pop-Up Threads (1)  Can be useful in distributed systems –handling incoming messages (service requests)

29 ITEC502 컴퓨터 시스템 및 실습 29 Pop-Up Threads (2)  Pop-up threads are all brand-new! –no history to restore –they are also all identical –it is cheap to create them!  Pop-up threads in kernel space –easier and faster than in user space –direct access to kernel resources (tables, IO devices … ) –but, what if they are buggy??

30 ITEC502 컴퓨터 시스템 및 실습 30 Making Single-Threaded Code Multithreaded (1) Conflicts between threads over the use of a global variable

31 ITEC502 컴퓨터 시스템 및 실습 31 Making Single-Threaded Code Multithreaded (2) Threads can have private global variables

32 ITEC502 컴퓨터 시스템 및 실습 32 Making Single-Threaded Code Multithreaded (3)  Other problems –Many library functions are not reentrant –Difficulty with signals/interrupts –Difficulty with stack management  Those problems may be solved one way or another –But tricky –May require substantial system redesign, redefining system calls and library functions  also should be backward compatible!!!!

33 ITEC502 컴퓨터 시스템 및 실습 33 Summary  A thread is a flow of control within process –Multithreaded process contains several different flows of control within the same address space  Benefits of multi-threading includes –Increased responsiveness, Resource sharing within the process –Economy, Ability to take advantage of multiprocessor architecture  User-level thread are thread that are visible to the programmer and are unknown to the kernel  OS kernel supports and manages kernel-level threads

34 ITEC502 컴퓨터 시스템 및 실습 34 Review 1.Threads vs. Process 2.Thread Model 3.Thread Usage 4.Implementing Threads 5.Scheduler Activations 6.Pop-up Threads 7.Making Single-Threaded Code Multithreaded


Download ppt "ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH."

Similar presentations


Ads by Google