void *dining(void *args) {
	int num = (int)args;
	int sleep_time, eat_time;

	while (TRUE) {
		sleep_time = rand() % TRYING_SEGMENTS + 1;
		sleep(sleep_time);
		eat_time = rand() % MAX_EAT_TIME + 1;
		if (getting(num) == 0) {
			sleep(eat_time);
			releasing(num);
		}
		else 
			printf("Someone is using the fork of %dth philosopher!\n", num);
	}
}
void print_info(const exlcm_sync_t * msg)
/*****************************************************************************
*   Input    :
*   Output   :
*   Function :
******************************************************************************/ 	
{
	printf("----------------- PRINT INFO ----------------\n");
	printf("  Sender   		= %u\n", msg->sender);
	printf("  Reciver   		= %u\n", msg->receiver);
	printf("  Operation   		= %u\n", msg->operation);
	printf("  Number_of_packets	= %lld\n", msg->number_of_packets);
	printf("  timestamp   	= %f\n", msg->timestamp);
	
	printf("------------------------------------------------\n");
	printf("Remote realtime  =  %f\n" , getting(msg, REMOTETIME, getting(msg, DATASIZE, ZERO)));
	printf("Local realtime  =  %f\n" , getting(msg, LOCALTIME, getting(msg, DATASIZE, ZERO)));
	printf("OFFSET  =  %f\n" ,getting(msg, OFFSET, getting(msg, DATASIZE, ZERO)));
	printf("------------------------------------------------\n");
	printf("SCALETIME		=	%f\n", getting(msg, SCALETIME, getting(msg, DATASIZE, ZERO)));
	printf("MEAN OFFSET		=	%f\n", getting(msg, OFFSETMEAN, getting(msg, DATASIZE, ZERO)));
	printf("LSLR OFFSET		=	%f\n", getting(msg, LSLR, getting(msg, DATASIZE, ZERO)));
	printf("NEW TIME MEANOFFSET	=	%f\n", getting(msg, NEWTIMEMEAN, getting(msg, DATASIZE, ZERO)));
	printf("NEW TIME LSLR		=	%f\n", getting(msg, NEWTIMELSLR, getting(msg, DATASIZE, ZERO)));
	printf("----------------- END PRINT INFO ----------------\n");
	
		struct NODE_INFO * i = &rbs.nodes[msg->sender].end;
	printf("DATASIZE = %f\n", getting(msg, DATASIZE,ZERO));
   int dataSIZE = 0;
   while(i->next != NULL)
   {
	   printf("LSLR = %f\n", i->lslr);
	   	i = i->next;
		dataSIZE++;
	}
	printf("LSLR = %f\n", i->lslr);
}	
int RBS(const exlcm_sync_t * msg)
/*****************************************************************************
*   Input    :
*   Output   :
*   Function :
******************************************************************************/ 	
{
	struct timeval tv;			// create timestruct
	exlcm_sync_t my_data;		// create a struct to LCM
	
	switch(msg->operation)		// statemachine of handling LCM packets
		{
			case BEACONSENDER:	// case when beacon message is recieved	
				tv = get_localrealtime();	// get timestamp for received packets
				rbs.realtime = (double)tv.tv_sec + (double)tv.tv_usec / (double)1000000;	// calculate time in sec
				my_data = construct_msg(NODE,ALL_NODES,RECIEVER_TO_RECIEVER,rbs.realtime); // define send struct
				exlcm_sync_t_publish(lcm, SYNC_CHANNEL, &my_data);		// publish the struct
				print_info(msg);				// print all approprivately informations
				break;	
			case RECIEVER_TO_RECIEVER:	// case when nodes inform each other
				if(msg->sender != NODE)   		// ONly run RBS for others nodes  
				{
					//printf("---------------- RECEIVER TO REVEVER SENDER ------------------------\n");
					handle_linkedlist(msg);		// handle list with packets
					
					if(inserting(msg, REMOTETIME, sample_remoterealtime(msg))) // inserting time for remote nodes
						printf("FAILURE IN INSERTING REMOTETIME \n");
					
					if(getting(msg, DATASIZE, ZERO) == ZERO & rbs.nodes[msg->sender].reach_max == ZERO)	// this is also run the first time
					{
						rbs.realtime_0 = rbs.realtime;		// first time for current stored
						if(inserting(msg, LOCALTIME, 0))	// store realtime in nodes
							printf("FAILURE IN INSERTING REMOTETIME first run \n");	
					}
					else
					{
						if(inserting(msg, SCALETIME, rbs.realtime - rbs.realtime_0))	// store scalerealtime in nodes
							printf("FAILURE IN INSERTING SCALETIME \n");		
					}
					
					if(inserting(msg, LOCALTIME, rbs.realtime))	// store realtime in nodes
							printf("FAILURE IN INSERTING REALTIME \n");	
					
					if(inserting(msg, OFFSET, sample_remoterealtime(msg) - rbs.realtime))		// store offset (remote - current)
						printf("FAILURE IN INSERTING OFFSET \n");
					
					
					
					mean_offset(rbs.nodes[msg->sender].end);		// calculate meanoffset
					if(rbs.nodes[msg->sender].end->next != NULL)	// skip first sample
						leastsquarelinarregression(rbs.nodes[msg->sender].end);	// calculate leastsquareregression
					
					//printf("----------------END RECEIVER TO REVEVER SENDER ------------------------\n");
					
					print_info(msg);				// print all approprivately informations
					rbs.nodes[msg->sender].n++;		// count x increment
					
				}
				break;
			default:
				break;
			}
	return 0;
}