Repository navigation
Expand file tree
/
Copy pathWeek3_Linux_IntFile.cpp
More file actions
83 lines (72 loc) · 2.01 KB
/
Copy pathWeek3_Linux_IntFile.cpp
File metadata and controls
83 lines (72 loc) · 2.01 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>
// 共享内存结构
struct shared_data {
int val1;
int val2;
};
int main() {
int shmid;
struct shared_data *shm;
pid_t pidB, pidC;
// 创建共享内存
shmid = shmget(IPC_PRIVATE, sizeof(struct shared_data), IPC_CREAT | 0666);
if (shmid == -1) {
perror("共享内存创建失败");
exit(1);
}
// 连接到共享内存
shm = (struct shared_data *) shmat(shmid, NULL, 0);
if (shm == (struct shared_data *) -1) {
perror("共享内存连接失败");
exit(1);
}
// 初始化共享变量
shm->val1 = 0;
shm->val2 = 0;
// 创建进程B
if ((pidB = fork()) == 0) {
// 进程B: 消费者,加法
while (1) {
sleep(2); // 等待2秒
if (shm->val1 < 0) break; // 检查终止条件
printf("B: Sum is %d\n", shm->val1 + shm->val2);
}
shmdt(shm); // 解除共享内存映射
exit(0);
} else if (pidB < 0) {
perror("进程B创建失败");
exit(1);
}
// 创建进程C
if ((pidC = fork()) == 0) {
// 进程C: 消费者,乘法
while (1) {
sleep(3); // 等待3秒
if (shm->val1 < 0) break; // 检查终止条件
printf("C: Product is %d\n", shm->val1 * shm->val2);
}
shmdt(shm); // 解除共享内存映射
exit(0);
} else if (pidC < 0) {
perror("进程C创建失败");
exit(1);
}
// 进程A: 生产者,获取用户输入
while (1) {
printf("Enter two integers (negative first to quit): ");
scanf("%d %d", &shm->val1, &shm->val2);
if (shm->val1 < 0) break; // 检查终止条件
}
// 等待进程B和C终止
wait(NULL);
wait(NULL);
// 清理共享内存
shmdt(shm); // 解除共享内存映射
shmctl(shmid, IPC_RMID, NULL); // 删除共享内存
return 0;
}