mirror of
https://github.com/drasko/codezero.git
synced 2026-01-23 16:23:16 +01:00
Added a shared memory test.
- libposix error printing now configurable via macro definition - shmget/at/dt tested.
This commit is contained in:
@@ -22,9 +22,14 @@ void wait_pager(l4id_t partner)
|
||||
// printf("Pager synced with us.\n");
|
||||
}
|
||||
|
||||
pid_t parent_of_all;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
printf("\n%s: Started with thread id %d\n", __TASKNAME__, self_tid());
|
||||
|
||||
printf("\n%s: Started with thread id %d\n", __TASKNAME__, getpid());
|
||||
|
||||
parent_of_all = getpid();
|
||||
|
||||
wait_pager(0);
|
||||
|
||||
@@ -34,6 +39,8 @@ void main(void)
|
||||
|
||||
mmaptest();
|
||||
|
||||
shmtest();
|
||||
|
||||
forktest();
|
||||
|
||||
fileio();
|
||||
|
||||
@@ -10,13 +10,11 @@
|
||||
|
||||
int global = 0;
|
||||
|
||||
pid_t parent_of_all;
|
||||
|
||||
int forktest(void)
|
||||
{
|
||||
pid_t myid;
|
||||
|
||||
parent_of_all = getpid();
|
||||
|
||||
/* 16 forks */
|
||||
for (int i = 0; i < 4; i++)
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <tests.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int shmtest(void)
|
||||
{
|
||||
@@ -17,35 +18,53 @@ int shmtest(void)
|
||||
void *bases[2] = { 0 , 0 };
|
||||
int shmids[2];
|
||||
|
||||
printf("Initiating shmget()\n");
|
||||
test_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");
|
||||
test_printf("SHMGET", errno);
|
||||
goto out_err;
|
||||
} else
|
||||
printf("SHMID returned: %d\n", shmids[i]);
|
||||
test_printf("SHMID returned: %d\n", shmids[i]);
|
||||
}
|
||||
printf("Now shmat()\n");
|
||||
test_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]);
|
||||
if ((int)(bases[i] = shmat(shmids[i], NULL, 0)) == -1) {
|
||||
test_printf("SHMAT", errno);
|
||||
goto out_err;
|
||||
} else
|
||||
test_printf("SHM base address returned: %p\n", bases[i]);
|
||||
}
|
||||
printf("Now shmdt()\n");
|
||||
/* Write to the bases */
|
||||
*((unsigned int *)bases[0]) = 0xDEADBEEF;
|
||||
*((unsigned int *)bases[1]) = 0xFEEDBEEF;
|
||||
|
||||
test_printf("Now shmdt()\n");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (shmdt(bases[i]) < 0)
|
||||
perror("SHMDT");
|
||||
else
|
||||
printf("SHM detached OK.\n");
|
||||
if (shmdt(bases[i]) < 0) {
|
||||
test_printf("SHMDT", errno);
|
||||
goto out_err;
|
||||
} else
|
||||
test_printf("SHM detached OK.\n");
|
||||
}
|
||||
printf("Now shmat() again\n");
|
||||
test_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]);
|
||||
bases[i] = shmat(shmids[i], NULL, 0);
|
||||
|
||||
/* SHMAT should fail since no refs were left in last detach */
|
||||
if ((int)bases[i] != -1) {
|
||||
test_printf("SHM base address returned: %p, "
|
||||
"but it should have failed\n", bases[i]);
|
||||
goto out_err;
|
||||
}
|
||||
}
|
||||
|
||||
if (getpid() == parent_of_all)
|
||||
printf("SHM TEST -- PASSED --\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out_err:
|
||||
printf("SHM TEST -- FAILED --\n");
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user