【问题标题】:wait one process to finish and execute another process等待一个进程完成并执行另一个进程
【发布时间】: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更好。

标签: linux bash shell ubuntu


【解决方案1】:

您可以在bash 中使用wait 实现一种简单的进程同步方法,该方法在运行下一个之前等待一个或多个后台作业完成。

您通常通过将& 运算符附加到命令末尾来在后台运行作业。此时,新创建的后台进程的 PID(进程 ID)存储在一个特殊的 bash 变量中:$!wait 命令允许在运行下一条指令之前终止该进程。

这可以通过一个简单的例子来证明

$ cat mywaitscript.sh

#!/bin/bash

sleep 3 &

wait $!     # Can also be stored in a variable as pid=$!

# Waits until the process 'sleep 3' is completed. Here the wait on a single process is done by capturing its process id

echo "I am waking up"

sleep 4 &
sleep 5 &

wait                    # Without specifying the id, just 'wait' waits until all jobs started on the background is complete.

echo "I woke up again"

命令输出

$ time ./mywaitscript.sh
I am waking up
I woke up again

real    0m8.012s
user    0m0.004s
sys     0m0.006s

您可以看到脚本运行完成大约需要 8 秒。时间的细分是

  1. sleep 3 将需要整整 3 秒才能完成其执行

  2. sleep 4sleep 5 都是依次启动的,运行时间大约为 ~5s。

您可以将类似的逻辑应用于上述问题。希望这能回答您的问题。

【讨论】:

  • @Camusensei:更新!我必须这样做以演示如何将特殊变量也存储在另一个变量中。
  • 感谢您的编辑。现在,只要您可以将评论从 pid=$(echo $!) 更改为 pid=$! ... :)
【解决方案2】:

您的代码还有许多其他问题,但答案是您应该在使用它之前声明 anywait(因此在您的脚本中向上移动它)。

请考虑使用http://www.shellcheck.net/ 至少抑制脚本中最明显的错误/错误。

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2012-03-04
    • 2011-04-22
    • 2015-02-25
    相关资源
    最近更新 更多