Implementing Semaphores, locks and condition variables

http://www.cs.jhu.edu/~yairamir/cs418/os3/sld019.htm

 

http://stackoverflow.com/questions/938309/implementing-semaphores-locks-and-condition-variables

 

 Semaphores, locks, condition variables etc. are operating system concepts and must typically be implemented in terms of features of the operating system kernel. It is therefore not generally possible to study them in isolation - you need to consider the kernel code too. Probably the best way of doing this is to take a look at the Linux Kernel, with the help of a book such as Understandiong The Linux Kernel.

 

Semaphore at the very simplest is just a counter you can add and subtract from with a single atomic operation. Wikipedia has an easy to understand explanation that pretty much covers your question about them:

http://en.wikipedia.org/wiki/Semaphore_(programming)

The minix stuff is pretty good. A simpler example is the MicroC/OS stuff. It comes with a textbook that goes into good detail, all the source is there. It has the basic elements there and the code is small enough that you can understand it in a relatively short period of time.

http://www.micrium.com/products/rtos/kernel/rtos.html

http://en.wikipedia.org/wiki/MicroC/OS-II

Another thing you can do, is make a faked out OS in an application on linux. I did this by setting up the basic tick with an itimer, then swapping threads around with the function call swapcontext (man 2 swapcontext) which will save the regs on the stack. That gets the ugly stuff out of the way and you are left to implement the semaphores/mutexes/timers and all that. It was quite fun.

Despite what some of the posts say, assembler is not required. A knowledge of it will always help. It never hurts to understand how the internals/complilers/etc work when you are writing even high level applications.

 

转载于:https://www.cnblogs.com/cutepig/archive/2010/08/31/1813298.html

你可能感兴趣的:(Implementing Semaphores, locks and condition variables)