12 - Threads (1)OutlineThread BasicsPOSIX ThreadsAnnouncementsReading: MOS 2.2.1 - 2.2.6Thread BasicsPOSIX ThreadsThread Data StructuresProcess creation is inefficientProblems with ProcessesThreads vs. ProcessesAdvantages and DisadvantagesExample: Web ServerEfficiency: Amdahl's LawComplexity: Shared VariablesThread DynamicsProcesses (usually) don't share memory directlyA thread is an independently executed sequence ofexecuted instructions within a process.A thread cannot exist without a process.Unlike processes, threads share non-execution-related resources.Applications can have multiple processes withmultiple threads.Certain items must now be stored per-process andper-thread:How are the separate stacks managed in processaddress space?> Each process starts with a single "main" thread> Threads can create new threads> Threads can end at any point> Threads can wait for other threads> Threads can yield execution to other threadsSimple web server has 2 jobs:Facilitating connection is an I/O-bound task, whileperforming work is (should be) a CPU-bound task.Doing both in a single thread would cause longresponse times, since many requests come in.I/O wait would cause work to be delayed.Multi-core multithreading can give performanceimprovements, but improvement is not linear.Amdahl's law gives an upper bound on programspeedup as a function of the fraction of theprogram's serial work and the number of cores. Derivation:Multithreading requires some initialization andsynchronization that must be done serially.Accesses need to be coordinated somehowonly defines API, not implementationkernel keeps PCBs in a process tableseparates program execution data from PCB:PCB contains:TCB contains program execution dataPCB contains everything elseSynchronization algorithm requiredPA 2 autograder is uploaded. It may be updatedone more time, but we will re-run it on our end ifneeded.1)2)3)--------Must create and copy data to a large PCBexecuting the main() functionthere can be parent/child relationshipsif main thread returns, exit syscall willterminate process, killing all threads.Significant overheadSeparate address spaces and resources sousers can't interfere with one another.Inter-process communication (IPC) hasoverhead due to syscallsProcess is needed to provide address spaceand other resources to one or more threads.Multiprocessing(single thread)Multithreading(single process)codecoderegsregsregsregsPCPCPCstackstackstackPCstackdatadataheapheapsingle-threadedprocessmultithreadedprocessPer-Process:Advantages:- Address space- Global variables- Open files- Child processes- Pending alarms- Signals & handlers- Accounting- Program counter- Registers- Stack- Status/statePer-Thread:Disadvantages:e.g. ready, running, blockedsee MOS Figure 2-12kernel spacethread 0 stackthread 1 stackthread 2 stackheapbssdatatextffffffff_ffffffff00000000_00000000AddressSpacethread 0 SPthread 1 PCthread 0 PCthread 2 PCthread 1 SPthread 2 SPshared---shared address space=> no syscalls required==> no comms overhead=> no copying required==> fast thread creationbugs can crash other threads,or the entire processaccessing shared datasimultaneously requiressynchronizationPrograms can perform I/O andcompute tasks concurrentlyPrograms can take advantageof multi-core parallelism---------- Process state/status- PID- PC- Registers- Scheduling info- Memory management info- Allocated resources- etc.e.g. program counter, registers, scheduling info, pending I/Oe.g. memory management info, accounting info-PerformanceNo IsolationComplexityEfficiency- Manage client connections- Perform work for the clientI/O-boundDispatcher threadWorker threadmost time is spent waiting on I/Omost time is spent on CPU computeCPU-boundSSTPNserial portion of program as fractionserial portionsingle core runtime in unit timeOn a single core:Single-core Runtime:N-core Runtime:Speedup:Example:An application is 75% parallel and 25% serial.Moving from 1 to 2 cores gives the following upperbound on speedup:For N -> infinity:Serial portion of an application has adisproportionate effect on performance gain frommulticore parallelism.Multiple threads may access the same globalvariable concurrently.parallel portionnumber of coresthread 0modify(&val)modify(&val)0x1000 : 0x12345678 (val)0x1004 : ...0x1008 : ...modify(&val)thread 1thread 2globalmemoryExample:Code would be correct when executed serially, butinterleaving causes issues:POSIX defines an API for threads:Recall Process Control BlockThreads have an analog: Thread Control Block1: void push_back(LIST * list, int v)2: {3: NODE * n = malloc(sizeof(NODE));4: n->value = v;5: n->next = NULL;6: list->tail->next = n;7: list->tail = n;8: }thread A: run 3,4,5thread B: run 3,4,5,6thread A: run 6,7thread B: run 7listheadtailcreated by thread Acreated by thread BpthreadsFunctionpthread_createcreate a new threadterminate the calling threadwait for specific thread to exitrelease CPU to another threadcreate and init thread attribute structremove thread attribute structnote: pthread_yield is deprecated in favor of sched_yieldWindows has a different thread APIpthread_exitpthread_joinsched_yieldpthread_attr_initpthread_attr_destroyDescriptionA subset of the API:Example:see MOS Figure 2-14Thread Data StructuresstackheapbssdatatextPCB............pidTID: 2TID: 1TID: 0PCSPSPSPSPPCPCPCPCBPCBPCBthread 0 stackthread 1 stackthread 2 stackheapbssdatatextthread tablePCB...PIDUIDGIDAddress SpaceSeparating out the execution state from memoryallows concurrent execution of:different parts of the program codesame parts of program code with different data--