linux-sysprog · beginner · ~15 min

eventfd counter

Model the additive eventfd counter.

Challenge

An eventfd holds a 64-bit counter; each write adds its value, and a read returns (and resets) the accumulated total. Model that accumulation.

Task

Implement long eventfd_total(const long *adds, int n) that returns the sum of the n values written.

Input

  • adds: array of n values, each written to the eventfd.
  • n: number of writes (may be 0).

Output

The sum of the first n values (0 if n == 0).

Example

eventfd_total([1,2,3,4], 4)   ->   10
eventfd_total([1,2,3,4], 0)   ->   0

Edge cases

  • n == 0: return 0.

Input format

adds: array of n values written; n: write count (>= 0).

Output format

Sum of the n written values as a long.

Constraints

No writes means a total of 0.

Starter code

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.