Benefits of Multi-threadEase in data sharing, can be done using:-globalvariables,and- dynamically allocated memory Processes share resources via shared memory or messagepassing, which must be explicitly arranged by theprogrammerkeyboard inputOf course,this leads tothemutualexclusion&the synchronizationproblems (willbetalkedin laterchapters)Reading fromDoingkeyboardcalculation16
Benefits of Multi-thread • Ease in data sharing, can be done using: – global variables, and – dynamically allocated memory. • Processes share resources via shared memory or message passing, which must be explicitly arranged by the programmer 16 Reading from keyboard Doing calculation Of course, this leads to the mutual exclusion & the synchronization problems (will be talked in later chapters) keyboard input
Benefitsof Multi-thread.Economy- Allocating memory and resources for process creation iscostly, dozens of times slower than creating threads-Context-switch between processes is also costly,severaltimes of slower:Scalability Threads may be running in parallel on different cores17
Benefits of Multi-thread 17 • Economy – Allocating memory and resources for process creation is costly, dozens of times slower than creating threads – Context-switch between processes is also costly, several times of slower • Scalability – Threads may be running in parallel on different cores
ProgrammingChallengesIdentifyingtasks- Divide separate and concurrent tasksBalance- Tasks should perform equal work of equal valueDatasplitting- Data must be divided to run on separate coresDatadependency-Synchronizationis needed. Testing and debugging18
Programming Challenges 18 • Identifying tasks – Divide separate and concurrent tasks • Balance – Tasks should perform equal work of equal value • Data splitting – Data must be divided to run on separate cores • Data dependency – Synchronization is needed • Testing and debugging
Multi-threading Motivation Thread Concept Thread Models19
19 Multi-threading - Motivation - Thread Concept - Thread Models
Recall ProcessStructureUserspaceGlobalDynamically-variableallocatedCode+memoryconstantsLocalvariableProcessProcessstructure(PCB)KernelSpace20
Global variable Local variable Dynamicallyallocated memory Code + constants Process structure (PCB) 20 Recall Process Structure Kernel Space User space Process