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