Outline for EECS 678 with Professor Kulkarni at KU
Outline for EECS 678 with Professor Kulkarni at KU
Popular in Course
Popular in Department
This 29 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at Kansas taught by a professor in Fall. Since its upload, it has received 18 views.
Reviews for Outline for EECS 678 with Professor Kulkarni at KU
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
Chapter 4 Threads Outline What are threads How do they compare with processes Why are threads important What are the common multithreading models What are the common multithreading libraries Discussion on threading issues Examples of threads in contemporary OSes EECS 678 Introduction to Operating Systems 7 Spring 2009 Process Overview l The basic unit of CPU utilization is a process I To run a program a sequence of instructions create a process I Process properties fork gt exec can be used to start new program execution processes are well protected from each other contextswitching between processes is fairly expensive interprocess communication used for information sharing and co ordination between processes gt shared memory gt message passing EECS 678 Introduction to Operating Systems 7 Spring 2009 Is the Process Abstraction Always Suitable I Consider characteristics for a game software 0 different code sequences for different game objects gt soldiers cities airplanes cannons usercontrolled heroes etc 0 each object is more or less independent I Problems 0 single monolithic process may not utilize resources optimally gt can create a process for each object 0 action of an object depends on game state gt sharing and coordination of information necessary gt IPC is expensive 0 number of objects proceed simultaneously gt may involve lots of context switches gt process context switches are expensive EECS 678 Introduction to Operating Systems 7 Spring 2009 Is the Process Abstraction Always Suitable 2 l Ability to run multiple sequences of code threads of control for different object 0 individual process only offers one thread of control I Way for threads of control to share data effectively 0 processes NOT designed to do this I Protection between threads of control not very important 0 all in one application anyway 0 process is an overkill l Switching between threads of control must be efficient o context switching involves a lot of overhead l Different threads of control may share most information o processes duplicate entire address space EECS 678 Introduction to Operating Systems 7 Spring 2009 Threads to the Rescue l Threads are designed to achieve all the above requirements 0 do as little as possible to allow execution of a thread of control I Thread are known as a lightweight process 0 only the necessary context information is regenerated gt threadcontext PC registers stack other misc info gt processcontext also includes data and code regions 0 threads are executed within a process gt code and data shared among different threads gt reduced communication overhead 0 smaller context gt faster context switching o a single address space for all threads in a process gt reduced interthread protection EECS 678 Introduction to Operating Systems 7 Spring 2009 Thread Basics l Thread a lightweight process 0 have their own independent flow of control 0 share process resources with other sibling threads 0 exist within the context space of the same process I Threads shared data process instructions a most data 0 open files descriptors signals and signal handlers current working directory 0 user and group id 0 l Threads specific data thread id registers stack pointer threadspecific data stack of activation records signal mask scheduling properties return value EECS 678 Introduction to Operating Systems 7 Spring 2009 Single and Multithreaded Process nrlddlmsm m 55m n Thread mmazu var mm mama varlk mi van 20 quot vars mumew van1 7 ext mama quot3 vaz2 numem m numezn pmsm mm mun am may m mama 0 may W 1 mama 1 map may 43quot Emma heap source httpscomputingllnlgovtuto alspthreads EECS 573 Inkoductxon Lo Opa39aung Systems 7 Spnng 2009 Thread Benefits l Responsiveness o for an interactive user if part of the application is blocked I Resource Sharing o easier via memory sharing 0 be aware of synchronization issues I Economy 0 sharing reduces creation contextswitching and space overhead l Scalability 0 can exploit computational resources of a multicore CPU EECS 678 Introduction to Operating Systems 7 Spring 2009 Thread Programming In Linux I Threads can be created using the Pthreads library 0 IEEE POSIX C language thread programming interface I Pthreads API 0 Thread management functions to create destroy detach join setquery thread attributes O Mutexes functions to enforce synchronization Create destroy lock unlock mutexes 0 Condition variables functions to manage thread communication Create destroy wait and signal based on specified variable values EECS 678 Introduction to Operating Systems 7 Spring 2009 Pth reads Example include ltpthreadhgt include ltstdiohgt int sum data shared by all threads void runnervoid param thread function prototype int main int argc char argv pthreadt tid thread identifier pthreadattrt attr set of thread attributes ifatoiargvl lt 0 fprintfstderr d must be gt0n atoiargvl return 1 get the default thread attributes pthreadattrinitampattr create the thread pthreadcreateamptid ampattr runner argvl wait for the thread to exit pthreadjointid NULL fprintfstdout sum dn sum EECS 678 Introduction to Operating Systems 7 Spring 2009 Pth reads Example 2 cont from previous page The thread will begin control in this function void runner void param int i upper atoiparam sum 0 foril iltupper i sum i pthreadexit0 EECS 678 Introduction to Operating Systems 7 Spring 2009 Pthread Example API Calls l pthreadattrinit initialize the thread attributes object 0 int pthreadattrinitpthreadattrt attr 0 defines the attributes of the thread created I pthreadcreate create a new thread 0 int pthreadcreatepthreadt restrict thread const pthreadattrt restrict attr void startroutinevoid void restrict arg 0 upon success a new thread id is returned in thread I pthreadjoin wait for thread to exit 0 int pthreadjoinpthreadt thread void valueptr o calling process blocks until thread exits l pthreadexit terminate the calling thread 0 void pthreadexitvoid valueptr 0 make return value available to thejoining thread EECS 678 Introduction to Operating Systems 7 Spring 2009 User Vs Kernel Level Threads l Userlevel threads manage threads in user code 0 advantages gt efficient and flexible in space speed switching and scheduling o disadvantages gt one thread blocked on O can block all threads gt difficult to automatically take advantage of SMP o examples gt GNU Portable Threads Free BSD39s userland threads QuickThreads l Kernellevel threads kernel manages the threads 0 Advantages gt removes disadvantages of userlevel threads 0 Disadvantages gt greater overhead due to kernel involvement 0 Examples gt Pthreads Windows XP threads etc EECS 678 Introduction to Operating Systems 7 Spring 2009 Multithreading Models l Relationships between user and kernel threads 0 ManytoOne o OnetoOne o ManytoMany EECS 678 Introduction to Operating Systems 7 Spring 2009 Many to One Multithreading Model I Many userlevel threads mapped to single kernel thread 0 examples Solaris Green Threads GNU Portable Threads g s lt kernel thread EECS 678 Introduction to Operating Systems 7 Spn39ng 2009 One to One Multithreading Model I Each userlevel thread maps to kernel thread 0 examples V ndows NTXP2000 Linux Solaris 9 and later lt user thread EECS 678 Introducu39on to Operating Systems 7 Spring 2009 Many to Many Multithreading Model I m user level threads mapped to n kernel threads 0 operating system can create a sufficient number of kernel threads 0 examples Solaris prior to v9 Windows NT2000 ThreadFber package E E g user thread lt kemel thread EECS 678 Introduction to Operating Systems 7 Spring 2009 Twolevel Multithreading Model I Similar to MM except that it also allows a user thread to be bound to kernel thread 0 examples IRIX HPUX Tru64 UNIX Solaris 8 and earlier 3 3 g 3 lt kernel thread EECS 678 Introduction to Operating Systems 7 Spn39ng 2009 Threading Issues Semantics of fork and exec system calls Thread cancellation of target thread Signal handling Thread pools Th readspecific data Scheduler activations EECS 678 Introduction to Operating Systems 7 Spring 2009 Semantics of fork and exec l Does fork duplicate only the calling thread or all threads 0 some systems provide two versions of fork I How about exec 0 most systems maintain the semantics of exec l Observations O exec called immediately after fork gt duplicating all threads is unnecessary o exec not called after fork gt new process should duplicate all threads EECS 678 Introduction to Operating Systems 7 Spring 2009 Thread Cancellation l Terminating a thread before it has finished I Asynchronous cancellation o terminates the target thread immediately 0 allocated resources may not all be freed easily 0 status of shared data may remain illdefined l Deferred cancellation 0 target thread terminates itself 0 orderly cancellation can be easily achieved 0 failure to check cancellation status may cause issues EECS 678 Introduction to Operating Systems 7 Spring 2009 Signal Handling l Signals are used in UNIX systems to notify a process that a particular event has occurred l A signal handler is used to process signals 0 08 may deliver the signal to the appropriate process 0 OS or process handles the signal I Types of signals 0 synchronous generated by some event in the process 0 asynchronous generated by an event outside the process I Where to deliver a signal in multithreaded programs deliver the signal to the thread to which the signal applies deliver the signal to every thread in the process deliver the signal to certain threads in the process assign a specific thread to receive all signals for the process EECS 678 Introduction to Operating Systems 7 Spring 2009 Thread Pools l Concerns with multithreaded applications 0 continuously creating and destroying threads is expensive 0 overshooting the bound on concurrently active threads I Thread Pools 0 create a number of threads in a pool where they await work 0 number of threads can be proportional to the number of processors l Advantages o faster to service a request with an existing thread than create a new thread every time 0 allows the number of threads in the applications to be bound to the size of the pool EECS 678 Introduction to Operating Systems 7 Spring 2009 Linux Thread Implementation l Linux refers to them as tasks rather than threads I Thread creation is done through clone system call I clone allows a child task to share the address space of the parent task process ag meaning CLONEiFS Filesystem information is shared CLONEJIM The same memory space is shared CLONEisIGHAND Signal handlers are shared CLONEiFILES The set of open files is shared EECS 678 Introduction w Opaaung Systems 7 Spring 2009 Windows XP Thread Implementation l Implements the onetoone mapping kernellevel l Each thread contains 0 A thread id 0 Register set 0 Separate user and kernel stacks 0 Private data storage area I The register set stacks and private storage area are known as the context of the threads I The primary data structures of a thread include 0 ETHREAD executive thread block 0 KTHREAD kernel thread block 0 TEB thread environment block EECS 678 Introduction to Operating Systems 7 Spring 2009 Windows XP Threads ETHREAD thread start address pointer to parent process KTHREAD scheduling gt and synchronization information kernel stack TEB thread identifier user stack threadlocal storage kernel space user space EECS 678 Introduction to Operating Systems 7 Spring 2009 Multicore Processors l Multiple processing cores on a single chip I Reasons for a shift to multicore processors 0 power wall 0 limits to frequency scaling o transistor scaling still a reality I Multicore programming Vs multicomputer programming 0 samechip communication is faster 0 memory sharing is easier and faster EECS 678 Introduction to Operating Systems 7 Spring 2009 Single Core Vs Multicore Execution single core T1 T2 T3 T4 T1 T2 T3 T4 T1 time core 1 core 2 Single core execution T1 T3 T1 T3 T1 T2 T4 T2 T4 T2 time Multiple core execution EECS 678 Introduction to Operating Systems 7 Spring 2009 Challenges for Multicore Programming l Dividing activities I Balance I Data splitting I Data dependency l Testing and debugging EECS 678 Introduction to Operating Systems 7 Spring 2009
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'