Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <windows.h>
- using namespace std;
- void regionCritica(int);
- void Proceso1();
- void Proceso2();
- bool S1,S2;
- int turno;
- int x=0, cen=0;
- int main()
- {
- cout<<"EJERCICIO NUMERO 1: ";
- cout<<"QUE PROCESO QUIERE QUE EMPIECE PRIMERO LA EJECUCIÓN \n1) o 2)";
- cout<<"\nOP= ";
- cin>>turno;
- thread pro1(Proceso1);
- thread pro2(Proceso2);
- if(turno==1) //en este caso siempre consideramos al turno 1
- {
- pro1.join();
- pro2.join();
- }
- if(turno==2)
- {
- pro2.join();
- pro1.join();
- }
- system("pause");
- return 0;
- }
- void Proceso1()
- {
- while(x<=5)
- {
- S1= true;
- while(S2== true)
- {
- if(turno == 2)
- {
- cout <<"\nProceso1 detenido\n";
- S1 = false;
- while(turno == 2)
- {}
- S1= true;
- }
- }
- do
- {
- cout <<"\nProceso1 esta en la seccion Critica\n";
- system("pause");
- regionCritica(1);
- if(x==6)
- {
- cen=1;
- }
- }while((x>=4&&x<=7) && cen==0);
- cout <<"\nProceso1 saliendo de la seccion Critica\n";
- turno = 2;
- cout<<"\nturno del proceso2";
- S1= false;
- }
- }
- void Proceso2()
- {
- while(x<=8)
- {
- S2= true;
- while(S1== true)
- {
- if(turno == 1)
- {
- cout <<"\nProceso2 detenido\n";
- S2= false;
- while(turno == 1)
- {}
- S2= true;
- }
- }
- do
- {
- cout <<"\nProceso2 esta en la seccion Critica\n";
- system("pause");
- regionCritica(2);
- }while(x>=6&& x<=10);
- cout <<"\nProceso2 saliendo de la seccion Critica\n";
- turno = 1;
- cout<<"\nturno del preceso1";
- S2= false;
- }
- }
- void regionCritica(int a)
- {
- x=x+a;
- cout << "Variable:" <<x<< endl;
- Sleep(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement