/* * Exemplo de uso de memória compartilhada. */ #include #include #include #include #include #define SHMSZ 27 /* Será arredondado para um múltiplo de PAGE_SIZE */ char str_global[10]; int main() { int shmid; key_t key; char str_local[10]; char *shm; /* Chave arbitrária para o segmento compartilhado */ key = 5677; /* Criação do segmento de memória e obtenção do seu identificador. */ if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { perror("shmget"); exit(1); } /* Tentativa de associação próximo à área de pilha. */ if ((shm = shmat(shmid, str_local-0x100000, SHM_RND)) == (char *) -1) { printf("shmat at 0x%x\n", (unsigned int) str_local-0x100000); perror("shmat"); exit(1); } printf("Processo cliente: %s\n", shm); printf("shm = 0x%x\n", (unsigned int) shm); shmctl(shmid, IPC_RMID, NULL); return 0; }