Advertisement
Guest User

thread c aviao

a guest
Sep 1st, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "unistd.h"
  3. #include "stdlib.h"
  4. #include "pthread.h"
  5.  
  6. #define AV 3
  7.  
  8. pthread_mutex_t lock_pista = PTHREAD_MUTEX_INITIALIZER;
  9.  
  10. void * aviao(void * arg) {
  11.     int i = *((int *) arg);
  12.     while(1) {
  13.         printf("%d: voando e vai pousar\n",i);
  14.         sleep(10);
  15.         pthread_mutex_lock(&lock_pista);
  16.         printf("%d: pousando\n",i);
  17.         sleep(2);
  18.         printf("%d: pousou, liberando a pista\n",i);
  19.         pthread_mutex_unlock(&lock_pista);
  20.         sleep(5);
  21.        
  22.         printf("%d: querendo decolar\n",i);
  23.         sleep(10);
  24.         pthread_mutex_lock(&lock_pista);
  25.         printf("%d: decolando\n",i);
  26.         sleep(2);
  27.         printf("%d: decolou, pista liberada\n",i);
  28.         pthread_mutex_unlock(&lock_pista);
  29.         sleep(5);
  30.     }
  31. }
  32.  
  33. int main() {
  34.    
  35.     pthread_t a[AV];
  36.    
  37.     int i,*id;
  38.        
  39.     for (i=0;i<AV;i++) {
  40.         id = (int *) malloc(sizeof(int));
  41.         *id = i;       
  42.         pthread_create(&a[i], NULL, aviao, &i);
  43.     }
  44.    
  45.     pthread_join(a[0], NULL);
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement