From Processes to ThreadsProcesses, Threads and ProcessorsProcesses and ThreadsThe Case for ThreadsSlide 5Programmer’s ViewIntroducing ThreadsContext switch time for which entity is greater?How Can it Help?Slide 10Overlapping Requests (Concurrency)Threads have their own…?Threads vs. ProcessesImplementing ThreadsThreads’ Life CycleThreads have the same scheduling states as processesUser-level vs. Kernel-level threadsLanguages vs. SystemsLatency and ThroughputRelationship between Latency and ThroughputThread or Process PoolWhen a user level thread does I/O it blocks the entire process.1From Processes to Threads2Processes, Threads and ProcessorsHardware can interpret N instruction streams at onceUniprocessor, N==1Dual-core, N==2Sun’s Niagara T2 (2007) N == 64, but 8 groups of 8An OS can run 1 process on each processor at the same timeConcurrent execution increases perforamnceAn OS can run 1 thread on each processor at the same time3Processes and ThreadsProcess abstraction combines two conceptsConcurrencyEach process is a sequential execution stream of instructionsProtectionEach process defines an address spaceAddress space identifies all addresses that can be touched by the programThreadsKey idea: separate the concepts of concurrency from protectionA thread is a sequential execution stream of instructionsA process defines the address space that may be shared by multiple threadsThreads can execute on different cores on a multicore CPU (parallelism for performance) and can communicate with other threads by updating memory4The Case for ThreadsConsider the following code fragmentfor(k = 0; k < n; k++)a[k] = b[k] * c[k] + d[k] * e[k];Is there a missed opportunity here? On a Uni-processor? On a Multi-processor?5The Case for ThreadsConsider a Web serverget network message (URL) from clientget URL data from diskcompose responsesend responseHow well does this web server perform?6Programmer’s Viewvoid fn1(int arg0, int arg1, …) {…}main() {…tid = CreateThread(fn1, arg0, arg1, …);…}At the point CreateThread is called, execution continues in parent thread in main function, and execution starts at fn1 in the child thread, both in parallel (concurrently)7Introducing ThreadsA thread represents an abstract entity that executes a sequence of instructionsIt has its own set of CPU registers It has its own stackThere is no thread-specific heap or data segment (unlike process)Threads are lightweightCreating a thread more efficient than creating a process.Communication between threads easier than btw. processes.Context switching between threads requires fewer CPU cycles and memory references than switching processes.Threads only track a subset of process state (share list of open files, pid, …)Examples:OS-supported: Windows’ threads, Sun’s LWP, POSIX threadsLanguage-supported: Modula-3, JavaThese are possibly going the way of the Dodo8Context switch time for which entity is greater?1. Process2. Thread9How Can it Help?How can this code take advantage of 2 threads?for(k = 0; k < n; k++)a[k] = b[k] * c[k] + d[k] * e[k];Rewrite this code fragment as:do_mult(l, m) {for(k = l; k < m; k++)a[k] = b[k] * c[k] + d[k] * e[k];}main() { CreateThread(do_mult, 0, n/2); CreateThread(do_mult, n/2, n);What did we gain?10How Can it Help?Consider a Web server Create a number of threads, and for each thread doget network message from clientget URL data from disksend data over networkWhat did we gain?11Overlapping Requests (Concurrency)get network message (URL) from clientget URL data from disksend data over networkget network message (URL) from clientget URL data from disksend data over networkRequest 1Thread 1Request 2Thread 2Time(disk access latency)(disk access latency)Total time is less than request 1 + request 212Threads have their own…?1. CPU2. Address space3. PCB4. Stack5. Registers13Threads vs. ProcessesThreadsA thread has no data segment or heapA thread cannot live on its own, it must live within a processThere can be more than one thread in a process, the first thread calls main & has the process’s stackIf a thread dies, its stack is reclaimedInter-thread communication via memory.Each thread can run on a different physical processorInexpensive creation and context switchProcessesA process has code/data/heap & other segmentsThere must be at least one thread in a processThreads within a process share code/data/heap, share I/O, but each has its own stack & registersIf a process dies, its resources are reclaimed & all threads dieInter-process communication via OS and data copying.Each process can run on a different physical processorExpensive creation and context switch14Implementing ThreadsProcesses define an address space; threads share the address spaceProcess Control Block (PCB) contains process-specific information Owner, PID, heap pointer, priority, active thread, and pointers to thread informationThread Control Block (TCB) contains thread-specific informationStack pointer, PC, thread state (running, …), register values, a pointer to PCB, …CodeInitialized dataHeapDLL’smapped segmentsProcess’s address spaceStack – thread1PCSPStateRegisters…PCSPStateRegisters…TCB for Thread1Stack – thread2PCSPStateRegisters…PCSPStateRegisters…TCB for Thread215Threads’ Life CycleThreads (just like processes) go through a sequence of start, ready, running, waiting, and done states RunningRunningReadyReadyWaitingWaitingStartStartDoneDone16Threads have the same scheduling states as processes1. True2. False17User-level threads (M to 1 model)+ Fast to create and switch+ Natural fit for language-level threads- All user-level threads in process block on OS callsE.g., read from file can block all threads-User-level scheduler can fight with kernel-level schedulerKernel-level threads (1 to 1 model)+ Kernel-level threads do not block process for syscall+ Only one scheduler (and kernel has global view)- Can be difficult to make efficient (create & switch)User-level vs. Kernel-level threadsuserkernelProcess0 Process118Kernel-level threads have won for systemsLinux, Solaris 10, Windowspthreads tends to be kernel-level threadsUser-level threads still used for languages (Java)User tells JVM how many underlying system threadsDefault: 1 system threadJava runtime intercepts blocking calls, makes them non-blockingJNI code that makes blocking syscalls
View Full Document