diff options
[SP/P10] Solved exercises
1 files changed, 24 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practice10/ex2-2.c | 24 |
diff --git a/Year 3/System programming/Practice10/ex2-2.c b/Year 3/System programming/Practice10/ex2-2.c new file mode 100644 index 0000000..2043360 --- /dev/null +++ b/Year 3/System programming/Practice10/ex2-2.c @@ -0,0 +1,24 @@ +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> + +int main() { + const char msg[] = "hello world"; + size_t msglen = strlen(msg) + 1; + + int shmd = shm_open("/shm", O_RDONLY, 0755); + ftruncate(shmd, msglen); + char* memory = mmap(NULL, msglen, PROT_READ, MAP_SHARED, shmd, 0); + + char buf[1024]; + strncpy(buf, memory, msglen); + write(1, buf, msglen); + + munmap(memory, msglen); + close(shmd); + shm_unlink("/shm"); + return 0; +} |
