[SOLVED] How can i do Multithreading in shell scripting.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have shell script "A" which is calling a oralce pl/sql procedure. , i want to initiate its multiple threads at a time.
Problem statement is : user will give any value and value will be any numeric value let user give 25 then there should be 25 instance of script A per second and it will keep on continue [26 threads per second] no matter how a much time a thread take to complete it should create 25 threads after every second.
#!/bin/bash
# Run script: ./scriptname value
UserValue=$1
# loop forever
while true
do
# start threads
for i in $(seq 1 $UserValue)
do
echo "started instance no: $i"
# /path/to/script/A &
done
# sleep 1 second
sleep 1
done
A few comments on the above:
- The sleep 1 sleeps one second, but does this after all the oracle procedure's are started.
- The commented out part is were your procedure goes. Do add an & at the end. This makes sure the process is pushed to the background and bash can continue doing its thing.
- No error checking is implemented (example: did the user give a numerical value).
- Script can be stopped by hitting ctrl-c
Do test the above as is first to see if you want this (./script 10 for example).
Within bash, you technically can not run via multithreading, but you can run in parallel. A very good example can be seen here - http://blog.dbotelho.com/2009/01/mul...h-bash-script/
I hope this helps in your understanding of what you will need to do.
I just got out of bed. Fresh coffee and cigarettes handy, you on the other hand are doing the last stages of a long shift (if I'm not mistaken)
Dude, I still have three and a half hours to go! Eight and a half hours down so far.... Come on 0600!
But yes, I do see where you are coming from. I've written my share of multi-threaded applications, and each time I see the term, I think of straight-forward multi-threading. As far as your script, if I were to use that, to think about a different approach on running the script, I am thinking more towards using nohup just to be safe.
As am executing : "# /path/to/script/A & " script "A" in below mentioned script and Script "A" is executing a pl/sql procedure. it runs fine if i use : /path/to/script/A . AND when i use : " /path/to/script/A & " if given value in loop is equal to 1 then this code is working fine but if given value in loop is more than 1 then it is throwing error [Error is given below ] Means if i execute more than one backend instance with " /path/to/script/A & " below given errors are there .. :-(
code of script calling script "A"
#!/bin/bash
# Run script: ./scriptname value
UserValue=$1
# loop forever
while true
do
# start threads
for i in $(seq 1 $UserValue)
do
echo "started instance no: $i"
# /path/to/script/A &
done
# sleep 1 second
sleep 1
done
----
code of script "A" executing pl/sql procedure
Write your code so that you can spawn any number of instances of it ... using the good ol' "&" capability of the shell ... and so that each one of those instances will do their appointed job and survive until expressly terminated (e.g. by a kill command).
Design them ... using bash-scripting or (probably) not ... so that they listen for requests from some queue and, when they receive a request, carry it out then deliver the results then listen on the queue once again.
What you've created is exactly the sort of "server" that you find in the back room or behind the counter in any fast-food restaurant. The people who work there survive the shift. They might be busy-as-hell or they might be twiddling their thumbs (and five tour-buses full of hungry passengers might show up at any moment). But no one's sitting in the back room with a box full of "Instant Employee - Just Add Water," and no one's keeling over after serving you and nobody's shooting them dead.
Each instance of the service process concentrates only upon its assigned task and is neither aware of nor in direct communication with any of the others. Meanwhile, there's a manager process (maybe it's just you and the shell) overseeing their activities while (of course... ) not actually doing any of the work.
Also... remember that "spawning multiple threads" does not multiply the capabilities of the underlying system: it divides it. If you have a lot of work to do and think that "throwing multiple processes at it" will help, you might well find that the opposite is true. "Don't guess... Measure."
Last edited by sundialsvcs; 09-26-2011 at 10:50 AM.
True , bash aint running parallel.
still, i did wrote a piece of software script.cfg for some test.csv word processing.
bash is counting lines in that file , counting how many CPU u got, split lines/cores +1 ,clone script.cfg as many times as cores u got, "sed" some variables inside clones , make them executable and run them simultaneous. make a messaging mechanism to notify when all are done, and after that recombine partial result into a single result.
In this way , some test file having 30k lines is split in 4 parts( amd quad core) ,and each clone is processing just 1/4 of test file, thus CPU is running 100% on all cores. When finish , it combines all 4 parts into a single file.
mechanism to check out when all cores are done can be made using " wait", but for the sake of controlling ,i did it in my way.
let "split_lines=$file_lines/$cores"
let "split_lines=$split_lines+1"
split -d --lines=$split_lines fisier.tmp CPU
( for script in `seq 1 $cores`;
do
let sufix=script-1
cat script.cfg > "CPU0$sufix.script"
chmod +x CPU0$sufix.script
sed -i 's/filenumber/CPU0'$sufix'/g' CPU0$sufix.script
./CPU0$sufix.script &
done
)
( while [ 1 ]
do
ok=$(ls ok.* | wc -l)
if [ "$ok" = "$cores" ] ; then ./script_reintregire.sh ; exit
fi
sleep 1
linii_realizate=$(cat final.CPU* | wc -l)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.