Multi threading - running multiple processes at the same time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multi threading - running multiple processes at the same time
# 1  
Old 07-31-2016
Multi threading - running multiple processes at the same time

so i've been using this a lot in a lot of my scripts:

Code:
( columnA & columnAPID=$! & columnB & columnBPID=$! & columnC & columnCPID=$! &) &
wait ${columnAPID}
wait ${columnBPID}
wait ${columnCPID}

It seems to work as ive seen it dramatically reduce run time of my scripts.

however, i'm wondering if there's another way to do the above? a better way, maybe? one that' is of course portable and will be faster??

i'm using /bin/sh
# 2  
Old 07-31-2016
Quote:
Originally Posted by SkySmart
so i've been using this a lot in a lot of my scripts:

Code:
( columnA & columnAPID=$! & columnB & columnBPID=$! & columnC & columnCPID=$! &) &
wait ${columnAPID}
wait ${columnBPID}
wait ${columnCPID}

It seems to work as ive seen it dramatically reduce run time of my scripts.

however, i'm wondering if there's another way to do the above? a better way, maybe? one that' is of course portable and will be faster??

i'm using /bin/sh
You're using an unneeded subshell and running several background jobs that do not need to be in the background. Your script could more simply (and slightly more efficiently) be written as:
Code:
columnA & columnAPID=$!
columnB & columnBPID=$!
columnC & columnCPID=$!
wait ${columnAPID}
wait ${columnBPID}
wait ${columnCPID}

or if you don't have any other background jobs and aren't going to check the exit status of each individual wait command to verify that all three jobs completed successfully:
Code:
columnA &
columnB &
columnC &
wait

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-01-2016
As the title of this thread suggests you confuse multithreading and multitasking.

The difference between a thread and a task is that a task is a process in its own rights: complete with its own process environment, its own stack, its own text segments, data segments, and so on. A thread has nothing of that. It is just a separate succession of machine statements sharing the process environment with all the other threads which make up this certain process.

Multitasking is having several processes run at once. Multithreading means that one process has several threads (usually utilizing a CPU specifically designed to support that).

What you do here is multitasking. You start several processes (even the same executable started several times will create separate processes) and if these processes are multithreaded depends on the exectuable.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 08-01-2016
Quote:
Originally Posted by bakunin
As the title of this thread suggests you confuse multithreading and multitasking.

The difference between a thread and a task is that a task is a process in its own rights: complete with its own process environment, its own stack, its own text segments, data segments, and so on. A thread has nothing of that. It is just a separate succession of machine statements sharing the process environment with all the other threads which make up this certain process.

Multitasking is having several processes run at once. Multithreading means that one process has several threads (usually utilizing a CPU specifically designed to support that).

What you do here is multitasking. You start several processes (even the same executable started several times will create separate processes) and if these processes are multithreaded depends on the exectuable.

I hope this helps.

bakunin
thank you for this. any additional ideas on how to make what i have in my original post more efficient?

i read somewhere that i can "pipe" the functions, but i haven't seen an example on how to do this.
# 5  
Old 08-01-2016
Let me see if I understand this. You have three scripts that do something or nothing and they do whatever they do faster if you run them in parallel than if you run them serially. And, you want us to tell you how to convert those three scripts into functions that can be piped together to do something or nothing faster. If these three scripts run faster in parallel instead of serially, why would connecting them with pipes make them run faster? Connecting three scripts with pipes implies that the output of one of the scripts is input to one of the other scripts and the output of that script is input to the third script; but if you are running them in parallel, that can't be true.

Don't you think we might have a better chance of helping you figure out if there is a way to optimize what your three scripts are doing if we knew something about what those three scripts are doing?
These 3 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-01-2016
Quote:
Originally Posted by SkySmart
any additional ideas on how to make what i have in my original post more efficient?

i read somewhere that i can "pipe" the functions, but i haven't seen an example on how to do this.
I do not know what you mean by "pipe functions".

What i can suggest, though, is to make the parallelisation scheme more flexible, but if this is possible at all depends on the exact problem you are working on and how much parallelisation it allows for.

Suppose you have some "stream" of values you want to work on coming from somewhere, like in the following loop "VAL" is fed consecutive values:

Code:
while read VAL ; do
     some_command "$VAL"
done

To work on several values of VAL at the same time we first need to define a fanout value: it only makes sense to have so many parallel instances of some_command running at the same time. How many exactly depends on your system, the nature of some_command, the nature of your input data (the values of VAL) and perhaps a few other things but there will be *some* value whic is optimal. Let us call this value FANOUT. Here is what we are going to do, first in pseudocode:

