#include #include #include #include int main (void) { key_t mykey; int shmid; mykey = IPC_PRIVATE; printf ("creating private key \n"); shmid = shmget (mykey, 65536, 0777); if (shmid != -1) printf ("shm id %x\n", shmid); else printf ("error %d, %s\n", errno, strerror (errno)); printf ("creating shmget.c:1\n"); mykey = ftok ("shmget.c", 1); shmid = shmget (mykey, 65536, 0777 | IPC_CREAT); if (shmid != -1) printf ("shm id %x\n", shmid); else { printf ("error %d, %s\n", errno, strerror (errno)); return 1; } printf ("creating shmget.c:1 (should be same keyid)\n"); mykey = ftok ("shmget.c", 1); shmid = shmget (mykey, 65536, 0777 | IPC_CREAT); if (shmid != -1) printf ("shm id %x\n", shmid); else { printf ("error %d, %s\n", errno, strerror (errno)); return 1; } printf ("creating exclusive shmget.c:2\n"); mykey = ftok ("shmget.c", 2); shmid = shmget (mykey, 65536, 0777 | IPC_CREAT | IPC_EXCL); if (shmid != -1) printf ("shm id %x\n", shmid); else { printf ("error %d, %s\n", errno, strerror (errno)); return 1; } printf ("creating exclusive shmget.c:2 (should fail)\n"); shmid = shmget (mykey, 65536, 0777 | IPC_CREAT | IPC_EXCL); if (shmid != -1 || (shmid == -1 && errno != EEXIST)) { printf ("unexpected pass: shm id %x\n", shmid); return 1; } else printf ("error %d, %s\n", errno, strerror (errno)); printf ("creating non-exclusive shmget.c:2 (should be the same as the prior id)\n"); shmid = shmget (mykey, 65536, 0777 | IPC_CREAT); if (shmid != -1) printf ("shm id %x\n", shmid); else { printf ("error %d, %s\n", errno, strerror (errno)); return 1; } return 0; }