#include #include #include #include #include int main (void) { key_t mykey; int shmid; char *testch = NULL; mykey = IPC_PRIVATE; printf ("creating private key \n"); shmid = shmget (mykey, 65536, 0777); if (shmid != -1) { printf ("shm id %x\n", shmid); printf ("attaching to private key\n"); testch = (char *) shmat (shmid, NULL, 0); printf ("returned %x\n", testch); printf ("writing \'hello\'\n"); snprintf (testch, 7, "hello\n"); printf ("reading\n"); printf ("value: %s\n", testch); } 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); printf ("attaching to private key\n"); testch = (char *) shmat (shmid, NULL, 0); printf ("returned %x\n", testch); printf ("current value %s\n", testch); printf ("waiting for enter\n"); getc (stdin); printf ("writing \'hello\'\n"); snprintf (testch, 7, "hello\n"); printf ("reading\n"); printf ("value: %s\n", testch); printf ("waiting for enter\n"); getc (stdin); } else printf ("error %d, %s\n", errno, strerror (errno)); 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)); printf ("creating exclusive shmget.c:2\n"); mykey = ftok ("shmget.c", 2); printf ("%x %x %x %x\n", 0777, IPC_CREAT, IPC_EXCL, 0777 | IPC_CREAT | IPC_EXCL); 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)); printf ("creating exclusive shmget.c:2 (should fail with EEXIST)\n"); shmid = shmget (mykey, 65536, 0777 | IPC_CREAT | IPC_EXCL); if (shmid != -1 || errno != EEXIST) printf ("FAIL: shm id %x, errno %d\n", shmid, shmid == -1 ? 0 : errno); else printf ("PASS: 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 0; }