Task ThreadContents T he deference between Unix Process and Mach Task Mach Task Multiple Tasks Mach Thread Multiple Threads Sharing Data among Threads Mach Thread Calls Mach Task CallsThe deference between Unix Process and Mach Task Unix Mach Process Running Computation Resources [file descriptor, memory] Task [Resource Containers] Thread [Computation] Thread [Computation]Mach Task Collection of system resource Provide a protection boundary Tasks are not related to their creating task Memory TaskB Safe! TaskA E rror!Multiple Tasks Protection for a set of resources Additional resources A different set of resources. TaskA TaskB KernelMach Thread H ave their own set of registers and their own state information Execute program code stored in the address space of a Mach task The threads in a t ask run concurrently Single CPU, Multiple processor unused Thread Stack Thread Stack unused BSS Data Text Mach MemoryMultiple Threads When computing can be overlapped with I/O When the hardware provides more than one processor compute compute I/O Time compute compute compute I/O Thread X Thread A Thread B I/O I/O compute I/O I/OMultiple Threads When computing jobs are frequently created and destroyed Database queries changes here Thread B Record Sue and Sam meeting 2 P.M. on 6/7 Thread C Cancel Ed’s lunch with Carol at noon on 6/1 Thread A Is Dan free 9 A.M. on 6/12 ? Calendar ……Sharing Data among Threads Using multiple threads requires synchronizing access to shared data. Race condition A solution for a problem Locking the shared data : Atomic characteristic Task Shared Data Thread B Thread A lock Task Shared Data Thread B Thread A unlock can useMach Thread Calls [spawning] thread_create () thread_set_state () thread_get_state () thread_resume () thread_suspend () thread_self () exit() thread_terminate () kern_return_t status ; task_t parent_task ; thread_t new_thread ; state = thread_create ( parent_task , new_thread ); System call SUCCESS return KERN_SUCCESS state = thread_create ( task_self (), new_thread ); FAIL KERN_INVALID_ARGUMENT KERN_INVALID_SHORTAGE Target task An identifier for the new threadMach Thread Calls [preparing] kern_return_t status ; task_t th ; Int flavor; Thread_state_t new_state ; Thread_state_t cur_state ; Unsigned int count; state = thread_set_state ( th , flavor, new_state , count); Specify the state information state = thread_get_state ( th , flavor , cur_state , count ); thread_create () thread_set_state () thread_get_state () thread_resume () thread_suspend () thread_self () exit() thread_terminate () Specified thread State information The size of new_stateMach Thread Calls [Running] Kern_return_t status ; thread_t th ; status = thread_resume ( th ); Start the thread running thread suspend count = 0; status = thread_suspend ( th ); Stop thread_create () thread_set_state () thread_get_state () thread_resume () thread_suspend () thread_self () exit() thread_terminate ()Mach Thread Calls [Demise ] kern_return_t status; thread_t unneeded_thread ; state = thread_terminate ( unneeded_thread ); Demise of a thread Mach 2.5 exit() thread_create () thread_set_state () thread_get_state () thread_resume () thread_suspend () thread_self () exit() thread_terminate () thread_self ()Mach task Calls [Resuming] kern_return_t status; task_t a_task ; status = task_suspend ( a_task ); status = task_resume ( a_task ); task_suspend () task_resume () task_threads () vm_deallocate () task_info () Resuming all thread Thread suspend count = 0 Task’s suspend count = 0Mach task Calls [Finding] kern_return t status; task_t a_task ; Thread_array_t thread_list Int thread_count status = task_threads ( a_task , thread_list , thread_count ); task_suspend () task_resume () task_threads () vm_deallocate () task_info () Finding All ThreadsMach task Calls [Requesting] kern_return_t status; task_t a_task ; Int which_info ; t ask_info_t info_array ; u nsigned int * info_array_size ; status = task_info ( a_task , which_info , info_array , info_array_size ); task_suspend () task_resume () task_threads () vm_deallocate () task_info () Requesting task InformationMach task Calls [Specialized IPC port] task_suspend () task_resume () task_threads () vm_deallocate () task_info () task_set_special_port () task_get_special_port () kern_return_t status; task_t a_task ; Int which_port port_t special_port status = task_set_special_port ( a_task,which_port , special_port ); status = task_get_special_port ( a_task , which_port , returned_port ); To register a port To ask the registered port notify port task exception portEnd! Thank you!{nameOfApplication=Show}