Get the App
SLTechnology News&Howtos  ›  Servers  › 

Example usage of Shell multithread programming under Linux system

Shulou Source: shulou.com Published: 2022-06-01 12:15:02 09月22日 Update

This article mainly explains "the example usage of Shell multithreaded programming under Linux system", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Shell multithreaded programming example usage under Linux system"!

#!/ bin/bash#------------------------------------#This example illustrates a technique for simulating multithreading with wait and read commands #This technique is often used for multihost checks, such as ssh login, ping, etc. This single process is relatively slow and does not consume CPU #Also illustrates the control of multithreading #--------------------------- #Create a fifo file exec 6$tmp_fifo file #Point fd6 to fifo type rm $tmp_fifo thread=15 #Define the number of threads here for((i=0;i&6 #In fact, put $thread carriage characters in fd6 for(i=0;i&6 #After the process ends, add a carriage character to fd6, that is, add the read -u6 minus}& done wait #Wait for all background subprocesses to end exec 6>&- #Close df6 exit 0

Description:

Commands in this program

mkfifo tmpfile

Command in Linux

mknod tmpfile p

Effect? The fruit is the same. The difference is that mkfifo is POSIX standard, so it is recommended. This command creates a first-in, first-out pipeline file and assigns it the file identifier 6. Pipeline files are a way of communicating between processes, and it's important to note that

exec 6$tmp_fifofile #point fd6 to fifo type

Without this sentence, writing data to the file $tmp_fifofile or &6 blocks the program until a read reads the data from the pipe file. If you execute this sentence, you can write data to fifo files continuously during the program without blocking, and the data will be saved for reading by the read program.

By running the command:

time ./ multithread.sh >/dev/null

Final operation time: 50/15 = 3 groups (15 per group)+1 group (5

Tags: Threads files processes commands data programs types instances systems programming time pipes hosts content background skills pointing understanding learning control Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology vpn Redmi Shulou Tech Info