Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example usage of Shell multithread programming under Linux system

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report