semaphore

Semaphores and concurrent programming

Hello. For a homework assignment i need to program the following scenario. This is going to be done using semaphores using BACI (which is C--) There are 2 unisex restrooms that can hold 4 people each. Since it is unisex only people of the same sex can be in the restroom at the same time and FIFO is not important. I have the basic "al...

What situations can PHP semaphore be applied to?

I am wondering what would be a good situation to utilize semaphore in PHP or in general, trying to expand my horizons. ...

Mutual Exclusion and Semaphores

Hello. I am writing a program (for homework) that simulates a unisex bathroom. Only 4 people are allowed at a time and men and woman cannot enter if the other sex is already using the bathroom. My problem is with allowing a max of 4 people in the bathroom. As you can see from the output, only 1 person is getting into the restroom at ...

Testing a semaphore by counting.

There’s an article about semaphores on OS X. The author tests the semaphore by incrementing and decrementing a static variable in two threads. With semaphore guarding the variable access, the variable ends up being zero. Without the guard the variable ends up having some bogus value. I tried the code and it works. What I don’t understand...

single-lane bridge problem

If you're unfamiliar with the problem, it's something like this. I didn't come to ask for an answer, I have actually finished all of my coding. I have just found that my solution doesn't solve it the best way possible, because my solution only allows one car at a time on the bridge. I was hoping I could get some tips about how to go ab...

Semaphore and synchronization

I could not quite understand the following from the semaphore description in javadocs. Note that no synchronization lock is held when acquire() is called as that would prevent an item from being returned to the pool. The semaphore encapsulates the synchronization needed to restrict access to the pool, separately from any ...

Semaphore Syncing

I have two semaphores x (initially at 1) , and y (initially at 0). My thread function code is somewhat like this: ... wait(x); //setting some vars signal(x); wait(y); ... I want to ensure that the threads wait on y in line, ie. if the first thread completed the x-guarded section first, it should get to wait on y first, & so on....

Wait & Signal order

If the following pieces of code execute in the order in which I have put them, can I be sure that thread 1 is awoken first by thread 3, later followed by thread 2? main: sem_init(&x,0,0); thread 1: sem_wait(&x); thread 2: sem_wait(&x); thread 3: sem_post(&x); ...

Value of uninitialized semaphore

With reference to the semaphore implementation of semaphore.h in Linux, does an uninitialized semaphore sem_t default to 0? I just discovered that I had forgotten to initialize my semaphores to 0. Even so, the program worked fine without sem_init. (To all the purists, I completely agree that doing so is a bad practice.) ...

Possible Stack Corruption

With reference to my previous question about GDB not pinpointing the SIGSEGV point, My thread code is as follows: void *runner(void *unused) { do { sem_wait(&x); ... if(/*condition 1 check*/) { sem_post(&x); sleep(5); sem_wait(&x); if(/*repeat condition 1 check; after atleast 5 seconds*/) { printf("LEAVING....

What happens to a Thread that fails to acquire a Semaphore?

What happens when a thread cannot acquire a Semaphore (due to lack of permit). Will it be moved to the wait state? EDIT:Will the thread start resume the previous execution sequence, when the semaphore becomes available. ...

how to manage EINTR errno in sem_timedwait

Can you help me to understand why it is recommended to use: while ((s = sem_timedwait(&sem, &ts)) == -1 && errno == EINTR) continue; // Restart when interrupted by handler (EINTR: The call was interrupted by a signal handler) Instead of simply: s = sem_timedwait(&sem, &ts); In witch cases I have to manage EINTR ? ...

Producer-Consumer model - binary semaphore or mutex??

This is mainly about the understanding of the concept, which confuses me. Mutex means that one thread takes the control of the access of shared resource, performs operations and unlocks it, then only other thread can gain access to lock while binary semaphore is like a thread can gain access to the shared resource but gaining acces...

Exiting gracefully from a multithreaded process

I'm running a multi-threaded C program (process?) , making use of semaphores & pthreads. The threads keep interacting, blocking, waking & printing prompts on stdout continuously, without any human intervention. I want to be able to exit this process (gracefully after printing a message & putting down all threads, not via a crude CTRL+C S...

Cancelling a Blocked Pthread

How do I cause a thread to respond to pthread_cancel() if it is blocked on a sem_wait()? ...

When should we use mutex and when should we use semaphore

When should we use mutex and when should we use semaphore ? ...

Python How to share a serial port with two different threads (Class A, Class B)

Hello guys, thank your for reading. Well, I have a single python process which is using a serial port (unique resource) which is manage using an instance of a (A=Class A). Exist two different thread initialized using B=Class_B() and C=Class_C(), which are constantly using the serial port resource through the objected already created cal...

Multithreading: classical Producer Consumer algorithm

Hi all, Something I don't get about the classical algorithm for the Producer-Consumer problem (from Wikipedia:) semaphore mutex = 1 semaphore fillCount = 0 semaphore emptyCount = BUFFER_SIZE procedure producer() { while (true) { item = produceItem() down(emptyCount) down(mutex) putItemIn...