threads chapter 4. modern process & thread –process is an infrastructure in which execution...
Post on 22-Dec-2015
227 views
TRANSCRIPT
![Page 1: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/1.jpg)
Threads
Chapter 4
![Page 2: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/2.jpg)
Modern Process & Thread
– Process is an infrastructure in which execution takes place (address space + resources)
– Thread is a program in execution within a process context – each thread has its own execution stack
DataData
ProcessProcess
Stac
k
ProgramProgram
Operating SystemOperating System
Thread
Thread
Thread
Stac
k
…
Stac
k
![Page 3: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/3.jpg)
MS-DOS
Some variants of UNIX
Multithreaded Approaches
Windows, Solaris, modern versions of UNIX
Single-threaded approaches
JAVA run-time environment
![Page 4: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/4.jpg)
Thread (Lightweight Process) Has an execution state (running, ready, etc.) State is saved when not running Has an execution stack Some per-thread static storage for local variables Access to the memory and resources of its process
all threads of a process share this
![Page 5: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/5.jpg)
Benefits of Threads
Takes less time to create a new thread than a process
Less time to terminate a thread than a process
Less time to switch between two threads within the same process
Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel
Shared Address Space All threads in a process implicitly use that process’s address
space
Threads can share a program and data• If you want sharing, encode your work as threads in a process• Threads in separate processes can not facilitate sharing
![Page 6: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/6.jpg)
Thread ExamplesForeground to background work
(spreadsheet: one thread reading user input, another executing the commands and updating the spreadsheet; yet another making periodic backups)
Asynchronous processing(ex: a thread performing periodic backups against power failures in a word-processor)
Fast execution(on a Multiprocessor system, multiple threads can execute in parallel)
Modular program structure(different tasks/activities in a program may be implemented using different threads)
Client/Server computing
![Page 7: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/7.jpg)
User-Level Threads (ULT) All thread management is done by the application
The kernel is not aware of the existence of threads and schedules the process as a unit and assigns a single execution state (Ready, Running, Blocked, etc.)
Any application can be programmed to be multithreaded by using a threads library which contains code for creating and destroying threads, for passing messages and data between threads, for scheduling thread execution, and for saving and restoring thread contexts.
Disadvantages: when a ULT executes a blocking system call, all of the
threads within the same process are blocked can not take advantage of multiprocessing
![Page 8: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/8.jpg)
Kernel-Level Threads (KLT)
W2K, Linux, and OS/2 are examples of this approach
All of the work of thread management is done by the kernel. i.e. no thread management code in the program.
Kernel maintains context information for the process as a whole and for individual threads within the process
Scheduling is done by the kernel on a thread basis
Advantages: Kernel can simultaneously schedule multiple threads from
the same process on multiple processors If one thread in a process is blocked, the kernel can
schedule another thread of the same process
Disadvantage: transfer of control from one thread to the next in the same process requires a mode switch to the kernel
![Page 9: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/9.jpg)
Combined Approaches
Example: SUN Solaris
Thread creation, destruction, bulk of scheduling and synchronization are done in the user space.
multiple threads within the same application can run in parallel on multiple processors
if one is blocked, another thread within the same application need not block
![Page 10: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/10.jpg)
POSIX Thread Library (Linux)
http://web.mst.edu/~ercal/284/ThreadExamples/thread_handout.doc
Thread creation pthread_create()
Synchronization pthread_mutex_lock(), pthread_mutex_unlock(), pthread_mutex_trylock()
Suspension pthread_cond_wait(), pthread_cond_signal(), pthread_join()
Yielding pthread_yield()
Termination pthread_exit()
![Page 11: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/11.jpg)
![Page 12: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/12.jpg)
Symmetric Multiprocessing Kernel can execute on any processor
Typically each processor does self-scheduling from the pool of available processes or threads
![Page 13: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649d7f5503460f94a629f9/html5/thumbnails/13.jpg)
Multiprocessor Operating System Design Considerations
Concurrent processes or threads
Synchronization
Scheduling
Memory Management
Reliability and Fault Tolerance