A few notes on the rise of multi-core processors and Moore's Law Wikipedia transistor count chart Used to be each CPU was a separate unit These days, many per physical die The count has been increasing, too So we're expected to make use of more than one! Division of labor: Specific roles for each CPU Do you have as many roles as CPUs? Separate threads for each of something you have a lot of Example: We have 100 files, and will use a thread on each of them Example: The server will spawn for each of our 100 clients Example: We need to find matches for a genetic sequence in each of 10,000 genomes This kind of problem is often solved by a computing cluster Division of a large problem between many processors, where each does exactly the same thing Example: Applying an image filter This is the kind of thing a GPU is good at "exactly the same thing" means "run the same short program" Map/Reduce: What is the average color in the video? Summarize each frame, then add up the sums In an OS: Generally speaking, avoid large processing problems in the OS The OS is supposed to be lightweight Otherwise it's like a heavy paddle, or lead saddle, or a fancy truck So most concurrency problems will have to do with multiple invocations from userspace Imagine 10 programs call read at the same time... Concurrency and Lock Types: atomic operation spinlock semaphore mutex Atomic Operation: atomic = can't be split people used to think that Atomic Operation Example: incrementation x = x + 2 is NOT atomic atomic_inc is Sometimes called "test and set" These are in hardware for many architectures! Not in pthreads GCC built-in atomic operations Find a random example Spinlock: Works like a mutex, but "busy waits" Imagine a 4 year old waiting for the bathroom DEFINE_SPINLOCK(lockname) spin_lock(&lockname) spin_unlock(&lockname) NOT recursive We'll try that later Not now because it will probably crash the computer Why? Time to do two context switches Imagine you go away and do something else for a while pthread_spin_init, etc find a random example Semaphore: Allows a number of users Seattle freeway signals V and P operations increase and decrease, in Dutch, kind of (Wikipedia) Can be decreased and increased by different threads! General case of a mutex Mutex is like a semaphore that only allows one use Except that only the original thread can unlock struct semaphore and sema_init sem_overview in section 7 for POSIX find a random example Mutex: Like we saw in pthreads yesterday Added later than semaphores Why? Semaphores are more complex Mutex supports recursive locking These are favored over semaphores when appropriate mutex_lock, etc. find a random example The Big Kernel Lock Introduced with SMP Removed in 2011 with version 2.6.39 What happens when we recursively try to acquire a spinlock?