//* Shared memory demo - client - by D. Marshall *

/* * shm-client version 2 - client program to demonstrate shared memory. */ #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int shmid; char *shm, *s; /* * Let's use the same memory identifier that the server is using. * Instead asking the user, it could be sent automaticlaly to the * client using a pipe */ fprintf(stdout, "Please enter the value of the memory identifier: "); fscanf(stdin, " %d", &shmid); /* * Now we attach the segment to our data space. */ if ((shm = (char *) shmat(shmid, NULL, 0)) == (char *) -1) { fprintf(stderr, "shmat\n"); exit(1); } /* * Now read what the server put in the memory. */ for (s = shm; *s != '\0'; s++) putchar(*s); putchar('\n'); /* * Finally, change the first character of the * segment to '*', indicating we have read * the segment. */ *shm = '*'; /* * now detach the shared memory segment */ if ( shmdt(shm) == -1) { fprintf(stderr, "Unable to detach\n"); } exit(0); }