/****************************************************************
 *                                                              *
 *  LIBDIST V1.0						*
 *                                                              *
 *  sem-test.c - test semaphores                                *
 *                                                              *
 *  Last changed: 19.02.96                                      *
 *  Author: Frank Kargl (frank.kargl@informatik.uni-ulm.de)     *
 *                                                              *
 *  Restrictions: works only for Solaris 2.6 or above           *
 *                                                              *
 ****************************************************************/

#include "libdist.h"
#include "config.h"

#include <synch.h>	/* for sema_t */

/* name of semaphore */
#define NAME "name"

/* function to run concurrent as thread */
/* sequentialisation is reached via semaphore */

void *function(void *arg) {
	sema_t *semaphore;
	char c;

	c=*((char *)arg);

	/* create semaphore or get handle if allready existant */
	semaphore=dl_sem_create(NAME,1);
	if (semaphore==NULL) {
		printf("FEHLER!!!\n");
		return NULL;
	}

	/* lock everything */
	dl_sem_p(semaphore);
	printf("A%c\n",c);
	sleep(5);
	printf("B%c\n",c);
	sleep(5);
	printf("C%c\n",c);
	sleep(5);
	/* and free it again */
	dl_sem_v(semaphore);
	return NULL;
}

int main() {
	
	/* start function a number of times */
	dl_thr_create(function,"1");
	dl_thr_create(function,"2");
	dl_thr_create(function,"3");
	sleep(120);
	return 0;

}
