/* sharedmem.c Started by Jeff Ondich on 4/11/96 Last modified on 4/3/02 This program demonstrates how to use shared memory in Linux. */ #include #include #include #include #include char pSharedBuffer[100]; int main( void ) { int pid, status; /* Initialize the (un)shared memory. */ strcpy( pSharedBuffer, "Mom, there's a singing moose outside!" ); fprintf( stderr, "%s\n", pSharedBuffer ); /* Fork a child process */ pid = fork(); if( pid == -1 ) { perror( "fork() failure" ); } else if( pid != 0 ) { fprintf( stderr, "The parent %d is waiting.\n", getpid() ); wait( &status ); fprintf( stderr, "The state of affairs is: %s\n", pSharedBuffer ); } else { fprintf( stderr, "The child %d will try to scare the moose.\n", getpid() ); strcpy( pSharedBuffer, "The moose is gone now." ); } fprintf( stderr, "%d is quitting\n", getpid() ); return( 0 ); }