Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 30 Jul 2014, 07:45
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
run commands in parallel
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [6 Posts]  
Author Message
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Fri 25 Jun 2010, 09:43    Post subject:  run commands in parallel
Subject description: - multi threading in bash, c, xarg
 

In this prog, the music plays then when it stops
the image is displayed
How to run at the same time?

Code:
#! /bin/bash
madplay ivy.mp3 "$@"
xcowsay --cow=Lobster6 --time=1 --dream=puppylogo96.png "$@"


Sure I have asked this before - can not find reply

_________________
Puppy WIKI

Last edited by Lobster on Sun 27 Jun 2010, 00:51; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
ken geometrics

Joined: 23 Jan 2009
Posts: 76
Location: California

PostPosted: Fri 25 Jun 2010, 09:49    Post subject: Re: run commnds in parallel
Subject description: - multi threading in bash
 

Lobster wrote:
In this prog, the music plays then when it stops
the image is displayed
How to run at the same time?

Code:
#! /bin/bash
madplay ivy.mp3 "$@"
xcowsay --cow=Lobster6 --time=1 --dream=puppylogo96.png "$@"


Sure I have asked this before - can not find reply


Many things will run in multiple tasks if you put the "&" on the end of the line.

Try:
Code:

madplay ivy.mp3 "$@" &
xcowsay --cow=Lobster6 --time=1 --dream=puppylogo96.png "$@" &

echo "And this too"
Back to top
View user's profile Send private message 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Fri 25 Jun 2010, 13:08    Post subject:  

Thanks ken geometrics,

I will just put on my dunce hat
and go stand in the corner . . . Embarassed

_________________
Puppy WIKI
Back to top
View user's profile Send private message Visit poster's website 
Pizzasgood


Joined: 04 May 2005
Posts: 6270
Location: Knoxville, TN, USA

PostPosted: Fri 25 Jun 2010, 23:44    Post subject:  

As mentioned, sticking an ampersand on the end of a command will background it, so that it runs in parallel. However, what if you have a group of commands that should execute sequentially, but you want the group to run in parallel to the rest of the script? Well, you could put it into a function and run the function in parallel. Another option is to put it into a subshell by wrapping it in parenthesis, and then stick an ampersand after the closing paren.

The following is an example of that. It has a block of code at the top that will run in parallel to the rest of the code. That first part just opens four terminals.

The second part of the code, which will run at the same time as the first part (indeed, it will probably finish first), draws a picture in your terminal. For best results, use a terminal with a black background. With most terminals, that can be achieved by running them with the the -bg black -fg white options.

I got a bit carried away on that second part Cool

Code:
#!/bin/sh
#This opens several terminals, one per second.  At the same time, a
#Different set of code gradually draws an image on the screen.

#Create the files
(
        #try to find a terminal
        for i in xterm rxvt urxvt sakura; do
                which $i >/dev/null && terminal=$i
        done
        for i in {0..3}; do
                urxvt &
                sleep 1
        done
) &

########################################################################

#This part does the drawing of cool things.
#Note:  If you want to use this independently of this example, just cut
#everything below the following line.  All the above is irrelevant.
###--------- CUT AFTER THIS --------------------------------------------
#!/bin/sh
#Draws a pretty picture

#This looks best in a terminal with a black background.  For example:
#    rxvt -bg black -fg white

h=12
tw=4 #even for alignment
th=3
#increasing this dehances the chrome factor
scarcity=9

###################################################
#define a bunch of colors
#the capitalized versions are "bold" or "bright"

 black='\033[0;30m'
 BLACK='\033[1;30m'
 white='\033[0;37m'
 WHITE='\033[1;37m'

   red='\033[0;31m'
   RED='\033[1;31m'
 green='\033[0;32m'
 GREEN='\033[1;32m'
yellow='\033[0;33m'
YELLOW='\033[1;33m'
  blue='\033[0;34m'
  BLUE='\033[1;34m'
