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

1

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

2

You ve got a point there.You said is right. I believe that to be completed.

3

Yesterday someone tell me the <a href="http://www.air-yeezy.com">air yeezy </a>shoes is not as comfortable as the <a href="http://www.air-yeezy.com/products/?catagory-c58_p1.html">kanye west louis vuitton</a> and <a href="http://www.air-yeezy.com/views/?product-1203.html ">nike air yeezy</a> shoes
The <a href="http://www.air-yeezy.com">nike air yeezy</a> is so good as <a href="http://www.christianlouboutinstores.com">louboutin</a>or
<a href="http://www.christianlouboutinstores.com">christian louboutin sale</a>or
<a href="http://www.christianlouboutinstores.com">christian louboutin</a>or
<a href="http://www.christianlouboutinstores.com">christian louboutin shoes</a>or
<a href="http://www.christianlouboutinstores.com">christian louboutin pumps</a>or
<a href="http://www.christianlouboutinstores.com">louboutin shoes</a>or
<a href="http://www.christianlouboutinstores.com/christian-louboutin-boots-c-4.html">louboutin boots</a>or
<a href="http://www.christianlouboutinstores.com/christian-louboutin-pumps-c-3.htmll">louboutin pumps</a>or
<a href="http://www.christianlouboutinstores.com/christian-louboutin-sandals-c-2.html">louboutin sandals</a>

4

Perfect converter software! Great post! Thanks very much!

5

Perfect converter software! Great post! Thanks very much!

6

Perfect converter software! Great post! Thanks very much!

7

It is interesting. Tell to me, please - where to me to learn more about it?

<a href="http://supermarkethq.com/users/23133">avelox pills order online </a>

<b>buy avelox pills online without a perscription generic avelox without prescription buy avelox online in canada overnight delivery on line no prescription pharmacy avelox buying avelox without prescription avelox online fast delivery no prescription avelox discounted avelox without a prescription avelox 400 buy avelox online at discount prices </b>
<a href=http://supermarkethq.com/users/23133>where can i find avelox </a>

Post a Comment

eSpace podcast Prodcast

RSS iTunes