-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.sh
executable file
·61 lines (56 loc) · 1.16 KB
/
thread.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
#usage ./thread threadId limit
TMP_OS=$(uname | tr "[:upper:]" "[:lower:]")
if [[ "{$TMP_OS}" = *darwin* ]]; then
LOCK='lockfile ../lock.lock'
UNLOCK='rm -f ../lock.lock'
elif [[ "{$TMP_OS}" = *linux* ]]; then
LOCK='lockfile-create ../lock'
UNLOCK='lockfile-remove ../lock'
fi
ID=$1
if [[ "$2" = /* ]]
then
: # Absolute path
EXEC=$2
else
: # Relative path
EXEC=$(pwd)/$2
fi
if [[ "$3" = /* ]]
then
: # Absolute path
DATA_FILE=$3
else
: # Relative path
DATA_FILE=$(pwd)/$3
fi
LIMIT=$(awk 'END{print NR}' "$DATA_FILE")
COUNTER='../syncedCount.txt'
mkdir "$1"
for arg in "${@:4}" ; do
cp -rf "$arg" "$1"
done
cd "$1" || exit
while :
do
$LOCK
index=$(cat $COUNTER)
echo "Thread $1 says: old index value: ""$index"
index=$((index+1))
echo "Thread $ID says: new index value: ""$index"
echo "$index" > "$COUNTER"
$UNLOCK
if [ "$index" -le "$LIMIT" ]; then
WORK_LOAD=$(sed -n "$index"p < "$DATA_FILE")
echo "Thread $ID says: working on " "$WORK_LOAD"
echo ""
sh "$EXEC" "$WORK_LOAD" > "../standardOutput/$WORK_LOAD.txt" 2>&1
else
echo "Thread $ID done, no more work"
echo ""
cd ..
rm -rf "$1"
exit
fi
done