threads questions csci 1730 systems programming
play

Threads: Questions CSCI 1730 Systems Programming How is a thread - PDF document

Threads: Questions CSCI 1730 Systems Programming How is a thread different from a process? Why are threads useful? Threads, and How can POSIX threads be useful? other IPC: What are problems with threads? Shared Memory, and


  1. Threads: Questions CSCI 1730 Systems Programming ● How is a thread different from a process? ● Why are threads useful? Threads, and ● How can POSIX threads be useful? other IPC: ● What are problems with threads? Shared Memory, and Message Queus ● Resources: ● https://computing.llnl.gov/tutorials/pthreads/ 2 Maria Hybinette, UGA Maria Hybinette, UGA Review : What is a Process? Review: What Makes up a Process? ● Program code (text) 3GB A process is a program in execution … User Mode » Compiled version of the text Address Space ● Data (cannot be shared) A thread have stack » global variables (1) an execution stream and – Uninitialized (BSS segment) sometimes (2) a context listed separately. – Initialized routine1 ● Execution stream var1 ● Process stack (scopes) var2 » stream of instructions » function parameters » sequential sequence of instructions heap » return addresses arrayB[10] ; » 1“thread” of control data Running on a » local variables and functions arrayA[10] = {0} ● Process ‘context’ ( Review ) thread ● <<Shared Libraries >> » Everything needed to run (restart) the process … ● Heap: Dynamic memory (alloc) code data files main » Registers ● OS Resources, environment routine1 registers stack text – program counter, stack pointer, general purpose … routine2 » open files, sockets » Address space » Credential for security 0x0 – Everything the process can access in memory ● Registers address space are the shared 3 resources of a(ll) thread(s) in a 4 – Heap, stack, code » program counter, stack pointer program Maria Hybinette, UGA Maria Hybinette, UGA What are are problem’s with processes? Processes versus Threads ● How do processes ( independent memory space) communicate ? Solution: A thread is a “lightweight process” (LWP) » Complicated/Not really that simple (seen it, tried it – and you have too): ● An execution stream that shares an address space – Message Passing: » Overcome data flow over a file descriptor main() ● Remote machine (send and receive): Sockets » Overcome setting up `tighter memory’ space { ● Local machine via message queues i = 55; ● Multiple threads within a single process fork(); » http://beej.us/guide/bgipc/output/html/multipage/mq.html // what is i – Pipes Examples: – Signal ● Two processes (copies of each other) examining memory – Shared Memory: Set up a shared memory area address 0xffe84264 see different values (i.e., different contents) ● http://beej.us/guide/bgipc/output/html/multipage/shm.html » Slow/Overhead: All of the methods above add some kernel » same frame of reference overhead lowering performance ● Two threads examining memory address 0xffe84264 see same value (i.e., same contents) – Process Creation is heavy weight ● Illustrate: ctest/i-threading.c, ctest/i-process.c ● Allocate space/heavy weight 5 6 Maria Hybinette, UGA Maria Hybinette, UGA

  2. What Makes up a Thread? Single and Multithreaded Process User Mode Process Threads 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 Threads instead of a Processes? ● Advantages of Threads: ● Divide large task across several cooperative threads » Thread operations cheaper than corresponding ● Multi-threaded task has many performance benefits process operations ● Examples: – In terms of: Creation, termination, (context) switching » Web Server: create threads to: » IPC cheap through shared memory – Get network message from client – No need to invoke kernel to communicate between threads – Get URL data from disk ● Disadvantages of Threads: – Compose response – Send a response » True Concurrent programming is a challenge (what » Word processor: create threads to: does this mean? True concurrency?) – Display graphics » Synchronization between threads needed to use shared variables (more on this later – this is HARD). – Read keystrokes from users – Perform spelling and grammar checking in background 9 10 Maria Hybinette, UGA Maria Hybinette, UGA Why are Threads Challenging? pthread1 Example: Output? Why are Threads Challenging? ● Example: Transfer $50.00 between two main() { accounts and output the total balance of the pthread_t t1, t2; accounts: char *msg1 = “Thread 1”; char *msg2 = “Thread 2”; int ret1, ret2; ret1 = pthread_create( &t1, NULL, print_fn, (void *) msg1 ); M = Balance in Maria’s account (begin $100 ) ret2 = pthread_create( &t2, NULL, print_fn, (void *) msg2 ); if( ret1 || ret2 ) T = Balance in Tucker’s account (begin $50 ) { fprintf(stderr, “ERROR: pthread_created failed.\n”); B = Total balance exit(1); ● Tasks: } pthread_join( t1, NULL ); Idea: on distributing T = 50, M = 100 pthread_join( t2, NULL ); the tasks: printf( “Thread 1 and thread 2 complete.\n” ); (1) One thread debits M = M - $50.00 } and credits (2) The other Totals void print_fn(void *ptr) T = T + $50.00 Does that work? { printf(“%s\n”, (char *)ptr); B = M + T 11 12 } Maria Hybinette, UGA Maria Hybinette, UGA

  3. Why are Threads Challenging? Common Programming Models T = 50, M = 100 ● Tasks: ● Manager/worker » Single manager handles input and assigns work to the M = M - $50.00 One thread debits worker threads & credits T = T + $50.00 ● Producer/consumer B = M + T One thread totals » Multiple producer threads create data (or work) that is handled by one of the multiple consumer threads ● Pipeline M = M - $50.00 M = M - $50.00 B = M + T » Task is divided into series of subtasks, each of which is handled in series by a different thread T = T + $50.00 B = M + T M = M - $50.00 B = M + T T = T + $50.00 T = T + $50.00 B = $150 B = $100 B = $150 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Thread Support Latencies ● Comparing user-level threads, kernel threads, and processes ● Three approaches to provide thread support ● Thread/Process Creation Cost: » User-level threads (Pthreads) » Evaluate –with Null fork: the time to create, schedule, execute, and complete the entity that invokes the null procedure » Kernel-level threads (not cover) ● Thread/Process Synchronization Cost: – Kernel manages the threads (avoids blocking) » Evaluate – with Signal-Wait: the time for an entity to signal a waiting entity and then wait on a condition (overhead of synchronization) » Hybrids Procedure call = 7 us User Level Kernel Level Kernel Trap = 17 us Processes Threads Threads Null fork 34 948 11,300 Signal-wait 37 441 1,840 30X,12X 15 16 Maria Hybinette, UGA Maria Hybinette, UGA User-Level Threads POSIX Pthreads ● P-threads is a standard set of C library functions ● Many-to-one thread mapping for multithreaded programming » Implemented by user-level runtime libraries » IEEE Portable Operating System Interface, POSIX, section – Create, schedule, synchronize threads at 1003.1 standard, 1995 user-level, state in user level space » OS is not aware of user-level threads ● Pthread Library (60+ functions) – OS thinks each process contains only a P P ● Programs must include the file pthread.h single thread of control ● Programs must be linked with the pthread library ● Advantages ( -lpthread ) » Does not require OS support; Portable » Can tune scheduling policy to meet application (user level) » Done by default by some gcc’s (e.g., on Mac OS X) demands » Lower overhead thread operations since no system calls ● Disadvantages » Cannot leverage multiprocessors (no true parallelism) 17 18 » Entire process blocks when one thread blocks Maria Hybinette, UGA Maria Hybinette, UGA

Recommend


More recommend