mirror of
https://github.com/drasko/codezero.git
synced 2026-01-16 21:03:16 +01:00
File open was failing when using 2 files with same name. TODO: Look at it in the future. Need to increase writeable file size in fs0. 16 pages don't work.
65 lines
1.4 KiB
C
65 lines
1.4 KiB
C
/*
|
|
* Test mmap/munmap posix calls.
|
|
*
|
|
* Copyright (C) 2007, 2008 Bahadir Balban
|
|
*/
|
|
#include <sys/ipc.h>
|
|
#include <sys/shm.h>
|
|
#include <sys/types.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <tests.h>
|
|
|
|
#define PAGE_SIZE 0x1000
|
|
|
|
int mmaptest(void)
|
|
{
|
|
int fd;
|
|
void *base;
|
|
int x = 0x1000;
|
|
|
|
if ((fd = open("./newfile3.txt", O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) < 0)
|
|
perror("open:");
|
|
else
|
|
printf("open: Success.\n");
|
|
|
|
/* Extend the file */
|
|
if ((int)lseek(fd, PAGE_SIZE*16, SEEK_SET) < 0)
|
|
perror("lseek");
|
|
else
|
|
printf("lseek: Success.\n");
|
|
|
|
if (write(fd, &x, sizeof(x)) < 0)
|
|
perror("write");
|
|
else
|
|
printf("write: Success.\n");
|
|
|
|
printf("%s: Calling mmap()\n", __TASKNAME__);
|
|
if ((int)(base = mmap(0, PAGE_SIZE*16, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) < 0)
|
|
perror("mmap");
|
|
else
|
|
printf("mmap: Success: %p\n", base);
|
|
|
|
*(unsigned int *)(base + PAGE_SIZE*2) = 0x1000;
|
|
printf("%s: Calling msync()\n", __TASKNAME__);
|
|
if (msync(base + PAGE_SIZE*2, PAGE_SIZE, MS_SYNC) < 0)
|
|
perror("msync");
|
|
else
|
|
printf("msync: Success: %p\n", base);
|
|
|
|
printf("%s: Calling munmap()\n", __TASKNAME__);
|
|
if (munmap(base + PAGE_SIZE*2, PAGE_SIZE) < 0)
|
|
perror("munmap");
|
|
else
|
|
printf("munmap: Success: %p\n", base);
|
|
*(unsigned int *)(base + PAGE_SIZE*3) = 0x1000;
|
|
*(unsigned int *)(base + PAGE_SIZE*1) = 0x1000;
|
|
|
|
return 0;
|
|
}
|
|
|