Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "parking.h"
  4. #include <string.h>
  5. #include <sys/sem.h>
  6. #include <sys/msg.h>
  7. #include <sys/shm.h>
  8. #include <signal.h>
  9. #include <unistd.h>
  10.  
  11. int primero(HCoche hc);
  12. int siguiente(HCoche hc);
  13. int mejor(HCoche hc);
  14. int peor(HCoche hc);
  15. void manejadoraTiempo (int signall);
  16. int tipoMensaje(long posicionAcera, int algoritmo);
  17. void manejadorafCommit(HCoche hc);
  18.  
  19. void manejadoraPermisoAvance(HCoche hc);
  20.  
  21.  
  22. void manejadoraPermisoAvanceCommit(HCoche hc);
  23.  
  24. int idsemaforo;
  25. int idbuzon;
  26. int idmemoria;
  27. char *punteromemoria; //Hay que declararlas globales para la manejadora
  28. void finalizar(int signall);
  29. int flag=1;
  30. int vectorPIDS[100];
  31. int choferes;
  32.  
  33. int main (char argc, char * argv[]){
  34. //INICIO DE LA FASE DE RECOGIDA DE ARGUMENTOS
  35. int velocidad, debug=0, prioridadA=0, prioridadD=0;
  36. struct sigaction ss;
  37. TIPO_FUNCION_LLEGADA f_llegadas[4] = {primero, siguiente, mejor, peor};
  38. int posicion,algoritmo,k;
  39.  
  40.  
  41.  
  42. //ESTABLECEMOS LA FUNCION QUE TIENE EL SIGINT EN EL MAIN
  43. /*void salidaSIGINT;
  44. salidaSIGINT = exit(0);
  45. signal(SIGINT, salidaSIGINT);*/ //PREGUNTAR A FRAN
  46. //FIN ESTABLECEMOS LA FUNCION QUE TIENE EL SIGINT EN EL MAIN
  47.  
  48.  
  49. //DECLARACION DE SENALES Y MASCARA
  50.  
  51. /*sigset_t conjuntoTotal;
  52. sigfillset(&conjuntoTotal);
  53. sigprocmask(SIG_SETMASK,&conjuntoTotal,NULL); //De esta forma bloqueamos todas las senales*/
  54.  
  55. sigset_t senalesSinSIGINTSIGALRM;
  56. sigfillset(&senalesSinSIGINTSIGALRM);
  57. sigdelset(&senalesSinSIGINTSIGALRM,SIGINT); //De esta forma creamos un conjunto de senales menos el SIGINT, //haciendo despues sigsuspend para cambiar las senales que estan bloqueadas por este conjunto //que no tiene SIGINT (es decir, solo SIGINT no esta bloqueada)
  58. sigdelset(&senalesSinSIGINTSIGALRM,SIGALRM);
  59. sigprocmask(SIG_SETMASK,&senalesSinSIGINTSIGALRM,NULL);
  60.  
  61.  
  62. sigfillset(&ss.sa_mask);//LLenamos la mascara con las señales
  63. sigdelset(&ss.sa_mask,SIGINT);//quita de la máscara la señal SIGINT
  64. sigdelset(&ss.sa_mask,SIGALRM);
  65.  
  66. //FIN DECLARACION DE SENALES Y MASCARA
  67.  
  68. if (argc !=3 && argc !=4 && argc !=5){//El primer argumento es el nombre del programa
  69. printf("Error en el número de parámetros\n");
  70. return -1;
  71.  
  72. }
  73. else if (argc ==3){
  74. printf("Dos argumentos: %s %s\n", argv[1], argv[2]);
  75. velocidad = atoi(argv[1]);
  76. choferes = atoi(argv[2]);
  77. if (velocidad<0){
  78. printf("Velocidad errónea (menor que 0)\n");
  79. return -1;
  80. }
  81. else if (choferes<=0){
  82. printf("Número de chóferes erróneo (menor o igual que 0)\n");
  83. return -1;
  84. }
  85. else {
  86. printf("Debug desactivado\n");
  87. }
  88. }
  89. else if (argc==4) {
  90. printf("Tres argumentos: %s %s %s \n", argv[1], argv[2], argv[3]);
  91. velocidad = atoi(argv[1]);
  92. choferes = atoi(argv[2]);
  93.  
  94. //DEBUG DE ERRORES PARA BORRAR AL FINAL
  95.  
  96. printf("Velocidad = %d\n", velocidad);
  97. printf("Chóferes = %d\n",choferes);
  98. printf("Tercer argumento : %s\n", argv[3]);
  99.  
  100. //DEBUG DE ERRORES PARA BORRAR AL FINAL
  101.  
  102. if (velocidad<0){
  103. printf("Velocidad errónea (menor que 0)\n");
  104. return -1;
  105. }
  106. if (choferes<=0){
  107. printf("Número de chóferes erróneo (menor o igual que 0)\n");
  108. return -1;
  109. }
  110. if (!strcmp(argv[3],"D") || !strcmp(argv[3],"d")){
  111. debug=1;
  112. printf("Debug activado\n");
  113. }
  114. else if (!strcmp(argv[3],"PD") || !strcmp(argv[3],"pd")){
  115. prioridadD=1;
  116. printf("Prioridad desaparcar\n");
  117. }
  118. else if (!strcmp(argv[3],"PA") || !strcmp(argv[3],"pa")){
  119. prioridadA=1;
  120. printf("Prioridad aparcar\n");
  121. }
  122. }
  123. else if (argc==5) {
  124. printf("Cuatro argumentos: %s %s %s %s\n", argv[1], argv[2], argv[3], argv[4]);
  125. velocidad = atoi(argv[1]);
  126. choferes = atoi(argv[2]);
  127.  
  128. //DEBUG DE ERRORES PARA BORRAR AL FINAL
  129.  
  130. printf("Velocidad = %d\n", velocidad);
  131. printf("Chóferes = %d\n",choferes);
  132. printf("Tercer argumento : %s\n", argv[3]);
  133. printf("Cuarto argumento: %s\n", argv[4]);
  134.  
  135. //DEBUG DE ERRORES PARA BORRAR AL FINAL
  136.  
  137. if (velocidad<0){
  138. printf("Velocidad errónea (menor que 0)\n");
  139. return -1;
  140. }
  141.  
  142. if (choferes<=0){
  143. printf("Número de chóferes erróneo (menor o igual que 0)\n");
  144. return -1;
  145. }
  146. //Entramos al tercer argumento
  147. if (!strcmp(argv[3],"D") || !strcmp(argv[3],"d")){
  148. debug=1;
  149. printf("Debug activado\n");
  150. }
  151. else if (!strcmp(argv[3],"PD") || !strcmp(argv[3],"pd")){
  152. prioridadD=1;
  153. printf("Prioridad desaparcar\n");
  154. }
  155. else if (!strcmp(argv[3],"PA") || !strcmp(argv[3],"pa")){
  156. prioridadA=1;
  157. printf("Prioridad aparcar\n");
  158. }
  159. //Entramos al cuarto argumento
  160. if ((!strcmp(argv[4],"D") || !strcmp(argv[4],"d") && debug==0)){
  161. debug=1;
  162. printf("Debug activado\n");
  163. }
  164. else if ((!strcmp(argv[4],"D") || !strcmp(argv[4],"d") && debug==1)){
  165. printf("Debug doblemente activado. ERROR.\n");
  166. return -1;
  167. }
  168. if ((!strcmp(argv[4],"PD") || !strcmp(argv[4],"pd")) && prioridadA==0 && prioridadD==0){
  169. prioridadD=1;
  170. printf("Prioridad desaparcar\n");
  171. }
  172. else if (!strcmp(argv[4],"PD") || !strcmp(argv[4],"pd") && (prioridadA==1 || prioridadD==1)){
  173. printf("Error al establecer prioridades\n");
  174. return -1;
  175. }
  176. if ((!strcmp(argv[4],"PA") || !strcmp(argv[4],"pa")) && prioridadA==0 && prioridadD==0){
  177. prioridadA=1;
  178. printf("Prioridad aparcar\n");
  179. }
  180. else if (!strcmp(argv[4],"PA") || !strcmp(argv[4],"pa") && (prioridadA==1 || prioridadD==1)){
  181. printf("Error al establecer prioridades\n");
  182. return -1;
  183. }
  184. }
  185. //FIN DE LA FASE DE RECOGIDA DE ARGUMENTOS
  186. //INICIO DE LA FASE DE DECLARACIÓN DE MECANISMOS IPC
  187.  
  188. int numSem;
  189. numSem=PARKING_getNSemAforos();
  190. idsemaforo = semget (IPC_PRIVATE,numSem+choferes, IPC_CREAT | 0600);
  191. printf("El número de semaforos es %d \n",numSem);
  192.  
  193. int tamanoMemoria;
  194. tamanoMemoria=PARKING_getTamaNoMemoriaCompartida();
  195. idmemoria = shmget(IPC_CREAT,tamanoMemoria,IPC_CREAT | 0600);
  196. punteromemoria = (char *) shmat(idmemoria,0,0);
  197. printf("ID MEMORIA: %d", idmemoria);
  198.  
  199. idbuzon = msgget(IPC_PRIVATE,IPC_CREAT | 0600);
  200. //CREACION DE LAS ACERAS
  201. for(algoritmo=0;algoritmo<4;algoritmo++){
  202. struct acera;
  203. for(posicion=0;posicion<80;posicion++){
  204. acera.pos=tipoMensaje(posicion,algoritmo);
  205. msgsnd(idbuzon, &acera.pos, sizeof(acera), MSG_NOERROR);
  206. }
  207. }
  208. //FIN CREACION DE LAS ACERAS
  209.  
  210.  
  211. //FIN DE LA FASE DE DECLARACION DE MECANISMOS IPC
  212.  
  213. PARKING_inicio(velocidad, f_llegadas, idsemaforo, idbuzon, idmemoria, debug);
  214.  
  215.  
  216.  
  217. //CREACION DE PROCESOS
  218.  
  219. int valor_devuelto, valor_choferes;
  220. TIPO_FUNCION_APARCAR_COMMIT fCommit = &manejadorafCommit;
  221. TIPO_FUNCION_PERMISO_AVANCE permisoAvance = &manejadoraPermisoAvance;
  222. TIPO_FUNCION_PERMISO_AVANCE_COMMIT permisoAvanceCommit = &manejadoraPermisoAvanceCommit;
  223. valor_devuelto=fork(); //Case 0 es para el hijo y default para el padre
  224. switch (valor_devuelto){
  225. case -1:
  226. perror("ERROR al crear el hijo");
  227. break;
  228. case 0:
  229. signal(SIGALRM, manejadoraTiempo);
  230. alarm(1);
  231. while(1);
  232. break;
  233. default:
  234. vectorPIDS[0]=getppid();
  235. for(k=0;k<1;k++)//Bucle de creacion de los choferes
  236. {
  237. valor_choferes=fork();
  238. vectorPIDS[k+1]=valor_choferes;
  239. switch (valor_choferes){
  240. case -1:
  241. perror("ERROR al crear chofer");
  242. break;
  243. case 0://CODIGO DE LOS CHOFERES
  244. {
  245. struct PARKING_mensajeBiblioteca mensajebb;//Mensaje que se recibe de la biblioteca
  246. //printf("PID: %d\n" , vectorPIDS[k]);
  247. while(1){
  248. msgrcv(idbuzon,&mensajebb,sizeof(PARKING_mensajeBiblioteca)-sizeof(long),PARKING_MSG,0);
  249. if(mensajebb.subtipo==1){
  250. PARKING_aparcar(mensajebb.hCoche,0,fCommit,
  251. permisoAvance,
  252. permisoAvanceCommit);
  253. }
  254. if(mensajebb.subtipo==2){
  255. PARKING_desaparcar(mensajebb.hCoche,0,
  256. permisoAvance,
  257. permisoAvanceCommit);
  258. }
  259. }
  260. break;
  261. }
  262. default:
  263. signal(SIGINT,finalizar);
  264. PARKING_simulaciOn();
  265. break;
  266. }
  267. }
  268. break;
  269. }
  270.  
  271. //FIN DE CREACION DE PROCESOS
  272.  
  273. return 0;
  274. }
  275.  
  276.  
  277. void finalizar(int signall)
  278. {
  279. int i;
  280. printf("\n");
  281. signal(SIGINT, SIG_IGN);//Desactivamos el sigint dentro de la funcion finalizar
  282. if(semctl(idsemaforo,0,IPC_RMID) == -1)//Eliminamos el lote de semáforos
  283. {
  284. printf("Error eliminando el lote de semaforos\n");
  285. }
  286. else
  287. {
  288. printf("Lote de semaforos eliminado\n");
  289. }
  290. if(msgctl(idbuzon,IPC_RMID,0) == -1)//Eliminamos el buzon
  291. {
  292. printf("Error eliminando el buzon\n");
  293. }
  294. else
  295. {
  296. printf("Buzon eliminado\n");
  297. }
  298. if(shmdt(punteromemoria) == -1)//Liberamos la memoria compartida
  299. {
  300. printf("Error liberando la zona de memoria compartida\n");
  301. }
  302. else
  303. {
  304. printf("Zona de memoria compartida liberada\n");
  305. }
  306. if(shmctl(idmemoria,IPC_RMID,0) == -1)//Eliminamos la memoria compartida
  307. {
  308. printf("Error eliminando la zona de memoria compartida\n");
  309. }
  310. else
  311. {
  312. printf("Zona de memoria compartida eliminada con exito\n");
  313. }
  314.  
  315. for(i=1;i<choferes+1;i++){
  316. kill(vectorPIDS[i], SIGKILL);
  317. }
  318. signal(SIGINT,finalizar); //Dejamos de nuevo la senal SIGINT asociada a la funcion finalizar
  319. }
  320.  
  321.  
  322.  
  323. int tipoMensaje(long posicionAcera, int algoritmo){
  324. return (posicionAcera*100) + algoritmo+1;
  325. }
  326.  
  327. /*int *tipoMensajeBiblioteca(int posicionCodificada){
  328. int vector[2];
  329. vector[0]=posicionCodificada/10;
  330. vector[1]=posicionCodificada%10;
  331. return vector;
  332. }*/
  333.  
  334.  
  335.  
  336. void manejadoraTiempo (int signall){
  337. static int contador = 0;
  338. //printf("\nHOLA BUENAS TARDES\n");
  339. contador++;
  340. if (contador==5){
  341. //flag = 0;
  342. PARKING_fin(0);
  343. kill(vectorPIDS[0],SIGINT);
  344. }
  345. alarm(1);
  346. }
  347.  
  348.  
  349. int primero(HCoche hc){
  350. int i,j;
  351. int posicionRecibida;
  352. int flagOcupado=0;
  353. struct PARKING_mensajeBiblioteca mensajeComprobacion;
  354.  
  355. for (i=0;i<80;i++){
  356. posicionRecibida=i*10 + 1;
  357. if((msgrcv(idbuzon, &mensajeComprobacion, sizeof(PARKING_mensajeBiblioteca)-sizeof(long), posicionRecibida,0)!=-1)){ //Mensaje encontrado luego esta libre
  358. for(j=0;j<PARKING_getLongitud(hc);j++){
  359. posicionRecibida=posicionRecibida+10;
  360. if(posicionRecibida>791 || (msgrcv(idbuzon, &mensajeComprobacion, sizeof(PARKING_mensajeBiblioteca)-sizeof(long), posicionRecibida,0)==-1)){
  361. flagOcupado=1;
  362. j=PARKING_getLongitud(hc);
  363. }
  364. }
  365. if (flagOcupado==1){//no se puede aparcar
  366. flagOcupado=0;
  367. }
  368. else {//si se puede aparcar
  369. posicionRecibida=posicionRecibida-(PARKING_getLongitud(hc)-1)*10;
  370. posicionRecibida=posicionRecibida/10;
  371. printf("POSICION %d\n", posicionRecibida);
  372. return posicionRecibida;
  373. }
  374. }
  375. }
  376. return -1;
  377. }
  378.  
  379.  
  380. int siguiente(HCoche hc){
  381. return -2;
  382. }
  383.  
  384.  
  385. int mejor(HCoche hc){
  386. return -2;
  387. }
  388.  
  389.  
  390. int peor(HCoche hc){
  391. return -2;
  392. }
  393.  
  394.  
  395. void manejadorafCommit(HCoche hc){ //Sirve para que los coches aparquen en orden
  396. printf("Funcion fCommit");
  397. //struct PARKING_mensajeBiblioteca mensajebb;
  398. //msgrcv(idbuzon,&mensajebb,sizeof(PARKING_mensajeBiblioteca)-sizeof(long),PARKING_MSG,0);
  399. }
  400.  
  401.  
  402. void manejadoraPermisoAvance(HCoche hc){
  403. printf("Funcion permisoAvance");
  404. }
  405.  
  406.  
  407. void manejadoraPermisoAvanceCommit(HCoche hc){
  408. printf("Funcion permisoAvanceCommit");
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement