Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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
} 
like image 423
eakn Avatar asked May 13 '16 08:05

eakn


1 Answers

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.

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: $! and wait command allows this process to be terminate before running the next instruction.

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. The breakdown on the time is

  1. sleep 3 will take full 3s to complete its execution

  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.

You can apply the similar logic to your question above. Hope this answers your question.

like image 101
Inian Avatar answered Oct 16 '22 23:10

Inian