
/* cift12-c.c -- CIF test 12, process forking, C part */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "cifccsp.h"

void out_string (char *str, Channel *out)
{
	unsigned char *ch = (unsigned char *)str;

	while (*ch != '\0') {
		/* ChanOut (out, ch, 1); */
		ChanOutChar (out, *ch);
		ch++;
	}
	return;
}

void proc_test12 (Process *me, Channel *out)
{
	out_string ("Hello from proc_test12! (forked)\n", out);

	return;
}

void my_process (Process *me, Channel *kroc_err)
{
	Process *p;
	Time t;

	out_string ("Hello, world! (from C).  Allocating some other processes and going parallel..\n", kroc_err);

	p = ProcAlloc (proc_test12, 4096, 1, kroc_err);
	ProcFork (p);

	ProcTime (&t);
	ProcTimeAfter (t + 1000000);

	return;
}


/* allocates process and returns address to occam world */
void real_cift12_init (int *raddr, Channel *c)
{
	Process *p = ProcAlloc (my_process, 4096, 1, c);

	*raddr = (int)p;
	return;
}

/* frees memory used by a process -- lets hope it's finished :) */
void real_cift12_free (int addr)
{
	Process *p = (Process *)addr;

	ProcAllocClean (p);
	return;
}

void _cift12_init (int *ws) { real_cift12_init ((int *)(ws[0]), (Channel *)(ws[1])); }
void _cift12_free (int *ws) { real_cift12_free ((int)(ws[0])); }

