Concurrent ProgrammingHardware ArchitecturesConcurrent SystemsApplication classesConcurrent ProgrammingProcess ConceptProcess StatesProcess Scheduling QueuesThe Thread Model (1)Thread Usage (1)Thread Usage (2)Client and server with threadsAlternative server threading architecturesThreads: MotivationThreadsState associated with processes and threadsThe Thread Model (2)The Thread Model (3)Threadscont’dUser-Level ThreadsImplementing Threads in User SpaceKernel ThreadsImplementing Threads in the KernelHybrid ImplementationsMultithreading ModelsMany-to-OneMany-to-One ModelOne-to-One ModelOne-to-one ModelMany-to-Many ModelMany-to-Many ModelPthreadsJava ThreadsCreating and Using threadsJava thread constructor and management methodsCreating threadsCooperating concurrent processesSynchronization MechanismsRace ConditionsAssignment 11CS 475 - Spring 2003 1Concurrent ProgrammingProf. Sanjeev SetiaConcurrent & Distributed Software SystemsCS 475CS 475 - Spring 2003 2Hardware ArchitecturesUniprocessorsShared-memory multiprocessorsDistributed-memory multicomputersDistributed systems2CS 475 - Spring 2003 3Concurrent SystemsEssential aspects of any concurrent systemExecution context - state of a concurrent entityScheduling - deciding which context will run nextSynchronization - mechanisms that enable execution contexts to coordinate their use of shared resourcesCS 475 - Spring 2003 4Application classesMulti-threaded ProgramsProcesses/Threads on same computerWindow systems, Operating systemsDistributed computingProcesses/Threads on separate computersFile servers, Web serversParallel computingOn same (multiprocessor) or different computersGoal: solve a problem faster or solve a bigger problem in the same time3CS 475 - Spring 2003 5Concurrent ProgrammingProcess = Address space + one thread of control Concurrent program = multiple threads of controlMultiple single-threaded processesMulti-threaded processCS 475 - Spring 2003 6Process ConceptA process includes:program counter code segmentstack segmentdata segmentProcess = Address Space+ One threadofcontrolStackTextHeapAuxiliaryregions02NAddress space4CS 475 - Spring 2003 7Process States Possible process statesrunningblockedreadyTransitions between states shownCS 475 - Spring 2003 8Process Scheduling QueuesReady queue – set of all processes residing in main memory, ready and waiting to execute.Device queues – set of processes waiting for an I/O device.Processes migrate between the various queues during their lifetime.5CS 475 - Spring 2003 9The Thread Model (1)(a) Three processes each with one thread(b) One process with three threadsCS 475 - Spring 2003 10Thread Usage (1)A word processor with three threads6CS 475 - Spring 2003 11Thread Usage (2)A multithreaded Web serverCS 475 - Spring 2003 12Client and server with threadsServerN threadsInput-outputClientThread 2 makesT1Thread 1requests to servergenerates resultsRequestsReceipt &queuing7CS 475 - Spring 2003 13Alternative server threading architectures a. Thread-per-requestb. Thread-per-connection c. Thread-per-objectremoteworkersI/OremoteremoteI/Oper-connection threads per-object threadsobjectsobjectsobjectsCS 475 - Spring 2003 14Threads: MotivationTraditional UNIX processes created and managed by the OS kernel Process creation expensive - fork system call Context switching expensiveCooperating processes - no need for protection (separate address spaces)8CS 475 - Spring 2003 15ThreadsExecute in same address spaceseparate execution stack, share access to code and (global) dataSmaller creation and context-switch timeCan exploit fine-grain concurrencyEasier to write programs that use asynchronous I/O or communicationCS 475 - Spring 2003 16State associated with processes and threadsProcess ThreadAddress space tables Saved processor registersCommunication interfaces, open files Priority and execution state (such asBLOCKED)Semaphores, other synchronizationobjectsSoftware interrupt handling informationList of thread identifiers Execution environment identifierPages of address space resident in memory; hardware cache entries9CS 475 - Spring 2003 17The Thread Model (2)Items shared by all threads in a processItems private to each threadCS 475 - Spring 2003 18The Thread Model (3)Each thread has its own stack10CS 475 - Spring 2003 19Threads cont’dLess protection against programming errorsUser-level vs kernel-level threadskernel not aware of threads created by user-level thread package (e.g. Pthreads), language (e.g. Java)user-level threads typically multiplexed on top of kernel level threads in a user-transparent fashion CS 475 - Spring 2003 20User-Level ThreadsThread management (scheduling, thread creation) done by user-level threads libraryExamples-POSIX Pthreads-Mach C-threads-Solaris threads-Java threads11CS 475 - Spring 2003 21Implementing Threads in User SpaceA user-level threads packageCS 475 - Spring 2003 22Kernel ThreadsSupported by the KernelExamples- Windows 95/98/NT/2000-Solaris- Tru64 UNIX-BeOS- Linux12CS 475 - Spring 2003 23Implementing Threads in the KernelA threads package managed by the kernelCS 475 - Spring 2003 24Hybrid ImplementationsMultiplexing user-level threads onto kernel-level threads13CS 475 - Spring 2003 25Multithreading ModelsMany-to-OneOne-to-OneMany-to-ManyCS 475 - Spring 2003 26Many-to-OneMany user-level threads mapped to single kernel thread.If one user-level thread makes a blocking system call, the entire process is blocked even though other user-level threads may be “ready”Used on systems that do not support kernel threads.14CS 475 - Spring 2003 27Many-to-One ModelCS 475 - Spring 2003 28One-to-One ModelEach user-level thread maps to kernel thread.Examples- Windows 95/98/NT/2000-OS/215CS 475 - Spring 2003 29One-to-one ModelCS 475 - Spring 2003 30Many-to-Many ModelAllows many user level threads to be mapped to many kernel threads.Allows the operating system to create a sufficient number of kernel threads.Solaris 2 Windows NT/2000 with the ThreadFiberpackage16CS 475 - Spring 2003 31Many-to-Many ModelCS 475 - Spring 2003 32Pthreadsa POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.API specifies behavior of the thread library, implementation is up to development of the library.Common in UNIX operating systems.17CS 475 - Spring 2003 33Java
View Full Document