chapter 2 threads questions
play

Chapter 2: Threads: Questions ! How is a thread different from a - PDF document

Chapter 2: Threads: Questions ! How is a thread different from a process? CSCI [4|6]730 ! Why are threads useful? Operating Systems ! How can POSIX threads be useful? ! What are user-level and kernel-level threads? ! What are problems with


  1. Chapter 2: Threads: Questions ! How is a thread different from a process? CSCI [4|6]730 ! Why are threads useful? Operating Systems ! How can POSIX threads be useful? ! What are user-level and kernel-level threads? ! What are problems with threads? Threads 2 Maria Hybinette, UGA Maria Hybinette, UGA Review : What is a Process? Review : What Makes up a Process? A process is a program in execution ! User Mode Address ! Program code (text) A thread have Space ! Data heap (1) an execution stream and (2) a context » global variables » heap (dynamically allocated memory) ! Execution stream routine1 ! Process stack » stream of instructions var1 var2 stack » sequential sequence of instructions » function parameters » “thread” of control Running on a » return addresses text main routine1 ! Process ‘context’ (seen picture of this already) thread » local variables and functions routine2 » Everything needed to run (restart) the process ! ! OS Resources code data files data arrayA » Registers arrayB registers stack ! Registers – program counter, stack pointer, general purpose ! » program counter, stack pointer » Address space – Everything the process can access in memory address space are the shared resources 3 4 – Heap, stack, code of a(ll) thread(s) in a program Maria Hybinette, UGA Maria Hybinette, UGA What are are problem’s with processes? Processes versus Threads Solution: A thread is a “lightweight process” (LWP) ! How do processes ( independent memory ! An execution stream that shares an address space space) communicate ? » Overcome data flow over a file descriptor » Not really that simple (seen it, tried it – and you have main() » Overcome setting up `tighter memory’ space too): { i = 55; ! Multiple threads within a single process – Message passing (send and receive) fork(); // what is i Examples: – Shared Memory: Set up a shared memory area (easier)? ! Two processes (copies of each other) examining memory ! Problems: address 0xffe84264 see different values (i.e., different contents) » Overhead: Both methods add some kernel overhead lowering performance » same frame of reference ! Two threads examining memory address 0xffe84264 see » Complicated: IPC is not really that ‘natural’ same value (i.e., same contents) – increases the complexity of your code ! Illustrate: i-threading.c, i-forking.c 5 6 Maria Hybinette, UGA Maria Hybinette, UGA

  2. What Makes up a Thread? Single and Multithreaded Process User Mode Address ! Own stack (necessary?) Space Program Counter code data files code data files ! Own registers (necessary?) heap Stack Pointer » Own program counter registers stack registers registers registers » Own stack pointer ! State (running, sleeping) routine1 routine1 stack stack stack var1 var1 ! Signal mask var2 var2 stack text main routine1 routine2 data arrayA arrayB address space are the shared resources 7 8 of a(ll) thread(s) in a program Maria Hybinette, UGA Maria Hybinette, UGA Why Support Threads? Why Support Threads? ! Divide large task across several cooperative threads ! Divide large task across several cooperative threads ! Multi-threaded task has many performance benefits ! Multi-threaded task has many performance benefits ! Examples: ! Adapt to slow devices » Web Server: create threads to: » One thread waits for device while other threads computes ! Defer work – Get network message from client » One thread performs non-critical work in the background, – Get URL data from disk when idle – Compose response ! Parallelism – Send a response » Each thread runs simultaneously on a multiprocessor » Word processor: create threads to: – Display graphics – Read keystrokes from users – Perform spelling and grammar checking in background 9 10 Maria Hybinette, UGA Maria Hybinette, UGA Why are Threads Challenging? Why Threads instead of a Processes? pthread1 Example: Output? main() � { � ! Advantages of Threads: � pthread_t t1, t2; � � char *msg1 = “Thread 1”; char *msg2 = “Thread 2”; � » Thread operations cheaper than corresponding � int ret1, ret2; � process operations � ret1 = pthread_create( &t1, NULL, print_fn, (void *)msg1 ); � � ret2 = pthread_create( &t2, NULL, print_fn, (void *)msg2 ); � – In terms of: Creation, termination, (context) switching � if( ret1 || ret2 ) � » IPC cheap through shared memory � { � � � fprintf(stderr, “ERROR: pthread_created failed.\n”); � – No need to invoke kernel to communicate between � � exit(1); � threads � } � ! Disadvantages of Threads: � pthread_join( t1, NULL ); � � pthread_join( t2, NULL ); � » True Concurrent programming is a challenge (what � printf( “Thread 1 and thread 2 complete.\n” ); � does this mean? True concurrency?) } � void print_fn(void *ptr) � » Synchronization between threads needed to use { � shared variables (more on this later – this is HARD). � printf(“%s\n”, (char *)ptr); � 11 12 } � Maria Hybinette, UGA Maria Hybinette, UGA

  3. Why are Threads Challenging? Why are Threads Challenging? ! Example: Transfer $50.00 between two accounts and output the total balance of the T = 50, M = 100 ! Tasks: accounts: M = M - $50.00 One thread debits & credits T = T + $50.00 M = Balance in Maria’s account (begin $100 ) B = M + T One thread totals T = Balance in Tucker’s account (begin $50 ) B = Total balance ! Tasks: M = M - $50.00 M = M - $50.00 B = M + T Idea: on distributing T = 50, M = 100 T = T + $50.00 B = M + T M = M - $50.00 the tasks: (1) One thread debits M = M - $50.00 B = M + T T = T + $50.00 T = T + $50.00 and credits (2) The other Totals T = T + $50.00 Does that work? B = $150 B = $100 B = $150 B = M + T 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Common Programming Models Thread Support ! Manager/worker ! Three approaches to provide thread support » Single manager handles input and assigns work to the » User-level threads worker threads » Kernel-level threads ! Producer/consumer » Hybrid of User-level and Kernel-level threads » Multiple producer threads create data (or work) that is handled by one of the multiple consumer threads ! Pipeline » Task is divided into series of subtasks, each of which is handled in series by a different thread 15 16 Maria Hybinette, UGA Maria Hybinette, UGA Latencies User-Level Threads ! Comparing user-level threads, kernel threads, and ! Many-to-one thread mapping processes » Implemented by user-level runtime ! Thread/Process Creation Cost: libraries » Evaluate –with Null fork: the time to create, schedule, execute, and complete – Create, schedule, synchronize threads at the entity that invokes the null procedure user-level, state in user level space ! Thread/Process Synchronization Cost: » OS is not aware of user-level threads » Evaluate – with Signal-Wait: the time for an entity to signal a waiting entity and – OS thinks each process contains only a then wait on a condition (overhead of synchronization) P P single thread of control ! Advantages » Does not require OS support; Portable Procedure call = 7 us User Level Kernel Level Kernel Trap = 17 us Processes Threads Threads » Can tune scheduling policy to meet application (user level) demands Null fork 34 948 11,300 » Lower overhead thread operations since no system calls ! Disadvantages Signal-wait 37 441 1,840 » Cannot leverage multiprocessors (no true parallelism) 30X,12X 17 18 » Entire process blocks when one thread blocks Maria Hybinette, UGA Maria Hybinette, UGA

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend