Created
March 19, 2015 00:35
-
-
Save lettergram/338b53ece021409c36d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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