# Include <stdio. h>
# Include <sys/IPC. h>
# Include <sys/SHM. h>
# Include <unistd. h>
# Include <string. h>
# Include <stdlib. h>
# Include <ctype. h>
# Include <stdio. h>
# Derefined segsize 100
Void usage ()
{
Fprintf (stderr, "shmtool-a utility for tinkering with shared memory/N ");
Fprintf (stderr, "/nusage: shmtool/N ");
Fprintf (stderr, "(w) rite <text>/N ");
Fprintf (stderr, "(r) EAD/N ");
Fprintf (stderr, "(d) elete/N ");
Fprintf (stderr, "(m) ode change <octal mode>/N ");
Exit (1 );
}
Void writeshm (INT shmid, char * segptr, char * text)
{
Strcpy (segptr, text );
Printf ("Don.../N ");
}
Void readshm (INT shmid, char * segptr)
{
Printf ("segptr: % s/n", segptr );
}
Void removeshm (INT shmid)
{
Shmctl (shmid, ipc_rmid, 0 );
Printf ("Shared Memory segmentmask for deletion/N ");
}
Void changemode (INT shmid, char * Mode)
{
Struct shmid_ds myshmds;
Shmctl (shmid, ipc_stat, & myshmds); // put the structure of the shared memory in myshmds
Printf ("Old permisssion were: % O/N", myshmds. shm_perm.mode );
Sscanf (mode, "% O", (unsigned int *) & myshmds. shm_perm.mode );
Shmctl (shmid, ipc_set, & myshmds); // set the Shared Memory attribute based on the value of myshmds
Printf ("New permissions are: % O/N", myshmds. shm_perm.mode );
}
Int main (INT argc, char * argv [])
{
Key_t key;
Int shmid;
Char * segptr;
If (argc = 1)
Usage ();
If (shmid = shmget (Key, segsize, ipc_creat | ipc_excl | 0666) =-1)
{
Printf ("shared memory segment exit-opening as client/N ");
If (shmid = shmget (Key, segsize, 0) =-1)
{
Perror ("shmget ");
Exit (1 );
}
}
Else
{
Printf ("creating new shared memory segment/N ");
}
If (segptr = shmat (shmid, 0, 0) = NULL)
{
Perror ("shmat ");
Exit (1 );
}
Switch (tolower (argv [1] [0])
{
Case 'W ':
Writeshm (shmid, segptr, argv [2]); // write characters to the memory using pointer Variables
Readshm (shmid, segptr); // read information from the shared memory
Break;
Case 'r ':
Readshm (shmid, segptr );
Break;
Case 'D ':
Removeshm (shmid );
Break;
Case 'M ':
Changemode (shmid, argv [2]);
Break;
Default:
Usage ();
}
Return 0;
}