#include #include #include #include #include #include #define MEM_SIZE (64*1024) // 64k bytes #define MY_KEY 0xBAD02152 // Arbitrary key to "name" memory segment #define MY_ADDR 0x42000000 // Where we suggest the memory to be mapped int main(int args, char* argv) { char line[256]; int mem_handle; int i,j,l,err; char* mem_area; char c; fprintf(stderr,"Reader PID=%d\n",getpid()); // Lookup segment with specific key. Size and flags are not used mem_handle = shmget(MY_KEY,0, 0); if (mem_handle==-1) { fprintf(stderr,"Memory [key=%08X] could not be found: ", MY_KEY); perror(0); // Print out specific error in errno exit(1); } // Map segment into this address space mem_area = (char*) shmat(mem_handle,(void*)MY_ADDR,0); if (mem_area==(char*)-1) { fprintf(stderr,"Could not attach [addr=%08X]: ",MY_ADDR); perror(0); // Print out specific error in errno exit(1); } fprintf(stderr,"Reader memory segment mapped to %08x\n",mem_area); while (1) { // Read line of lenght up to 256 bytes for (l=0; l < 256; l++) { c = getchar(); if (c==EOF || c=='\n') break; line[l] = c; } fprintf(stdout,"[%s]\n",mem_area); // Pick up string in mem_area fflush(stdout); } exit(0); }