Shared Memory instance:

Source: Internet
Author: User

# 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.