Code:
while less processes than FANOUT
     if input left
          start_new_process with next-input
     else
          exit loop
     fi
end while
wait <for all sub-processes ending>

here is a sketch for a solution in shell (ksh actually):

Code:
typeset -i iFanOut=15     # replace this with a sensible value
typeset chVal=""

read chVal
while : ; do
     if [ $(jobs -p | wc -l) -le $iFanOut ] ; then
          some_command "$chVal" &
          if ! read chVal ; then
               break
          fi
    fi
    sleep 5                  # replace with a sensible value
done
wait ....

It should be feasible to pack this into a generic function and call that with the command to execute as parameters. Something like this (not tested):

Code:
function multitask
{
typeset -i iFanOut=15     # replace this with a sensible value
typeset chCmd="$1"
typeset chVal=""

if ! read chVal ; then
     return
fi
while : ; do
     if [ $(jobs -p | wc -l) -le $iFanOut ] ; then
          $chCmd "$chVal" &
          if ! read chVal ; then
               break
          fi
    fi
    sleep 5                  # replace with a sensible value
done
wait

return 0
}


# main ()
produce_list | multitask "some_command"

Save for that i share Don Craguns astute observations: if you show us "something like" your problem you are likely to get "something like" an answer, most probably producing only "something like" the desired result instead of the real thing.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multi threading in UNIX

Hi, Can we apply multi threading in Unix. I am using bash shell. We have a generic script to load the data to table based on file input. For each file there is an individual table to load. For each file found in directory I want to load the data in parallel to target table using ... (3 Replies)
Discussion started by: vedanta
3 Replies

2. Shell Programming and Scripting

Processes running response time

Hi All I have been asked to write scripts within our monitoring tool for a vast requirement set. One of the requirements is below: • Lowest, Highest & Average response times of the Documentum process threads serving client requests Essentially they want a view where we can see the... (4 Replies)
Discussion started by: simpsa27
4 Replies

3. UNIX for Beginners Questions & Answers

Many processes running at the same time

Hello everybody , I launched cron to execute a task every hour but the job takes more than hour that's why I'm getting more than 1000 cron processes running at the same time !!! My question is how to tell cron not to execute unless the job terminated in order to have only one process running .... (14 Replies)
Discussion started by: beautymind
14 Replies

4. UNIX for Dummies Questions & Answers

Confusion over Multi Threading

Hi, I am trying to get my head round Multi Threading and I have a few queries to try and clear up my confusion Q1. Is multi threading a hardware / chip level concept, an OS level or an application level concept ? I am trying to work out where SMT architecture fits in. Q2. What's the multi... (3 Replies)
Discussion started by: jimthompson
3 Replies

5. Programming

Multi-threading

In this piece i implemented the gossip method. The first thread is invoked from inside the (msg is first sent from node -1 to 0 from main()) and the other threads are invoked from inside of the thread function itself. I used two mutexes and a condition variable to control the synchronization. ... (4 Replies)
Discussion started by: saman_glorious
4 Replies

6. Programming

Multi-threading

Hi, If we create 10 threads to invoke runQuery method at same time, Will queryProcessor will be overriden sometime or 10 different copies will be created? We are not using any sunchronzation mechnism in runQuery(). so there is not gurantee on QueryProcessor class variables right OR each 10... (1 Reply)
Discussion started by: jramesh1
1 Replies

7. Programming

Multi threading?

I am not sure if multi threading is the correct term, but here is what I am trying to do. I have a while loop that displays the number 1, pauses, displays the number 2, pauses , displays the number 3 ad infinitum. It just keeps counting. While the screen displays the sequence of numbers counting... (4 Replies)
Discussion started by: enuenu
4 Replies

8. Programming

Regarding Multi-Threading

Hi All, Here's my question I have a 385 MB file containing 5,000,000 records. I need to read from the file and load into a table. Initially i thought of doing it in a single thread (execution of a single program) but when calculated accounted 16 hours of time on a standard benchmark. Hence... (5 Replies)
Discussion started by: matrixmadhan
5 Replies

9. Programming

Multi threading using fork

Hi, I have written a code which will run a set of process using fork. I want to know from You how can i start another job when one of my job in my loop is completed My code is #include<stdio.h> #include<ctype.h> main() { int pid,cid; ChildProcess(); ... (1 Reply)
Discussion started by: sureshraju_ma
1 Replies

10. Programming

Multi-threading questions

I've been doing some reading lately about threading (Posix threads) and I'm really curious about a couple things that I've read. I'm not sure if many people here have threading experience, but I thought it would be nice to be able to discuss some questions about it. (For the record, I did... (1 Reply)
Discussion started by: DreamWarrior
1 Replies
Login or Register to Ask a Question