All pastes #488930 Raw Edit

Something

public c v1 · immutable
·published 2007-05-15 07:49 UTC
rendered paste body
/* * Copyright (C) 2007 pluknet. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/time.h>enum {	idle = 0,	begin,	work,	end} state_hdr;#define NCPU 8struct cpu_hdr {	int state;	struct timeval time;	int said[4];} cpu[NCPU];static intdo_work(int remain) {	int howlong;	int i;	struct timeval tp;	for (i = 0; i < NCPU; ++i) {		switch (cpu[i].state) {		case idle:			break;		case begin:			if (cpu[i].said[begin]++ == 0) {				cpu[i].said[end] = 0;				printf("CPU#%d: state=work ", i);			}			gettimeofday(&(cpu[i].time), NULL);			howlong = random() & 7;			printf("howlong=%d\n", howlong);			cpu[i].time.tv_sec += howlong;			cpu[i].state = work;			break;		case work://			if (cpu[i].said[work]++ == 0)//				printf("i=%d state=work\n", i);			gettimeofday(&tp, NULL);			if (tp.tv_sec >= cpu[i].time.tv_sec)				cpu[i].state = end;			break;		case end:			if (cpu[i].said[end]++ == 0) {				cpu[i].said[begin] = 0;				printf("CPU#%d: state=free\n", i);			}			cpu[i].state = idle;			remain--;			break;		default:			printf("\n\n\t\tNOT REACHABLE!\n\n\n");			/* ENOTREACH */			return (-1);		}	}	return remain;}intmain(void) {	const int G = 40;	/* total request amount */	int s = 0;		/* current request */	int i, j;	int request = 0;	int remain = 0;	srandomdev();	for (i = 0; i < NCPU; ++i) {		cpu[i].state = idle;		for (j = 0; j < 4; ++j)			cpu[i].said[j] = 0;	}	while (s++ < G) {		if (request == 0)			request = random() & 1;		if (request)			printf("s=%d presents\n", s);		else			printf("s=%d absents\n", s);		if (request == 1)			for (i = 0; i < NCPU; ++i)				if (cpu[i].state == idle) {					cpu[i].state = begin;					request = 0;					remain++;					break;				}		if (request == 1) {			s--;			sleep(1);		}		if ((remain = do_work(remain)) == -1)			return(-1);	}//	printf("%d\n", remain);	while((remain = do_work(remain)))//	printf("%d\n", remain);		if (remain == -1)			return(-1);	return(0);}