semaphore

How does semaphore work?

Hello, Can the semaphore be lower than 0? I mean, say I have a semaphore with N=3 and I call "down" 4 times, then N will remain 0 but one process will be blocked? And same the other way, if in the beginning I call up, can N be higher than 3? Because as I see it, if N can be higher than 3 if in the beginning I call up couple of times, t...

How to know which processes open/use a specific semaphore?

We have a semaphore opened by 281 processes, is there any way to get all the pids of these processes? ipcs -a|grep 67108878 s 67108878 0xcef73014 --ra-ra---- oracle dba oracle dba 281 17:54:58 9:27:30 ...

Correctly destroying named System V semaphores

I'm using named System V semaphores to lock a file across all my apps on OSX and linux. Not prettiest of APIs by any definition. It seems to work, but I can't quite figure out how to properly destroy the semaphore after everybody is done with it. General logic is like this: Creating: [1] Thread or process tries to open a semaphore se...

sem_init(...): What is the pshared parameter for?

In a graduate class, we've had to use semaphores to accomplish work with threads. We were directed to use sem_init along with a bunch of other sem_* procedure but we were not given much information about the details of each of these sem_* methods. The prototype (and header file) of sem_init is the following: #include <semaphore.h> i...

Multi-process synchronization - better choice than semaphores?

I've got a queue resource that is shared across multiple producers and multiple consumers. All are independent processes; no one process "owns" the queue. By nature of the implementation access to the queue must be controlled and only one process must be allowed to push or pop at any given moment. I figured using a POSIX named semapho...

Django: Simple rate limiting

Many of my views fetch external resources. I want to make sure that under heavy load I don't blow up the remote sites (and/or get banned). I only have 1 crawler so having a central lock will work fine. So the details: I want to allow at most 3 queries to a host per second, and have the rest block for a maximum of 15 seconds. How could ...

UNIX/OSX version of semtimedop

GLibC has a method semtimedop which allows you to perform an operation (a semaphore acquire in this case) which times out after a certain amount of time. Win32 also provides WaitForSingleObject which provides similar functionalty. As far as I can see there is no equivalent on OSX or other Unices. Can you suggest either the equivalent fo...

Calling a function in child thread in Qt?

I have a main thread that invokes a child thread function at different times but I am not sure whether that is right way of doing it in Qt.What is wrong with the below code and looking for better alternative There is a main thread running infinitly when ever main thread releases the lock child does a piece of work. #include <QtCore/QC...

How to display the process currently holding a semaphore?

In userspace Linux, I have a process blocking on a semaphore, as found by strace. Once the error condition occurs, the blocking is repeatable, so there must be another process that holds the semaphore and did not release it. Is there a way to know which other process is currently holding the semaphore? ipcs lists the semaphore, so does...

sem_init on OS X

I am working on some code which uses the pthread and semaphore libraries. The sem_init function works fine on my ubuntu machine, but on OS X the sem_init function has absolutely no effect. Is there something wrong with the library or is there a different way of doing it? This is the code I am using to test. sem_t sem1; sem_t sem2; sem_t...

Semaphore timeout mechanism in C#

Does anyone know how .NET handles a timeout on a call to Semaphore.WaitOne(timeout)? I'd expect a TimeoutException, but the MSDN documentation doesn't list this in the list of expected exceptions, and I can't seem to find it documented anywhere. Thanks in advance! ...

Setting Access permissions on Semaphore?

I am assuming that once a semaphore is created by a process, it will be accessible by any process/user. Is it possible to put access restrictions on a particular semaphore so that it can be accessible by only certain processes/users or only certain processes can release the semaphore. I see some problems if we make a semaphore accessibl...

What is the Mutex and semaphore In c#? where we need to implement?

What is the Mutex and semaphore In c#? where we need to implement? How can we work with them in multithreading? ...

C semaphores and a "barrier" between threads

I'm trying to solve a problem our Operating Systems professor showed us from a previous exam to prepare for the next one. The problem is to have two threads that execute concurrently and may complete in a different amount of time. After a particular thread is completed, it needs to block until the other thread is completed, then they ma...

FIFO semaphore test

Hello everyone, I have implemented FIFO semaphores but now I need a way to test/prove that they are working properly. A simple test would be to create some threads that try to wait on a semaphore and then print a message with a number and if the numbers are in order it should be FIFO, but this is not good enough to prove it because that ...

Semaphore controlled resources - what is the clean shutdown sequence/pattern

If i control a pool of resources with a semaphore, what is the clean shutdown sequence for this resource pool? class ResourcePool { Semaphore resourceSemaphore; Stack<ResourceClass> resources; public ResourcePool() { resources ... // Init some Resources in the stack resourceSemaphore= new Semaphore(resou...

Multiprocess Synchronization with a Single Semaphore

We're covering multithreaded programming in a class I'm taking. The professor offered a bonus question that I have been trying, to no avail, to figure out: Each of processes P0, P1, P2 and P3 have to wait for the other three to cross or reach a particular synchronization point in their code, and only then may that process cross its own ...

Are "benaphores" worth implementing on modern OS's?

Hi all, Back in my days as a BeOS programmer, I read this article by Benoit Schillings, describing how to create a "benaphore": a method of using atomic variable to enforce a critical section that avoids the need acquire/release a mutex in the common (no-contention) case. I thought that was rather clever, and it seems like you could ...

Blackberry Semaphore class

Hello, I can't seem to find anything equivalent to a Semaphore in the Blackberry Java Reference. What am I missing? java.util.concurrent isn't even there. Thanks! Sean ...

Multi-threaded BASH programming - generalized method?

Ok, I was running POV-Ray on all the demos, but POV's still single-threaded and wouldn't utilize more than one core. So, I started thinking about a solution in BASH. I wrote a general function that takes a list of commands and runs them in the designated number of sub-shells. This actually works but I don't like the way it handles acc...