1. Introduction

In this tutorial, we’ll explain the term “handler” in the context of computer programming.

2. Handlers

A handler is often an abstract object with a well-defined interface, representing a shared (system) resource that can’t be exposed directly to users because of security or complexity.

For example, if we want to access a file on the disk, we’ll make an open system call to the underlying operating system. This call opens the file stream, map it to a file handler, and returns it:

File Handler

We use this handler to access the file further, read its content, add new text to it, or close it:

File Handler Operations

A handler is usually an object of very small size (4 bytes to 16 bytes). We store it in the process control block of the process that opens it.

3. Types of Handlers

There are several types of handlers.

3.1. Memory Handler

Coming now to the memory handler, we use it to perform operations on memory.

We use a handler in memory management to represent a big object in system memory. Then, we perform read/write, move, and swap operations on this object using a memory handler. The benefit is that we don’t expose the actual physical memory address of the object we’re handling. This way, we separate references from storage.

3.2. Signal Handler

A signal is a message that a process or a thread receives when an event of interest occurs in the programming environment. The idea is to inform the process or thread about some critical happening it needs to deal with without delay.

Every process usually registers a handler function that the operating system can call once the signal is fired. The operating system suspends the execution of the process/thread and starts running the signal handler. All predefined signals are OS-level constructs, and the OS kernel handles them. The signal handler will catch the signal, process it, and then return the control to its process or thread.

To conclude, we can say that a signal handler is usually a non-blocking asynchronous and highly compact code unit that runs in the background.

3.3. Interrupt Handler

Every hardware device uses an interrupt to signal urgent and critical events to the central processor. The CPU stops the current activity after receiving an interrupt and reacts to it. It does so by invoking the interrupt handler for the device that made it.

An interrupt handler is a small but highly efficient function we use to deal with an interrupt. We call it the interrupt service routine or ISR. Its job is to handle the device for the interrupt and restore its pre-interrupt state.

3.4. Event Handler

Our program might be interested to process certain events instantaneously. For example, if we have a learning management system (LMS), we want to process the click on the submit button of a quiz almost immediately. So, for event processing, we use event handlers.

An event handler is an object that we use in the system to notify our program about the occurrence of those events that are of value to us. To implement it, we use an event listener system along with our program.

The event listener system will poll all relevant events for our program. For this system to work, we need to register the corresponding handlers with the listener. The event listener will invoke the appropriate handler when it receives the event and transfer control to the handler for further processing. Handlers run atomically i.e., they execute completely in one pass.

So, we can say that all event handlers are executed synchronously in the order of their registration binding.

3.5. Exception Handler

An exception is any unplanned and often erroneous event that occurs while our program is running. For example, if we try to read a file from the disk that doesn’t exist physically on the disk, we’ll get a “file not found” exception.

An exception can halt and disrupt normal execution. We usually handle it by defining an exception handler.

In the above example, we can put the code to access the file in a so-called try block, and specify the handler in the catch block.

4. Conclusion

In this article, we learned the concept of the handler in computer programming and briefly visited important types of handlers that we encounter in various types of software.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.