linux-sysprog · beginner · ~15 min

Chunks needed for workers

Compute a balanced work split (ceil division).

Challenge

When you split work across worker threads, the largest worker's load is the ceiling of items divided by workers. Compute that chunk size.

Task

Implement int chunks_needed(int items, int workers) that returns how many items each worker handles when items are split as evenly as possible (ceiling division).

Input

  • items: total number of items.
  • workers: number of workers.

Output

ceil(items / workers), or 0 if workers <= 0.

Example

chunks_needed(10, 5)   ->   2
chunks_needed(10, 3)   ->   4   (ceiling)
chunks_needed(5, 0)    ->   0

Edge cases

  • workers <= 0: return 0 (avoid divide-by-zero).

Input format

items: total items; workers: worker count.

Output format

Ceiling of items / workers, or 0 if workers <= 0.

Constraints

Guard workers <= 0.

Starter code

int chunks_needed(int items, int workers) {
    /* TODO */
    return 0;
}

Common mistakes

Checking errno without first verifying function returned failure; neglecting to handle EINTR in retry loops.

Edge cases to handle

Interrupted system calls (EINTR); resource exhaustion (ENOMEM, EMFILE); permission denied errors (EACCES).

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.