Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice10 / ex2-1.c
blob: db839c12b85731d21f297277d9d66623f8209131 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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);

	int shmd = shm_open("/shm", O_RDWR | O_CREAT | O_TRUNC, 0755);
	ftruncate(shmd, msglen);
	char* memory = mmap(NULL, msglen, PROT_WRITE, MAP_SHARED, shmd, 0);

	strncpy(memory, msg, msglen);

	munmap(memory, msglen);
	close(shmd);
	return 0;
}