Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write Shell script to simulate multithreading function

Shulou Source: shulou.com Published: 2022-06-03 09:18:29 10月10日 Update

This article mainly explains "how to write Shell scripts to simulate multi-threading function". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to write Shell scripts to simulate multi-threading function" together!

The code is as follows:

#!/ bin/bash

function pinghost {

ping $1 -c 1 -w 10 |grep rtt|cut -d "/" -f6

}

tmp_fifo file ="/tmp/$.fifo" #The ID number of the current process the script runs as the file name

mkfifo $tmp_fifo #Create a random fifo pipe file

exec 6 $tmp_fifo file #definition file descriptor 6 points to the fifo pipeline file

rm $tmp_fifofile

thread=10

for ((i=0;i&6

while read domain

do

read -u6 #reads line from file descriptor 6 (actually pointing to fifo pipe)

{

pinghost ${domain}; #Execute pinghost function

echo >&6 #Write a blank line to the fifo pipe file again.

}& #Put it in the background

done&- #delete file descriptor 6

exit 0

Description:{} This part of the statement is put into the background as a child process execution, this part is almost completed at the same time, when fifo 10 empty lines after reading while loop

Continue to wait for fifo data to be read in read. After 10 sub-processes in the background, queue up to fifo to enter blank lines in order, so that fifo has data again. The for statement continues to execute.

Thank you for reading, the above is "how to write Shell scripts to simulate multi-threading function" content, after the study of this article, I believe we have a deeper understanding of how to write Shell scripts to simulate multi-threading function, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Files scripts pipes processes functions threads background blank lines learning size content pointing data statements loops code again functions at the same time actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Linux MySQL Apple