DOC PREVIEW
UCSD CSE 120 - Threads; Weaving Control Flow

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 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 30 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 30 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 30 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 30 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 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 4:Lecture 4:Threads; weavingThreads; weaving control flowcontrol flowCSE 120: Principles of Operating SystemsAlex C. SnoerenHW 1 Due NOWCSE 120 – Lecture 4: Threads 2AnnouncementsAnnouncements Homework #1 due now Project 0 due tonight Project groups◆ Please send project group info to Jose◆ Project 1 will start on ThursdayCSE 120 – Lecture 4: Threads 3ProcessesProcesses 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.) Creating a new process is costly because of all of thedata structures that must be allocated and initialized◆ Recall struct proc in Solaris◆ …which does not even include page tables, etc. Communicating between processes is costly becausemost communication goes through the OS◆ Overhead of system calls and copying dataCSE 120 – Lecture 4: Threads 4Parallel ProgramsParallel Programs Also recall our Web server example that forks off copiesof itself to handle multiple simultaneous requests◆ Or any parallel program that executes on a multiprocessor 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 orphysically) This situation is very inefficient◆ Space: PCB, page tables, etc.◆ Time: create data structures, fork and copy addr space, etc.CSE 120 – Lecture 4: Threads 5The Soul of a ProcessThe Soul of a Process 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.) What don’t they share?◆ Each has its own execution state: PC, SP, and registers Key idea: Why don’t we separate the concept of aprocess from its execution state?◆ Process: address space, privileges, resources, etc.◆ Execution state: PC, SP, registers Exec state also called thread of control, or threadCSE 120 – Lecture 4: Threads 6ThreadsThreads Modern OSes (Solaris, NT, modern Unix) separate theconcepts of processes and threads◆ The thread defines a sequential execution stream within aprocess (PC, SP, registers)◆ The process defines the address space and general processattributes (everything but threads of execution) A thread is bound to a single process◆ Processes, however, can have multiple threads◆ Each process has at least one thread Threads become the unit of scheduling◆ Processes are now the containers in which threads execute◆ Processes become static, threads are the dynamic entities◆ Each CPU runs one thread at a timeCSE 120 – Lecture 4: Threads 7Threads in a ProcessThreads in a ProcessStack (T1)CodeStatic DataHeapStack (T2)Stack (T3)Thread 1Thread 3Thread 2PC (T1)PC (T3)PC (T2)CSE 120 – Lecture 4: Threads 8Thread Design SpaceThread Design SpaceOne Thread per ProcessMany Address Spaces(Early Unix)One Thread per ProcessOne Address Space(MSDOS)Many Threads per ProcessMany Address Spaces(Solaris, Linux, NT, MacOS)Many Threads per ProcessOne Address Space(Java VM)AddressSpaceThreadCSE 120 – Lecture 4: Threads 9Why Use Threads?Why Use Threads? Separating threads and processes makes it easier tosupport parallel applications◆ Creating concurrency does not require creating new processes◆ Low-overhead sharing between threads in same process Concurrency (multithreading) can be very useful◆ Improving program structure◆ Handling concurrent events (e.g., Web requests)◆ Taking advantage of multiple CPUs◆ Overlapping I/O with computation But, brings a whole new meaning to Spaghetti Code◆ Forcing OS students to learn about synchronization…CSE 120 – Lecture 4: Threads 10 Using fork() to create new processes to handle requests inparallel is overkill for such a simple task Recall our forking Web server:while (1) {int sock = accept();if ((child_pid = fork()) == 0) {Handle client requestClose socket and exit} else {Close socket}}Threads: Concurrent ServersThreads: Concurrent ServersCSE 120 – Lecture 4: Threads 11Threads: Concurrent ServersThreads: Concurrent Servers 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);}CSE 120 – Lecture 4: Threads 12Scheduling ThreadsScheduling Threads No longer just scheduling processes, but threads◆ Kernel scheduler used to pick among PCBs◆ Now what? We have basically two options◆ Kernel explicitly selects among threads in a process◆ Hide threads from the kernel, and have a user-level schedulerinside each multi-threaded process Why do we care?◆ Think about the overhead of switching between threads◆ Who decides which thread in a process should go first?◆ What about blocking system calls?CSE 120 – Lecture 4: Threads 13Kernel-Level ThreadsKernel-Level Threads OS now manages threads and processes◆ All thread operations are implemented in the kernel◆ The OS schedules all of the threads in the system OS-managed threads are called kernel-level threadsor lightweight processes◆ NT: threads◆ Solaris: lightweight processes (LWP) Scheduler deals in threads◆ PCBs are no longer scheduled◆ If a thread blocks, another threadin the same process can runKernel ready queueCSE 120 – Lecture 4: Threads 14Kernel Thread LimitationsKernel Thread Limitations Kernel-level threads make concurrency much cheaperthan processes◆ Much less state to allocate and initialize However, for fine-grained concurrency, kernel-levelthreads 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 needsof all programmers, languages, runtimes, etc. For such fine-grained concurrency, need even“cheaper” threadsCSE 120 – Lecture 4: Threads 15User-Level ThreadsUser-Level Threads To make threads cheap and fast, they need to beimplemented at user level◆ User-level threads are managed entirely by a run-time system(a.k.a. user-level thread library) Invisible to kernel◆ A thread represented inside processby a PC, registers, stack, and


View Full Document

UCSD CSE 120 - Threads; Weaving Control Flow

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

Threads

Threads

15 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; Weaving Control Flow
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; Weaving Control Flow 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; Weaving Control Flow 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?