linux-sysprog · beginner · ~15 min
Model the additive eventfd counter.
An eventfd holds a 64-bit counter; each write adds its value, and a read returns (and resets) the accumulated total. Model that accumulation.
Implement long eventfd_total(const long *adds, int n) that returns the sum of the n values written.
adds: array of n values, each written to the eventfd.n: number of writes (may be 0).The sum of the first n values (0 if n == 0).
eventfd_total([1,2,3,4], 4) -> 10
eventfd_total([1,2,3,4], 0) -> 0
n == 0: return 0.adds: array of n values written; n: write count (>= 0).
Sum of the n written values as a long.
No writes means a total of 0.
long eventfd_total(const long *adds, int n) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.