1CSE 120CSE 120Principles of Operating Principles of Operating SystemsSystemsFall 2002Fall 2002Lecture 5: ThreadsLecture 5: ThreadsGeoffrey M. VoelkerGeoffrey M. VoelkerOctober 8, 2002 CSE 120 – Lecture 5 – Threads 2AnnouncementsAnnouncementsz UCSD CSE Programming Contest This Friday (10/11)◆ Why should you go? To be like John:http://www-cse.ucsd.edu/users/calder/UCSDProgramContest/2October 8, 2002 CSE 120 – Lecture 5 – Threads 3Announcements (2)Announcements (2)z Chancellor’s 5K run on 10/18◆ Finish ahead of me and you get a gold star on your midterm» Yes, that’s right, a gold star!◆ Send me mail after the results are finalizedOctober 8, 2002 CSE 120 – Lecture 5 – Threads 4ProcessesProcessesz Recall that a process includes many things◆ An address space (defining all the code and data pages)◆ OS resources (e.g., open files) and accounting information◆ Execution state (PC, SP, regs, etc.)z Creating a new process is costly because of all of the data structures that must be allocated and initialized◆ FreeBSD: 81 fields, 408 bytes◆ …which does not even include page tables, etc.z Communicating between processes is costly because most communication goes through the OS◆ Overhead of system calls and copying data3October 8, 2002 CSE 120 – Lecture 5 – Threads 5Parallel ProgramsParallel Programsz Also recall our Web server example that forks off copies of itself to handle multiple simultaneous requests◆ Or any parallel program that executes on a multiprocessor z To execute these programs we need to◆ Create several processes that execute in parallel◆ Cause each to map to the same address space to share data» They are all part of the same computation◆ Have the OS schedule these processes in parallel (logically or physically)z This situation is very inefficient◆ Space: PCB, page tables, etc.◆ Time: create data structures, fork and copy addr space, etc.October 8, 2002 CSE 120 – Lecture 5 – Threads 6Rethinking ProcessesRethinking Processesz What is similar in these cooperating processes?◆ They all share the same code and data (address space)◆ They all share the same privileges◆ They all share the same resources (files, sockets, etc.)z What don’t they share?◆ Each has its own execution state: PC, SP, and registersz Key idea: Why don’t we separate the concept of a process from its execution state?◆ Process: address space, privileges, resources, etc.◆ Execution state: PC, SP, registersz Exec state also called thread of control, or thread4October 8, 2002 CSE 120 – Lecture 5 – Threads 7ThreadsThreadsz Modern OSes (Mach, Chorus, NT, modern Unix) separate the concepts of processes and threads◆ The thread defines a sequential execution stream within a process (PC, SP, registers)◆ The process defines the address space and general process attributes (everything but threads of execution)z A thread is bound to a single process◆ Processes, however, can have multiple threadsz Threads become the unit of scheduling◆ Processes are now the containers in which threads execute◆ Processes become static, threads are the dynamic entitiesOctober 8, 2002 CSE 120 – Lecture 5 – Threads 8Threads in a ProcessThreads in a ProcessStack (T1)CodeStatic DataHeapStack (T2)Stack (T3)Thread 1Thread 3Thread 2PC (T1)PC (T3)PC (T2)5October 8, 2002 CSE 120 – Lecture 5 – Threads 9Thread Design SpaceThread Design SpaceOne Thread/ProcessMany Address Spaces(Unix)One Thread/ProcessOne Address Space(MSDOS)Many Threads/ProcessMany Address Spaces(Mach, OSF, NT, Chorus)Many Threads/ProcessOne Address Space(Pilot, Java)Address SpaceThreadOctober 8, 2002 CSE 120 – Lecture 5 – Threads 10Process/Thread SeparationProcess/Thread Separationz Separating threads and processes makes it easier to support multithreaded applications◆ Creating concurrency does not require creating new processesz Concurrency (multithreading) can be very useful◆ Improving program structure◆ Handling concurrent events (e.g., Web requests)◆ Writing parallel programsz So multithreading is even useful on a uniprocessor6October 8, 2002 CSE 120 – Lecture 5 – Threads 11Threads: Concurrent ServersThreads: Concurrent Serversz Using fork() to create new processes to handle requests in parallel is overkill for such a simple taskz Recall our forking Web server:while (1) {int sock = accept();if ((child_pid = fork()) == 0) {Handle client requestClose socket and exit} else {Close socket}}October 8, 2002 CSE 120 – Lecture 5 – Threads 12Threads: Concurrent ServersThreads: Concurrent Serversz Instead, we can create a new thread for each requestweb_server() {while (1) {int sock = accept();thread_fork(handle_request, sock);}}handle_request(int sock) {Process requestclose(sock);}7October 8, 2002 CSE 120 – Lecture 5 – Threads 13KernelKernel--Level ThreadsLevel Threadsz We have taken the execution aspect of a process and separated it out into threads◆ To make concurrency cheaperz As such, the OS now manages threads and processes◆ All thread operations are implemented in the kernel◆ The OS schedules all of the threads in the systemz OS-managed threads are called kernel-level threadsor lightweight processes◆ NT: threads◆ Solaris: lightweight processes (LWP)October 8, 2002 CSE 120 – Lecture 5 – Threads 14Kernel Thread LimitationsKernel Thread Limitationsz Kernel-level threads make concurrency much cheaper than processes◆ Much less state to allocate and initializez However, for fine-grained concurrency, kernel-level threads still suffer from too much overhead◆ Thread operations still require system calls» Ideally, want thread operations to be as fast as a procedure call◆ Kernel-level threads have to be general to support the needs of all programmers, languages, runtimes, etc.z For such fine-grained concurrency, need even “cheaper” threads8October 8, 2002 CSE 120 – Lecture 5 – Threads 15UserUser--Level ThreadsLevel Threadsz To make threads cheap and fast, they need to be implemented at user level◆ Kernel-level threads are managed by the OS◆ User-level threads are managed entirely by the run-time system (user-level library)z User-level threads are small and fast◆ A thread is simply represented by a PC, registers, stack, and small thread control block (TCB)◆ Creating a new thread, switching between threads, and synchronizing threads are done via procedure call» No kernel involvement◆ User-level thread operations 100x faster than kernel
View Full Document