Unformatted text preview:

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 onceUniprocessor, N==1Dual-core, N==2Sun’s Niagara T2 (2007) N == 64, but 8 groups of 8An OS can run 1 process on each processor at the same timeConcurrent execution increases perforamnceAn OS can run 1 thread on each processor at the same time3Processes and ThreadsProcess abstraction combines two conceptsConcurrencyEach process is a sequential execution stream of instructionsProtectionEach process defines an address spaceAddress space identifies all addresses that can be touched by the programThreadsKey idea: separate the concepts of concurrency from protectionA thread is a sequential execution stream of instructionsA process defines the address space that may be shared by multiple threadsThreads 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 instructionsIt has its own set of CPU registers It has its own stackThere is no thread-specific heap or data segment (unlike process)Threads are lightweightCreating 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 threadsLanguage-supported: Modula-3, JavaThese 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 doget network message from clientget URL data from disksend data over networkWhat did we gain?11Overlapping Requests (Concurrency)get network message (URL) from clientget URL data from disksend data over networkget network message (URL) from clientget URL data from disksend 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 informationStack 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 callsE.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 systemsLinux, Solaris 10, Windowspthreads tends to be kernel-level threadsUser-level threads still used for languages (Java)User tells JVM how many underlying system threadsDefault: 1 system threadJava runtime intercepts blocking calls, makes them non-blockingJNI code that makes blocking syscalls


View Full Document

UT CS 372 - From Processes to Threads

Documents in this Course
MapReduce

MapReduce

17 pages

Processes

Processes

19 pages

MapReduce

MapReduce

17 pages

Load more
Download From Processes to Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view From Processes to Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view From Processes to Threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?