Get the App
SLTechnology News&Howtos  ›  Development  › 

How to deal with multithreading in batch program

Shulou Source: shulou.com Published: 2022-06-03 07:52:39 10月13日 Update

This article mainly shows you "how to deal with multithreading in batch programs". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to deal with multithreading in batch programs".

The following content will give a simple example of using batch processing to do multi-process concurrency under WINDOWS in order to achieve the effect of "multithreading".

Example: all IP addresses of PING 192.168.0.1 to 192.168.0.254 are required, and the result of whether PING can be reached is output to log.txt.

The implementation is as follows:

Rem main program .bat, only one line of code, call checkip.bat, after testing, CPU2.6G MEM1G can concurrency about 40 processes

The code is as follows:

For / l%% I in (1jie 1254) do (start / min cmd / c checkip.bat 192.168.0%% I)

The rem checkip.bat content is as follows:

The code is as follows:

Ping 1-n 1

If errorlevel%==0 (echo 1 alive > > log.txt) else (echo 1 dead > > log.txt)

According to the above information, I modified it myself, and only one main program is needed to execute the code:

Program evolution

The code is as follows:

@ echo off

If "% 1" = "" (for / l% an in (1meme 1100) do (start / min cmd / c% 0 10.120.6.%%a&ping 127.0.0.1-n 1 > nul))

If not "% 1" = = "(ping-a% 1-n 1 | find" reply "/ i&&echo alive% 1 > > LogIP.txt | | echo dead% 1 > > LogIP.txt)

In order to make the code too long and easy to read, I divided it into two lines, which can be done on one line. You can modify the statements that you run directly in the cmd window. If you can't write them, you can ask me again.

Program knowledge points description:

1. Determine whether the parameter is empty

two。 Using the start command, note the difference between goto and call

3. Use ping for delay

4. Use the find command to determine whether the IP is powered on and log it

5. The output of prompt information is prohibited.

Program re-evolution

To clear the log file after each execution, modify the code as follows:

The code is as follows:

@ echo off

If "% 1" = "" (

If exist "logip.txt" del LogIp.txt

For / l an in (1meme 1100) do (start / min cmd / c 0 10.120.6.%%a&ping 127.0.0.1-n 11 > nul))

If not "% 1" = = "(ping-a% 1-n 1 | find" reply "/ i&&echo alive% 1 > > LogIP.txt | | echo dead% 1 > > LogIP.txt)

Of course, if you want to use two bat files, you can write two lines of code directly in two files, or one line for each file.

Description:

1. At the end of the for statement, ping 127.0.0.1-n 1 1 > nul,-n 1 indicates ping1 times, followed by 1 > nul means normal information redirection forbids output, and other post handles for specific blog content are redirected.

two。 The sentence if exist "logip.txt" del LogIp.txt can be replaced by echo [% date%% time%] > LogIp.txt. The function of the two sentences is the same, depending on the individual usage.

These are all the contents of this article entitled "how to deal with multithreading in batch programs". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Code program content thread file processing one line article statement output two main program information command log knowledge process learning help evolution Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info vpn OPPO Reno NVidia Xiaomi