Skip to main content
deleted 150 characters in body
Source Link
Digital Trauma
  • 8.9k
  • 2
  • 25
  • 42

(Convention - .txt are just plain text files. .sh files are shell script files.).

Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.

A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:

#!/bin/bash

pid_count=0
for counter in $(seq 1 5)
do
    xterm -e "bash script044.txt" &
    pid_list+=" $!"
    (( pid_count++ ))
    if (( pid_count++pid_count > 2 )); then
        wait -n $pid_list
        (( pid_count-- ))
    fi
done
 
exit

This adds the pid of every xterm started to the list pid_list, and increments a counter every time a subprocess is started. If the counter is greater than 3 then we wait for any of the pids in $pid_listnext subprocess to finish. When wait returns, then we know one has finished, so we can then decrement the counter and go around again to start the next xterm.

You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.


As @chepner points out, the required -n option is only available in bash 4.3 or later.

(Convention - .txt are just plain text files. .sh files are shell script files.).

Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.

A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:

#!/bin/bash

pid_count=0
for counter in $(seq 1 5)
do
    xterm -e "bash script044.txt" &
    pid_list+=" $!"
    (( pid_count++ ))
    if (( pid_count > 2 )); then
        wait -n $pid_list
        (( pid_count-- ))
    fi
done
 
exit

This adds the pid of every xterm started to the list pid_list, and increments a counter. If the counter is greater than 3 then we wait for any of the pids in $pid_list to finish. When wait returns, then we know one has finished, so we can then decrement the counter and go around again to start the next xterm.

You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.

(Convention - .txt are just plain text files. .sh files are shell script files.).

Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.

A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:

#!/bin/bash

pid_count=0
for counter in $(seq 1 5)
do
    xterm -e "bash script044.txt" &
    if (( ++pid_count > 2 )); then
        wait -n
        ((pid_count--))
    fi
done

This increments a counter every time a subprocess is started. If the counter is greater than 3 then we wait for the next subprocess to finish. When wait returns, we then decrement the counter and go around again to start the next xterm.

You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.


As @chepner points out, the required -n option is only available in bash 4.3 or later.

edited body
Source Link
Digital Trauma
  • 8.9k
  • 2
  • 25
  • 42

(Convention - .txt are just plain text files. .sh files are shell script files.).

Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.

A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:

#!/bin/bash

pid_count=0
for counter in $(seq 1 5)
do
    xterm -e "bash script044.txt" &
    pid_list+=" $$"$!"
    (( pid_count++ ))
    if (( pid_count > 2 )); then
        wait -n $pid_list
        (( pid_count-- ))
    fi
done

exit

This adds the pid of every xterm started to the list pid_list, and increments a counter. If the counter is greater than 3 then we wait for any of the pids in $pid_list to finish. When wait returns, then we know one has finished, so we can then decrement the counter and go around again to start the next xterm.

You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.

(Convention - .txt are just plain text files. .sh files are shell script files.).

Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.

A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:

#!/bin/bash

pid_count=0
for counter in $(seq 1 5)
do
    xterm -e "bash script044.txt" &
    pid_list+=" $$"
    (( pid_count++ ))
    if (( pid_count > 2 )); then
        wait -n $pid_list
        (( pid_count-- ))
    fi
done

exit

This adds the pid of every xterm started to the list pid_list, and increments a counter. If the counter is greater than 3 then we wait for any of the pids in $pid_list to finish. When wait returns, then we know one has finished, so we can then decrement the counter and go around again to start the next xterm.

You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.

(Convention - .txt are just plain text files. .sh files are shell script files.).

Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.

A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:

#!/bin/bash

pid_count=0
for counter in $(seq 1 5)
do
    xterm -e "bash script044.txt" &
    pid_list+=" $!"
    (( pid_count++ ))
    if (( pid_count > 2 )); then
        wait -n $pid_list
        (( pid_count-- ))
    fi
done

exit

This adds the pid of every xterm started to the list pid_list, and increments a counter. If the counter is greater than 3 then we wait for any of the pids in $pid_list to finish. When wait returns, then we know one has finished, so we can then decrement the counter and go around again to start the next xterm.

You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.

added 26 characters in body
Source Link
Digital Trauma
  • 8.9k
  • 2
  • 25
  • 42

(Convention - .txt are just plain text files. .sh files are shell script files.).

Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.

A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:

#!/bin/bash

pcount=0pid_count=0
for counter in $(seq 1 5)
do
    xterm -e "bash script044.txt" &
    plist+="pid_list+=" $$"
    ((pcount++ pid_count++ ))
    if (( pcountpid_count > 2 )); then
        wait -n $plist$pid_list
        ((pcount pid_count-- ))
    fi
done

exit

This adds the pid of every xterm started to the list plistpid_list, and increments a counter. If the counter is greater than 3 then we wait for any of the pids in the pid list$pid_list to finish. When wait returns, then we know one has finished, so we can then decrement the counter and go around again to start the next xterm.

You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.

(Convention - .txt are just plain text files. .sh files are shell script files.).

Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.

A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:

#!/bin/bash

pcount=0
for counter in $(seq 1 5)
do
    xterm -e "bash script044.txt" &
    plist+=" $$"
    ((pcount++))
    if (( pcount > 2 )); then
        wait -n $plist
        ((pcount--))
    fi
done

exit

This adds the pid of every xterm started to the list plist, and increments a counter. If the counter is greater than 3 then we wait for any of the pids in the pid list to finish. When wait returns, then we know one has finished, so we can then decrement the counter and go around again to start the next xterm.

You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.

(Convention - .txt are just plain text files. .sh files are shell script files.).

Your mainscript.txt script has a race condition. Specifically the while loop starts its next iteration before the script044.txt script is able to create the temporary file. In fact the whole loop is iterated through before any of these files get created.

A more robust way to deal with this sort of thing is to forget the temporary files and use the shell builtin wait instead:

#!/bin/bash

pid_count=0
for counter in $(seq 1 5)
do
    xterm -e "bash script044.txt" &
    pid_list+=" $$"
    (( pid_count++ ))
    if (( pid_count > 2 )); then
        wait -n $pid_list
        (( pid_count-- ))
    fi
done

exit

This adds the pid of every xterm started to the list pid_list, and increments a counter. If the counter is greater than 3 then we wait for any of the pids in $pid_list to finish. When wait returns, then we know one has finished, so we can then decrement the counter and go around again to start the next xterm.

You can remove all the tempfilename-related lines from the script044.txt - they are no longer needed.

Source Link
Digital Trauma
  • 8.9k
  • 2
  • 25
  • 42
Loading