//--------------------------------------------------------- // com.shm.cc //--------------------------------------------------------- #include #include #include #include #include #include //--------------------------------------------------------- int main() { int key = ftok("/tmp/com.shm", 3); int shmid = shmget(key, 100, IPC_CREAT | 0600); char *s = nullptr; assert(shmid != -1); // shmget ok? switch(fork()) { case -1: std::cout << "fallo en fork()!"; break; case 0: s = static_cast(shmat(shmid, nullptr, 0)); std::cout << "hijo escribe: "; for (char c = 'a'; c <= 'z'; ++c) std::cout << (*s++ = c); *s = 0; // string ending '\0' std::cout << std::endl; break; default: wait(nullptr); // wait for child s = static_cast(shmat(shmid, nullptr, 0)); std::cout << " padre lee: " << s << std::endl; break; } } //---------------------------------------------------------