简体   繁体   English

等待一个进程完成并执行另一个进程

[英]wait one process to finish and execute another process

I want to make a synchronization between the process.My computer has 2 core.User can enter the simulation number from command line.If input is greater than the 2, the 3rd and rest processes has to wait until one of the processes is finished.If one of them is finished, next process should be executed.For example, first 2 process is already proceeding and lets say, 1th one is finished before 2nd process.Now 3rd process should be executed.I am new in bash, I figured out.It is seen that anywait: command not found.How can I do that? 我想在进程之间进行同步。我的计算机有2个核心。用户可以从命令行输入模拟号。如果输入大于2,则第3个和其余进程必须等到其中一个进程完成。如果其中一个完成,则应执行下一个进程。例如,前两个进程已经在进行,让我们说,第一个进程在第二个进程之前完成。现在应该执行第三个进程。我是bash的新手,我想出来了可以看到anywait:命令未找到。我怎么能这样做? Here is my script: 这是我的脚本:

#!/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
} 

You can achieve a simple way of process synchronization in bash using wait which waits for one or more number of background jobs to complete before running the next. 您可以使用waitbash实现一种简单的进程同步方式,等待一个或多个后台作业在运行下一个之前完成。

You generally run jobs in the background by appending the & operator to the end of a command. 您通常通过将&运算符附加到命令的末尾来在后台运行作业。 At that point the PID (process ID) of the newly created background process is stored in a special bash variable: $! 此时,新创建的后台进程的PID (进程ID)存储在一个特殊的bash变量中: $! and wait command allows this process to be terminate before running the next instruction. wait命令允许在运行下一条指令之前终止此进程。

This can be demonstrated by a simple example 这可以通过一个简单的例子来证明

$ 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"

Command ouput 命令输出

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

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

You can see the script has taken ~8s to run to completion. 你可以看到脚本已经花了〜8s才能完成运行。 The breakdown on the time is 时间的细分是

  1. sleep 3 will take full 3s to complete its execution sleep 3将花费全部3秒来完成其执行

  2. sleep 4 and sleep 5 are both started sequentially one after next and it has taken the max(4,5) which is approximately ~5s to run. sleep 4sleep 5都是接下来一个接一个地开始,并且它已经花费了大约(5,5),大约是〜5s。

You can apply the similar logic to your question above. 您可以对上述问题应用类似的逻辑。 Hope this answers your question. 希望这能回答你的问题。

Your code has many other problems, but the answer is that you should declare anywait before using it (so moving it up in your script). 您的代码还有许多其他问题,但答案是您应该在使用它之前声明anywait(因此在脚本中将其移动)。

Please consider using http://www.shellcheck.net/ to at least suppress the most obvious errors/mistakes in your script. 请考虑使用http://www.shellcheck.net/来至少抑制脚本中最明显的错误/错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM