Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <uuid/uuid.h>
- #include "acoes.c"
- typedef struct book{
- //struct orderbook
- ACAO action;
- //ação que entra no orderbook
- struct LIST *Sells;
- //ações de venda
- struct LIST *Buys;
- //ações de compra
- sem_t *s;
- //semaforo para o orderbook
- }ORDERBOOK;
- struct book orderBook[11];
- //orderbooks para as listas
- struct LIST{
- ACAO accao;
- struct LIST *prox;
- }; //LISTA
- //Insere, de forma ordenada, a ação na lista
- void insertOrderedList(struct LIST **p0, ACAO x){
- struct LIST *p, *pa = NULL, *corr = *p0;
- int cont = TRUE;
- p = (struct LIST *) malloc(sizeof(struct LIST));
- p->accao = x;
- p->prox = NULL;
- while(corr != NULL && cont){
- if(x.price < corr->accao.price){
- cont = FALSE;
- }
- else{
- pa = corr;
- corr = corr->prox;
- }
- }
- p->prox = corr;
- if(pa == NULL){
- *p0 = p;
- }
- else{
- pa->prox = p;
- }
- }
- //Imprime a lista
- void printList(struct LIST *p0){
- struct LIST *p = p0;
- while(p != NULL){
- printf("%s %d %lf %d\n", p->accao.title, p->accao.idCorretor, p->accao.price, p->accao.quantity);
- p = p->prox;
- }
- }
- //Remove a ação
- ACAO removeAction(struct LIST **p0, ACAO x){
- struct LIST *pa0, *pa, *p, *px, *pxx;
- int segIgualX;
- pa0 = NULL;
- p = *p0;
- while(p != NULL){
- if(p->accao.idTitle == x.idTitle){
- segIgualX = TRUE;
- px = p;
- while(segIgualX){
- pa = p;
- p = p->prox;
- if(p == NULL){
- segIgualX = FALSE;
- }
- else{
- segIgualX = p->accao.idTitle == x.idTitle;
- }
- }
- if(pa0 == NULL){
- *p0 = p;
- }
- else{
- pa0->prox = p;
- }
- pa->prox = NULL;
- while(px != NULL){
- pxx = px;
- px = px->prox;
- free(pxx);
- pxx = NULL;
- }
- p = NULL;
- }
- else{
- pa0 = p;
- p = p->prox;
- }
- }
- }
- //Guarda as negociações no ficheiro
- void saveFile(int quantity, char *title, double price, time_t timestamp){
- FILE *file;
- file = fopen ("simulador.log", "a");
- sleep(1);
- //Escreve no ficheiro
- fprintf(file, "Sold: %d ações\nTitle: %s\nPrice: %lf €\nDate: %s\n\n", quantity, title, price, ctime(×tamp));
- fclose(file);
- }
- //Negoceia a ação
- void negotiateAction(struct LIST **p0, ACAO x){
- struct LIST *p;
- int quantitySold = 0;
- p = *p0;
- while(p != NULL){
- //Preço de venda <= compra???
- if(x.price <= (p->accao.price*(-1))){
- // Venda < Compra
- if(x.quantity < p->accao.quantity){
- //Compra ações em venda
- quantitySold = x.quantity;
- p->accao.quantity = p->accao.quantity - quantitySold;
- x.quantity = 0;
- }
- //Venda >= Compra
- else{
- //Compra ações de compra
- quantitySold = p->accao.quantity;
- x.quantity = x.quantity - quantitySold;
- p->accao.quantity = 0;
- removeAction(p0, p->accao);
- insertOrderedList(p0, x);
- }
- }
- p = p->prox;
- }
- if(quantitySold > 0){
- saveFile(quantitySold, x.title, x.price, x.timestamp);
- }
- }
- void *reader(void *ff);
- void createPipe(char pipeName[]);
- void createPipe(char pipeName[]){
- int ret_val = mkfifo(pipeName,0666);
- //criar pipe name
- if((ret_val==-1)&&(errno!=EEXIST)){
- perror("ERROR PIPE NAME");
- }
- }
- //orderbook
- int title(char *t){
- //verificar se titulo da ação está numa lista do orderbook
- if(strcmp(t,"Tit0")==0)
- return 0;
- else if(strcmp(t,"Tit1")==0)
- return 1;
- return 10;
- }
- //orderbook
- void insertAction(ACAO a){
- int t=title(a.title);
- //verifica lista do orderbook
- orderBook[t].action=a;
- //coloca a ação no orderbook
- sem_post(orderBook[t].s);
- //abre o semáforo para a thread negociar
- }
- void *reader(void *ff){
- //lê ações de cada fifo
- int readFf = (*(int*)ff);
- fd_set readset;
- int err=0, size=0;
- ACAO data;
- while(1){
- FD_ZERO(&readset);
- //inicializa set
- FD_SET(readFf,&readset);
- err = select(readFf+1,&readset,NULL,NULL,NULL);
- //verifica a leitura
- if(err>0 && FD_ISSET(readFf,&readset)){
- FD_CLR(readFf,&readset);
- //limpa flags
- read(readFf,&data,sizeof(data));
- //lê dados FIFO
- printf("Title \t Broker \t Price \t\t Quantity \t Time\n");
- printf("%s \t %d \t\t %lf \t %d \t\t %s \n",data.title,data.idCorretor,data.pr ice,data.quantity,ctime(&(data.timestamp)));
- insertAction(data);
- //insere na lista que vão ser tratadas pelas threads do orderbook
- }
- }
- }
- //Cria o ficheiro para guardar as negociações
- void createFile(){
- FILE *file;
- file = fopen ("simulador.log", "w");
- fprintf(file, "Negotiations\n\n");
- fclose(file);
- }
- //orderbook
- void
- *negotiation(void *ob){
- while(1){
- ORDERBOOK *b = (ORDERBOOK*)ob;
- sem_wait(b->s);
- //coloca semaforo em espera
- ACAO a=b->action;
- if(a.price>0){
- //venda
- if(b->Buys==NULL){
- //não há compras
- insertOrderedList(&(b->Sells),a);
- }
- else{
- negotiateAction(&(b->Buys),a);
- }
- }
- else{
- //compra
- if(b->Sells==NULL){
- //não há vendas
- insertOrderedList(&(b->Buys),a);
- }
- else{
- negotiateAction(&(b->Sells),a);
- }
- }
- }
- }
- //orderbook
- pthread_t orderThread[11];
- sem_t bs[11];
- //semaforos para as listas do orderbook
- void initiate(){
- int i;
- for(i=0;i<11;i++){
- sem_init(&bs[i],0,0);
- //inicializa os semáforos
- orderBook[i].s=&bs[i];
- //coloca o semáforo no orderbook
- pthread_create(&orderThread[i],NULL,negotiation,&orderBook[i]);
- //cria thread para o orderbook
- }
- }
- void waitThread(pthread_t *threads, int n){
- int i;
- //Espera por Threads
- for (i = 0; i < n; ++i){
- pthread_join(threads[i], NULL);
- }
- }
- int main(){
- pthread_t readerThread[5];
- createFile();
- int readFf1, readFf2, readFf3, readFf4, readFf5;
- createPipe(P1);
- //cria pipes
- createPipe(P2);
- createPipe(P3);
- createPipe(P4);
- createPipe(P5);
- readFf1 = open(P1, O_RDONLY|O_NONBLOCK);
- //abrir canais de leitura
- readFf2 = open(P2, O_RDONLY|O_NONBLOCK);
- readFf3 = open(P3, O_RDONLY|O_NONBLOCK);
- readFf4 = open(P4, O_RDONLY|O_NONBLOCK);
- readFf5 = open(P5, O_RDONLY|O_NONBLOCK);
- pthread_create( &readerThread[0], NULL, &reader, (void*) (&readFf1));
- pthread_create( &readerThread[1], NULL, &reader, (void*) (&readFf2));
- pthread_create( &readerThread[2], NULL, &reader, (void*) (&readFf3));
- pthread_create( &readerThread[3], NULL, &reader, (void*) (&readFf4));
- pthread_create( &readerThread[4], NULL, &reader, (void*) (&readFf5));
- initiate();
- //iniciar orderbook
- printf("\n\n START \n\n");
- printf("\n\n PIPE CREATION \n\n");
- printf("\n\n READING CHANNELS FOR PIPES \n\n");
- printf("\n\n THREADS CREATED FOR READING PIPES \n\n");
- printf("\n\n SIMULATOR READY\n\n");
- waitThread(readerThread,5);
- waitThread(orderThread,11);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement