iKernel

shm2.c

Nov 7th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/shm.h>
  4. #include <sys/ipc.h>
  5. #include <string.h>
  6.  
  7.  
  8. #define     MEM_SIZE    1024
  9. #define     PASS_UNICO  55
  10.  
  11.  
  12. int main(){
  13.     int memoriaID;
  14.     char *shmPointer;
  15.     puts("------ Este proceso lee ------");
  16.  
  17.     key_t clave_unica_mem = ftok("/bin/ls", PASS_UNICO);
  18.    
  19.     if((memoriaID = shmget(clave_unica_mem, MEM_SIZE, 0666 | IPC_CREAT)) == -1)
  20.     {
  21.         fprintf(stderr,"Error al reservar la memoria.");
  22.         exit(EXIT_FAILURE);
  23.     }
  24.    
  25.     shmPointer = shmat(memoriaID,(void *)0,0); // Obtenemos el puntero a memoria compartida
  26.    
  27.     printf("Texto escrito en memoria compartida: %s\n", shmPointer); // Leemos la memoria compartida
  28.            
  29.     shmdt((void *)shmPointer); // Liberamos la memoria compartida, se pasa como parametro el puntero que hace referencia a la memoria compartida.
  30.     if(shmctl(memoriaID,IPC_RMID,NULL)==-1) // Liberamos la memoria compartida
  31.     {
  32.         fprintf(stderr,"Error al liberar la memoria");
  33.         exit(EXIT_FAILURE);
  34.     }
  35.  
  36.     return 0;
  37. }
Add Comment
Please, Sign In to add comment