clear all;

function y = f(x)
  y = sin(x);
endfunction

x = linspace(0, 2 * pi, 10);
y = nan(1, length(x));
for i=1:length(x)
  filename = sprintf("test%d.mat", i);
  pid = fork();
  if (pid == 0)
    fprintf(stderr, "process %d started\n", getpid());
    y = f(x(i));
    save("-binary", filename, "y");
    exit(0);
  elseif (pid > 0)
    [pidr, status, msg] = waitpid(pid);
    if (WIFEXITED(status))
      fprintf(stderr, "process %d returned with code %d\n", pid, WEXITSTATUS(status));
    elseif (WIFSIGNALED(status))
      fprintf(stderr, "process %d killed by signal %d\n", pid, WTERMSIG(status));
    endif
    y(i) = load(filename, "y").y;
    unlink(filename);
  endif
endfor

plot(x, y);
