Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <pthread.h>
  6. #include <semaphore.h>
  7.  
  8. struct valores{
  9. int x;
  10. int y;
  11. }
  12. pthread_attr_t atributos;
  13.  
  14. sem_t semaforosuma, semaforomult, semaforomain; // Declaración de semáforos
  15.  
  16. pthread_t th1, th2. th3, th4; // Declaración de threads
  17.  
  18. // Definición de funciones
  19. void multiplica (producto){
  20.  
  21. sem_wait(&semaforomult);
  22.  
  23. struct valores *amultiplicar = amultiplicar
  24. int producto;
  25.  
  26. producto=a*b;
  27.  
  28. sem_post(&semaforomult);
  29. sem_post(&semaforomain);
  30.  
  31. pthread_exit(0);
  32. }
  33. void suma (void * ptr){
  34.  
  35. sem_wait(&semaforosuma);
  36.  
  37. printf("\nSuma ha esperado bien\n");
  38.  
  39. pthread_exit(0);
  40. }
  41. //void imprime (void * ptr){
  42. // char *men=(char*)ptr;
  43. // write(1,men,strlen(men));
  44. // pthread_exit(0);
  45. //}
  46.  
  47.  
  48. int main (void){
  49. pthread_attr_init (&atributos); // Inicialización de atributo
  50. sem_init(&semaforomult, 0,0); // Inicialización del semáforo
  51. sem_init(&semaforosuma, 0,0); // Inicialización del semáforo
  52. sem_init(&semaforomain, 0,0); // Inicialización del semáforo
  53.  
  54. struct valores amultiplicar, asumar;
  55.  
  56. // PRIMER PRODUCTO
  57.  
  58. producto.x=2;
  59. producto.y=4;
  60. sem_post(&semaforomult);
  61. sem_wait(&semaforomain);
  62.  
  63. // SEGUNDO PRODUCTO
  64.  
  65. // producto.x=2;
  66. // producto.y=3;
  67.  
  68. // sem_post(&semaforomult);
  69. // sem_wait(&semaforomain);
  70.  
  71. pthread_create(&th1, &atributos,multiplica, amultiplicar);
  72. pthread_create(&th3, &atributos,suma, asumar);
  73. // pthread_create(&th4, &atributos,(resultado) imprime, "\n El thread 3 se ha ejecutado \n");
  74.  
  75.  
  76.  
  77. printf("Main termina");
  78. sem_destroy(&semaforomult);
  79. sem_destroy(&semaforosuma);
  80. sem_destroy(&semaforomain);
  81. sleep(1);
  82.  
  83. exit(0);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement