Parallel processes to INC- and DEC-rement shared counter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parallel processes to INC- and DEC-rement shared counter
# 1  
Old 12-18-2010
Question Parallel processes to INC- and DEC-rement shared counter

QUESTION: How do I run processes in parallel, so that the counter (in counter.txt) would vary in value (instead of just "0" and "1")? That is, how to not sequentially run inc.sh and dec.sh?

The shared counter (a single number starting as 0) is in a file counter.txt.

counter.sh is (supposed to be; this is my problem) running (sub)processes in parallel:
Code:
#!/bin/bash
#counter.sh
N=${1:-100}
start=0

# First create counter.txt file with 0 as a start.
touch counter.txt
echo $start > counter.txt
echo "Start value: $start"

USAGE="usage: bash $0 #N# [-s]"
if [ $# -gt 2 ]; then
	echo "$USAGE"
	exit 1
elif [ $# -eq 2 ]; then
	synchronized=true
fi

# create 2*N (parallel) processes:
if [[ $synchronized ]]; then
	echo "SYNchronized:"
	# This for loop is the crux of the counter.sh script:
	for (( i = 0; i < $N; i++ )); do
		(       # Aren't brackets ( and ) enough for parallelism?
		bash inc.sh $i	-s   # switch name (-s) is arbitrary; enough to have 2 args
		bash dec.sh $(($i+1)) -s
		)
	done

else
	echo "A-SYNchronized:"
	for (( i = 0; i < N; i++ )); do
		(
		bash inc.sh $i	
		bash dec.sh $(($i+1))
		)
	done
fi

final=$(cat counter.txt)
echo "Final value: $final"

counter.sh calls this tiny script dec.sh (inc.sh differs only in plus "+", instead of "-"):
Code:
#!/bin/bash
#dec.sh (inc.sh has a plus (+)instead of minus)
if [ $# -eq 2 ]; then
	lockfile -1 dec.lock
fi

number=$(cat counter.txt)
echo -n "Dec $1: $number -> "
number=$(($number - 1)) # minus is plus (+) in inc.sh
echo $number > counter.txt
echo "$number"

if [ $# -eq 2 ]; then
	rm -f dec.lock
fi

The output of counter.sh is completely deterministic:
Code:
courteous@courteous-P5K ~/Documents/test $ bash counter.sh 5 -s
Start value: 0
SYNchronized:
Inc 0: 0 -> 1
Dec 1: 1 -> 0
Inc 1: 0 -> 1
Dec 2: 1 -> 0
Inc 2: 0 -> 1
Dec 3: 1 -> 0
Inc 3: 0 -> 1
Dec 4: 1 -> 0
Inc 4: 0 -> 1
Dec 5: 1 -> 0
Final value: 0

QUESTION: How do I run processes in parallel, so that the counter (in counter.txt) would vary in value (instead of just "0" and "1")? That is, how to not sequentially run inc.sh and dec.sh?

Last edited by courteous; 12-18-2010 at 10:14 AM..
# 2  
Old 12-18-2010
Have you a question for this forum?
# 3  
Old 12-18-2010
[SOLVED]

The solution is for dec.sh and inc.sh to lock counter.txt (and not as it is in OP). And also run both processes in for loop with ampersand (&). All this by trial&error ... will have to learn what braces actually are good for.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

(bash) Script Processes in Parallel

Hello all, I tried to parralise my treatments but after a while 'ps -ef' display all child process <defunct> (zombie) Parent bash script to process all files (>100000) in directory: for filename in /Data/*.txt; do ./child_pprocess.sh $filename & done exit(0)I understand that the... (1 Reply)
Discussion started by: namnetes
1 Replies

2. Shell Programming and Scripting

Keep up constant number of parallel processes

Hi guys, I am struggling with adapting my script to increase the performance. I created a ksh script to process a lot of files in parallel. I would like to know how can I do in such a way that a constant number of processes is always up (until all is finished). What I have is (not actual... (8 Replies)
Discussion started by: lurkerro
8 Replies

3. Shell Programming and Scripting

Deleting all the parallel processes launched when the main script receives a ctrl+c

Hi, I have a shell script that creates 2 parallel processes. When I press ctrl+c, i want the parallel process to get killed as well. #!/bin/bash cmd1="script1.py" cmd2="script2.py" ${cmd1} & pid1=$! echo ${pid1} ${cmd2} & pid2=$! (7 Replies)
Discussion started by: sana.usha
7 Replies

4. Homework & Coursework Questions

processes and shared memory

Hi again! I have 2 questions ..: How can i create exactly one number of processes ? For example i want to create l*n processes and i tried this: for(i=0;i<l*n;i++){ pid=fork()} But it creates more than l*n Also, i want each child to run another x.c program with 3 command line... (1 Reply)
Discussion started by: giampoul
1 Replies

5. Homework & Coursework Questions

processes and shared memory

Hi again! I have 2 questions ..: How can i create exactly one number of processes ? For example i want to create l*n processes and i tried this: for(i=0;i<l*n;i++){ pid=fork()} But it creates more than l*n Also, i want each child to run another x.c program with 3 command line... (1 Reply)
Discussion started by: giampoul
1 Replies

6. UNIX for Advanced & Expert Users

AIX: Finding processes attached to shared memory

Is there some way to tell what processes are attached to a shared memory segment? We have a system on which I perform "icps -ma" and there are several segments pending deletion having numerous processes attached to them and I can't tell what processes they are. Neither the creator's pid nor last... (7 Replies)
Discussion started by: DreamWarrior
7 Replies

7. Shell Programming and Scripting

Retention of Variable Value when a script is called by different processes in parallel- Linux 2.6.9

Hi, I have a generic FTP script which will be called by 28 different processes in parallel (through a GUI tool) may or may not be at the exact moment (there could be a delay of about a minute or so). ./FTP.ksh 1 (1 through 28) This script after importing file from remote m/c... (1 Reply)
Discussion started by: dips_ag
1 Replies

8. Shell Programming and Scripting

Processes in Shared Memory

Hello , I would like to know how to check if a given process id belongs to particualr shared memory segment . Please help Thanks in advance (3 Replies)
Discussion started by: rmv
3 Replies

9. Programming

how to view loaded shared libraries by running processes in linux

anybody knows how to view loaded shared libraries by running processes in linux enviornment? any command or tool ? thanks a lot (3 Replies)
Discussion started by: princelinux
3 Replies

10. Shell Programming and Scripting

How to run processes in parallel?

In a korn shell script, how can I run several processes in parallel at the same time? For example, I have 3 processes say p1, p2, p3 if I call them as p1.ksh p2.ksh p3.ksh they will run after one process finishes. But I want to run them in parallel and want to display "Process p1... (3 Replies)
Discussion started by: sbasak
3 Replies
Login or Register to Ask a Question