Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 19, 2015 00:35
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/338b53ece021409c36d3 to your computer and use it in GitHub Desktop.
Save lettergram/338b53ece021409c36d3 to your computer and use it in GitHub Desktop.
int fd;
char * mapped_memory;
/**
* Open a file and map it to memory,
* once it is mapped to memory we can close
* the file because we have access.
*/
fd = open(argv[1], O_RDWR | O_CREAT);
mapped_memory = mmap(NULL, MAX, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if(strncmp(mapped_memory, "exit", 4) == 0)
mapped_memory[0] = '\0';
/** If Reader **/
if (!strcmp(argv[2], "read")) {
while (1) {
mapped_memory[MAX - 1] = '\0';
printf("%s", mapped_memory);
if(strncmp(mapped_memory, "exit", 4) == 0)
exit(1);
fflush(stdout);
sleep(1);
}
/** If Writer **/
} else if (!strcmp(argv[2], "write")) {
while (strncmp(mapped_memory, "exit", 4) != 0)
fgets(mapped_memory, MAX, stdin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment