1/* 2 * shm-client - client program to demonstrate shared memory. 3 */ 4#include <sys/types.h> 5#include <sys/ipc.h> 6#include <sys/shm.h> 7#include <stdio.h> 8#include <barrelfish/barrelfish.h> 9 10#define SHMSZ 27 11 12int main(int argc, char *argv[]) 13{ 14 int shmid; 15 key_t key; 16 char *shm, *s; 17 18 /* 19 * We need to get the segment named 20 * "5678", created by the server. 21 */ 22 key = 5678; 23 24 /* 25 * Locate the segment. 26 */ 27 if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { 28 debug_printf("client: error in shmid\n"); 29 return 1; 30 } 31 32 debug_printf("client: shmget performed correctly id: %d\n", shmid); 33 34 /* 35 * Now we attach the segment to our data space. 36 */ 37 if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { 38 debug_printf("client: error in shmat\n"); 39 return 1; 40 } 41 42 debug_printf("client: shmat performed correctly shm: %p\n", shm); 43 44 /* 45 * Now read what the server put in the memory. 46 */ 47 for (s = shm; *s != 0; s++) 48 putchar(*s); 49 putchar('\n'); 50 51 debug_printf("client: data read, writing *\n"); 52 53 /* 54 * Finally, change the first character of the 55 * segment to '*', indicating we have read 56 * the segment. 57 */ 58 *shm = '*'; 59 60 debug_printf("client: exiting\n"); 61 62 return 0; 63} 64