1 / 18

Operating Systems Lecture 14 Threads 2 Read Ch 5.4 - 5.8

Operating Systems Lecture 14 Threads 2 Read Ch 5.4 - 5.8. User and Kernel Level Threads. User level threads : are implemented with user thread libraries. The kernel is unaware of the threads. Kernel level threads: Implemented by the Operating systems. Questions about User Level Threads.

carsyn
Télécharger la présentation

Operating Systems Lecture 14 Threads 2 Read Ch 5.4 - 5.8

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. Operating SystemsLecture 14 Threads 2 Read Ch 5.4 - 5.8 Operating System Concepts

  2. User and Kernel Level Threads • User level threads: • are implemented with user thread libraries. • The kernel is unaware of the threads. • Kernel level threads: • Implemented by the Operating systems. Operating System Concepts

  3. Questions about User Level Threads • When one thread opens a file, do the other threads see the file? • Does the OS choose the thread, the process or both to execute? • When one thread is blocked in a process (e.g. for I/O) what happens to the other threads? • When a process is swapped, what happens to the ready threads? • What are the key thread states? Operating System Concepts

  4. Questions about Kernel Level Threads • When one thread opens a file, do the other threads see the file? • Does the OS choose the thread, the process or both to execute? • When one thread is blocked in a process (e.g. for I/O) what happens to the other threads? • When a process is swapped, what happens to the ready threads? • What are the key thread states? • What is the difference between a threaded kernel and kernel threads? Operating System Concepts

  5. fork( ) and exec( ) with threads If one thread in a multi-threaded program calls fork( ), does the new process duplicate all threads or only the thread that made the call? It depends on the version of UNIX. Some UNIX versions have 2 versions of fork( )-- one that duplicates all threads and one that duplicates only one thread. What happens if exec( ) is called from a thread? The same as before--the entire process is replaced. If exec( ) is called immediately after fork( ) then it is not necessary to duplicate all threads. Operating System Concepts

  6. Thread Cancellation • Thread cancellation is the task of terminating a thread before it is completed. • The thread to be cancelled is called the target thread. • Asynchronous cancellation: One thread immediately terminates the target thread. This can cause problems: • Cancellation may occur while the thread is in the middle of updating shared data. • The OS may not reclaim all resources from the cancelled thread. • Deferred cancellation: Target thread can periodically check to determine whether it should terminate • This allows the target thread an opportunity to terminate itself in an orderly fashion. • Threads are only terminated at cancellation points. Operating System Concepts

  7. Signal Handling • A signal in UNIX is used to notify a process that an event has occurred. • A signal can be synchronous or asynchronous, depending on the event that generated it. • Response to a signal: • A signal is generated by the occurrence of an event. • The generated signal is delivered to a process. • Once delivered the signal must be handled. It is handled one of two ways: • The default signal handler (in the kernel) • A user-defined signal handler. Operating System Concepts

  8. Signals and Threads • Options for delivering signals to a multi-threaded process: • Deliver the signal to the thread to which the signal applies. • E.g. a divide by zero generates an asynchronous signal. • Deliver the signal to every thread in the process • E.g. when the user hits <Control>-C • Deliver the signal to certain threads in the process. • some threads can specify which signals they will accept and which they will block. • Typically, the signal is delivered only to the first thread that is not blocking it. • Assign a specific thread to receive all signals for the process (Solaris 2) • A special thread gets the signals and then delivers them to the first thread not blocking the signal. Operating System Concepts

  9. Thread Pools • The problem with allowing a process(e.g. a multi-threading web server) to create as many threads as it wants: • It takes time to create a thread prior to handling a service request. • Unlimited number of threads could exhaust system resources. • Solution: Thread pools. • A thread pool contains a limited number of threads created at process startup. • When the program needs a thread, it takes one from the pool. • When the thread is done with its service, it is returned to the pool. • If no thread is available, the program must wait for one to be returned to the pool. • Benefits of thread pools: • Faster--the process doesn't have to create the threads each time. • Limits the number of threads. Operating System Concepts

  10. Thread specific data • Threads share most of their data with other threads. • A thread may need its own data for certain tasks. This is called thread-specific data. • Example: Transaction processing. • Each transaction may be handled by a separate thread. • Each transaction may have a unique ID. • Most thread libraries provide support for thread-specific data. Operating System Concepts

  11. Pthreads • Pthreads is the POSIX standard defining an API for thread creation and synchronization. • It is a specification for thread behavior. Different operating systems may implement it in different ways. • Pthreads are generally used in UNIX-based systems. • Pthreads are considered user level threads. • Example: create a separate thread that computes the summation of the first n integers. Operating System Concepts

  12. #include <pthread.h> #include <stdio.h> int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* the thread */ main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of attributes for the thread */ /*some error checking here */ pthread_attr_init(&attr); /* get the default attributes */ pthread_create(&tid,&attr,runner,argv[1]); /* create the thread */ pthread_join(tid,NULL); /* now wait for the thread to exit */ printf("sum = %d\n",sum); } Operating System Concepts

  13. The thread function void *runner(void *param) { int upper = atoi(param); int i; sum = 0; if (upper > 0) { for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); } Operating System Concepts

  14. Solaris 2 threads • Solaris 2 uses an intermediate level of threads, called lightweight processes (LWPs), between the user level threads and the kernel level threads. • Each LWP is attached to a kernel level thread. • User threads are multiplexed among LWPs. • Bound threads are permanently attached to an LWP. Only one thread is attached to that LWP. • Unbound threads are not permanently attached to an LWP. These are multiplexed among the available LWPs. • The PCB includes a linked list of LWPs associated with the process. Operating System Concepts

  15. Diagram of Solaris 2 threads Operating System Concepts

  16. Solaris 2 process Operating System Concepts

  17. Windows 2000 threads • Windows applications run as a single process containing one or more threads. • Windows implements the one-to-one mapping. • Each thread contains - a thread id - register set - separate user and kernel stacks - private data storage area Operating System Concepts

  18. Linux and Java threads Linux threads: • Linux refers to them as tasks rather than threads. • Thread creation is done through clone() system call. • Clone() allows a child task to share the address space of the parent task (process) • A fork( ) command creates a separate process with a copy of the address space of the parent. • A clone( ) command creates a process with a pointer to the address space of the parent. Java Threads: • Java threads may be created by: • Extending Thread class • Implementing the Runnable interface. • Java threads are managed by the JVM. Operating System Concepts

More Related