lecture 10 -- Unix shared memory APIs * shmget to create or locate a chunk of memory * shmat to attach it * you specify how many bytes it should be, and permissions * key should be unique across the system (pick one that other people aren't using) * it persists until all processes that are using it detach it, and one of them removes it * don't go crazy and make lots of them with huge amounts of memory, or we'll have to reboot mercury #include #include #define SHMKEY (key_t)10398 int main () { int shmid; int *shared_int; if((shmid = shmget (SHMKEY, sizeof(int), IPC_CREAT | 0666)) == -1){ perror("shared get failed: "); exit(1); } printf("shmget is happy\n"); printf ("created shared mem with ID = %d\n", shmid); if ((shared_int = (int *) shmat(shmid, (const void *)0, 0)) == -1) { perror("shared attach failed: "); exit(1); } printf ("attached shared mem\n"); *shared_int = *shared_int + 1; printf("shared integer is %d\n", *shared_int); }