#include #include #include #include #include #include int main(int argc, char *argv[]) { int shmid1; int shmid2; int shmid3; struct shmid_ds buf; /* make and stat 2 shmem segments */ shmid1 = shmget(0x12345678, 1024 * 1024, IPC_CREAT | IPC_EXCL | 0600); if (shmid1 < 0) { perror("shmget 1 failed"); exit(1); } printf("id1 = %d / %#x\n", shmid1, shmid1); shmid2 = shmget(0x12345679, 1024 * 1024, IPC_CREAT | IPC_EXCL | 0600); if (shmid2 < 0) { perror("shmget 2 failed"); exit(1); } printf("id2 = %d / %#x\n", shmid2, shmid2); if (shmctl(shmid1, IPC_STAT, &buf) < 0) { printf("shmctl(%d / %#x, IPC_STAT): ERROR: %s\n", shmid1, shmid1, strerror(errno)); } if (shmctl(shmid2, IPC_STAT, &buf) < 0) { printf("shmctl(%d / %#x, IPC_STAT): ERROR: %s\n", shmid2, shmid2, strerror(errno)); } /* remove both */ if (shmctl(shmid1, IPC_RMID, NULL) < 0) { printf("shmctl(%d / %#x, IPC_RMID): ERROR: %s\n", shmid1, shmid1, strerror(errno)); } if (shmctl(shmid2, IPC_RMID, NULL) < 0) { printf("shmctl(%d / %#x, IPC_RMID): ERROR: %s\n", shmid2, shmid2, strerror(errno)); } /* make a third one */ shmid3 = shmget(0x1234567A, 1024 * 1024, IPC_CREAT | IPC_EXCL | 0600); if (shmid3 < 0) { perror("shmget 3 failed"); exit(1); } printf("id3 = %d / %#x\n", shmid3, shmid3); /* now observe stat behavior for the two old IDs */ if (shmctl(shmid1, IPC_STAT, &buf) < 0) { printf("shmctl(%d / %#x, IPC_STAT): ERROR: %s\n", shmid1, shmid1, strerror(errno)); } if (shmctl(shmid2, IPC_STAT, &buf) < 0) { printf("shmctl(%d / %#x, IPC_STAT): ERROR: %s\n", shmid2, shmid2, strerror(errno)); } /* clean up by removing third segment */ if (shmctl(shmid3, IPC_RMID, NULL) < 0) { printf("shmctl(%d / %#x, IPC_RMID): ERROR: %s\n", shmid3, shmid3, strerror(errno)); } return 0; }