home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
GianuzziV
/
SO1
/
scambio.c
< prev
next >
Wrap
C/C++ Source or Header
|
2005-04-02
|
5KB
|
153 lines
/*****************************************************
* scambio.c *
* *
* Uso di condition variable wait. *
* Scrive quanto tempo ci vuole per scambiare dati *
* fra 2 thread *
* *
* gcc -D_REENTRANT -o scambio scambio.c -lpthread *
* *
*****************************************************/
#define COUNT 1000
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
long int count=0;
long int totale=0;
void PrintAverage() {
printf("\nRound trip time medio = %ld\n\n", totale / count );
}
/***************************************
* out = out - in. *
* Out e' supposto essere >= in. *
***************************************/
tvsub( out, in )
register struct timeval *out, *in;
{
if( (out->tv_usec -= in->tv_usec) < 0 ) {
out->tv_sec--;
out->tv_usec += 1000000;
}
out->tv_sec -= in->tv_sec;
}
/********************************************
* Global value and condition to wait on *
********************************************/
typedef struct {
pthread_mutex_t mutex; /* Protects access to value */
pthread_cond_t cond; /* Signals change to value */
int value; /* Access protected by mutex */
} cond_struct_t;
cond_struct_t data = {
PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};
/**********************************************************
* Thread start routine. It will set the main thread's predicate
* and signal the condition variable.
**********************************************************/
void * wait_thread (int *hibernation)
{
int status;
sleep (*hibernation);
while(1) { /* the while loop ends when the main thread terminates */
/**** Wait for me to get the data ****/
status = pthread_mutex_lock (&data.mutex);
if (status = 0) {fprintf(stderr,"Lock error \n"); exit(-1);}
while (data.value == 1) {
status = pthread_cond_wait(&data.cond, &data.mutex);
if (status = 0) {fprintf(stderr,"Wait error \n"); exit(-1);}
}
status = pthread_mutex_unlock (&data.mutex);
if (status = 0) {fprintf(stderr,"Unlock error \n"); exit(-1);}
/**** OK, now I have the data. Send it back to the other thread */
status = pthread_mutex_lock (&data.mutex);
if (status = 0) {fprintf(stderr,"Lock error \n"); exit(-1);}
data.value = 1; /* Set predicate */
status = pthread_cond_signal (&data.cond);
if (status = 0) {fprintf(stderr,"Signal error \n"); exit(-1);}
status = pthread_mutex_unlock (&data.mutex);
if (status = 0) {fprintf(stderr,"Unlock error \n"); exit(-1);}
}
pthread_exit(0);
}
int main (int argc, char *argv[])
{
int status;
pthread_t wait_thread_id;
struct timespec timeout;
int hibernation = 1;
struct timezone tz; /* for timing */
register struct timeval start; /* for timing */
register struct timeval stop; /* for timing */
int i;
/* Create wait_thread. */
status =
pthread_create (&wait_thread_id, NULL, (void *) wait_thread,
&hibernation);
if (status = 0) {fprintf(stderr,"Create error \n"); exit(-1);}
/****** Insert comments to not do one RT outside the timing loop */
status = pthread_mutex_lock (&data.mutex);
if (status = 0) {fprintf(stderr,"Lock error \n"); exit(-1);}
while (data.value == 0) {
status = pthread_cond_wait(&data.cond, &data.mutex);
if (status = 0) {fprintf(stderr,"Wait error \n"); exit(-1);}
}
data.value = 0;
status = pthread_cond_signal (&data.cond);
status = pthread_mutex_unlock (&data.mutex);
if (status = 0) {fprintf(stderr,"Unlock error \n"); exit(-1);}
/****** End of insert comments to not do one RT outside the timing loop */
for(i=0;i<COUNT;i++) {
gettimeofday( &start, &tz );
/**** Wait for me to get the data ****/
status = pthread_mutex_lock (&data.mutex);
if (status = 0) {fprintf(stderr,"Lock error \n"); exit(-1);}
while (data.value == 0) {
status = pthread_cond_wait(&data.cond, &data.mutex);
if (status = 0) {fprintf(stderr,"Wait error \n"); exit(-1);}
}
status = pthread_mutex_unlock (&data.mutex);
if (status = 0) {fprintf(stderr,"Unlock error \n"); exit(-1);}
/**** OK, now I have the data. Send it back to the other thread */
status = pthread_mutex_lock (&data.mutex);
if (status = 0) {fprintf(stderr,"Lock error \n"); exit(-1);}
data.value = 0;
status = pthread_cond_signal (&data.cond);
status = pthread_mutex_unlock (&data.mutex);
if (status = 0) {fprintf(stderr,"Unlock error \n"); exit(-1);}
gettimeofday( &stop, &tz );
tvsub( &stop, &start );
printf("Round trip time = %d usecs\n",
stop.tv_sec*1000000+stop.tv_usec);
count++;
totale += stop.tv_sec*1000000+stop.tv_usec;
}
PrintAverage();
return 0;
}