linux-sysprog · beginner · ~15 min
Compute a balanced work split (ceil division).
When you split work across worker threads, the largest worker's load is the ceiling of items divided by workers. Compute that chunk size.
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).
items: total number of items.workers: number of workers.ceil(items / workers), or 0 if workers <= 0.
chunks_needed(10, 5) -> 2
chunks_needed(10, 3) -> 4 (ceiling)
chunks_needed(5, 0) -> 0
workers <= 0: return 0 (avoid divide-by-zero).items: total items; workers: worker count.
Ceiling of items / workers, or 0 if workers <= 0.
Guard workers <= 0.
int chunks_needed(int items, int workers) {
/* TODO */
return 0;
}
Checking errno without first verifying function returned failure; neglecting to handle EINTR in retry loops.
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.