Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. #include <unistd.h>
  2.  
  3. #include <stdio.h>
  4.  
  5. #include <stdlib.h>
  6.  
  7. #include <sys/types.h>
  8.  
  9. #include <sys/stat.h>
  10.  
  11. #include <errno.h>
  12.  
  13. #include <fcntl.h>
  14.  
  15. #include <pthread.h>
  16.  
  17. #include <semaphore.h>
  18.  
  19. #include <uuid/uuid.h>
  20.  
  21. #include "acoes.c"
  22.  
  23.  
  24. typedef struct book{
  25. //struct orderbook
  26.  
  27. ACAO action;
  28.  
  29. //ação que entra no orderbook
  30.  
  31. struct LIST *Sells;
  32.  
  33. //ações de venda
  34.  
  35. struct LIST *Buys;
  36.  
  37. //ações de compra
  38.  
  39. sem_t *s;
  40.  
  41. //semaforo para o orderbook
  42.  
  43.  
  44. }ORDERBOOK;
  45.  
  46.  
  47.  
  48. struct book orderBook[11];
  49.  
  50. //orderbooks para as listas
  51.  
  52. struct LIST{
  53.  
  54. ACAO accao;
  55.  
  56. struct LIST *prox;
  57.  
  58. }; //LISTA
  59.  
  60.  
  61. //Insere, de forma ordenada, a ação na lista
  62.  
  63.  
  64. void insertOrderedList(struct LIST **p0, ACAO x){
  65.  
  66. struct LIST *p, *pa = NULL, *corr = *p0;
  67.  
  68. int cont = TRUE;
  69.  
  70. p = (struct LIST *) malloc(sizeof(struct LIST));
  71.  
  72. p->accao = x;
  73.  
  74. p->prox = NULL;
  75.  
  76. while(corr != NULL && cont){
  77.  
  78. if(x.price < corr->accao.price){
  79.  
  80. cont = FALSE;
  81.  
  82. }
  83.  
  84. else{
  85.  
  86. pa = corr;
  87. corr = corr->prox;
  88.  
  89. }
  90.  
  91. }
  92.  
  93. p->prox = corr;
  94.  
  95. if(pa == NULL){
  96.  
  97. *p0 = p;
  98.  
  99. }
  100. else{
  101.  
  102. pa->prox = p;
  103.  
  104. }
  105.  
  106. }
  107.  
  108.  
  109.  
  110. //Imprime a lista
  111. void printList(struct LIST *p0){
  112.  
  113. struct LIST *p = p0;
  114.  
  115. while(p != NULL){
  116.  
  117. printf("%s %d %lf %d\n", p->accao.title, p->accao.idCorretor, p->accao.price, p->accao.quantity);
  118.  
  119. p = p->prox;
  120.  
  121. }
  122.  
  123. }
  124.  
  125.  
  126.  
  127. //Remove a ação
  128.  
  129.  
  130. ACAO removeAction(struct LIST **p0, ACAO x){
  131.  
  132. struct LIST *pa0, *pa, *p, *px, *pxx;
  133.  
  134. int segIgualX;
  135.  
  136. pa0 = NULL;
  137.  
  138. p = *p0;
  139.  
  140. while(p != NULL){
  141.  
  142. if(p->accao.idTitle == x.idTitle){
  143.  
  144. segIgualX = TRUE;
  145.  
  146. px = p;
  147.  
  148. while(segIgualX){
  149.  
  150. pa = p;
  151.  
  152. p = p->prox;
  153.  
  154. if(p == NULL){
  155.  
  156. segIgualX = FALSE;
  157.  
  158. }
  159.  
  160. else{
  161.  
  162. segIgualX = p->accao.idTitle == x.idTitle;
  163.  
  164. }
  165.  
  166. }
  167.  
  168. if(pa0 == NULL){
  169.  
  170. *p0 = p;
  171.  
  172. }
  173.  
  174. else{
  175.  
  176. pa0->prox = p;
  177.  
  178. }
  179.  
  180. pa->prox = NULL;
  181.  
  182. while(px != NULL){
  183.  
  184. pxx = px;
  185.  
  186. px = px->prox;
  187.  
  188. free(pxx);
  189.  
  190. pxx = NULL;
  191.  
  192. }
  193.  
  194. p = NULL;
  195.  
  196. }
  197.  
  198. else{
  199.  
  200. pa0 = p;
  201.  
  202. p = p->prox;
  203.  
  204. }
  205.  
  206. }
  207.  
  208. }
  209.  
  210.  
  211.  
  212. //Guarda as negociações no ficheiro
  213.  
  214.  
  215. void saveFile(int quantity, char *title, double price, time_t timestamp){
  216.  
  217. FILE *file;
  218.  
  219. file = fopen ("simulador.log", "a");
  220.  
  221. sleep(1);
  222.  
  223.  
  224. //Escreve no ficheiro
  225.  
  226. fprintf(file, "Sold: %d ações\nTitle: %s\nPrice: %lf €\nDate: %s\n\n", quantity, title, price, ctime(&timestamp));
  227.  
  228. fclose(file);
  229.  
  230. }
  231.  
  232.  
  233.  
  234. //Negoceia a ação
  235.  
  236.  
  237. void negotiateAction(struct LIST **p0, ACAO x){
  238.  
  239. struct LIST *p;
  240.  
  241. int quantitySold = 0;
  242.  
  243. p = *p0;
  244.  
  245. while(p != NULL){
  246.  
  247. //Preço de venda <= compra???
  248.  
  249. if(x.price <= (p->accao.price*(-1))){
  250.  
  251. // Venda < Compra
  252.  
  253. if(x.quantity < p->accao.quantity){
  254.  
  255. //Compra ações em venda
  256.  
  257. quantitySold = x.quantity;
  258.  
  259. p->accao.quantity = p->accao.quantity - quantitySold;
  260.  
  261. x.quantity = 0;
  262.  
  263. }
  264.  
  265. //Venda >= Compra
  266.  
  267. else{
  268.  
  269. //Compra ações de compra
  270.  
  271. quantitySold = p->accao.quantity;
  272.  
  273. x.quantity = x.quantity - quantitySold;
  274.  
  275. p->accao.quantity = 0;
  276.  
  277. removeAction(p0, p->accao);
  278.  
  279. insertOrderedList(p0, x);
  280.  
  281. }
  282.  
  283. }
  284.  
  285. p = p->prox;
  286.  
  287. }
  288.  
  289. if(quantitySold > 0){
  290.  
  291. saveFile(quantitySold, x.title, x.price, x.timestamp);
  292.  
  293. }
  294.  
  295. }
  296.  
  297.  
  298.  
  299. void *reader(void *ff);
  300.  
  301. void createPipe(char pipeName[]);
  302.  
  303.  
  304.  
  305. void createPipe(char pipeName[]){
  306.  
  307. int ret_val = mkfifo(pipeName,0666);
  308. //criar pipe name
  309.  
  310. if((ret_val==-1)&&(errno!=EEXIST)){
  311.  
  312. perror("ERROR PIPE NAME");
  313.  
  314. }
  315.  
  316. }
  317.  
  318. //orderbook
  319.  
  320.  
  321. int title(char *t){
  322. //verificar se titulo da ação está numa lista do orderbook
  323.  
  324. if(strcmp(t,"Tit0")==0)
  325. return 0;
  326.  
  327.  
  328. else if(strcmp(t,"Tit1")==0)
  329. return 1;
  330.  
  331.  
  332. return 10;
  333.  
  334. }
  335.  
  336.  
  337. //orderbook
  338. void insertAction(ACAO a){
  339.  
  340. int t=title(a.title);
  341.  
  342. //verifica lista do orderbook
  343.  
  344. orderBook[t].action=a;
  345.  
  346. //coloca a ação no orderbook
  347.  
  348. sem_post(orderBook[t].s);
  349. //abre o semáforo para a thread negociar
  350.  
  351. }
  352.  
  353.  
  354.  
  355. void *reader(void *ff){
  356. //lê ações de cada fifo
  357.  
  358. int readFf = (*(int*)ff);
  359.  
  360. fd_set readset;
  361.  
  362. int err=0, size=0;
  363.  
  364. ACAO data;
  365.  
  366.  
  367. while(1){
  368.  
  369. FD_ZERO(&readset);
  370. //inicializa set
  371.  
  372. FD_SET(readFf,&readset);
  373.  
  374. err = select(readFf+1,&readset,NULL,NULL,NULL);
  375. //verifica a leitura
  376.  
  377. if(err>0 && FD_ISSET(readFf,&readset)){
  378.  
  379. FD_CLR(readFf,&readset);
  380. //limpa flags
  381.  
  382. read(readFf,&data,sizeof(data));
  383. //lê dados FIFO
  384.  
  385. printf("Title \t Broker \t Price \t\t Quantity \t Time\n");
  386.  
  387. 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)));
  388.  
  389. insertAction(data);
  390. //insere na lista que vão ser tratadas pelas threads do orderbook
  391.  
  392. }
  393.  
  394. }
  395.  
  396. }
  397.  
  398.  
  399.  
  400. //Cria o ficheiro para guardar as negociações
  401.  
  402.  
  403. void createFile(){
  404.  
  405. FILE *file;
  406.  
  407. file = fopen ("simulador.log", "w");
  408.  
  409. fprintf(file, "Negotiations\n\n");
  410.  
  411. fclose(file);
  412.  
  413. }
  414.  
  415.  
  416. //orderbook
  417. void
  418. *negotiation(void *ob){
  419.  
  420. while(1){
  421. ORDERBOOK *b = (ORDERBOOK*)ob;
  422.  
  423. sem_wait(b->s);
  424. //coloca semaforo em espera
  425.  
  426. ACAO a=b->action;
  427.  
  428. if(a.price>0){
  429. //venda
  430.  
  431. if(b->Buys==NULL){
  432. //não há compras
  433. insertOrderedList(&(b->Sells),a);
  434.  
  435. }
  436.  
  437. else{
  438.  
  439. negotiateAction(&(b->Buys),a);
  440.  
  441. }
  442.  
  443. }
  444.  
  445. else{
  446. //compra
  447.  
  448. if(b->Sells==NULL){
  449. //não há vendas
  450.  
  451. insertOrderedList(&(b->Buys),a);
  452.  
  453. }
  454.  
  455. else{
  456.  
  457. negotiateAction(&(b->Sells),a);
  458.  
  459. }
  460.  
  461. }
  462.  
  463. }
  464.  
  465. }
  466.  
  467.  
  468.  
  469. //orderbook
  470.  
  471.  
  472. pthread_t orderThread[11];
  473.  
  474. sem_t bs[11];
  475.  
  476. //semaforos para as listas do orderbook
  477.  
  478.  
  479. void initiate(){
  480.  
  481. int i;
  482.  
  483. for(i=0;i<11;i++){
  484.  
  485. sem_init(&bs[i],0,0);
  486. //inicializa os semáforos
  487.  
  488. orderBook[i].s=&bs[i];
  489. //coloca o semáforo no orderbook
  490.  
  491. pthread_create(&orderThread[i],NULL,negotiation,&orderBook[i]);
  492. //cria thread para o orderbook
  493.  
  494. }
  495.  
  496. }
  497.  
  498.  
  499.  
  500. void waitThread(pthread_t *threads, int n){
  501.  
  502. int i;
  503.  
  504. //Espera por Threads
  505.  
  506. for (i = 0; i < n; ++i){
  507.  
  508. pthread_join(threads[i], NULL);
  509.  
  510. }
  511.  
  512. }
  513.  
  514.  
  515.  
  516. int main(){
  517.  
  518. pthread_t readerThread[5];
  519.  
  520.  
  521. createFile();
  522.  
  523. int readFf1, readFf2, readFf3, readFf4, readFf5;
  524.  
  525. createPipe(P1);
  526.  
  527. //cria pipes
  528.  
  529. createPipe(P2);
  530.  
  531. createPipe(P3);
  532.  
  533. createPipe(P4);
  534.  
  535. createPipe(P5);
  536.  
  537.  
  538. readFf1 = open(P1, O_RDONLY|O_NONBLOCK);
  539.  
  540. //abrir canais de leitura
  541.  
  542. readFf2 = open(P2, O_RDONLY|O_NONBLOCK);
  543.  
  544. readFf3 = open(P3, O_RDONLY|O_NONBLOCK);
  545.  
  546. readFf4 = open(P4, O_RDONLY|O_NONBLOCK);
  547.  
  548. readFf5 = open(P5, O_RDONLY|O_NONBLOCK);
  549.  
  550.  
  551. pthread_create( &readerThread[0], NULL, &reader, (void*) (&readFf1));
  552.  
  553. pthread_create( &readerThread[1], NULL, &reader, (void*) (&readFf2));
  554.  
  555. pthread_create( &readerThread[2], NULL, &reader, (void*) (&readFf3));
  556.  
  557. pthread_create( &readerThread[3], NULL, &reader, (void*) (&readFf4));
  558.  
  559. pthread_create( &readerThread[4], NULL, &reader, (void*) (&readFf5));
  560.  
  561.  
  562. initiate();
  563.  
  564. //iniciar orderbook
  565.  
  566. printf("\n\n START \n\n");
  567.  
  568. printf("\n\n PIPE CREATION \n\n");
  569.  
  570. printf("\n\n READING CHANNELS FOR PIPES \n\n");
  571.  
  572. printf("\n\n THREADS CREATED FOR READING PIPES \n\n");
  573.  
  574. printf("\n\n SIMULATOR READY\n\n");
  575.  
  576.  
  577. waitThread(readerThread,5);
  578.  
  579. waitThread(orderThread,11);
  580.  
  581. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement