Skip to content

Instantly share code, notes, and snippets.

@ramytamer
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save ramytamer/b6f0dfa906ec68b13d2e to your computer and use it in GitHub Desktop.

Select an option

Save ramytamer/b6f0dfa906ec68b13d2e to your computer and use it in GitHub Desktop.
Shared memory
///////////////////
// shared memory //
///////////////////
/**
* Open a shared memory
* @return shared memory file descriptor
* if(shmfd < 0) err
*/
int shmfd = shm_open( "MSG_NAME", O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG);
/**
* Truncate shared memory size
* @param1: shared memory file descriptor
* @param2: integer the ammount of data will be shared
* ex: (3 * sizeof(char)) will share 3 characters.
*/
int msg_size;
ftrucate(shmfd, msg_size)
/**
* Requesting The shared segment
* offset: where to start
* if(!shared_msg) err;
*/
int offset = 0;
msg_type * shared_msg = (msg_type *) mmap(NULL, msg_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, offset);
/**
* Unlink the shared memory (remove it)
*/
shm_unlink("MSG_NAME");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment