In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve shell multithreaded programming under Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Multithreaded programming in Linux has the advantages of improving the response of applications and making multi-cpu systems more effective. The following editor will explain the process of multithreaded programming through the example of shell multithreaded programming under Linux.
#! / bin/bash
#
# this example illustrates a technique for simulating multithreading with wait and read commands
# this technique is often used for multi-host checking, such as ssh login, ping, etc., where a single process is slow and does not cost cpu
# also explains the control of multithreading
#
Function a_sub
{
# A function is defined here as a thread (child process)
The function of sleep 3 # thread is sleep 3s
}
Tmp_fifofile= "/ tmp/$.fifo" mkfifo $tmp_fifofile # create a new file of type fifo
Exec 6 "$tmp_fifofile # points fd6 to fifo type
Rm $tmp_fifofile thread=15 # the number of threads is defined here
For
((iTuno "$thread;i++)); do echo
Done "& 6 # actually put $thread carriage return in fd6.
For
Do # 50 cycles, which can be understood as 50 hosts, or other
Read-U6 # A read-U6 command executes once, subtracts a carriage return from fd6, and then executes down
When there is no carriage return in # fd6, it stops here to control the number of threads.
{# this virgin subprocess starts to execute and is placed in the background
A_sub & &
{# this can be used to determine the logic of a child process
Echo "a_sub is finished"
}
| | |
{echo "sub error"
}
Echo "& 6 # when the process is over, add a carriage return to the fd6, that is, add the one subtracted by read-U6
}
& done wait # waits for all backstage subprocesses to finish
Exec 6 "&-# close df6 exit 0
Description:
Commands in this program
Mkfifo tmpfile
And commands in linux
Mknod tmpfile p
Effective? The results are the same. The difference is that mkfifo is the POSIX standard, so it is recommended. This command creates a first-in, first-out pipe file and assigns it a file marker of 6. It is important to note that pipe files are a way of communicating between processes.
Exec 6 "$tmp_fifofile # points fd6 to fifo type
Without this sentence, when writing data to the file $tmp_fifofile or & 6, the program will be blocked until a read reads the data in the pipe file. After executing the above sentence, you can continue to write data to files of type fifo without blocking while the program is running, and the data will be saved for read programs to read.
By running the command:
Time . / multithread.sh "/ dev/null
Final operation time: 50 amp 15 = 3 groups (15 each) + 1 group (5 "15 form a group) = 4 groups, each group takes 3 seconds
Then 3 * 4 = 12 seconds.
Traditional non-multithreaded code operation time: 50 * 3 = 150 seconds.
This is the end of the content of "how to achieve shell multithreaded programming under Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.