Advertisement
Guest User

Untitled

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