08.07.2015 Views

Topic Notes: Process Synchronization - Courses

Topic Notes: Process Synchronization - Courses

Topic Notes: Process Synchronization - Courses

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CS 322 Operating Systems Spring 2008As the name suggests, this is a spin lock. But aren’t we supposed to be avoiding these? Let’s seewhat this is all about. It is a macro. It actually ends up calling a function spinlock defined in/usr/src/lib/libc r/uthread/uthread spinlock.cWhile we cannot obtain the lock with an atomic test and set operation, we yield the CPU and tryagain. Note that the atomic test operation, defined in /usr/src/lib/libc r/arch/i386/atomic lock.S, uses the xchg instruction on the x86. This instruction is an atomic swapof the value in a register with the values in a memory location. Note that each architecture hassomething implemented in assembly that will provide equivalent functionality.OK, so back in the mutex lock common() function, we have exclusive access to the mutexdata structure. We can now see if it’s locked (if it has a non-NULL owner). If not, we take it. If so,we add ourself to the list of waiters. The thread kern sched state unlock() functionthen unlocks the mutex, and we change our state to PS MUTEX WAIT, so we will not be scheduleduntil the mutex is unlocked.When it comes time to unlock, we end up in the mutex unlock common() function. Webasically get the lock on the mutex (spinlock). If there are threads waiting on the lock, we pick thenext one, set its state to PS RUNNING and do some other bookkeeping so it can run. If no threadwas waiting, the assignment inside the if statement sets the owner to NULL, unlocking the mutexfor the next lock attempt.So what about the condition variable?This is implemented in /usr/src/lib/libc r/uthread/uthread cond.c.Initialization is straightforward - not much of interest here.pthread cond wait() is where the interesting functionality lives. Once we see that the callis valid, the current thread is added to the condition variable’s waiting queue, and given a “neverwake up” status, set its state to PS COND WAIT.pthread cond signal() selects a thread that is waiting on the condition variable and wakesit up by setting its state to PS RUNNING.SysV SemaphoresFreeBSD also implements System V semaphores.SysV (along with BSD) was one of the two major “flavors” of Unix.Most Unix systems were either BSD-based (SunOS up to 4.x, Ultrix) or SysV-based (Irix, Solaris).Despite being (as you may have guessed) BSD-based, FreeBSD relies on a semaphore implementationfrom a long, long time ago from SysV.These are described in great detail in Bach.They are allocated in groups, stored in arrays.See Example:/cluster/examples/prodcons-sysvsemaphore23

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!