Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Threads 1 Threads Operating Systems 370 Text: Chapter 4 Operating System Concepts with Java, Silberschatz, Glavin, Gagne Objectives: During this class the student shall be able to: List items threads share with other threads of the same process. Describe the states a thread may be in. Describe the differences between user and kernel threads, and describe the advantages of different models: Many-to-one, one-to-one, many-to-many. Describe how processes differ from threads. Define the advantages and disadvantages of threads versus processes. Define deferred cancellation, and describe why it is better than asynchronous cancellation Define thread pool and describe the advantages of a thread pool. Program java threads. Time Allocation: Class time will be allocated as follows: Intro. Thread models Threads versus Processes Java Syntax Homework 2 Discussion Mini-Lab TOTAL: 0.5 hour 0.5 hour 0.5 hour 0.5 hour 0.5 hour 1 hour 3.5 hours Threads Threads Use of threads: Foreground / Background work: Allocation of different functions to different threads. Asynchronous processing: e.g. periodic backups. Speed execution: simultaneous execution on multiprocessors Organizing programs: easier to program multiple (simultaneous) functions. Example Use: Web server: When request is received, server process creates thread to service request. Unit of execution: Process: Has own program code / data area. Thread: Shares program code / data area with other threads in the same process. Each Thread must have separate: Program counter Register set Stack: including local variables, procedure arguments Shared resources with other threads: Code or text section Data section O.S resources: e.g. open files, signals. Thread States (Example Java): New = New: Placed on the ready list. Blocked = Wait for an event Runnable = Ready or Running Dead = Exit 2 Threads 3 Programming Threads Thread functions: POSIX (Standard UNIX C/C++): pthread_create(): creates a thread pthread_exit(): kills itself pthread_kill(): sends a signal to a specified thread pthread_join(): waits for a thread to exit. pthread_self(): returns thread id synchronization functions Signals can send <ctl-C> or exceptions to other threads. Can specify thread_id to send signal to Thread can specify which signal it will handle Java Implementation Define a class to contain a thread, by defining: class Worker1 extends Thread OR class Worker2 implements Runnable (Recommended) The parent thread creates the object and invokes its start() function. The start function creates the child thread and calls the object’s run() function. Thread terminates when thread returns from run(). Thread Pool: Allocate N threads to handle all requests Enables threads to be allocated and deallocated quickly. Enables groups of threads to be allocated in a tree format – and destroyed. Enables a bunch of tasks to be allocated to a fixed set of threads; tasks may wait until a thread becomes available. Enables a parent thread to send interrupts to a set of threads easily. Terminating other threads Direct or Asynchronous Cancellation: Parent thread kills child thread directly Deferred Cancellation: Parent thread tells child thread to terminate itself at first convenient instant. Java: send notification: interrupt() Java: receive notification: interrupted() or isInterrupted() Java: returns from a system call via an InterruptException Direct cancellations can cause problems when a thread is in the middle of using a resource. Threads 4 Types of Thread Implementations User Thread Package A package outside the OS creates and schedules threads. Many-to-one model: Many user threads associated with one process. If one thread blocks, then entire process blocks. Thus all threads block. Kernel Thread Support The OS creates and schedules threads. One-to-one model: Each user thread is associated with one kernel thread. If one thread blocks, the kernel can schedule other threads. If multiprocessor configuration, kernel can allocate threads of same process on different processors. Most systems: Windows, Linux, Solaris 9 Many-to-Many Model: Combination of above N user threads share M kernel threads (where N > M) There may be a flexible and changing assignment of user threads to kernel threads. Bound: User-thread is assigned permanently to kernel thread Unbound: User threads share available kernel threads Example: IRIX, HP-UX Questions for discussion: Assume an application with 4 user threads How many kernel threads should be allocated, at minimum, for a 3-processor system, assuming maximum parallel execution is desirable? Assuming 2 threads block for lengthy periods, what is the minimum number of kernel threads that should be allocated? Threads 5 Processes Versus Threads Advantages (Thread vs. Process): Much quicker to create a thread than a process. Much quicker to switch between threads than to switch between processes. Threads share data easily Disadvantages (Thread vs. Process): No security between threads: One thread can stomp on another thread's data. For many-to-one model, If one thread blocks, all threads in task block. Questions for Discussion For the following code: Allocate integer count = 1 Open file “file1” Print “Hello” Create Thread/Process Parent: Increment count Open “file3” Print count Child Thread/Process: Increment count Open file “file 2” Print count For processes versus threads: How many times is “Hello” printed? What files are open in parent and in child? What is count in parent versus in child? What does parent and child print, and in what order? Threads 6 Threads Mini-Lab Introduction: The purpose of this mini-lab is to learn what threads share with other threads. Also, what are the advantages and disadvantages of having multiple threads share an object? Method: Run the ParentLaugh program. Each ‘External’ thread generates a new object with a thread attached. ‘Internal’ threads generate a thread on the current (same) object. Three threads are created which laugh different laughs: “Ha”, “Ho”, “He”. The laughs can be spaced out with an n-second delay before every print, or no delay. In order to learn what threads share, we will observe: 1) which data variables are shared; 2) whether signals (such as CTL-C are shared; 3) the number of processes running. Use 2 commands: $ ps –al $ ps –al H Results: Below are the results for internal and external mode, with varying sleep times: (Note laughs per line, interspersing of Ha/Ho/He, any other differences you see.) ParentLaugh Results One Object (Internal) Multiple Objects (External) Sleep 0 Sleep 1 CTL-C Effect on Threads Number of processes visible via $ ps –al And $ ps –al H Threads Analysis: Now explain what you think is going on in this code and O.S. between the differences you see (internal versus external), and how threads differ from working with processes: Conclusion: What did you learn from all this? 7