Recall:ProcessinMemoryUser-space memory of Process AmaxstackDynamicallyallocatedmemoryGlobalLocalvariablevariableCodeheapdatatext011
Recall: Process in Memory • User-space memory of Process A 11 Global variable Local variable Dynamicallyallocated memory Code
Multi-thread-internalsUser-space memory ofaprocessCode- All threads share the same code.LocalLocalA thread starts with one specificfunction.DynamicWe name it the thread functionFunctionsA&BinthediagramGlobal variablesThe thread function can invokeFunction Aother functions or system callsFunction BBut, a thread could never return tothe caller of the thread function.Code12
Multi-thread – internals 12 Page 12 Function A Function B Code Local Local Global variables Dynamic - All threads share the same code. - A thread starts with one specific function. - We name it the thread function - Functions A & B in the diagram - The thread function can invoke other functions or system calls - But, a thread could never return to the caller of the thread function. Code User-space memory of a process
Multi-thread-internalsUser-space memory ofaprocessDynamicallyallocatedmemoryGlobalvariablesLocalLocal- All threads share the same globalvariable zone and the sameDynamicdynamically allocated memory-All threads canread fromand writeGlobal variablesto both areasFunction AFunction BCode13
Multi-thread – internals 13 Page 13 Function A Function B Code Local Local Global variables Dynamic - All threads share the same global variable zone and the same dynamically allocated memory - All threads can read from and write to both areas Global variables User-space memory of a process Dynamically allocated memory
Multi-thread-internalsUser-space memoryofaprocessLocalLocalLocalvariables-Eachthread has its ownmemoryDynamicrangeforthelocal variables- So, the stack is the private zone forGlobal variableseachstackFunctionAFunctionBCode14
Multi-thread – internals 14 Page 14 Function A Function B Code Local Local Global variables Dynamic - Each thread has its own memory range for the local variables - So, the stack is the private zone for each stack User-space memory of a process Local variables
BenefitsofMulti-thread Responsiveness and multi-tasking- Multi-threading design allows an application to doparalleltasks simultaneously- Example: Although a thread is blocked, the process canstill depend on another thread to do other things!-Especially importantfor interactive applications (userinterface)Status:BLOCKEDStatus: RUNNINGIt'd benicetoassignone threadfor one blockingsystem/library call.Reading fromDoingkeyboardcalculation15
Benefits of Multi-thread • Responsiveness and multi-tasking – Multi-threading design allows an application to do parallel tasks simultaneously – Example: Although a thread is blocked, the process can still depend on another thread to do other things! – Especially important for interactive applications (user interface) 15 Reading from keyboard Status: BLOCKED Doing calculation Status: RUNNING It’d be nice to assign one thread for one blocking system/library call