#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/time.h>
#include <limits.h>
#include "common.h"
#include "mba/cfg.h"
#include "mba/time.h"
#include "mba/svsem.h"
#include "mba/misc.h"

static void
run(svsem_t *sem, int n, int verbose)
{
	int i, rst;
	char buf[PATH_MAX];

	for (i = 0; i < n; i++) {
		sprintf(buf, "/tmp/file.%d", i);
		svsem_wait(sem);

		if (copen(buf, O_CREAT, 0600, &rst) == -1) {
			MSG("");
		} else if (verbose) {
printf("%d %d: %d %s\n", getpid(), i, rst, buf);
		}

		sleep(1);
	}
	
}

int
OpenCre(int verbose, struct cfg *cfg, char *args[])
{
	int nprocs = atoi(args[0]), n = atoi(args[1]), i, j, status;
	pid_t *pids;
	svsem_t sem;

	if (svsem_create(&sem, 0, 1) == -1) {
		AMSG("");
		return -1;
	}

	if ((pids = malloc(nprocs * sizeof *pids)) == NULL) {
		PMNO(errno);
		return -1;
	}

	for (i = 0; i < nprocs; i++) {
		if ((pids[i] = fork()) == 0) {
			run(&sem, n, verbose);
			exit(0);
		}
	}
	for (j = 0; j < n; j++) {
		sleep(2);
		if (verbose) {
			printf("%d\n", j);
		}
		svsem_post_multiple(&sem, nprocs);
	}
	do {
		waitpid(pids[--i], &status, 0);
		if ((errno = WEXITSTATUS(status))) {
			perror("");
		}
		tcase_printf(verbose, "process complete\n");
	} while(i);

	free(pids);
	svsem_destroy(&sem);

	tcase_printf(verbose, "done");
	cfg = NULL;
    return 0;
}
