root / tests / testclone.c @ 564c8f99
History | View | Annotate | Download (1.4 kB)
1 | 1b6b029e | bellard | #include <stdlib.h> |
---|---|---|---|
2 | 1b6b029e | bellard | #include <stdio.h> |
3 | 7fb9a24e | bellard | #include <string.h> |
4 | 1b6b029e | bellard | #include <signal.h> |
5 | 1b6b029e | bellard | #include <unistd.h> |
6 | 1b6b029e | bellard | #include <inttypes.h> |
7 | 1b6b029e | bellard | #include <pthread.h> |
8 | 1b6b029e | bellard | #include <sys/wait.h> |
9 | 1b6b029e | bellard | #include <sched.h> |
10 | 1b6b029e | bellard | |
11 | 1b6b029e | bellard | int thread1_func(void *arg) |
12 | 1b6b029e | bellard | { |
13 | 1b6b029e | bellard | int i;
|
14 | 1b6b029e | bellard | char buf[512]; |
15 | 1b6b029e | bellard | |
16 | 1b6b029e | bellard | for(i=0;i<10;i++) { |
17 | 1b6b029e | bellard | snprintf(buf, sizeof(buf), "thread1: %d %s\n", i, (char *)arg); |
18 | 1b6b029e | bellard | write(1, buf, strlen(buf));
|
19 | 1b6b029e | bellard | usleep(100 * 1000); |
20 | 1b6b029e | bellard | } |
21 | 1b6b029e | bellard | return 0; |
22 | 1b6b029e | bellard | } |
23 | 1b6b029e | bellard | |
24 | 1b6b029e | bellard | int thread2_func(void *arg) |
25 | 1b6b029e | bellard | { |
26 | 1b6b029e | bellard | int i;
|
27 | 1b6b029e | bellard | char buf[512]; |
28 | 1b6b029e | bellard | for(i=0;i<20;i++) { |
29 | 1b6b029e | bellard | snprintf(buf, sizeof(buf), "thread2: %d %s\n", i, (char *)arg); |
30 | 1b6b029e | bellard | write(1, buf, strlen(buf));
|
31 | 1b6b029e | bellard | usleep(120 * 1000); |
32 | 1b6b029e | bellard | } |
33 | 1b6b029e | bellard | return 0; |
34 | 1b6b029e | bellard | } |
35 | 1b6b029e | bellard | |
36 | 1b6b029e | bellard | #define STACK_SIZE 16384 |
37 | 1b6b029e | bellard | |
38 | 1b6b029e | bellard | void test_clone(void) |
39 | 1b6b029e | bellard | { |
40 | 1b6b029e | bellard | uint8_t *stack1, *stack2; |
41 | 1b6b029e | bellard | int pid1, pid2, status1, status2;
|
42 | 1b6b029e | bellard | |
43 | 1b6b029e | bellard | stack1 = malloc(STACK_SIZE); |
44 | 1b6b029e | bellard | pid1 = clone(thread1_func, stack1 + STACK_SIZE, |
45 | 1b6b029e | bellard | CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello1");
|
46 | 1b6b029e | bellard | |
47 | 1b6b029e | bellard | stack2 = malloc(STACK_SIZE); |
48 | 1b6b029e | bellard | pid2 = clone(thread2_func, stack2 + STACK_SIZE, |
49 | 1b6b029e | bellard | CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello2");
|
50 | 1b6b029e | bellard | |
51 | 1b6b029e | bellard | while (waitpid(pid1, &status1, 0) != pid1); |
52 | 1b6b029e | bellard | while (waitpid(pid2, &status2, 0) != pid2); |
53 | 1b6b029e | bellard | printf("status1=0x%x\n", status1);
|
54 | 1b6b029e | bellard | printf("status2=0x%x\n", status2);
|
55 | 1b6b029e | bellard | printf("End of clone test.\n");
|
56 | 1b6b029e | bellard | } |
57 | 1b6b029e | bellard | |
58 | 1b6b029e | bellard | int main(int argc, char **argv) |
59 | 1b6b029e | bellard | { |
60 | 1b6b029e | bellard | test_clone(); |
61 | 1b6b029e | bellard | return 0; |
62 | 1b6b029e | bellard | } |