semaphore

How do I recover a semaphore when the process that decremented it to zero crashes?

瘦欲@ 提交于 2019-11-27 11:39:17
I have multiple apps compiled with g++, running in Ubuntu. I'm using named semaphores to co-ordinate between different processes. All works fine except in the following situation: If one of the processes calls sem_wait() or sem_timedwait() to decrement the semaphore and then crashes or is killed -9 before it gets a chance to call sem_post() , then from that moment on, the named semaphore is "unusable". By "unusable", what I mean is the semaphore count is now zero, and the process that should have incremented it back to 1 has died or been killed. I cannot find a sem_*() API that might tell me

Producer Consumer program using semaphores and pthreads

大兔子大兔子 提交于 2019-11-27 11:00:06
问题 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

Differences between System V and Posix semaphores

天涯浪子 提交于 2019-11-27 10:53:16
What are the trade-offs between using a System V and a Posix semaphore? From O'Reilly : One marked difference between the System V and POSIX semaphore implementations is that in System V you can control how much the semaphore count can be increased or decreased; whereas in POSIX, the semaphore count is increased and decreased by 1. POSIX semaphores do not allow manipulation of semaphore permissions, whereas System V semaphores allow you to change the permissions of semaphores to a subset of the original permission. Initialization and creation of semaphores is atomic (from the user's

How do I choose between Semaphore and SemaphoreSlim?

旧时模样 提交于 2019-11-27 10:25:37
问题 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? 回答1: 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

pthread_cond_wait versus semaphore

送分小仙女□ 提交于 2019-11-27 09:04:35
问题 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 ? 回答1: A semaphore is

信号量

本秂侑毒 提交于 2019-11-27 08:29:39
# 信号量可能在不同的领域中 对应不同的知识点 """ 互斥锁:一个厕所(一个坑位) 信号量:公共厕所(多个坑位) """ from threading import Semaphore,Thread import time import random sm = Semaphore(5) # 造了一个含有五个的坑位的公共厕所 def task(name): sm.acquire() print('%s占了一个坑位'%name) time.sleep(random.randint(1,3)) sm.release() for i in range(40): t = Thread(target=task,args=(i,)) t.start() 来源: https://www.cnblogs.com/wkq0220/p/11354498.html

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

天涯浪子 提交于 2019-11-27 08:19:27
问题 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

信号量

时光怂恿深爱的人放手 提交于 2019-11-27 08:09:09
# 信号量可能在不同领域对应不同的知识点 ''' 互斥锁:代表一个坑位 信号量:代表一个公共厕所,有多个坑位 ''' from threading import Semaphore,Thread import time import random sm = Semaphore(4) # 制造一个有五个坑位的厕所 def task(name): sm.acquire() print('%s 抢到了坑位' % name) time.sleep(random.randint(1, 3)) sm.release() if __name__ == '__main__': for i in range(20): t = Thread(target=task, args=(i,)) t.start() 来源: https://www.cnblogs.com/asdaa/p/11353319.html

C & low-level semaphore implementation

穿精又带淫゛_ 提交于 2019-11-27 03:33:26
问题 I was thinking about how to implement semaphores (not binary) using less asm code as possible. I haven't succeeded in thinking and writing it without using a mutex, so here's the best I could do till now: Global: #include <stdlib.h> #include <pthread.h> #include <stdatomic.h> #include <stdbool.h> typedef struct { atomic_ullong value; pthread_mutex_t *lock_op; bool ready; } semaphore_t; typedef struct { atomic_ullong value; pthread_mutex_t lock_op; bool ready; } static_semaphore_t; /* use with

Semaphore的简介及应用场景

非 Y 不嫁゛ 提交于 2019-11-27 03:16:34
Semaphore 是一个计数信号量,常用于限制可以访问某些资源(物理或逻辑的)线程数目。 常用函数: 信号量的构造函数 非公平: public Semaphore(int permits);//permits就是允许同时运行的线程数目 公平(获得锁的顺序与线程启动顺序有关): public Semaphore(int permits,boolean fair);//permits就是允许同时运行的线程数目 创建一个信号量 Semaphore semaphore = new Semaphore(2); 从信号量中获取一个许可 semaphore.acquire(); 释放一个许可(在释放许可之前,必须先获获得许可。) semaphore.release(); 尝试获取一个许可,若获取成功返回true,若获取失败返回false semaphore.tryAcquire(); 所有函数: // 创建具有给定的许可数和非公平的公平设置的 Semaphore。 Semaphore(int permits) // 创建具有给定的许可数和给定的公平设置的 Semaphore。 Semaphore(int permits, boolean fair) // 从此信号量获取一个许可,在提供一个许可前一直将线程阻塞,否则线程被中断。 void acquire() // 从此信号量获取给定数目的许可