#include #include #include #include #include #include struct test{ int n; float x; } ; int main() {int shmid,i; void *shmptr; // usually void; cast off it... fprintf(stderr,"\nNow, read entire file into shmem\n"); // Open the file int fd=open("abc.txt",O_RDONLY); // find # bytes int numBytes=lseek(fd,0,SEEK_END); // Back to beginning lseek(fd,0,SEEK_SET); // Allocate shmem needed shmid=shmget(getuid(),numBytes+1,IPC_CREAT|0600); // Attach shmptr=shmat(shmid,0,0); // Read file into shmem with 1 read numBytes=read(fd,shmptr,numBytes); // Did it work? fprintf(stderr,"The file: \n %s \n",shmptr); // OK. Put a struct into shared memory. // First, cast a pointer into shmem struct test *a=(struct test*)(shmptr+48); // Fill in the members a->n=100; a->x=2.345; // Print from shmem printf("Struct Contents: The int is %d and the float is %f\n",a->n,a->x); // Now, print entire shmem printf("\nShared Memory Contents...\n"); write(1,shmptr,numBytes); // Detach memory shmdt(shmptr); // Remove shmem shmctl(shmid,IPC_RMID,0); fprintf(stderr,"\nShmem Removed: \n"); }