semaphore

Producer Consumer program using semaphores and pthreads

∥☆過路亽.° 提交于 2019-11-28 18:02:47
I have written a code for producer-consumer problem.But I am not getting the output.There is no compilation error,but warning in my program.I am confused.Trying very hard.But can't get it.Please tell me what is wrong in my program.What will be the correct program.I am getting frustrated.Please help guys. Here is the code- #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include </usr/include/semaphore.h> #define BUFF_SIZE 5 /* total number of slots */ #define NP 3 /* total number of producers */ #define NC 3 /* total number of consumers */ #define NITERS 4 /* number of items

Binary Semaphore vs a ReentrantLock

☆樱花仙子☆ 提交于 2019-11-28 17:29:47
I've been trying to understand Reentrant locks and Semaphores ( the nesting of Reentrant locks vs release/unlock mechanism ). It seems that having a Semaphore requires you to write a more thoroughly tested application because the release() method does not check if the thread releasing the permit is actually holding it. When I tested my test code, I found out that this may subsequently increase the number of permits beyond the initial limit. On the other hand, if a thread is not holding a reentrant lock when it invokes the unlock method, we get an IllegalMonitorException. So would it be right

How do I choose between Semaphore and SemaphoreSlim?

♀尐吖头ヾ 提交于 2019-11-28 17:20:55
Their public interfaces appear similar. The documentation states that the SemaphoreSlim is a lightweight alternative and doesn't use Windows Kernel semaphores. This resource states that the SemaphoreSlim is much faster. In what situations does the SemaphoreSlim make more sense over the Semaphore and vice versa? Andrew Barber One difference is that SemaphoreSlim does not permit named semaphores, which can be system-wide. This would mean that a SemaphoreSlim could not be used for cross-process synchronization. The MSDN documentation also indicates that SemSlim should be used when "wait times are

In what situation do you use a semaphore over a mutex in C++?

痴心易碎 提交于 2019-11-28 16:50:59
Throughout the resources I've read about multithreading, mutex is more often used and discussed compared to a semaphore. My question is when do you use a semaphore over a mutex? I don't see semaphores in Boost thread. Does that mean semaphores no longer used much these days? As far as I've understand, semaphores allow a resource to be shared by several threads. This is only possible if those threads are only reading the resource but not writing. Is this correct? Boost.Thread has mutexes and condition variables. Purely in terms of functionality, semaphores are therefore redundant[*], although I

Delete all SYSTEM V shared memory and semaphores on UNIX-like systems

混江龙づ霸主 提交于 2019-11-28 16:43:56
问题 How can I delete all not used semaphores and shared memory with a single command on a UNIX-like system, e.g., Ubuntu? 回答1: Here, save and try this script (kill_ipcs.sh) on your shell: #!/bin/bash ME=`whoami` IPCS_S=`ipcs -s | egrep "0x[0-9a-f]+ [0-9]+" | grep $ME | cut -f2 -d" "` IPCS_M=`ipcs -m | egrep "0x[0-9a-f]+ [0-9]+" | grep $ME | cut -f2 -d" "` IPCS_Q=`ipcs -q | egrep "0x[0-9a-f]+ [0-9]+" | grep $ME | cut -f2 -d" "` for id in $IPCS_M; do ipcrm -m $id; done for id in $IPCS_S; do ipcrm

Need to understand the usage of SemaphoreSlim

落花浮王杯 提交于 2019-11-28 15:55:15
问题 Here is the code I have but I don't understand what SemaphoreSlim is doing. async Task WorkerMainAsync() { SemaphoreSlim ss = new SemaphoreSlim(10); List<Task> trackedTasks = new List<Task>(); while (DoMore()) { await ss.WaitAsync(); trackedTasks.Add(Task.Run(() => { DoPollingThenWorkAsync(); ss.Release(); })); } await Task.WhenAll(trackedTasks); } void DoPollingThenWorkAsync() { var msg = Poll(); if (msg != null) { Thread.Sleep(2000); // process the long running CPU-bound job } } What does

Difference between Mutex, Semaphore & Spin Locks

家住魔仙堡 提交于 2019-11-28 15:21:30
I am doing experiments with IPC, especially with Mutex, Semaphore and Spin Lock. What I learnt is Mutex is used for Asynchronous Locking (with sleeping (as per theories I read on NET)) Mechanism, Semaphore are Synchronous Locking (with Signaling and Sleeping) Mechanism, and Spin Locks are Synchronous but Non-sleeping Mechanism. Can anyone help me to clarify these stuff deeply? And another doubt is about Mutex, when I wrote program with thread & mutex, while one thread is running another thread is not in Sleep state but it continuously tries to acquire the Lock. So Mutex is sleeping or Non

pthread_cond_wait versus semaphore

别说谁变了你拦得住时间么 提交于 2019-11-28 15:12:45
What are the pros / cons of using pthread_cond_wait or using a semaphore ? I am waiting for a state change like this : pthread_mutex_lock(&cam->video_lock); while(cam->status == WAIT_DISPLAY) { pthread_cond_wait(&cam->video_cond, &cam->video_lock); } pthread_mutex_unlock(&cam->video_lock); Using a properly initialised semaphore, I think I could do it like this : while(cam->status == WAIT_DISPLAY) { sem_wait(&some_semaphore); } What are the pros and cons of each method ? A semaphore is suited cleanly to a producer-consumer model, although it has other uses. Your program logic is responsible for

Wait for completion block of writeImageToSavedPhotosAlbum by semaphore

て烟熏妆下的殇ゞ 提交于 2019-11-28 14:36:11
In my app I open the camera by a picker and after the photo has been taken I'd like to safe it by the following method the assets library. The method freezes after the call of the writeImageToSavedPhotosAlbum. Without the semaphores the methods work perfectly. But than I miss to receive the assetURL. + (NSURL*)safeImageToAssetsLibrary:(UIImage *)image metadata:(NSDictionary *)metadata { ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; __block NSURL *retAssestURL = nil; dispatch_semaphore_t semaWaitingForSafeImage = dispatch_semaphore_create(0); dispatch_queue_t queue = dispatch_get

Creating a method to perform animations and wait for completion using a semaphore in objective c

回眸只為那壹抹淺笑 提交于 2019-11-28 14:16:55
I am trying to create a method which makes use of UIView's "+animateWithDuration:animations:completion" method to perform animations, and wait for completion. I am well aware that I could just place the code that would normally come after it in a completion block, but I would like to avoid this because there is a substantial amount of code after it including more animations, which would leave me with nested blocks. I tried to implement this method as below using a semaphore, but I don't think this is the best way to do it, especially because it doesn't actually work. Can anyone tell me what is