예제 #1
0
파일: pipe.c 프로젝트: mcavo/so-2015-1c-tp2
unsigned
PutPipeTimed(Pipe_t *p, void *data, unsigned size, unsigned msecs)
{
	unsigned nbytes;
	char *d;

	if ( !size || !EnterMonitor(p->monitor) )
		return 0;

	// No se pueden escribir más bytes que el tamaño del pipe
	if ( size > p->size )
		size = p->size;

	// Si hay un timeout finito, calcular deadline.
	Time_t deadline = (msecs && msecs != FOREVER) ? Time() + msecs : 0;

	// Para que la escritura sea atómica, esperar a que haya lugar para escribir todos los bytes
	while ( (p->size - p->avail) < size ) 
	{
		// Desistir si es condicional, si no esperar
		if ( !msecs || !WaitConditionTimed(p->cond_put, msecs) )
		{
			LeaveMonitor(p->monitor);
			return 0;
		}
		// Si hay que seguir esperando con deadline, recalcular timeout
		if ( deadline && (p->size - p->avail) < size )
		{
			Time_t now = Time();
			msecs = now < deadline ? deadline - now : 0;
		}
	}

	// Hay por lo menos size bytes libres, escribir
	nbytes = size;
	d = data;
	while ( size-- )
	{
		*p->tail++ = *d++;
		if ( p->tail == p->end )
			p->tail = p->buf;
	}

	// Despertar eventuales lectores bloqueados
	if ( !p->avail )
		BroadcastCondition(p->cond_get);
	p->avail += nbytes;

	// Retornar cantidad de bytes escritos
	LeaveMonitor(p->monitor);
	return nbytes;
}
예제 #2
0
int main()
{
    int cv1=0;
    int l1=CreateLock();
    Write("\n Running cvtest5.c\n",22,1);
    Write("\nTESTING: CVTEST5.C TRIES TO ACCESS THE CV NOT CREATED BY IT",60,1);
    Exec("../test/cvtest1",15);
    Yield();
    Yield();
    Yield();
    WaitCondition(cv1,l1);
    SignalCondition(cv1,l1);
    BroadcastCondition(cv1,l1);
    Exit(0);
}
예제 #3
0
파일: pipe.c 프로젝트: mcavo/so-2015-1c-tp2
unsigned
GetPipeTimed(Pipe_t *p, void *data, unsigned size, unsigned msecs)
{
	unsigned i, nbytes;
	char *d;

	if ( !size || !EnterMonitor(p->monitor) )
		return 0;

	// Si hay un timeout finito, calcular deadline.
	Time_t deadline = (msecs && msecs != FOREVER) ? Time() + msecs : 0;

	// Bloquearse si el pipe está vacío
	while ( !p->avail ) 
	{
		// Desistir si es condicional, si no esperar
		if ( !msecs || !WaitConditionTimed(p->cond_get, msecs) )
		{
			LeaveMonitor(p->monitor);
			return 0;
		}
		// Si hay que seguir esperando con deadline, recalcular timeout
		if ( deadline && !p->avail )
		{
			Time_t now = Time();
			msecs = now < deadline ? deadline - now : 0;
		}
	}

	// Leer lo que se pueda
	for ( nbytes = min(size, p->avail), d = data, i = 0 ; i < nbytes ; i++ )
	{
		*d++ = *p->head++;
		if ( p->head == p->end )
			p->head = p->buf;
	}

	// Despertar eventuales escritores bloqueados
	BroadcastCondition(p->cond_put);
	p->avail -= nbytes;

	// Retornar cantidad de bytes leídos
	LeaveMonitor(p->monitor);
	return nbytes;
}
예제 #4
0
int main()
{
	unsigned int i=0;
	Write("\nTESTING WAIT AND BROADCAST",27,1);
	l1=CreateLock();
	c1=CreateCondition();
	c2=CreateCondition();
	Fork(clt1);
	Fork(clt2);
	Yield();
	Yield();
	Yield();
	Yield();
	Yield();
	Yield();
	/*for(i=0;i<100000;i++){}*/
	AcquireLock(l1);
	counter=counter+3;
	BroadcastCondition(c1,l1);
	ReleaseLock(l1);
	Exit(0);
}