Q:

Creating and killing processes and child processes completely

I have a program which is in Windows from the command prompt using a command like this
C:\Users\AndyI> ./myprogram.exe datafile.txt

I want to use a Perl program to create many instances of datafile.txt (say, datafile.001.txt, datafile.002.txt etc.) and call myprogram.exe to process them in parallel.
I've had problems illustrated by this generic program
$exit  = '';
$count = 0;

while ( $exit eq '' ) {

    $pid = fork();

    if ( $pid == 0 ) {          # if this is a child process
        $exit = 'yes';
        system 'notepad.exe';   # open an instance of Notepad
    }
    else {
        system("taskkill /im notepad.exe /f");  # Kill all instances
        $count++;
        print "$count instances\n";
    }

    if ( $count > 500 ) {       # In case of infinite loop
        $exit = 'yes';
    }
}

sleep 100;      # So I have time to read the output

I am using fork() to create child processes, and each child uses system to start up an instance of Notepad. The parent process attempts to kill the Notepad process with taskkill, and this repeats in a loop.
The program generates (and kills) only 64 instances of Notepad before halting. It's clear that I'm not handling these processes properly and there are bits hanging around which fill up some kind of table.
If I don't kill Notepad there is still a limit of 64. This perhaps indicates that my cleanup isn't working and I've killed the Notepad process but let the child live.
Oddly the system call yields a negative $pid but those PIDs are not recognised by taskkill or Task Manager.
How do I generate and kill many instances of Notepad without hitting the limit?
EDIT:  Following mob's thoughts, here's another script, but it still won't let me spawn and kill more than 64 Notepads:
    $exit='';
    $count=0;
while($exit eq ''){

    $pid=system 1,'notepad.exe';       # open an instance of Notepad
    print "$pid\n";
    sleep 1;        # Slows it down for easy viewing - not necessary
    system("taskkill /pid $pid /f");      # Kill by PID
    $count++;
    print"$count instances\n";

    if($count>100){     # in case of infinite loop!
        $exit='yes';
    }

}

sleep 100;      # so I have time to read the output!

Any thoughts?  I haven't found a use for waitpid or wait - can you clarify what you mean?  I need to kill, not wait.  (again, not a phrase for search engines!).
EDIT2:  Parallel::ForkManager (thanks Borodin!)
use Parallel::ForkManager;
my $pm=Parallel::ForkManager->new(20);    # maximum number of child processes at any one time

PROGS:
for(my $i=1;$i<=100;$i++){
    print"$i\n";
    sleep 1;
    $pm->start and next PROGS;
    my $pid=system 1,'notepad.exe';
    sleep 10;
    system("taskkill /pid $pid /f");
    $pm->finish;
}

This essentially does what the first edit does, but somehow does it more cleanly - perhaps scrubbing away the remnants of the child that the earlier effort did.  I would still love to know why!
Every second, it kicks off a child process which runs the code between $pm->start and $pm->finish.  That child lives for 10s before killing Notepad and dying, which allows about 10 Notepads to live at any one time.  And they vital thing is that more than 64 Notepads can live and die before the whole program exits.
I had wanted to kill each program from the main process, but as I will know when I want it do die before I start it, this ought to work.
Lots more to try - thanks for the help so far, and you can call this solved (ish).

A:

For something like this, and on Windows, you might want to try the underdocumented system 1, LIST syntax. 

system(1, @args) spawns an external process and immediately returns its process designator, without waiting for it to terminate.  Return value
  may be used subsequently in "wait" or "waitpid".

Importantly, the return value is a proper process id that can be passed to taskkill (as well as to Perl's waitpid), not a pseudo-process id (a negative number) that you get from a fork call on Windows. There is no need to use a fork call with this syntax.
$n = "001";
while (-f "datafile$n.txt") {
    $pid = system 1, "fancyprogram.exe", "datafile$n.txt";
    $n++;
    ...
}

A module like Forks::Super could also help a lot in a script with a lot of background processes to manage. Some of the ways it could help are in throttling the number of background jobs (running only as many jobs at a time as your system can reasonably handle), settings timeouts on background jobs, or setting priorities or CPU affinity on jobs.

