【发布时间】:2016-09-09 08:32:39
【问题描述】:
我想在进程之间进行同步。我的电脑有 2 个核心。用户可以从命令行输入模拟编号。如果输入大于 2,则第 3 和其余进程必须等到其中一个进程已完成。如果其中一个已完成,则应执行下一个进程。例如,前 2 个进程已经在进行中,假设第 1 个进程在第 2 个进程之前完成。现在应该执行第 3 个进程。我是 bash 的新手,我想通了。看到anywait:找不到命令。我该怎么做?这是我的脚本:
#!/bin/bash
# My first script
count=2
echo -n "Please enter the number of simulation :"
read number
echo "Please enter the algorithm type "
printf "0 for NNA\n1 for SPA\n2 for EEEA :"
while read type; do
case $type in
0 ) cd /home/cea/Desktop/simulation/wsnfuture
taskset -c 0 ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/NNA/NNA0/0 &
taskset -c 1 ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/NNA/NNA0/1 &
while [ $count -lt $number ]; do
anywait
cd /home/cea/Desktop/simulation/wsnfuture
mkdir /home/cea/Desktop/simulation/RESULTS/NNA/NNA$count
taskset -c $((count % 2)) ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/NNA/NNA$count/$count &
count=$((count + 1))
done
;;
1 ) while [ $count -lt $number ]; do
cd /home/cea/Desktop/simulation/wsnfuture1
taskset -c $((count % 2)) ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/SPA/$count &
count=$((count + 1))
done
;;
2 ) while [ $count -lt $number ]; do
cd /home/cea/Desktop/simulation/wsnfuture2
taskset -c $((count % 2)) ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/EEEA/$count &
count=$((count + 1))
done
;;
* ) echo "You did not enter a number"
echo "between 0 and 2."
echo "Please enter the algorithm type "
printf "0 for NNA\n1 for SPA\n2 for EEEA :"
esac
done
function anywait(){
while ps axg | grep -v grep | grep wsnfuture> /dev/null; do sleep 1; done
}
【问题讨论】:
-
您能否像我的回答一样使用
wait命令解决您的问题? -
您可以使用select语句进行用户响应,等待等待,如果您仍然需要ps,那么pgrep更好。