Exemplo n.º 1
0
int main(int argc, char * argv[]){
	pthread_t tid[2];
	stock_market market_madrid;
	int exit = 0;
	pthread_mutex_t exit_mutex;

	// Init market and concurrency mechanisms
	init_market(&market_madrid, "stocks.txt");
	init_concurrency_mechanisms();
	pthread_mutex_init(&exit_mutex,NULL);
	
	// Init broker_info structure for the broker thread
	broker_info info_b1;
	strcpy(info_b1.batch_file, "batch_operations.txt");
	info_b1.market = &market_madrid;
	
	// Init exec_info structure for the operation_executer thread
	exec_info info_ex1;
	info_ex1.market = &market_madrid;
	info_ex1.exit = &exit;
	info_ex1.exit_mutex = &exit_mutex;
	
	// Init reader_info for the stats_reader thread
	//reader_info info_re1;
	//info_re1.market = &market_madrid;
	//info_re1.exit = &exit;
	//info_re1.exit_mutex = &exit_mutex;
	//info_re1.frequency = 100000;
	
	// Create broker and exec threads
	pthread_create(&(tid[0]), NULL, &broker, (void*) &info_b1);
	pthread_create(&(tid[1]), NULL, &operation_executer, (void*) &info_ex1);
	
	// Join broker threads
	void * res;
	pthread_join(tid[0],&res);
	
	// Put exit flag = 1 after brokers completion
	pthread_mutex_lock(&exit_mutex);
	exit = 1;
	pthread_mutex_unlock(&exit_mutex);
	
	// Join the rest of the threads
	pthread_join(tid[1],&res);
	
	// Print final statistics of the market
	print_market_status(&market_madrid);
	
	// Destroy market and concurrency mechanisms
	delete_market(&market_madrid);
	destroy_concurrency_mechanisms();
	pthread_mutex_destroy(&exit_mutex);
  
	return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[]){
    signal(SIGINT,leave);
    signal(SIGSEGV,leave2);
    FILE *input=stdin;
//     FILE *output=stdout;
//     FILE *output2=stdout;
    
//     output2=fopen("iq.txt","w");
//     output=fopen("ob.txt","w");
	  
    int ifinput=0;
    int ifoutput=0;
    int ifhash=0;
    int ifbinary=1;
    int ifselected=0;

    int messagecount=1000;
    int count=0;
    int ifsymbol=0;
    double du=0;
    
    char in[]="-i";
    char symbolname[]="-s";
    char hash[]="-h";
    char binary[]="-b";
    char dasht[]="-t";
    char dashm[]="-m";
    char dashn[]="-n";
    
    int dm=0;

    
   struct lnode *hashmaphead[SIZE];
   struct lnode *hashmaptail[SIZE];

   
    char command;
    unsigned int id;
    char side;

    char symbol[5];
    unsigned int quantity;
    double price;
    

    
    sellhead=NULL;
    selltail=NULL;
    buyhead=NULL;
    buytail=NULL;
    
    for(int i=0;i<SIZE;i++){
	hashmaphead[i]=NULL;
	hashmaptail[i]=NULL;
    }
    
    
    
    
    for (int i=0;i<argc;i++){
      
      if (strcmp(argv[i],in)==0){
	input=fopen(argv[i+1],"r");
	
	
	
      }
      
      if (strcmp(argv[i],symbolname)==0){
	ifsymbol=1;
	strcpy (symbol2,argv[i+1]);
	
      }
      
      if (strcmp(argv[i],dashm)==0){
	M= atoi( (argv[i+1]) );
	
      }
      
      if (strcmp(argv[i],dashn)==0){
	N= atoi( (argv[i+1]) );
	
      }      
      
      if (strcmp(argv[i],dasht)==0){
	ifpred=1;

      }      
      
      
      
      
      
    }
    
    double prevN[N+1];
    double lastFSO[M];
    double lastsignal[M];
    
    t= prevN;
    lastF=lastFSO;


    for(int i=0;i<N+1;i++){
	prevN[i]=0;
    }
    
    for(int i=0;i<M;i++){
	lastF[i]=0;
	lastsignal[i]=0;	
    } 
    


    if (!ifsymbol){
    printf("Please enter -s symbol\n");
    exit(0);
    }
    
    
    
    

    
    init_market(input, symbol2);
    pthread_t thread;
    pthread_create(&thread,NULL,market,NULL);
    
    unsigned int length;
    char* start;
    char* buffer;

    
    while(1){
      

    buffer=get_inputs(&length);
    start=buffer;
    
    if (length<=0) break;
	
      while (1){
      
	
       command=*((char*)buffer);
       buffer++;

	
// 	 if(ifbinary==1){
//    
//       if(fread(&command,sizeof(char),1,input));
//       else break;
// 
//       } 
//       else
//       fscanf(input,"%c",&command);
      			

	if (command=='A'){
	  
	id=*( (unsigned int*)buffer);
	buffer=buffer+sizeof(unsigned int);

	
	side=*((char*)buffer);
        buffer=buffer+sizeof(char);

        strncpy(symbol,buffer,4);

   	symbol[4]='\0';     
	buffer=buffer+4*sizeof(char);
        

        quantity=*((unsigned int*)buffer);
        buffer=buffer+sizeof(unsigned int);
        
	price=*((double*) buffer);
        buffer=buffer+sizeof(double);

// 	if (ifbinary==0){
// 	fscanf(input," %ld %c %s %d %lf\n",&id,&side,symbol,&quantity,&price);
//       
// 	}
// 	else{
// 	fread(&id,sizeof(unsigned int),1,input);
// 		fread(&side,sizeof(char),1,input);
// 			fread(symbol,4*sizeof(char),1,input);
// 			symbol[4]='\0';
// 				fread(&quantity,sizeof(unsigned int),1,input);
// 					fread(&price,sizeof(double),1,input);
// 	  
// 	}
	

	

	if (strncmp(symbol2,symbol,4)==0){
	   	count++;
		globalcount++;
	  	 int index=id%SIZE;
	node=newNode(&hashmaphead[index],&hashmaptail[index], id, side, quantity, price);
	

	
	
	

	  
	if (side=='B'){
	  
	  newSortNode(&buyhead, &buytail, node);

	}
	
	else if (side=='S'){
	  newSortNode(&sellhead, &selltail, node);

	}
	
	}
	}
	
	
	
	else if (command=='X'){
	  
	  
	id=*( (unsigned int*)buffer);
	buffer=buffer+sizeof(unsigned int);
/*	
	side=*((char*)buf);
        buffer=buffer+sizeof(char);*/

        strncpy(symbol,buffer,4);

   	symbol[4]='\0';     
	buffer=buffer+4*sizeof(char);
        
/*
        quantity=*((unsigned int*)buffer);
        buffer=buffer+sizeof(unsigned int);
        
	price=*((double*) buffer);
        buffer=buffer+sizeof(double);
	  */
// 	  if (ifbinary==0){
// 	  fscanf(input," %ld %s\n",&id,symbol);
// 	  }
// 	  else{
// 	    	fread(&id,sizeof(unsigned int),1,input);
// 			fread(symbol,4*sizeof(char),1,input);
// 
// 	  }
// 	  
	  

	
	
	
	if (strncmp(symbol2,symbol,4)==0){
	  	 count++;
		 globalcount++;
	node=hashmaphead[id%SIZE];
	while (node!=NULL ){
		if((node->id)==id){
		  break;
		}
		else
		node=node->next;
	  }

	

	  
 	if (node!=NULL) {
   	if (node->side=='B'){
   	  deleteSortNode(&buyhead, &buytail, node);
    	}
  	
   	else if (node->side=='S'){
   	  deleteSortNode(&sellhead, &selltail, node);
  	}
	}
 	
	
	  
	  deleteNode(&hashmaphead[id%SIZE], &hashmaptail[id%SIZE],node);
	
	}
	  
	}
	
	else if (command=='T'){
	  
	  	id=*( (unsigned int*)buffer);
	buffer=buffer+sizeof(unsigned int);
	
// 	side=*((char*)buf);
//         buffer+=sizeof(char);

        strncpy(symbol,buffer,4);

   	symbol[4]='\0';     
	buffer=buffer+4*sizeof(char);
        

        quantity=*((unsigned int*)buffer);
        buffer=buffer+sizeof(unsigned int);
        
// 	price=*((double*) buffer);
//         buffer=buffer+sizeof(double);
	  
// 	  if (ifbinary==0){
// 	  fscanf(input," %ld %s %d\n",&id,symbol,&quantity  );	
// 	  }
// 	  else{
// 	    	    	fread(&id,sizeof(unsigned int),1,input);
// 			fread(symbol,4*sizeof(char),1,input);
//  			symbol[4]='\0';
// 
// 			fread(&quantity,sizeof(unsigned int),1,input);
//  			
// 			
// 	  }
// 	  
	  

	  
	if (strncmp(symbol2,symbol,4)==0){
	  
	    count++;
	    globalcount++;
	    node=hashmaphead[id%SIZE];
	  while (node!=NULL ){
		if((node->id)==id){
		  break;
		}
		else
		node=node->next;
	  }
	  C=node->price;
	  setQuantity(&node,id,quantity);
	  
	  
	  
	  
	  
	  
	  
	  for (int i=0;i<N;i++){
	    prevN[i]=prevN[i+1];
	
	  }
	  
	  prevN[N]=C;
	  
	  L=10000.0;  
	  for (int i=0;i<N+1;i++){
	    if(L>prevN[i])
	      L=prevN[i];
	  }
	  
	  H=0.0;
	  
	  for (int i=0;i<N+1;i++){
	    if(H<prevN[i])
	      H=prevN[i];
	  }
	  
	  

	  for (int i=0;i<M-1;i++){
	    lastFSO[i]=lastFSO[i+1];
	  }
	  
	  FSO=((C-L)/(H-L))*100;
	  lastFSO[M-1]=FSO;
	  
	  
	  double sum=0;
	  for (int i=0;i<M;i++){
	    sum=sum+lastFSO[i];
	  }
	  
	  SSO=sum/M;
	  
	  for (int i=0;i<M-1;i++){
	    lastsignal[i]=lastsignal[i+1];
	  }
	  lastsignal[M-1]=SSO;
	  
	  
	  sum=0;
	  
	  for (int i=0;i<M;i++){
	    sum=sum+lastsignal[i];
	  }

	  signal_line=sum/M;
	  


	  
	  
	  
	  
	  
	  
	  
	  if(node->quantity==0){
	    
	     	if (node!=NULL) {
		    if (node->side=='B'){
		      deleteSortNode(&buyhead, &buytail, node);
		    }
		    
		    else if (node->side=='S'){
		      deleteSortNode(&sellhead, &selltail, node);
		    }
		    }
		    
		    
		      
		      deleteNode(&hashmaphead[id%SIZE], &hashmaptail[id%SIZE],node);
	    
	  }
	  


	  
	  
	  
	    break;
	  }
	  
	  
	
	}
	
else 	if (command=='C'){
  	id=*( (unsigned int*)buffer);
	buffer=buffer+sizeof(unsigned int);
/*	
	side=*((char*)buf);
        buffer+=sizeof(char);*/

        strncpy(symbol,buffer,4);
   	symbol[4]='\0';     
	buffer=buffer+4*sizeof(char);
        

        quantity=*((unsigned int*)buffer);
        buffer=buffer+sizeof(unsigned int);
        
// 	price=*((double*) buffer);
//         buffer=buffer+sizeof(double);
  
  
  
// 	  if (ifbinary==0){
// 	  fscanf(input," %ld %s %d\n",&id,symbol,&quantity  );	
// 	  }
// 	  else{
// 	    	    	fread(&id,sizeof(unsigned int),1,input);
// 			fread(symbol,4*sizeof(char),1,input);
//  			symbol[4]='\0';
// 			fread(&quantity,sizeof(unsigned int),1,input);
// 			
// 			
// 	  }
	  

	  
	  
	  if (strncmp(symbol2,symbol,4)==0){
	  
	    count++;
	    globalcount++;
	    node=hashmaphead[id%SIZE];
	  while (node!=NULL ){
		if((node->id)==id){
		  break;
		}
		else
		node=node->next;
	  }
	  
	  setQuantity(&node,id,quantity);
	  if(node->quantity==0){
	    
	     	if (node!=NULL) {
		    if (node->side=='B'){
		      deleteSortNode(&buyhead, &buytail, node);
		    }
		    
		    else if (node->side=='S'){
		      deleteSortNode(&sellhead, &selltail, node);
		    }
		    }
		    
		    
		      
		      deleteNode(&hashmaphead[id%SIZE], &hashmaptail[id%SIZE],node);
	    
	  }
	  }
	}
	
	else if (command=='R'){
	  	id=*( (unsigned int*)buffer);
	buffer=buffer+sizeof(unsigned int);
	
// 	side=*((char*)buf);
//         buffer+=sizeof(char);

        strncpy(symbol,buffer,4);

   	symbol[4]='\0';     
	buffer=buffer+4*sizeof(char);
        

        quantity=*((unsigned int*)buffer);
        buffer=buffer+sizeof(unsigned int);
        
	price=*((double*) buffer);
        buffer=buffer+sizeof(double);
// 	 if (ifbinary==0){
// 	  fscanf(input," %ld %s %d %lf\n",&id,symbol,&quantity,&price);
// 	  
// 	  }
// 	  else{
// 	    
// 	    	    	fread(&id,sizeof(unsigned int),1,input);
// 			fread(symbol,4*sizeof(char),1,input);
// 			symbol[4]='\0';
// 			fread(&quantity,sizeof(unsigned int),1,input);
// 			fread(&price,sizeof(double),1,input);
// 			
// 
// 	  }
// 	  

	  
	  
	  
	  
	if (strncmp(symbol2,symbol,4)==0){
		 count++;
		 globalcount++;
	  
	node=hashmaphead[id%SIZE];
	while (node!=NULL ){
		if((node->id)==id){
		  break;
		}
		else
		node=node->next;
	  }

	side=node->side;

	
	  
	//delete  

		  
		if (node!=NULL) {
		if (node->side=='B'){
		  deleteSortNode(&buyhead, &buytail, node);
		}

		else if (node->side=='S'){
		  deleteSortNode(&sellhead, &selltail, node);
		}
		}
		
		
	  
	deleteNode(&hashmaphead[id%SIZE], &hashmaptail[id%SIZE],node);
	
	
	
	//add
	int index=id%SIZE;
	node=newNode(&hashmaphead[index],&hashmaptail[index], id, side, quantity, price);

	

	  
	if (side=='B'){
	  newSortNode(&buyhead, &buytail, node);

	}
	
	else if (side=='S'){
	  newSortNode(&sellhead, &selltail, node);
	  
	}
	
	}
	    
	    
	    
	    
	  
	}
	
	


//  	if(count>=messagecount){
// 	  count=0;
// // 	   
// 	  double temp=du;
// 	  double temp1=du;
// 	    if(sellhead!=NULL){
// 	      temp=sellhead->price;
// 
// 	      
// 	    }
// 	    
// 	    if(buytail!=NULL){
// 	      temp1=buytail->price;
// 	    }
// 
// 	    
// 	      
// 	      
//  	    fprintf(output2,"%lf %lf\n",temp,temp1);
// 
// 	
//  	}

//       }
      
      
    }
    
    }
      
      fclose(input);
      

      
      
//      for(int i=0;i<SIZE;i++){
//       node=hashmaphead[i];
//       	while (node!=NULL ){
// 	    fprintf(output,"%d %c %d %lf\n",node->id,node->side,node->quantity,node->price);
// 	    node=node->next;
// 	
//       }
//       }
      

      
      
      
      


    return 0;
  
  
}