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

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [test0/] [src/] [clonetest.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
 * Clone test.
3
 */
4
#include <stdio.h>
5
#include <unistd.h>
6
#include <sys/types.h>
7
#include <sys/mman.h>
8
#include <sched.h>
9
#include <errno.h>
10
#include <tests.h>
11
 
12
int clone_global = 0;
13
 
14
extern pid_t parent_of_all;
15
 
16
int my_thread_func(void *arg)
17
{
18
        for (int i = 0; i < 25; i++)
19
                clone_global++;
20
        _exit(0);
21
}
22
 
23
int clonetest(void)
24
{
25
        pid_t childid;
26
        void *child_stack;
27
 
28
        /* Parent loops and calls clone() to clone new threads. Children don't come back from the clone() call */
29
        for (int i = 0; i < 20; i++) {
30
                if ((child_stack = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_GROWSDOWN, 0, 0)) == MAP_FAILED) {
31
                        test_printf("MMAP failed.\n");
32
                        goto out_err;
33
                } else {
34
                        test_printf("Mapped area starting at %p\n", child_stack);
35
                }
36
                // printf("mmap returned child stack: %p\n", child_stack);
37
 
38
                // ((int *)child_stack)[-1] = 5; /* Test mapped area */
39
 
40
                test_printf("Cloning...\n");
41
 
42
                if ((childid = clone(my_thread_func, child_stack,
43
                     CLONE_PARENT | CLONE_FS | CLONE_VM | CLONE_THREAD | CLONE_SIGHAND, 0)) < 0) {
44
                        test_printf("CLONE failed.\n");
45
                        goto out_err;
46
                } else {
47
                        test_printf("Cloned a new thread with child pid %d\n", childid);
48
                }
49
        }
50
 
51
        /* TODO: Add wait() or something similar and check that global is 100 */
52
 
53
        if (getpid() == parent_of_all)
54
                printf("CLONE TEST          -- PASSED --\n");
55
 
56
        return 0;
57
out_err:
58
        printf("CLONE TEST          -- FAILED --\n");
59
        return 0;
60
}
61
 

powered by: WebSVN 2.1.0

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