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