around this issue What does it mean by code holding semaphore can be preempted? It means that a process that is currently running in its critical section holding some lock for the purpose of synchronization can be preempted. Ideally interrupts have the highest priority, so unless you disable the interrupt on that processor core, the running process can be preempted and that might happen while the process is in its critical section.
will be helpful for those in need Semaphores aren't mutexes. You don't "hold" them. If the process is blocked, that means it's waiting for someone else to do an "up" or "V" operation on it in the future. There's no kernel tool that will tell you what the future behavior of software will be.
POSIX semaphore: Why come the parent process acquire semaphore before child releases it?
I wish this help you After a fork() there is no guarantee that the child runs before the parent, and I am pretty sure it is usually the parent process that continues execution on the OS scheduler after the fork(). The semaphore has to be in shared memory, as suggested by VoidPointer and QWR, and is stated in the man page:
it fixes the issue A Semaphore has a certain number of permits available. If acquire(int permits) is called, the given number of permits are taken from the semaphore. If a semaphore does not have enough permits, the method will block/suspend the calling thread until enough permits are available. If release(int permits) is called on the semaphore, the given number of permits are given back to the semaphore. If threads are waiting for permits (due to blocking acquire calls), they are woken up and try to gain their requested number of permits.
.NET 4.5 Semaphore WaitOne(0) doesn't block, but decrements semaphore count
I hope this helps you . Correct, it does not only test the semaphore state, a better wording would be
//Only call release when WaitOne returns true, also put it in a finally
//block to make sure it always gets called.
//Do something else because the resource was not available.
How to force a piece of code inside a thread to run uninterruptedly (keep it from being preempted by the OS)
This might help you No. You are running your code on preemptive OS so there is nothing in your power to prevent preemption. What is your real problem? Try to describe the actual problem you have, why did you come up with this requirement, and then perhaps one can offer advice to your actual problem..