-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp_process_supervisor.cpp
More file actions
44 lines (38 loc) · 1.17 KB
/
mp_process_supervisor.cpp
File metadata and controls
44 lines (38 loc) · 1.17 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
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
/*
Problem:
Implement a simple process supervisor that restarts a worker process if it
crashes, using exponential backoff and a max restart budget.
Requirements:
1. Parent process supervises one child worker.
2. Worker failure is detected via exit code / signal.
3. Parent restarts failed worker with backoff.
4. Parent stops after success or after max restarts are exhausted.
5. Keep the implementation and demo in a single file.
Follow-ups to practice:
- Supervise multiple worker types.
- Add heartbeats over pipes to detect hangs.
- Add rolling restart and graceful shutdown.
*/
int runWorker(int attempt) {
(void)attempt;
// TODO:
// Simulate worker behavior. Return 0 for success, non-zero for failure.
return 0;
}
int calculateBackoffMs(int restart_count) {
(void)restart_count;
// TODO:
// Implement exponential backoff with a max cap.
return 0;
}
int main() {
// TODO:
// 1. Loop: fork worker.
// 2. Child calls runWorker() and exits with returned code.
// 3. Parent waitpid()s and inspects status.
// 4. Restart on failure until budget is exhausted.
return 0;
}