Async/Await - OS Concept
There are many question about Async/Await in "dot net"? what really are? what is the issues it handle? is there any dedicated thread/mechanism monitor when a task completes, so notify thread pool to choose thread and continue execution? when to use it?
to answer this question let us deep dive a little in some Operating System (OS) concepts and mechanism to handle I/O.
What is I/O?:-
I/O (input/output), pronounced "eye-oh," describes any operation, program, or device that transfers data to or from a computer. Typical I/O devices are printers, network card, hard disks, keyboards, and mouses. In fact, some devices are basically input-only devices (keyboards and mouses); others are primarily output-only devices (printers); and others provide both input and output of data (hard disks, diskettes, writable CD-ROMs).
Programing examples require I/O operations:-
- when application query database it get data from hard disk(I/O)
- when call api, it call network card(I/O)
- when send document to printer(I/O)
When this happens :
- The (OS) ask device driver software to begin write operation by constructing I/O request (IRQ)(it generates a handle that is used later to know each request come from each process)
- Driver issues a command to the device, If the device supports Direct Memory Access (DMA), this can be as simple as writing the buffer address to a device register.
- IRQ is pending
- The os returns to BCL Liberary , which returns an incomplete task to "dot net" process, and CPU is free (switch back to other processes), no thread monitor or blocked.
- After writing occurs on the physical device by its microchip/controller.
- It notifies the CPU via an interrupt.
- The device driver’s Interrupt Service Routine (ISR) responds to the interrupt
- When CPU handle interputs it freeze and not do multitasking until interrupt is handled, (You noticed that if you install a bad printer driver, the pc does not respond correctly and unistall it and search for better one)
Interrupt : - An interrupt is a signal emitted by hardware or software when a process or an event needs immediate attention. It alerts the processor to a high-priority process requiring interruption of the current working process. In I/O devices, one of the bus control lines is dedicated for this purpose and is called the Interrupt Service Routine (ISR).
- To Handle issue of freezing (OS) when interput is bad or a take more processing and slow?
- First Level Interrupt Handler (FLIH) is a hard interrupt handler or fast interrupt handler. These interrupt handlers have more jitter while process execution, and they are mainly maskable interrupts.
- Second Level Interrupt Handler (SLIH) is a soft interrupt handler and slow interrupt handler. These interrupt handlers have less jitter
When a device raises an interrupt at the process, the processor first completes the execution of an instruction. Then it loads the Program Counter (PC) with the address of the first instruction of the ISR. Before loading the program counter with the address, the address of the interrupted instruction is moved to a temporary location. Therefore, after handling the interrupt, the processor can continue with the process.
Anyway, the ISR is properly written, so all it does is tell the device “thank you for the interrupt” and queue a Deferred Procedure Call (DPC).
.
The DPC takes the IRQ representing the write request and marks it as “complete”. However, that “completion” status only exists at the OS level; the process has its own memory space that must be notified. So the OS queues a special-kernel-mode Asynchronous Procedure Call (APC) to the thread owning the HANDLE
Then a pool thread is chosen to continue the task
Comments
Post a Comment