purple='\033[0;35m'
PURPLE='\033[1;35m'
  cyan='\033[0;36m'
  CYAN='\033[1;36m'

    NC='\033[0m'
  grey=$BLACK
###################################################

char1="${green}/${NC}"
char2="${green}^${NC}"
char3="${green}\\\\${NC}"
chart1="${yellow}|${NC}"
chart2="${yellow}=${NC}"
chart3="${yellow}|${NC}"

function draw_bulb(){
        char="*"
        color_pool=($RED $YELLOW $BLUE $CYAN $PURPLE)
        let "index=RANDOM%${#color_pool[@]}"
        echo "${color_pool[$index]}${char}${NC}"
}

function draw_item(){
        boring="$1"
        let 'decision=RANDOM%scarcity'
        if [ $decision -eq 0 ]; then
                draw_bulb
        else
                echo "$boring"
        fi
}

let 'w=h*2'

let 'cx=w/2'
let 'cy=h/2'

for x in $(seq 0 $cx); do
        line_start="${line_start} "
done
echo -e "${line_start}${WHITE} _A_ ${NC}"
echo -e "${line_start}${WHITE} /^\ ${NC}"

for y in $(seq 1 $h); do
        let 'x1=cx-y+1'
        let 'x2=cx+y-1'
        line=''
        for x in $(seq 0 $x1); do
                line="${line} "
        done
        line="${line} $(draw_item "$char1")"
        let 'start=x1+1'
        let 'end=x2-1'
        for x in $(seq $start 2 $end); do
                line="${line} $(draw_item "$char2")"
                #line="${line} ${char2}"
        done
        line="${line} $(draw_item "$char3")"
        echo -e "$line"
        let 'y++'
done
for y in $(seq 1 $th); do
        let 'x1=cx-tw/2'
        line=''
        for x in $(seq 0 $x1); do
                line="${line} "
        done
        line="${line} ${chart1}"
        let 'start=x1+1'
        let 'end=x1+tw'
        for x in $(seq $start 2 $end); do
                line="${line} ${chart2}"
        done
        line="${line} ${chart3}"
        echo -e "$line"
done

_________________
Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib

Back to top
View user's profile Send private message Visit poster's website 
Iguleder


Joined: 11 Aug 2009
Posts: 1872
Location: Israel, somewhere in the beautiful desert

PostPosted: Sat 26 Jun 2010, 10:16    Post subject:  

There's another method, in plain C. It was originally written for my own distro to speed up init so it starts things in parallel ... works great Smile

Code:

//dr 0.1 by Iguleder

// To compile:
// gcc -lpthread -o dr dr.c

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *runThread(void *command)
{

  system(command);

}

int main(int argc, char** argv) {
   
  pthread_t commandThreads[argc-1];
  long i;

  for(i=1; i<=argc-1; i++)
  {

    pthread_create(&commandThreads[i-1], NULL, runThread, (void *)argv[i]);
     
  }

  pthread_exit(NULL);

}


To use it: compile, put in /usr/bin, then do "dr <command 1> <command 2> <command 3> [ ... ]".

In your case:
dr "madplay ivy.mp3 $@" "xcowsay --cow=Lobster6 --time=1 --dream=puppylogo96.png $@"

_________________
My homepage
Back to top
View user's profile Send private message Visit poster's website MSN Messenger 
ICQ Number 
potong

Joined: 06 Mar 2009
Posts: 88

PostPosted: Sat 26 Jun 2010, 22:06    Post subject:  

Xargs has a -P n option which will run maximum number of n jobs at a time or use -P 0 to run all ( I don't think this option is available on BusyBox).

GNU parallel is more fancy and can do all manner of clever things on single/multiple processor machine(s) see here for a screen cast
and here for the home page

HTH

Potong
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [6 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0729s ][ Queries: 11 (0.0048s) ][ GZIP on ]