Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Last active December 30, 2015 10:09
Show Gist options
  • Select an option

  • Save EdgeCaseBerg/7814268 to your computer and use it in GitHub Desktop.

Select an option

Save EdgeCaseBerg/7814268 to your computer and use it in GitHub Desktop.
Forking the process for each object in a population for multiple generations. Simple concept code.
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define GENERATIONS 4
#define POPULATION_SIZE 80
int main(){
int pipefd[2];
pid_t pid;
int p,g;
for(g=0; g < GENERATIONS; ++g){
for(p=0; p < POPULATION_SIZE; ++p){
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
close(pipefd[1]);
close(pipefd[0]);
/* This is your child process space, call your ODE simulation start code here
*/
_exit(1);
}
close(pipefd[0]);
close(pipefd[1]);
/* The parent process continues executing here and you can have it do whatever.
* Some things that you might do, would be to review the fitness of each child
* (assuming you'd write it out to a file specific to the child running it) and
* then do things like ALPS or what have you.
*/
}
}
wait(NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment