Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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