Friday, July 16, 2010

Multitasking and Multithreading

The quotes are mostly come from the book Pro .NET 1.1 Remoting, Reflection, and Threading Threading refers to an application’s capability to handle executing more than one instruction set at a time. Multitasking refers to an operating system’s capability to run more than one application at a time. There are two types of multitasking
  1. Cooperative multitasking: An application executes until it cooperates with other applications by releasing the processor to them
  2. Preemptive multitasking: An application is allowed to execute for a short period before it is involuntarily interrupted by the operating system. The processor actually grants time to the process. This period is called time slice or a quantum. This style of sharing the processor time has a few problems
    1. Accessing the shared data (Is solved by synchronization)
    2. The cost of context switching
Process is a physical separation of memory and resources. Each process has at least one execution sequence called thread. The registers in use on the CPU define this thread, the stack used by the thread, and a container that keeps track of the thread’s current state. This container is called Thread Local Storage (TLS). A TLS for a thread, contains the registers, stack pointers, scheduled information, address spaces in memory, and information about other resources in use. The concept of spawning new threads within the same process is known as free threading. For computers that have more than one process, it is possible to set the ProcessAfinity property of the System.Diagnostics.Process class for all the threads of the process to run on one CPU. Windows knows when it needs to make a decision about thread scheduling by using interrupts. An interrupt is a mechanism that causes the normally sequential execution of CPU instructions to branch elsewhere in the computer memory without the knowledge of the execution program. When the thread comes to the interrupt, Windows uses a special function known as an interrupt handler to store the thread’s state in the TLS. To abort a thread we have to send the abortion request from another thread. A priority of 0 means that the thread is idle. Priorities between 16 and 31 means that the threads are real-time (Device drivers, file systems, input devices). As long as threads of a higher priority exist, threads in lower priority are not scheduled for execution.

No comments:

Post a Comment