Process Controlling Mechanism
7
A while back, we were developing a crawler here at eSpace. Before the launch we decided that we should have a control system where we can pause and resume the crawler, so as to have the flexibility to fix anything that may go wrong while the crawler is running. The crawler is implemented using java, with a multithreaded architecture and a thread control scheme, so it is not a good practice to add the logic of the threads control within the crawler thread control scheme and it may take some time to implement.
The crawler will be running on a linux OS, so the first thing that came to my mind was to try and see if the linux signals can do the task for me. Actually, it did it for me in no time! Here is how it is done:
kill -STOP [pid]
kill -CONT [pid]
The first command will pause the process and the second will resume it, but take care if your application is time dependent.
Let's see a simple example:
//When a thread started using this Runnable class it will //wait for 5 seconds then print the current time. private class Timer implements Runnable{ public void run() { try { Thread.sleep(1000 * 5); System.out.println(new Time(System.currentTimeMillis())); } catch (InterruptedException e) { e.printStackTrace(); } } } //ThreadHandler is a mechanism to limit the number //of threads that will be running concurently in a seamless way. //ThreadHandler constructor takes the maximum number //of threads to be running concurrently. public static void main(String[] args) throws Exception { ThreadHandler th = new ThreadHandler(5); for (int i = 0; i < 20; i++) { th.execute(th.new Timer()); } //this will wait for all the threads submitted //to the ThreadHandler to finish. th.pollingJoin(); System.out.print("Terminated"); }
I'll write soon another blog to show how the thread handler is implemented.
If we run this example, the output will be as follows:
16:55:35
16:55:35
16:55:35
16:55:35
16:55:35
16:55:40
16:55:40
16:55:40
16:55:40
16:55:40
16:55:45
16:55:45
16:55:45
16:55:45
16:55:45
16:55:50
16:55:50
16:55:50
16:55:50
16:55:50
Terminated
Let's now apply the linux signals on the process after running it on the background
java -jar case.jar &
[1] 22928
19:00:39
19:00:39
19:00:39
19:00:39
19:00:39
19:00:44
19:00:44
19:00:44
19:00:44
19:00:44
19:00:49
19:00:49
19:00:49
19:00:49
19:00:49
kill -STOP 22928
[1]+ Stopped
kill -CONT 22928
19:01:05
19:01:05
19:01:05
19:01:05
19:01:05
Terminated
[1]+ Done
Here we have sent a STOP signal to the java process so all the underlying threads are stopped, and then we sent a CONTinue signal after 16 seconds, after which the threads will continue processing till termination.
Written By:
Michael Youssef (blog.michaeleee.com)
Comments
Post a Comment
eSpace podcast Prodcast
Archive
Latest Comments
- zxdvcs Commented on Reading Arabic in a PuTTY Connection
- zxdvcs Commented on Reading Arabic in a PuTTY Connection
- Ed Hardy Commented on Check Out Our iGoogle Gadgets
- Ed Hardy Commented on Thin, the thin server that can!

