This article is just to showcase the SignalHandler in Java with an example of showing a Java program that prints some content infinite times and how to use it along with head, which just takes the given lines and expect to stop the execution.
Assume that you have a file of around 100M rows of data, if you would like to
cat the content of the file, it might take a couple of minutes for the operation to get completed. But assume that if you have to execute the below line,
cat my_big_file.txt|head -10
It would get completed instantaneously. What happens here is that,
cat start printing out the content of the file, head takes as much as it needs and says “I’m done”.
cat is able to hear that and stops reading the file and the whole execution completes.
cat, assume that you have some java program that returns an infinite stream of data. And how to make it work the same way as the
cat above? How would we make the Java program hear when the
head says “I’m done”?
The solution might not be apparent until you have a bit of understanding about
SIGPIPE. Signals are communications to the process from OS. In this context head saying “I’m done” is via
head takes the first 10 lines from the pipe and gets completed. And since there is no process to read from the pipe, the process that is writing to the pipe (
cat) receives a
SIGPIPE signal, on which it has a choice to act on.
Given enough context about
SIGPIPE, we have to explicitly write the code to handle the
SIGPIPE within a Java program. This example would showcase usage. The program prints a string infinite number of times. Without the implmentation of signal handling the program would never stop.
Usage of the above program,
This prints just the first 20 lines and stops.
java RespectSigPipe | head -20
The Signal/SignalHandler are part of sun.misc package so not guaranteed to stay always. Which is why you get warnings while compiling the example program.
You might be thinking “Why on earth anyone would use a Java program along with pipe?”. I got to know about this when I was playing around with a Graalvm compiled, CLI utility in Clojure. That utility achieves the lazy series feature using
Signal/SignalHandler. The use cases might be rare but anyway it’s good to know things.
For any feedback or corrections, please write in to: firstname.lastname@example.org