Está en la página 1de 6

UNIVERSIDAD NACIONAL JORGE BASADRE GROHMANN

Facultad de Ingeniería
Escuela Profesional de Ingeniería en Informática y Sistemas

CURSO: Algoritmos y programación paralela

TEMA: Alg. Eisenberg-McGuire y Alg. Lamport

DOCENTE: Ing. Jimmy Muñoz Miranda

ALUMNO: Johan Marcos Condori Orellana

CODIGO: 2019-119045

TACNA-PERÚ
2020
ALGORITMO DE LAMPORT
#include <iostream>
#include <thread>

using namespace std;

int nt[30];
bool s[30];
int n;

int ntmax()
{
int max;
max=nt[1];
for(int k=1;k<n ; k++)
{
if(nt[k]>max)
{
max = nt[k];
}
else
{

}
}

return max;
}

bool ntmayorque(int i, int j)


{
if(nt[i]>nt[j])
{
return true;
}
else if(nt[i]<nt[j])
{
return false;
}
else
{
return (j<i);;
}
}

void proceso_1(int n)
{
for(int i=1; i<=n; i++)
{
cout<<"el proceso "<<i<<" recoje ticket "<<nt[i]<<"\n";
s[i]=true;
nt[i]=1 + ntmax();
cout<<"el proceso "<<i<<" ya tiene ticket "<<nt[i]<<"\n";
s[i]=false;
for(int j=1; j<=n; j++)
{
while(s[j]==true)
{

}
while(nt[j]!=0 && ntmayorque(i,j))
{

}
cout<<"el proceso "<<i<<" ejecuta su seccion critica\n";
nt[i]=0;
cout<<"el proceso "<<i<<" termina de usar su ticket "<<nt[i]<<"\n";
cout<<"------------------------------------\n";
}
}

int main()
{
int p;
cout<<"ALGORITMO DE LANPORT\n";
cout<<"--------------------\n";
cout << "Ingrese la cantidad que va ha tomar n:";
cin>>n;

for(p=1; p<=n; p++)


{
s[p]=false;
cout<<"el proceso "<<p<<" ingresa a la espera\n";
}
cout<<"------------------------------------\n";
cout<<"------------------------------------\n";
thread p1(proceso_1,n);
p1.join();
}

Imagen de ejecución:
ALGORITMO DE EISENBERG
#include <iostream>
#include <conio.h>
#include <thread>
#define max 100
using namespace std;

int seccion[max];
int j,i,n,t;

void proceso_1(int r)
{
for(i=1; i<=r; i++){
do
{
seccion[i]=1;
j=t;
while(j!=i)
{
if (seccion[j]!=3)
{
j=t;
}
else
{
j=(j+1)%n;
}
}
seccion[i]=2;
j=0;
while((j<n) && (j==i || seccion[j]!=2))
{
j=j+1;
}
}
while (!((j>=n) && (t==i || seccion[t]==3)));
t=i;
cout<<endl;
cout<<"El proceso "<<i<<" esta iniciando la seccion critica\n";
cout<<"El proceso "<<i<<". termino la seccion critica\n";
j=(t+1)%n;
while(seccion[j]==3)
{
j=(j+1)%n;
}
t=i+1;
seccion[i]=3;
}
}

int main ()
{
cout<<"intento de EISENBERG\n";
cout<<"---------------------\n";

cout<<"\nIngrese cantidad de procesos : ";


cin>>n;
seccion[i]=3;
t=1;
thread p1(proceso_1,n);
p1.join();

return 0;
getch ();
}

Imagen de ejecución:

También podría gustarte