-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.c
206 lines (195 loc) · 4.6 KB
/
thread.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <pthread.h>
struct arguement
{
int op;
int ed;
};
struct shm_mem
{
int lock;
long and[4];
};
long accumulation(struct arguement* oped) {
long count = 0;
//printf("oped->op=%d\noped->ed=%d\n", oped->op, oped->ed);
int times = oped->op;
while (times <= oped->ed)
{
count += times;
times++;
}
//printf("%ld\n", count);
return count;
}
int main()
{
int shm_id; //共享内存标识符
struct shm_mem* shared;
void* shm = NULL;
shm_id = shmget(IPC_PRIVATE, 4 * sizeof(int), 0666);//创建共享内存
if (shm_id < 0) {
perror("shmget");
exit(1);
}
printf("successfully created segment : %d \n", shm_id);
system("ipcs -m"); /*调用ipcs命令查看IPC*/
shm = shmat(shm_id, (void*)0, 0);
if (shm == (void*)-1)
{
fprintf(stderr, "shmat fail\n");
}
printf("Memory attached at %x\n", (int)shm);
shared = (struct shm_mem*)shm;
shared->lock = 1;
pid_t pid = fork();
if (pid < 0) {
printf("vfork error");
}
else if (pid == 0) {
printf("this is child\n");
int status = 0;
long reason1, reason2, reason3, reason4;
int temp1, temp2, temp3, temp4;
struct arguement* arguement1;
struct arguement* arguement2;
struct arguement* arguement3;
struct arguement* arguement4;
pthread_t thread1, thread2, thread3, thread4;
arguement1 = (struct arguement*)malloc(sizeof(struct arguement));
arguement1->op = 1;
arguement1->ed = 25000;
//printf("%d,%d\n", arguement1->op, arguement1->ed);
status = pthread_create(&thread1, NULL, (void*)& accumulation, (void*)arguement1);
if (status != 0) {
printf("thread1 fails\n");
}
else {
printf("thread1 successful\n");
pthread_join(thread1, &reason1);
printf("thread1 return value is %ld\n", reason1);
while (1)
{
if (shared->lock == 1) {
shared->lock = 0;
printf("thread1 is writing\n");
shared->and [0] = reason1;
printf("thread1 writing over\n");
shared->lock = 1;
break;
}
else {
sleep(1);
}
}
}
arguement2 = (struct arguement*)malloc(sizeof(struct arguement));
arguement2->op = 25001;
arguement2->ed = 50000;
//printf("%d,%d\n", arguement2->op, arguement2->ed);
status = pthread_create(&thread2, NULL, (void*)& accumulation, (void*)arguement2);
if (status != 0) {
printf("thread2 fails\n");
}
else {
printf("thread2 successful\n");
pthread_join(thread2, &reason2);
printf("thread2 return value is %ld\n", reason2);
while (1)
{
if (shared->lock == 1) {
shared->lock = 0;
printf("thread2 is writing\n");
shared->and [1] = reason2;
printf("thread2 writing over\n");
shared->lock = 1;
break;
}
else {
sleep(1);
}
}
}
arguement3 = (struct arguement*)malloc(sizeof(struct arguement));
arguement3->op = 50001;
arguement3->ed = 75000;
//printf("%d,%d\n", arguement3->op, arguement3->ed);
status = pthread_create(&thread3, NULL, (void*)& accumulation, (void*)arguement3);
if (status != 0) {
printf("thread3 falis\n");
}
else {
printf("thread3 successful\n");
pthread_join(thread3, &reason3);
printf("thread3 return value is %ld\n", reason3);
while (1)
{
if (shared->lock == 1) {
shared->lock = 0;
printf("thread3 is writing\n");
shared->and [2] = reason3;
printf("thread3 writing over\n");
shared->lock = 1;
break;
}
else {
sleep(1);
}
}
}
arguement4 = (struct arguement*)malloc(sizeof(struct arguement));
arguement4->op = 75001;
arguement4->ed = 100000;
//printf("%d,%d\n", arguement4->op, arguement4->ed);
status = pthread_create(&thread4, NULL, (void*)& accumulation, (void*)arguement4);
if (status != 0) {
printf("线程4创建失败\n");
}
else {
printf("thread4 successful\n");
pthread_join(thread4, &reason4);
printf("thread4 return value is %ld\n", reason4);
while (1)
{
if (shared->lock == 1) {
shared->lock = 0;
printf("thread4 is writing\n");
shared->and [3] = reason4;
printf("thread4 writing over\n");
shared->lock = 1;
break;
}
else {
sleep(1);
}
}
}
shmdt(&shared);
}
else {
printf("this is father\n");
sleep(5);
unsigned long fin_reason = 0;
int i = 0;
for (; i < 4; i++) {
printf("and[%d]=%ld\n", i, shared-> and [i]);
fin_reason += shared-> and [i];
}
printf("1-100,000 add to %ld\n", fin_reason);
shmdt(&shared);
FILE* fp = NULL;
char name[20];
sprintf(name, "%ld", fin_reason);
printf("%s\n", name);
fp = fopen(name, "w+");
fclose(fp);
}
shmctl(shm_id, IPC_RMID, NULL);
return 0;
}