4_thread - 1 Threads Chapter 4 2 Thread What is thread?...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Threads Chapter 4 2 Thread What is thread? Thread vs. Process Thread and Process Context switch 3 Thread In a traditional single threaded systems, a process is: Resource ownership: memory address space, files, I/O resources Scheduling/execution unit: execution state/context, dispatch unit Multithreaded systems Separation of resource ownership & execution unit A thread is unit of execution, scheduling and dispatching A process is a container of resources, and a collection of threads 4 Thread Shared resources among all threads of a process Memory address space: global data, code, heap Open files, network sockets, other I/O resources User-id IPC facilities Private state of each thread: Execution state: running, ready, blocked, etc.. Execution context: Program Counter, Stack Pointer, other user-level registers Per-thread stack 5 Single and Multithreaded Processes 6 Single and Multithreaded Processes 7 Process Address Space single threaded address space kernel space code data heap stack kernel space code data heap thread 1 stack thread 2 stack thread 3 stack shared among threads multi-threaded address space 8 Threads vs. Processes Why multiple threads? Why not multiple processes? Yes, all roads lead to Rome But no, all roads are not equal 9 Threads vs. Processes Advantages of multi-threading over multi-processes Far less time to create/terminate thread than process An order of magnitude improvement Less states to create/copy/inherit, or to release Context switch is quicker between threads of the same process Communication btw. threads of the same process is more efficient Through shared memory Makes complex program control structure simpler Blocking I/O vs. non-blocking I/O 10 Threads vs. Processes: An Example The advantages of multi-threading in this example? Create Terminate Context switch main() { sock = socket( ); listen( ); while (1) { new_sock = accept (sock); if ( fork()== 0 ){ http_service(new_sock); exit(0); } } } http_service(int s){ /*serve HTTP request & exit*/ } main() { sock = socket( ); listen( ); while (1) { new_sock = accept (sock); pthread_create(&tid, 0, http_service, new_sock); } } http_service(int s){ /*serve HTTP request & exit*/ } 11 When to, and not to use threads? Applications Multiprocessor machines Handle slow devices Background operations Windowing systems Server applications to handle multiple requests No threads cases When each unit of execution require different authentication/user-id E.g., secure shell server 12 Thread context switch Typically called context switching - taking a thread off of the processor...
View Full Document

This note was uploaded on 10/13/2008 for the course CSC 246 taught by Professor Gu during the Spring '08 term at N.C. State.

Page1 / 45

4_thread - 1 Threads Chapter 4 2 Thread What is thread?...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online