DOC PREVIEW
UCSD CSE 120 - Threads

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CSE 120CSE 120Principles of Operating Principles of Operating SystemsSystemsSystemsSystemsWinter Winter 20072007Lecture 4: ThreadsLecture 4: ThreadsKeith Marzullo and Geoffrey M. VoelkerKeith Marzullo and Geoffrey M. VoelkerAnnouncementsAnnouncementsz Homework #1 due nowzProject 0 due tonightzProject 0 due tonightz Project groups Please send project group info to Jeremy or Mike Project 1 out (no time like the present)January 18, 2007 CSE 120 – Lecture 4 – Threads 22ProcessesProcessesz Recall that a process includes many thingsAn address space (defining all the code and data pages)p( g pg) 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 Recall struct proc in Solaris …which does not even include page tables, perhaps TLB flushing etcJanuary 18, 2007 CSE 120 – Lecture 4 – Threads 3flushing, etc.z Communicating between processes is costly because most communication goes through the OS Overhead of system calls and copying dataParallel ProgramsParallel Programsz Also recall our Web server example that forks off copies of itself to handle multiple simultaneous requestsof 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 January 18, 2007 CSE 120 – Lecture 4 – Threads 4pp(gyphysically)z This situation is very inefficient Space: PCB, page tables, etc. Time: create data structures, fork and copy addr space, etc.3Rethinking ProcessesRethinking Processesz What is similar in these cooperating processes?They all share the same code and data (address space)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?January 18, 2007 CSE 120 – Lecture 4 – Threads 5process from its execution state? Process: address space, privileges, resources, etc. Execution state: PC, SP, registersz Exec state also called thread of control, or threadThreadsThreadsz Modern OSes (Mach, Chorus, NT, modern Unix) separate the concepts of processes and threadsseparate 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 threadsJanuary 18, 2007 CSE 120 – Lecture 4 – Threads 6z Threads become the unit of scheduling Processes are now the containers in which threads execute Processes become static, threads are the dynamic entities4Threads in a ProcessThreads in a ProcessStack (T1) Thread 1HeapStack (T2)Stack (T3) Thread 3Thread 2January 18, 2007 CSE 120 – Lecture 4 – Threads 7CodeStatic DataPC (T1)PC (T3)PC (T2)Thread Design SpaceThread Design SpaceOne Thread/ProcessMany Address Spaces(Early Unix)One Thread/ProcessOne Address Space(MSDOS)Address SpaceThreadJanuary 18, 2007 CSE 120 – Lecture 4 – Threads 8Many Threads/ProcessMany Address Spaces(Mach, Unix, NT, Chorus)Many Threads/ProcessOne Address Space(Pilot, Java)Thread5Process/Thread SeparationProcess/Thread Separationz Separating threads and processes makes it easier to support multithreaded applicationssupport 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 programszSo multithreading is even useful on a uniprocessorJanuary 18, 2007 CSE 120 – Lecture 4 – Threads 9zSo multithreading is even useful on a uniprocessorThreads: Concurrent ServersThreads: Concurrent Serversz Using fork() to create new processes to handle requests in parallel is overkill for such a simple taskrequests 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 requestJanuary 18, 2007 CSE 120 – Lecture 4 – Threads 10Close socket and exit} else {Close socket}}6Threads: 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);}}January 18, 2007 CSE 120 – Lecture 4 – Threads 11handle_request(int sock) {Process requestclose(sock);}KernelKernel--Level ThreadsLevel Threadsz We have taken the execution aspect of a process and separated it out into threadsseparated 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 threadsorlightweight processesJanuary 18, 2007 CSE 120 – Lecture 4 – Threads 12or lightweight processes NT: threads Solaris: lightweight processes (LWP)7Kernel Thread LimitationsKernel Thread Limitationsz Kernel-level threads make concurrency much cheaper than processesthan 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 January 18, 2007 CSE 120 – Lecture 4 – Threads 13gppof all programmers, languages, runtimes, etc.z For such fine-grained concurrency, need even “cheaper” threadsUserUser--Level ThreadsLevel Threadsz To make threads cheap and fast, they need to be implemented at user levelimplemented 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


View Full Document

UCSD CSE 120 - Threads

Documents in this Course
Threads

Threads

14 pages

Deadlocks

Deadlocks

19 pages

Processes

Processes

14 pages

Paging

Paging

13 pages

Processes

Processes

18 pages

Threads

Threads

29 pages

Security

Security

16 pages

Paging

Paging

13 pages

Processes

Processes

32 pages

Lecture 2

Lecture 2

13 pages

Paging

Paging

8 pages

Threads

Threads

14 pages

Paging

Paging

13 pages

Paging

Paging

26 pages

Paging

Paging

13 pages

Lecture

Lecture

13 pages

Processes

Processes

14 pages

Paging

Paging

13 pages

Security

Security

17 pages

Processes

Processes

34 pages

Structure

Structure

10 pages

Lecture 3

Lecture 3

13 pages

Lecture 1

Lecture 1

28 pages

Threads

Threads

15 pages

Paging

Paging

30 pages

Load more
Download 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 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 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?