Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interrupt driven vs Event Driven I/O model

Tags:

While reading Node JS , the definition says that the I/O model is event Driven. How is this different than interrupts in multi threading environment ? In a multithreading environment, when I/O operation is completed , an interrupt is generated and waiting thread is now pushed in read to run state. In Node Js, based on the event thrown after the I/O is completed, the callback handler is pushed to the event Queue.

Why are the two different?

like image 734
learnerFromHell Avatar asked Mar 22 '16 01:03

learnerFromHell


1 Answers

I'm not sure that I understand the question but it is my understanding that...

The two concepts are different in that they apply to processing at a different level. Events are employed at a higher level (system/code) than interrupts, which happen at lower level (CPU). So even though the interrupts do happen at the CPU level, nodejs abstracts them (using a separate thread for I/O) that will execute a callback if defined. What a multi-threaded environment, such as Java, does is suspend and resume threads. This is not the same as the CPU-level Interrupts. Java does let the programmer implement interrupts like this:

for (int i = 0; i < importantInfo.length; i++) {
    // Pause for 4 seconds
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        // We've been interrupted: no more messages.
        return;
    }
    // Print a message
    System.out.println(importantInfo[i]);
}

but this is not at all like a CPU interrupt or a nodejs Event/Callback.

Even though nodejs is said to be single threaded, it does use multiple threads, however it only uses one main thread that is designed to spend little time on the CPU and pass on other IO or CPU intensive tasks to other tasks, so that is may be ready for handling the next (typically http) request

Some reference links:

http://www3.ntu.edu.sg/home/ehchua/programming/java/J5e_multithreading.html

http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html

Difference between interrupt and event

https://softwareengineering.stackexchange.com/questions/298493/nodejs-like-interrupt-handlers

like image 166
Daniel Avatar answered Oct 12 '22 10:10

Daniel