-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTarjeta_Red.c
147 lines (135 loc) · 3.98 KB
/
Tarjeta_Red.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**Tarjeta de Red**/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
#define MAX 20
#define N 10 // Numero de Hilos a Crear...//
#define MOD %
#define AND &&
#define OR ||
#define FALSE 0
#define TRUE 1
typedef int bool;
typedef struct
{
int Buffer_In, Buffer_Out;
sem_t Bloqueado;// semaforo usado por el firewall
sem_t total; //semaforo para el manejador de interrupciones
sem_t In, Out;
}Tarjeta_Red; ///Los semaforos ueden colocarse de forma global...///
int Prioridad_Proceso(pthread_t Hilo);
void Insertar_Peticion_Buffer(int *Buffer);
void Envio_Peticion(int *Buffer);
void *Eliminar_Peticion_Buffer(int *Buffer);
void Liberar_Buffer(int *Buffer);
void Solicitar_Tarjeta_Red(Tarjeta_Red *TR);
int main()
{
int i;
pthread_t Paquetes[N];
pthread_attr_t Hilo;
struct sched_param fifo_param;
Tarjeta_Red TR;
TR.Buffer_In = 0;
TR.Buffer_Out = 0;
sem_init(&TR.Bloqueado, 0, 1);
sem_init(&TR.In, 0, 1);
sem_init(&TR.Out, 0, 1);
sem_wait(&TR.Bloqueado);
sem_wait(&TR.In);
sem_wait(&TR.Out);
Solicitar_Tarjeta_Red(&TR);
return 0;
}
void Insertar_Peticion_Buffer(int *Buffer)
{
*Buffer = (*Buffer + 1);
printf("\nNuevo Paquete a Enviar...\n");
system("sleep 1.0");
}
void Envio_Peticion(int *Buffer)
{
if(*Buffer < MAX)
{
printf("\nPaquete Guardado en el Buffer...\n");
system("sleep 1.0");
Insertar_Peticion_Buffer(Buffer);
}else{
printf("\nBuffer LLeno...\n");
system("sleep 2.0");
}
}
void *Eliminar_Peticion_Buffer(int *Buffer) // Parametro *Buffer
{
if(*Buffer > 0)
{
printf("\nLiberando Buffer...\n");
system("sleep 1.0");
Liberar_Buffer(Buffer);
}
}
void Liberar_Buffer(int *Buffer) // Parametro *Buffer
{
*Buffer = (*Buffer - 1);
printf("\nPaquete enviado...\n");
printf("\nBuffer Liberado...\n");
system("sleep 1.0");
}
void Solicitar_Tarjeta_Red(Tarjeta_Red *TR)
{
int Valor;
if((sem_getvalue(&TR->Bloqueado, &Valor) == 0) AND (Valor == 1)) //Si el firewall no esta bloqueado puedo recibir y enviar de una vez...//
{
if((sem_getvalue(&TR->In, &Valor) == 0) AND (Valor == 0)) ///Si valor es igual 0, entonces esa es la señal de que es un paquete de entrada.///
{
printf("\n\t*Paquete de Entrada*\n");
system("sleep 1.0");
Envio_Peticion(&TR->Buffer_In);
printf("\nCapacidad del Buffer Entrada ->: %d\n", TR->Buffer_In);
system("sleep 1.0");
Eliminar_Peticion_Buffer(&TR->Buffer_In);
printf("\nCapacidad del Buffer Entrada ->: %d\n", TR->Buffer_In);
system("sleep 1.0");
sem_post(&TR->In);
}
if((sem_getvalue(&TR->Out, &Valor) == 0) AND (Valor == 0)) ///Si valor es igual 0, entonces esa es la señal de que es un paquete de salida.///
{
printf("\n\t*Paquete de Salida*\n");
system("sleep 1.0");
Envio_Peticion(&TR->Buffer_Out);
printf("\nCapacidad del Buffer Salida ->: %d\n", TR->Buffer_Out);
system("sleep 1.0");
Eliminar_Peticion_Buffer(&TR->Buffer_Out);
printf("\nCapacidad del Buffer Salida ->: %d\n", TR->Buffer_Out);
system("sleep 1.0");
sem_post(&TR->Out);
}
}else{
printf("\nFirewall Bloqueo el Envio y Recepcion de Datos...\n"); ///En este caso el Firewall bloqueo la tarjeta de Red, pero el igual puede recibir los paquetes pero no los envia...///
system("sleep 2.0");
if((sem_getvalue(&TR->In, &Valor) == 0) AND (Valor == 0))
{
printf("\n\t*Paquete de Entrada*\n");
system("sleep 1.0");
Envio_Peticion(&TR->Buffer_In);
printf("\nCapacidad del Buffer Entrada ->: %d\n", TR->Buffer_In);
system("sleep 1.0");
printf("\nNo se puede enviar porque el Firewall bloqueo la _Tarjeta de Red...\n");
system("sleep 1.0");
sem_post(&TR->In);
}
if((sem_getvalue(&TR->Out, &Valor) == 0) AND (Valor == 0))
{
printf("\n\t*Paquete de Salida*\n");
system("sleep 1.0");
Envio_Peticion(&TR->Buffer_Out);
printf("\nCapacidad del Buffer Salida ->: %d\n", TR->Buffer_Out);
system("sleep 1.0");
printf("\nNo se puede enviar porque el Firewall bloqueo la _Tarjeta de Red...\n");
system("sleep 1.0");
sem_post(&TR->Out);
}
}
}