Implementing an N process barrier using semaphores

This is well presented in The Little Book of Semaphores. n = the number of threads count = 0 mutex = Semaphore(1) barrier = Semaphore(0) mutex.wait() count = count + 1 mutex.signal() if count == n: barrier.signal() # unblock ONE thread barrier.wait() barrier.signal() # once we are unblocked, it’s our duty to unblock the next … Read more

Why use bzero over memset?

I don’t see any reason to prefer bzero over memset. memset is a standard C function while bzero has never been a C standard function. The rationale is probably because you can achieve exactly the same functionality using memset function. Now regarding efficiency, compilers like gcc use builtin implementations for memset which switch to a … Read more

Programmatically get the cache line size?

On Linux (with a reasonably recent kernel), you can get this information out of /sys: /sys/devices/system/cpu/cpu0/cache/ This directory has a subdirectory for each level of cache. Each of those directories contains the following files: coherency_line_size level number_of_sets physical_line_partition shared_cpu_list shared_cpu_map size type ways_of_associativity This gives you more information about the cache then you’d ever hope … Read more