semaphore

Modification to “Implementing an N process barrier using semaphores”

南笙酒味 提交于 2019-12-05 20:34:32
Recently I see this problem which is pretty similar to First reader/writer problem. Implementing an N process barrier using semaphores I am trying to modify it to made sure that it can be reuse and work correctly. n = the number of threads count = 0 mutex = Semaphore(1) barrier = Semaphore(0) mutex.wait() count = count + 1 if (count == n){ barrier.signal()} mutex.signal() barrier.wait() mutex.wait() count=count-1 barrier.signal() if(count==0){ barrier.wait()} mutex.signal() Is this correct? I'm wondering if there exist some mistakes I didn't detect. Your pseudocode correctly returns barrier

Making binary semaphore shared between multiple processes(not threads , Process Only) using POSIX in C language

会有一股神秘感。 提交于 2019-12-05 20:30:58
i am making binary semaphore shared between multiple processes(not threads , Process Only) using POSIX in C language. if i create binary semaphore using mutex, typedef struct BIN_SEMA { pthread_cond_t cv; /* cond. variable - used to block threads */ pthread_mutex_t mutex; /* mutex variable - used to prevents concurrent access to the variable "flag" */ int flag; /* Semaphore state: 0 = down, 1 = up */ } bin_sema; i will be able to use it amongst the threads only , but i want to share between processes. so my question is, how to make binary semaphore using posix counting semaphores ? It's not

Single producer/consumer circular buffer which only blocks consumer

前提是你 提交于 2019-12-05 19:40:24
I'd like to implement a buffer with single producer and a single consumer, where only the consumer may be blocked. Important detail here is that the producer can drop the update if the queue is full. I've considered converting a wait-free implementation, but at first glance there seems to be no easy way to notify the consumer new data has arrived without losing notifications. So I settled on the very simple approach below, using a counting semaphore (some error handling details omitted for clarity): Object ar[SIZE]; int head = 0, tail = 0; sem_t semItems; // initialized to 0 void enqueue

dispatch_semaphore_wait does not wait on semaphore

江枫思渺然 提交于 2019-12-05 18:55:53
I have developed the following method, which checks the app's ability to communicate with the server. The method performs a simple query and knows that if it gets a result, the app should be connected (basic ping mechanism). - (BOOL)isAppConnected { __block BOOL isConnected = NO; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [[SFRestAPI sharedInstance] performSOQLQuery:@"SELECT id FROM Account LIMIT 1" failBlock:^(NSError *e) { isConnected = NO; NSLog(@"NOT CONNECTED %@", e); NSLog(@"fail block ON THE MAIN THREAD? %hhd", [NSThread isMainThread]); dispatch_semaphore_signal

Golang: How to timeout a semaphore?

余生长醉 提交于 2019-12-05 15:36:57
Semaphore in Golang is implemented with a channel: An example is this: https://sites.google.com/site/gopatterns/concurrency/semaphores Context: We have a few hundred servers and there are shared resources that we want to limit access to. So for a given resource, we want to use a semaphore to limit access to only 5 concurrent access by those servers. In order to do that, we are planning to use a lock server. When a machine accesses the resource, it will first register with the lock server that it is accessing the resource by a key. And then when it is done, it will send another request to the

named and unnamed posix semaphores

大憨熊 提交于 2019-12-05 11:22:33
Planning to use a posix semaphore to sync 2 processes. Not quite sure which to use - named or unnamed. What are the advantages and disadvantages of each? How do I decide which to use? On which situations, is one preferable over the other? Thanks. If the two processes are unrelated you should use a named semaphore. If the two process are related (i.e. forked) or if you are just using the semaphore between threads you should use unnamed. The advantages of unnamed are that you don't have to keep track of the names and any permissions nor unlink them. And unnamed semaphores can be use as a simple

Semop: When decreasing a set of semaphores are all decremented at once or does it block on first failure?

十年热恋 提交于 2019-12-05 11:12:49
So if I have a semaphore set semid with num_of_sems semaphores and a sembuf *deleter_searchers_down struct sembuf *deleter_searchers_down = malloc(sizeof (*deleter_searchers_down) * num_of_sems); for (i = 0; i < num_of_sems; ++i) { (deleter_searchers_down + i)->sem_op = -1; (deleter_searchers_down + i)->sem_num = i; (deleter_searchers_down + i)->sem_flg = SEM_UNDO; } semop(semid, deleter_searchers_down, num_of_sems); The call to semop will attempt to lower all semaphores in the set at once or will it block once it attempts to lower the first semaphore that is 0 and continue after some other

If mutual exclusion is guaranteed, say with semaphores, is a program deadlock-free?

江枫思渺然 提交于 2019-12-05 10:28:43
I define mutual exclusion and deadlock as below, respectively: The mutual exclusion condition exists if at every moment, each shared resource is either assigned to exactly one process, or available. A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause. Say, binary semaphores are used, ensuring that only one of them can enter its critical region at the same time. Since each process does a down just before entering its critical region and an up just after leaving it, mutual exclusion is guaranteed. I understand there

Lab7:同步互斥

。_饼干妹妹 提交于 2019-12-05 07:30:37
并发进程的正确性 独立进程 不和其他进程共享资源或状态 确定性 -> 输入状态决定结果 可重现 -> 能够重现起始条件 调度顺序不重要 并发进程 在多个进程间有资源共享 不确定性 不可重现 并发进程的正确性 执行过程是不确定性和不可重现的 程序错误可能是间歇性发生的 并发的好处 共享资源 加速 模块化 同步问题 时间 A B 3:00 查看冰箱,没有面包 3:05 离开家去商店 3:10 到达商店 查看冰箱,没有面包了 3:15 购买面包 离开家去商店 3:20 到家,把面包放进冰箱 到达商店 3:25 购买面包 3:30 到家,把面包放进冰箱 解决 利用两个原子操作实现一个锁(lock) Lock.Acquire() 在锁被释放前一直等待,然后获得锁 如果两个线程都在等待同一个锁,并且同时发现锁被释放了,那么只有一个能够获得锁 breadlock.Acquire(); //进入临界区 if (nobread) { buy bread; //临界区 } breadlock.Release(); //退出临界区 进程的交互关系:相互感知程度 相互感知的程度 交互关系 进程间的影响 相互不感知(完全不了解其它进程的存在) 独立 一个进程的操作对其他进程的结果无影响 间接感知(双方都与第三方交互,如共享资源) 通过共享进行协作 一个进程的结果依赖于共享资源的状态 直接感知(双方直接交互

dispatch_semaphore_t reuse - What am I missing here?

余生颓废 提交于 2019-12-05 03:46:54
I have some code where I am using dispatch_semaphore_t to signal operation completion. When the semaphore is a member variable, it does not seem to behave correctly. I will show example code that works and an example that does not seem to work: @implementation someClass { dispatch_semaphore_t memberSem; dispatch_semaphore_t* semPtr; NSThread* worker; BOOL taskDone; } - (id)init { // Set up the worker thread and launch it - not shown here. memberSem= dispatch_semaphore_create(0); semPtr= NULL; taskDone= FALSE; } - (void)dealloc { // Clean up the worker thread as needed - not shown here. if(