mirror of
https://github.com/drasko/codezero.git
synced 2026-01-14 20:03:16 +01:00
Initial commit
This commit is contained in:
60
tasks/test1/src/mmaptest.c
Normal file
60
tasks/test1/src/mmaptest.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <tests.h>
|
||||
|
||||
#define PAGE_SIZE 0x1000
|
||||
|
||||
int mmaptest(void)
|
||||
{
|
||||
int fd;
|
||||
void *base;
|
||||
int x = 0x1000;
|
||||
|
||||
if ((fd = open("./newfile.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");
|
||||
|
||||
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;
|
||||
if (msync(base + PAGE_SIZE*2, PAGE_SIZE, MS_SYNC) < 0)
|
||||
perror("msync");
|
||||
else
|
||||
printf("msync: Success: %p\n", base);
|
||||
|
||||
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;
|
||||
}
|
||||
60
tasks/test1/src/mmaptest.c.orig
Normal file
60
tasks/test1/src/mmaptest.c.orig
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <tests.h>
|
||||
|
||||
#define PAGE_SIZE 0x1000
|
||||
|
||||
int mmaptest(void)
|
||||
{
|
||||
int fd;
|
||||
void *base;
|
||||
int x = 0x1000;
|
||||
|
||||
if ((fd = open("./newfile.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");
|
||||
|
||||
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;
|
||||
if (msync(base + PAGE_SIZE*2, PAGE_SIZE, MS_SYNC) < 0)
|
||||
perror("msync");
|
||||
else
|
||||
printf("msync: Success: %p\n", base);
|
||||
|
||||
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;
|
||||
}
|
||||
51
tasks/test1/src/shmtest.c
Normal file
51
tasks/test1/src/shmtest.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Test shmget/shmat/shmdt posix calls.
|
||||
*
|
||||
* Copyright (C) 2007 - 2008 Bahadir Balban
|
||||
*/
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <tests.h>
|
||||
|
||||
int shmtest(void)
|
||||
{
|
||||
//key_t keys[2] = { 5, 10000 };
|
||||
key_t keys[2] = { 2, 3 };
|
||||
void *bases[2] = { 0 , 0 };
|
||||
int shmids[2];
|
||||
|
||||
printf("Initiating shmget()\n");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if ((shmids[i] = shmget(keys[i], 27, IPC_CREAT | 0666)) < 0) {
|
||||
printf("Call failed.\n");
|
||||
perror("SHMGET");
|
||||
} else
|
||||
printf("SHMID returned: %d\n", shmids[i]);
|
||||
}
|
||||
printf("Now shmat()\n");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if ((int)(bases[i] = shmat(shmids[i], NULL, 0)) == -1)
|
||||
perror("SHMAT");
|
||||
else
|
||||
printf("SHM base address returned: %p\n", bases[i]);
|
||||
}
|
||||
printf("Now shmdt()\n");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (shmdt(bases[i]) < 0)
|
||||
perror("SHMDT");
|
||||
else
|
||||
printf("SHM detached OK.\n");
|
||||
}
|
||||
printf("Now shmat() again\n");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if ((int)(bases[i] = shmat(shmids[i], NULL, 0)) == -1)
|
||||
perror("SHMAT");
|
||||
else
|
||||
printf("SHM base address returned: %p\n", bases[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user