Thread: How to parallel several jobs in bash

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    How to parallel several jobs in bash

    Hi,

    I wonder how to in a bash script(or other languages if more convenient and/or fast) manage independent jobs(executables with command line arguments) and make them run in parallel? Is there a way to create several sub-process without waiting them to finish?

    Which one is faster: multi-threading or multi-process?

    Thanks and regards!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can put a command into the background:

    ./myscript.sh &

    This forks() and returns the command prompt right away *before* the script finishes, so you can start another one right away. You can put a string of these in a file:
    Code:
    #!/bin/bash
    
    /home/user/bin/myscript1.sh &
    /home/user/bin/myscript2.sh &
    /home/user/bin/myscript3.sh &
    and run that and all three will start almost simultaneously, ie, run in parallel.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks!
    is running background slower than foreground?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lehe View Post
    Thanks!
    is running background slower than foreground?
    AFAIK there is no reason for it to be, depending on the console interaction and how the script is written it could be faster, but it should be the same generally.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    The only issue would be a race condition for resources. If you have many processes fighting for the same disc, for example, then your overall performance will decline. Otherwise, each process will get a time slice for each task.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parallel Port to USB controller and outb()
    By coletek in forum Linux Programming
    Replies: 1
    Last Post: 06-05-2009, 06:57 AM
  2. Replies: 3
    Last Post: 04-10-2009, 12:57 AM
  3. Open C/C++ Jobs for Top Developers
    By Susan N in forum Projects and Job Recruitment
    Replies: 7
    Last Post: 06-04-2008, 08:56 PM
  4. Serial to Parallel
    By ssharish2005 in forum Tech Board
    Replies: 11
    Last Post: 09-10-2007, 01:11 PM
  5. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 03:28 AM