Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Implementacion con semaforo

Estas en el tema de Implementacion con semaforo en el foro de C/C++ en Foros del Web. No funciona en mi c++ me sale un error; @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código C++: Ver original #include <sys/ipc.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/sem.h> ...
  #1 (permalink)  
Antiguo 12/07/2011, 18:29
Avatar de soy_nicanor  
Fecha de Ingreso: mayo-2010
Mensajes: 374
Antigüedad: 14 años
Puntos: 3
Implementacion con semaforo

No funciona en mi c++ me sale un error;

Código C++:
Ver original
  1. #include <sys/ipc.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/sem.h>
  6. #include <unistd.h>
  7.  
  8. /*struct sembuf{
  9.     unsigned short sem_num;
  10.         short sem_op;
  11.         short sem_flg;
  12. };*/
  13.  
  14. union semun {
  15.         int val;                    /* value for SETVAL */
  16.         struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
  17.         unsigned short int *array;  /* array for GETALL, SETALL */
  18.         struct seminfo *__buf;      /* buffer for IPC_INFO */
  19.     };
  20.  
  21. typedef union semun arg;
  22.  
  23.  
  24.  
  25.  
  26. int semWait(int semID, short semNumber){
  27.     int r;
  28.     struct sembuf operacion[1];
  29.     operacion[0].sem_num = semNumber;
  30.     operacion[0].sem_op = -1;
  31.     operacion[0].sem_flg = 0;
  32.     r = semop(semID,operacion,1);
  33.     return r;
  34. }
  35.  
  36. int semSignal(int semID, int semNumber){
  37.     int r;
  38.     struct sembuf operacion[1];
  39.     operacion[0].sem_num = semNumber;
  40.     operacion[0].sem_op = 1;
  41.     operacion[0].sem_flg = 0;
  42.     r = semop(semID,operacion,1);
  43.     return r;
  44. }  
  45.  
  46. void printInfo(arg valores, int q){
  47.     int i;
  48.     printf("valor actual del semaforo :\n");
  49.     for (i = 0; i<q;i++){
  50.         printf("\t sem[%d] = %d\n",i,valores.array[i]);
  51.     }
  52.  
  53. }
  54.  
  55. int setValores(int *ID, int valor, int q){
  56.     int i;
  57.     arg valoresSem;
  58.     valoresSem.array = (unsigned short int *)malloc (sizeof(unsigned short int)*q);
  59.     for(i = 0; i<q; i ++){
  60.         valoresSem.array[i] = valor;
  61.        
  62.     }
  63.         semctl(*ID,0,SETALL,valoresSem);
  64.         free(valoresSem.array);
  65. }
  66.  
  67. int main(int argc, char* argv[]){
  68.  
  69.     int semaforoID;
  70.     short qSem = 2;
  71.     arg valoresSem;
  72.     valoresSem.array = (unsigned short int *)malloc (sizeof(unsigned short int)*qSem);
  73.     int i;
  74.  
  75.  
  76.     semaforoID = semget(ftok(argv[0],2), qSem, IPC_CREAT | 0666);
  77.  
  78.  
  79.     setValores(&semaforoID,0,2);
  80.     semctl(semaforoID,0,GETALL,valoresSem);
  81.  
  82.     printInfo(valoresSem,2);
  83.     sleep(1);
  84.     for (i = 0; i< 2 ; i++){
  85.         if (fork() == 0){
  86.             semWait(semaforoID,0); //Espera a que el padre lo deje entrar
  87.             printf("Soy el hijo\n");
  88.             semSignal(semaforoID,1); //le da lugar al padre
  89.            
  90.             exit(0);
  91.         }
  92.     }
  93.  
  94.  
  95.     printf("Soy el Padre\n");
  96.     printf("libera el 1er hijo\n");
  97.     semSignal(semaforoID,0); //Libera un hijo
  98.    
  99.     semWait(semaforoID,1); //Espera a que el hijo termine
  100.     printf("libera el 2do hijo\n");
  101.     semSignal(semaforoID,0); //Libera un hijo
  102.     semWait(semaforoID,1); //Espero que termine el hijo
  103.  
  104.     semSignal(semaforoID,0); //Libera al pedo para ver el valro
  105.     semctl(semaforoID,0,GETALL,valoresSem);
  106.     printInfo(valoresSem,2);
  107.  
  108.     free(valoresSem.array);
  109.     semctl(semaforoID,qSem , IPC_RMID);
  110.  
  111.  
  112.     return 0;
  113. }

A que se debee este error
  #2 (permalink)  
Antiguo 12/07/2011, 20:09
 
Fecha de Ingreso: abril-2010
Ubicación: Rosario
Mensajes: 1.850
Antigüedad: 14 años
Puntos: 228
Respuesta: Implementacion con semaforo

Cual es "este error"??
  #3 (permalink)  
Antiguo 13/07/2011, 08:46
Avatar de soy_nicanor  
Fecha de Ingreso: mayo-2010
Mensajes: 374
Antigüedad: 14 años
Puntos: 3
Respuesta: Implementacion con semaforo

un que ago? ♀

Me dice la libreria esta mal

#include <sys/ipc.h>
  #4 (permalink)  
Antiguo 13/07/2011, 10:58
 
Fecha de Ingreso: abril-2010
Ubicación: Rosario
Mensajes: 1.850
Antigüedad: 14 años
Puntos: 228
Respuesta: Implementacion con semaforo

Yo lo compile en mi linux y ningun problema. Entonces pense que podria ser algun problema de windows. Estas compilando para windows??

En una pagina habian dicho que se necesitaba perl pero no estoy seguro de eso y no tengo windows como para probarlo. Saludos

Etiquetas: funcion, implementacion
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:41.