Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 19, 2015 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/55c2c85695227f5b5cd7 to your computer and use it in GitHub Desktop.
Save lettergram/55c2c85695227f5b5cd7 to your computer and use it in GitHub Desktop.
key_t key;
int id;
char *msg;
int write_mode = 0644;
/**
* Make a key!
* This Key will be used to
* keep track of the datas
* location in memory
*/
if ((key = ftok(argv[0], 22)) == -1)
exit(1);
/**
* Obtains identifier for the shared
* memory location, given the key.
*
* FROM MAN PAGES:
* shmget: Returns the identifier
* of the shared memory segment
* associated with the value of the
* argument key.
*/
if ((id = shmget(key, SIZE, write_mode | IPC_CREAT)) == -1)
exit(1);
/**
* Given the identifier,
* uses shmat to retrieve
* the message from memory
*/
msg = shmat(id, 0, 0);
/**
* Using the pointer to the physical
* memory we either read or write to
* or from the mapped physical memory.
*/
char * header = "\nWriting Message: ";
if (argc == 2)
strncpy(msg, argv[1], SIZE);
else
header = "\nRead Message Contains: ";
printf("%s\"%s\"\n\n", header, msg);
/**
* Detatches from the physical
* memory location
*/
shmdt(msg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment