OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [test0/] [src/] [shmtest.c] - Blame information for rev 6

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Test shmget/shmat/shmdt posix calls.
3
 *
4
 * Copyright (C) 2007 - 2008 Bahadir Balban
5
 */
6
#include <sys/ipc.h>
7
#include <sys/shm.h>
8
#include <sys/types.h>
9
#include <fcntl.h>
10
#include <stdio.h>
11
#include <tests.h>
12
#include <unistd.h>
13
#include <errno.h>
14
 
15
int shmtest(void)
16
{
17
        //key_t keys[2] = { 5, 10000 };
18
        key_t keys[2] = { 2, 3 };
19
        void *bases[2] = { 0 , 0 };
20
        int shmids[2];
21
 
22
        test_printf("Initiating shmget()\n");
23
        for (int i = 0; i < 2; i++) {
24
                if ((shmids[i] = shmget(keys[i], 27, IPC_CREAT | 0666)) < 0) {
25
                        test_printf("SHMGET", errno);
26
                        goto out_err;
27
                } else
28
                        test_printf("SHMID returned: %d\n", shmids[i]);
29
        }
30
        test_printf("Now shmat()\n");
31
        for (int i = 0; i < 2; i++) {
32
                if ((int)(bases[i] = shmat(shmids[i], NULL, 0)) == -1) {
33
                        test_printf("SHMAT", errno);
34
                        goto out_err;
35
                } else
36
                        test_printf("SHM base address returned: %p\n", bases[i]);
37
        }
38
        /* Write to the bases */
39
        *((unsigned int *)bases[0]) = 0xDEADBEEF;
40
        *((unsigned int *)bases[1]) = 0xFEEDBEEF;
41
 
42
        test_printf("Now shmdt()\n");
43
        for (int i = 0; i < 2; i++) {
44
                if (shmdt(bases[i]) < 0) {
45
                        test_printf("SHMDT", errno);
46
                        goto out_err;
47
                } else
48
                        test_printf("SHM detached OK.\n");
49
        }
50
        test_printf("Now shmat() again\n");
51
        for (int i = 0; i < 2; i++) {
52
                bases[i] = shmat(shmids[i], NULL, 0);
53
 
54
                /* SHMAT should fail since no refs were left in last detach */
55
                if ((int)bases[i] != -1) {
56
                        test_printf("SHM base address returned: %p, "
57
                                    "but it should have failed\n", bases[i]);
58
                        goto out_err;
59
                }
60
        }
61
 
62
        if (getpid() == parent_of_all)
63
                printf("SHM TEST            -- PASSED --\n");
64
 
65
        return 0;
66
 
67
out_err:
68
        printf("SHM TEST            -- FAILED --\n");
69
        return 0;
70
 
71
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.