forked from Bananymous/banan-os
Userspace: Implement basic test for MAP_SHARED
This commit is contained in:
parent
f953f3d3ff
commit
3f164c6b82
|
@ -10,6 +10,7 @@ set(USERSPACE_PROJECTS
|
|||
id
|
||||
init
|
||||
ls
|
||||
mmap-shared-test
|
||||
poweroff
|
||||
Shell
|
||||
snake
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
cmake_minimum_required(VERSION 3.26)
|
||||
|
||||
project(mmap-shared-test CXX)
|
||||
|
||||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(mmap-shared-test ${SOURCES})
|
||||
target_compile_options(mmap-shared-test PUBLIC -O2 -g)
|
||||
target_link_libraries(mmap-shared-test PUBLIC libc)
|
||||
|
||||
add_custom_target(mmap-shared-test-install
|
||||
COMMAND sudo cp ${CMAKE_CURRENT_BINARY_DIR}/mmap-shared-test ${BANAN_BIN}/
|
||||
DEPENDS mmap-shared-test
|
||||
USES_TERMINAL
|
||||
)
|
|
@ -0,0 +1,115 @@
|
|||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FILE_NAME "test-file"
|
||||
#define FILE_SIZE (1024*1024)
|
||||
|
||||
int prepare_file()
|
||||
{
|
||||
int fd = open(FILE_NAME, O_RDWR | O_TRUNC | O_CREAT, 0666);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
void* null_buffer = malloc(FILE_SIZE);
|
||||
memset(null_buffer, 0x00, FILE_SIZE);
|
||||
|
||||
if (write(fd, null_buffer, FILE_SIZE) == -1)
|
||||
{
|
||||
perror("write");
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(null_buffer);
|
||||
close(fd);
|
||||
|
||||
printf("file created\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int job1()
|
||||
{
|
||||
int fd = open(FILE_NAME, O_RDONLY);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
void* addr = mmap(nullptr, FILE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (addr == MAP_FAILED)
|
||||
{
|
||||
perror("mmap");
|
||||
return 1;
|
||||
}
|
||||
|
||||
sleep(4);
|
||||
|
||||
size_t sum = 0;
|
||||
for (int i = 0; i < FILE_SIZE; i++)
|
||||
sum += ((uint8_t*)addr)[i];
|
||||
|
||||
munmap(addr, FILE_SIZE);
|
||||
close(fd);
|
||||
|
||||
printf("sum: %zu\n", sum);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int job2()
|
||||
{
|
||||
sleep(2);
|
||||
|
||||
int fd = open(FILE_NAME, O_RDWR);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
void* addr = mmap(nullptr, FILE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (addr == MAP_FAILED)
|
||||
{
|
||||
perror("mmap");
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(addr, 'a', FILE_SIZE);
|
||||
|
||||
munmap(addr, FILE_SIZE);
|
||||
close(fd);
|
||||
|
||||
printf("expecting: %zu\n", (size_t)'a' * FILE_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
if (int ret = prepare_file())
|
||||
return ret;
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == 0)
|
||||
return job1();
|
||||
|
||||
if (pid == -1)
|
||||
{
|
||||
perror("fork");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ret = job2();
|
||||
waitpid(pid, nullptr, 0);
|
||||
|
||||
return ret;
|
||||
}
|
Loading…
Reference in New Issue