Created
June 18, 2017 11:53
-
-
Save westfly/9f92cad2dc8e58940105d0e4ad4e17bb to your computer and use it in GitHub Desktop.
shell 管道控制多进程
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#usage multi_thread_template.sh | |
fifo="/tmp/$$.fifo" #建立管道$$表示shell分配的进程号 | |
mkfifo $fifo | |
exec 6<>$fifo #将fifo的fd与6号fd绑定 | |
thread_num=64 #启动的进程个数 | |
count=0; | |
#预分配资源 | |
while [[ $count -lt $thread_num ]]; do | |
echo >&6 | |
#let count=count+1 | |
count=$((count + 1 )) | |
done | |
#任务列表 | |
file_list=$1 | |
cat $file_list | while read url | |
do | |
read -u6 #请求一个资源 | |
{ | |
echo "Task Begin" | |
sleep 1 | |
echo $file #任务 | |
echo "Task End" | |
# produce a cook | |
echo >&6 #完成任务,释放一个资源 | |
}& | |
done | |
wait #等待所有的任务完成 | |
exec 6>&- #关闭fd 6描述符 | |
rm $fifo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment