Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <string.h>
- #define MEM_SIZE 1024
- #define PASS_UNICO 55
- int main(){
- int memoriaID;
- char *shmPointer;
- puts("------ Este proceso lee ------");
- key_t clave_unica_mem = ftok("/bin/ls", PASS_UNICO);
- if((memoriaID = shmget(clave_unica_mem, MEM_SIZE, 0666 | IPC_CREAT)) == -1)
- {
- fprintf(stderr,"Error al reservar la memoria.");
- exit(EXIT_FAILURE);
- }
- shmPointer = shmat(memoriaID,(void *)0,0); // Obtenemos el puntero a memoria compartida
- printf("Texto escrito en memoria compartida: %s\n", shmPointer); // Leemos la memoria compartida
- shmdt((void *)shmPointer); // Liberamos la memoria compartida, se pasa como parametro el puntero que hace referencia a la memoria compartida.
- if(shmctl(memoriaID,IPC_RMID,NULL)==-1) // Liberamos la memoria compartida
- {
- fprintf(stderr,"Error al liberar la memoria");
- exit(EXIT_FAILURE);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment