Q:

shell script to control the number of processes of an user

I have to launch a total number,  NUMTOT of a program (could be any, C, Mathematica, ns-2... mathematical environments).
The problem is that the computer where these processes run only admits a maximum number of executions, e.g. MAX, (MAX < NUMTOT). 
I run these programs with the following line in a terminal
./run.sh -d directory num

repeatedly, where run.sh is a script from which I invoke the math program; directory is the directory where my program is, and num indicates the number of new executions ( integer*num = NUM).
The script 'run.sh' makes 'num' directories with a copy of 'directory' (the program) and launch 'num' number of instances of the program. In my case are Matlab instances.
I would like an script that automatically keeps track of the number of processes running and when the maximum number is reduced to  (MAX-num), because 'num' has already finished, then launch the next 'num' simulations of Matlab until the total NUM of simulations is completed.
It would even better if the script could be able to maintain the MAX possible processes just invoking the next program when only one has just finished.
Actually, to know the number of running processes I run the following script
ps axu |grep plopez|grep simulacion|grep MATLAB|awk '{ $2} END{print NR}'

which gives the total Matlabs running in this moment.
How could I make the whole set of simulations just from a single script?

A:

The usual way to enforce a maximum number of processes by user is via pam_limits(8). Check the limits.conf(5) manpage for the details, and examples to change your /etc/security/limits.conf accordingly.
Specifically you want to configure the nproc parameter:
 nproc
    maximum number of processes

An example from the manpage with this parameter in use:
       @faculty        soft    nproc           20
       @faculty        hard    nproc           50

