-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp_parallel_sum.cpp
More file actions
61 lines (53 loc) · 1.56 KB
/
mp_parallel_sum.cpp
File metadata and controls
61 lines (53 loc) · 1.56 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
#include <cstddef>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <vector>
using namespace std;
/*
Problem:
Implement a fan-out / fan-in multiprocessing program that computes the sum of
an array in parallel using child processes and pipes.
Requirements:
1. Parent splits work across N child processes.
2. Each child computes a partial sum for its chunk.
3. Parent aggregates partial sums from pipes and waits for all children.
4. Keep the implementation and demo in a single file.
Follow-ups to practice:
- Handle very large payloads with shared memory instead of copying.
- Add a timeout and kill slow/hung workers.
- Return richer results (errors, stats) from children.
*/
bool writeExact(int fd, const void* buf, size_t len) {
(void)fd;
(void)buf;
(void)len;
// TODO:
// Loop until all bytes are written or an unrecoverable error occurs.
return false;
}
bool readExact(int fd, void* buf, size_t len) {
(void)fd;
(void)buf;
(void)len;
// TODO:
// Loop until all bytes are read, EOF is reached, or an error occurs.
return false;
}
long long sumChunk(const vector<int>& nums, size_t left, size_t right) {
(void)nums;
(void)left;
(void)right;
// TODO:
// Sum nums[left:right) and return partial sum.
return 0;
}
int main() {
// TODO:
// 1. Build input vector.
// 2. Create N pipes + fork N children.
// 3. Child computes chunk sum and writes to pipe.
// 4. Parent reads partial sums and aggregates.
// 5. waitpid() all children and validate result.
return 0;
}