-
Notifications
You must be signed in to change notification settings - Fork 0
/
2021.11.21.c
103 lines (71 loc) · 2.09 KB
/
2021.11.21.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void *thread1(void *arg)
{
pthread_t newthid;
newthid = pthread_self();
printf("child thread1, thread ID = %ld\n", newthid);
int k = 20;
printf("线程1中变量k地址\t = %p\n", &k);
int size = 17 * 1024 * 1024;// 17M
void *ptr = malloc(size);
printf("线程1中申请堆空间地址\t = %p\n", ptr);
int i = 10;
printf("线程1中变量i地址\t = %p\n", &i);
sleep(3600);
return NULL;
}
void *thread2(void *arg)
{
pthread_t newthid;
newthid = pthread_self();
printf("child thread2, thread ID = %ld\n", newthid);
int k = 20;
printf("线程2中变量k地址\t = %p\n", &k);
int size = 10 * 1024 * 1024;// 10M
void *ptr = malloc(size);
printf("线程2中申请堆空间地址\t = %p\n", ptr);
int i = 10;
printf("线程2中变量i地址\t = %p\n", &i);
sleep(3600);
return NULL;
}
int main(void)
{
pthread_t tid1;
pthread_t tid2;
printf("main thread ,thread ID = %ld\n", pthread_self());
#if 1
if (pthread_create(&tid1, NULL, (void *)thread1, NULL) != 0) {
printf("thread creation failed\n");
exit(1);
}
if (pthread_create(&tid2, NULL, (void *)thread2, NULL) != 0) {
printf("thread creation failed\n");
exit(1);
}
#endif
sleep(3);
int k = 20;
printf("主线程中变量k地址\t = %p\n", &k);
int size = 14 * 1024 * 1024;//14M
void *ptr = malloc(size);
printf("主线程中申请堆空间地址\t = %p\n", ptr);
int i = 10;
printf("主线程中变量i地址\t = %p\n", &i);
sleep(3600);
return 0;
}
/*
线程1中变量k地址 = 0x7f994a692ecc
线程1中申请堆空间地址 = 0x7f9948591010
线程1中变量i地址 = 0x7f994a692ed0
线程2中变量k地址 = 0x7f9949e91ecc
线程2中申请堆空间地址 = 0x7f99435ff010
线程2中变量i地址 = 0x7f9949e91ed0
主线程中变量k地址 = 0x7ffd39c00834
主线程中申请堆空间地址 = 0x7f99427fe010
主线程中变量i地址 = 0x7ffd39c00838
* /