in reply to Re: Re: Non-blocking pings on Win32
in thread Non-blocking pings on Win32

Thanks everybody. Since I have 5000+ servers to deal with, I have to be pretty creative with everything I do. Almost all the code I write has to be based on threads, if I hope to get anything done in a reasonable time.

As for the changes NetWallah made above, they do make sense and I should probably have included them. The real code I use in production is a bit different:

IP Addresses are pulled from a database. Since I don't want to use up a lot of memory putting all 5000+ entries into an array, I actually spawn the threads first, and then slowly feed entries into the Queue as I read them from the database.

I needed a "while (1)" because there are times in the thread's life when the queue is empty but it should wait rather than exiting, such as when it's first created. My production code has a shared flag indicating whether all entries have been loaded into the queue, and then each thread will "sleep 1; next;" or "last;" based on the status of that shared flag.

Also, rather than have a "result" array in memory holding thousands of results to print afterwards, each thread maintains an open connection to the database during its life, and uses it to store the results.

  • Comment on Re: Re: Re: Non-blocking pings on Win32

Replies are listed 'Best First'.
Re: Re: Re: Re: Non-blocking pings on Win32
by Ninthwave (Chaplain) on Dec 12, 2003 at 12:00 UTC

    Instead of using the shared flag would it be better to use Thread::Queue::Monitored as I have found that the array loading is pretty much a drag and am thinking of spawning the threads, loading the ips directly to the queues and at the end of the queing fire off a series of exit commands equal to the threads waiting.

    I am trying it now and hopefully have some code soon as an example, but was just curious if you had tried it?

    Update:
    It appears that Thread::Queue::Monitored only sets up one thread to Monitor the queue which I am going to try to use as the back end process to get the results of the pings instead of trying to shove them into the array. Have the Monitored Queue do the db processing.

    "No matter where you go, there you are." BB
      No, I have not tried using Thread::Queue::Monitored yet... But I will look into it, thanks.
Re: Re: Re: Re: Non-blocking pings on Win32
by Ninthwave (Chaplain) on Jan 13, 2004 at 10:03 UTC

    This is the code I have come up with. This is for a Win32 machine and I am using Win32::ODBC with MySQL. I have not coded it yet to be database independent but that will happen as I update the code but the thread processing works fine and I am able to scan a class A fairly efficiently. There seems to be a small memory leak in it but it does not affect the scan adversely. Memory grows very slowly over a large period of time. With the command line options though you can have the script scan ranges from a batch file or bash script exit and than do another range minimising the affects of the leak.

    The Pod is not complete as I am still working on the script. I have to remove references to cab extract as I am doing that process in another script and I have to describe all command line options though it is fairly evident from the code what the options do.

    One major note is we were getting responses where the ping replied by gethost returned null. I created a unique computer name based off that and am currently working on a clean up section that tries to identify the invidual devices.

    Any additional clean up or efficiency suggestions would be appreciated.

    "No matter where you go, there you are." BB