Advertisement
nigu

gla_thread

Dec 2nd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.05 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <curses.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define PASSO 1 /* entita dello spostamento del ladro */
  8. #define SU 65 /* Freccia su */
  9. #define GIU 66 /* Freccia giu */
  10. #define SINISTRA 68 /* Freccia sinsitra */
  11. #define DESTRA 67 /* Freccia destra */
  12. #define MAXX 80 /* Numero di colonne dello schermo */
  13. #define MAXY 24 /* Numero di righe dello schermo */
  14.      
  15. /* Struttura per la comunicazione tra figli e padre */
  16. struct pos {
  17.     char c; /* soggetto che invia il dato: ladro o guardia */
  18.     int x; /* coordinata x */
  19.     int y; /* coordinata y */
  20. };
  21.  
  22. pthread_mutex_t muta=PTHREAD_MUTEX_INITIALIZER;
  23.      
  24. void* ladro(void* arg) {
  25.     struct pos pos_ladro;
  26.     struct pos prev_pos_ladro;
  27.     long int r;
  28.     int dx,dy;
  29.     pos_ladro.x=1;
  30.     pos_ladro.y=1;
  31.     pos_ladro.c='$';
  32.  
  33.     //mvprintw(0,1,"%c",pos_ladro.c);
  34.  
  35.  
  36.     while(1) {
  37.            r=random();
  38.            if(r<RAND_MAX/2)
  39.               dx=PASSO;
  40.            else
  41.               dx=-PASSO;
  42.            if(pos_ladro.x+dx<1 || pos_ladro.x+dx>=MAXX)
  43.               dx=-dx;
  44.            pos_ladro.x+=dx;
  45.            r=random();
  46.            if(r<RAND_MAX/2)
  47.               dy=PASSO;
  48.            else
  49.               dy=-PASSO;
  50.            if(pos_ladro.y+dy<1 || pos_ladro.y+dy>=MAXY)
  51.               dy=-dy;
  52.            pos_ladro.y+=dy;
  53.            
  54.             pthread_mutex_lock(&muta);
  55.             mvprintw(prev_pos_ladro.y,prev_pos_ladro.x," ");
  56.            
  57.            mvprintw(pos_ladro.y,pos_ladro.x,"%c",pos_ladro.c);
  58.             refresh();
  59.  
  60.            
  61.             prev_pos_ladro.y = pos_ladro.y;
  62.             prev_pos_ladro.x = pos_ladro.x;
  63.  
  64.             pthread_mutex_unlock(&muta);
  65.             usleep(500000);
  66.     }
  67.  
  68. }
  69.  
  70.  
  71. void* avvocato(void* arg){
  72.     struct pos pos_avvocato;
  73.     struct pos prev_pos_avvocato;
  74.     long int r;
  75.     int dx,dy;
  76.     pos_avvocato.x=1;
  77.     pos_avvocato.y=10;
  78.     pos_avvocato.c='A';
  79.  
  80.     while(1) {
  81.            r=random();
  82.            if(r<RAND_MAX/2)
  83.               dx=PASSO;
  84.            else
  85.               dx=-PASSO;
  86.            if(pos_avvocato.x+dx<1 || pos_avvocato.x+dx>=MAXX)
  87.               dx=-dx;
  88.            pos_avvocato.x+=dx;
  89.            r=random();
  90.            if(r<RAND_MAX/2)
  91.               dy=PASSO;
  92.            else
  93.               dy=-PASSO;
  94.            if(pos_avvocato.y+dy<1 || pos_avvocato.y+dy>=MAXY)
  95.               dy=-dy;
  96.            pos_avvocato.y+=dy;
  97.            
  98.             pthread_mutex_lock(&muta);
  99.             mvprintw(prev_pos_avvocato.y,prev_pos_avvocato.x," ");
  100.            
  101.            mvprintw(pos_avvocato.y,pos_avvocato.x,"%c",pos_avvocato.c);
  102.             refresh();
  103.  
  104.            
  105.             prev_pos_avvocato.y = pos_avvocato.y;
  106.             prev_pos_avvocato.x = pos_avvocato.x;
  107.  
  108.             pthread_mutex_unlock(&muta);
  109.             usleep(500000);
  110.     }
  111.  
  112.  
  113.  
  114. }
  115.  
  116. void guardia(int pipeout) {
  117.             struct pos pos_guardia;
  118.             pos_guardia.c='#';
  119.             pos_guardia.x=MAXX-1;
  120.             pos_guardia.y=MAXY-1;
  121.             write(pipeout,&pos_guardia,sizeof(pos_guardia));
  122.             while(1) {
  123.                     char c;
  124.                             switch(c=getch()) {
  125.                                     case SU:
  126.                                             if(pos_guardia.y>0)
  127.                                                     pos_guardia.y-=1;
  128.                                             break;
  129.                                     case GIU:
  130.                                             if(pos_guardia.y<MAXY-1)
  131.                                                     pos_guardia.y+=1;
  132.                                             break;
  133.                                     case SINISTRA:
  134.                                             if(pos_guardia.x>0)
  135.                                                     pos_guardia.x-=1;
  136.                                             break;
  137.                                     case DESTRA:
  138.                                             if(pos_guardia.x<MAXX-1)
  139.                                                     pos_guardia.x+=1;
  140.                                             break;
  141.                             }
  142.                             write(pipeout,&pos_guardia,sizeof(pos_guardia));
  143.             }
  144.     }
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. void* guardia(void* arg);
  153. void controllo(void);
  154.  
  155. int main () {
  156.  
  157.     initscr(); /* inizializzazione dello schermo */
  158.             noecho(); /* i caratteri corrispondenti ai tasti premuti non saranno
  159.             * visualizzati sullo schermo del terminale */
  160.             srand((int)time(0)); /*inizializziamo il generatore di numeri random*/
  161.             curs_set(0); /* nasconde il cursore */
  162.  
  163.     thread_mutex_init(&mtx, NULL);
  164.    
  165.     pthread_t ladro_thread;
  166.     pthread_t avvocato_thread;
  167.     //pthread_t guardia_thread;
  168.     //pthread_t controllo_thread;
  169.    
  170.     pthread_create (&ladro_thread, NULL, &ladro, NULL);
  171.     pthread_create (&avvocato_thread, NULL, &avvocato, NULL);
  172.     //pthread_create (&guardia_thread, NULL, &ladro, NULL);
  173.     //pthread_create (&controllo_thread, NULL, &ladro, NULL);
  174.  
  175.     pthread_join(ladro_thread, NULL);
  176.     pthread_join(avvocato_thread, NULL);
  177.     //pthread_join(guardia_thread, NULL);
  178.     //pthread_join(controllo_thread, NULL);
  179.  
  180.     endwin();
  181.    
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement