Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6.  
  7. void regionCritica(int);
  8. void Proceso1();
  9. void Proceso2();
  10. bool S1,S2;
  11. int turno;
  12. int x=0, cen=0;
  13.  
  14. int main()
  15. {
  16.  
  17.     cout<<"EJERCICIO NUMERO 1: ";
  18.     cout<<"QUE PROCESO QUIERE QUE EMPIECE PRIMERO LA EJECUCIÓN \n1) o 2)";
  19.     cout<<"\nOP= ";
  20.     cin>>turno;
  21.     thread pro1(Proceso1);
  22.     thread pro2(Proceso2);
  23.     if(turno==1) //en este caso siempre consideramos al turno 1
  24.     {
  25.         pro1.join();
  26.         pro2.join();
  27.     }
  28.     if(turno==2)
  29.     {
  30.         pro2.join();
  31.         pro1.join();
  32.     }
  33.     system("pause");
  34.     return 0;
  35. }
  36.  
  37. void Proceso1()
  38. {
  39.     while(x<=5)
  40.     {
  41.         S1= true;
  42.         while(S2== true)
  43.         {
  44.             if(turno == 2)
  45.             {
  46.                 cout <<"\nProceso1 detenido\n";
  47.                 S1 = false;
  48.                 while(turno == 2)
  49.                 {}
  50.                 S1= true;
  51.             }
  52.         }
  53.         do
  54.         {
  55.             cout <<"\nProceso1 esta en la seccion Critica\n";
  56.             system("pause");
  57.             regionCritica(1);
  58.             if(x==6)
  59.             {
  60.                 cen=1;
  61.             }
  62.         }while((x>=4&&x<=7) && cen==0);
  63.         cout <<"\nProceso1 saliendo de la seccion Critica\n";
  64.         turno = 2;
  65.         cout<<"\nturno del proceso2";
  66.         S1= false;
  67.     }
  68. }
  69.  
  70. void Proceso2()
  71. {
  72.     while(x<=8)
  73.     {
  74.         S2= true;
  75.         while(S1== true)
  76.         {
  77.             if(turno == 1)
  78.             {
  79.                 cout <<"\nProceso2 detenido\n";
  80.                 S2= false;
  81.                 while(turno == 1)
  82.                 {}
  83.                 S2= true;
  84.             }
  85.         }
  86.         do
  87.         {
  88.             cout <<"\nProceso2 esta en la seccion Critica\n";
  89.             system("pause");
  90.             regionCritica(2);
  91.         }while(x>=6&& x<=10);
  92.         cout <<"\nProceso2 saliendo de la seccion Critica\n";
  93.         turno = 1;
  94.         cout<<"\nturno del preceso1";
  95.         S2= false;
  96.     }
  97. }
  98.  
  99. void regionCritica(int a)
  100. {
  101.     x=x+a;
  102.     cout << "Variable:" <<x<< endl;
  103.     Sleep(500);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement