最基本的并发执行,放在后台并发执行a、b,c、d
shell A&
shell B&
wait
shell C&
shell D&
#!/bin/bash
#创建管道文件,文件操作符绑定,删除管道文件
mkfifo testfifo
exec 1000<>testfifo
rm -rf testfifo
#通过一个for循环写入2个空行,这个2就是我们要定义的后台线程数量。
for((n=1;n<=2;n++))
do
echo >&1000
done
start=`date "+%s"`
echo $start
for((i=1;i<=20;i++))
do
#读取一次管道中的一行,减少操作附中的一个空行之后,执行一次任务
read -u1000
{
#`dd if=/dev/zero of=../swapfile4 bs=1024 count=662154`
#`dd if=/dev/zero of=../swapfile4 bs=1024 count=662154`
echo "line${i} finished."
sleep 2
echo >&1000
}&
done
wait
endtime=`date "+%s"`
echo $endtime
mt=`expr $endtime - $start`
echo "TIME: $mt"
#exec 1000>&-和exec 1000<&- 是关闭fd1000的意思
exec 1000>&-
exec 1000<&-
可以控制次数
#!/bin/bash
mkfifo testfifo
exec 1000<>testfifo
rm -rf testfifo
for((n=1;n<=2;n++))
do
echo >&1000
done
start=`date "+%s"`
echo $start
for((i=1;i<=1;i++))
do
read -u1000
{
`dd if=/dev/zero of=../swapfile3 bs=1024 count=662154`
echo "line${i} finished."
sleep 2
echo >&1000
}&
read -u1000
{
`dd if=/dev/zero of=../swapfile4 bs=1024 count=662154`
echo "line${i} finished."
sleep 2
echo >&1000
}&
done
wait
endtime=`date "+%s"`
echo $endtime
mt=`expr $endtime - $start`
echo "TIME: $mt"
exec 1000>&-
exec 1000<&-