zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: Szabolcs Szakacsits <szaka@sienet.hu>, zsh-users@sunsite.auc.dk
Subject: Re: Running N jobs from M all the time
Date: Tue, 12 Oct 1999 15:59:01 +0000	[thread overview]
Message-ID: <991012155901.ZM9091@candle.brasslantern.com> (raw)
In-Reply-To: <Pine.LNX.4.10.9910120601180.1839-100000@divine.city.tvnet.hu>

On Oct 12,  6:38am, Szabolcs Szakacsits wrote:
} Subject: Running N jobs from M all the time
}
} My question would be there is an easy way for $SUBJECT?

If I'm understanding you correctly, you want to do something like

repeat 1000 do
    if (( number_of_jobs >= number_of_processors )); then
	wait $any_job
    fi
    command &
done

And the complaint is that "wait" can only wait for one specific job or
for all of them, not for "the next one that exits."

} I know this can be done with ps, grep, wait, etc but a bit painful and
} not elegant/modern I'd like to use a cleaner way.

You've just discovered another use for the coprocess.  The rest of this
is just standard parallel programming stuff.

function run_parallel {
    # Runs N copies of command at once until M copies have run.

    emulate -L zsh
    if (( $# < 3 )); then
	print "usage: run_parallel N M command [args ...]" >&2
	return 1
    fi

    integer N=$1 M=$2
    shift 2

    if ((M <= N)); then
	repeat $M do $==* & done
	wait
    else
	# Set up a loop to read and write one byte at a time.  This
	# semaphores between the parallel children and the parent zsh.  
	# It's important to pass exactly one byte here and not rely on
	# reading complete lines, lest a race condition develop.

	# Unfortunately, a race still can occur, it's just less likely.
	# Maybe SMP systems are better about multiple writers on one FD?

	coproc while ((--M > 0)) && read -u0k; do print -n .; done

	# Start the first N children

	repeat $N do
	    ((--M))
	    { $==* ; print -np . } &
	done

	# Wait for the semaphore from each child and start another as
	# soon as we get it, up to M children.

	while ((--M > 0)) && read -pk; do
	    { $==* ; print -np . } &
	done

	# Wait for the last N semaphores.

	repeat $N do read -pk; done
    fi

    return $?
}

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


  reply	other threads:[~1999-10-12 16:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-10-12  4:38 Szabolcs Szakacsits
1999-10-12 15:59 ` Bart Schaefer [this message]
1999-10-13 15:32   ` Szabolcs Szakacsits
1999-10-12 19:52 ` Adam Spiers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=991012155901.ZM9091@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=szaka@sienet.hu \
    --cc=zsh-users@sunsite.auc.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).