Unformatted text preview:

6 3ULQFLSOHV RI 2SHUDWLQJ 6 VWHPV DOO Lecture 4 Threads Geoffrey M Voelker 3URFHVVHV Recall that a process includes many things Creating a new process is costly because of all of the data structures that must be allocated and initialized 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 FreeBSD 81 fields 408 bytes which does not even include page tables etc Communicating between processes is costly because most communication goes through the OS Overhead of system calls and copying data October 1 2001 CSE 120 Lecture 4 Threads 2 1 3DUDOOHO 3URJUDPV 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 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 This situation is very inefficient Space PCB page tables etc Time create data structures fork and copy addr space etc October 1 2001 CSE 120 Lecture 4 Threads 3 5HWKLQNLQJ 3URFHVVHV What is similar in these cooperating processes 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 a process from its execution state 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 Process address space privileges resources etc Execution state PC SP registers Exec state also called thread of control or thread October 1 2001 CSE 120 Lecture 4 Threads 4 2 7KUHDGV Modern OSes Mach Chorus NT modern Unix separate the concepts of processes and threads A thread is bound to a single process 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 Processes however can have multiple threads Threads become the unit of scheduling Processes are now the containers in which threads execute Processes become static threads are the dynamic entities October 1 2001 CSE 120 Lecture 4 Threads 5 7KUHDGV LQ D 3URFHVV Thread 1 Stack T1 Thread 2 Stack T2 Stack T3 Thread 3 Heap Static Data PC T3 PC T2 Code PC T1 October 1 2001 CSE 120 Lecture 4 Threads 6 3 7KUHDG HVLJQ 6SDFH One Thread Process One Address Space MSDOS One Thread Process Many Address Spaces Unix Many Threads Process One Address Space Pilot Java Many Threads Process Many Address Spaces Mach OSF NT Chorus Address Space Thread October 1 2001 CSE 120 Lecture 4 Threads 7 3URFHVV 7KUHDG 6HSDUDWLRQ Separating threads and processes makes it easier to support multithreaded applications Concurrency multithreading can be very useful Creating concurrency does not require creating new processes Improving program structure Handling concurrent events e g Web requests Writing parallel programs So multithreading is even useful on a uniprocessor October 1 2001 CSE 120 Lecture 4 Threads 8 4 7KUHDGV RQFXUUHQW 6HUYHUV Using fork to create new processes to handle requests in parallel is overkill for such a simple task Recall our forking Web server while 1 int sock accept if child pid fork 0 Handle client request else Close socket October 1 2001 CSE 120 Lecture 4 Threads 9 7KUHDGV RQFXUUHQW 6HUYHUV Instead we can create a new thread for each request web server while 1 int sock accept thread fork handle request sock handle request int sock Process request close sock October 1 2001 CSE 120 Lecture 4 Threads 10 5 HUQHO HYHO 7KUHDGV We have taken the execution aspect of a process and separated it out into threads As such the OS now manages threads and processes To make concurrency cheaper 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 threads or lightweight processes NT threads Solaris lightweight processes LWP October 1 2001 CSE 120 Lecture 4 Threads 11 HUQHO 7KUHDG LPLWDWLRQV Kernel level threads make concurrency much cheaper than processes Much less state to allocate and initialize However for fine grained concurrency kernel level threads still suffer from too much overhead Thread operations still require system calls Kernel level threads have to be general to support the needs of all programmers languages runtimes etc Ideally want thread operations to be as fast as a procedure call For such fine grained concurrency need even cheaper threads October 1 2001 CSE 120 Lecture 4 Threads 12 6 8VHU HYHO 7KUHDGV 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 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 threads October 1 2001 CSE 120 Lecture 4 Threads 13 8 7KUHDG LPLWDWLRQV But user level threads are not a perfect solution User level threads are invisible to the OS They are not well integrated with the OS As a result the OS can make poor decisions As with everything else they are a tradeoff Scheduling a process with idle threads Blocking a process whose thread initiated an I O even though the process has other threads that can execute Unscheduling a process with a thread holding a lock Solving this requires communication between the kernel and the user level thread manager October 1 2001 CSE 120 Lecture 4 Threads 14 7 HUQHO YV 8VHU 7KUHDGV Kernel level threads User level threads Integrated with OS informed scheduling Slow to create manipulate synchronize Fast to create manipulate synchronize Not integrated with OS uninformed scheduling Understanding the differences between kernel and user level threads is important For programming correctness performance For test taking October 1 2001 CSE 120 Lecture 4 Threads 15 HUQHO DQG 8VHU 7KUHDGV Another possibility is to use both kernel and user level threads Can associate a user level thread with a kernel level thread Or multiplex user level threads on top of kernel level threads Java Virtual Machine JVM Java threads


View Full Document

UCSD CSE 120 - Threads

Documents in this Course
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
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 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?