semaphore

Is there a built-in semaphore structure that allows for waiting on more than one resource?

落花浮王杯 提交于 2019-12-04 05:51:16
问题 I would like to be able wait/post more than one resource at a time. Is there a built-in c structure and interface that allows for this? Currently I am using semaphore.h however this interface has the limitation that it can only request a single resource at a time. I could do something like this: for (int i = 0; i < resources; i++) sem_wait(my_sem); But this would be time consuming if resources is large and I would also need to add another lock before this so that I am guaranteed that the

bathroom synchronization and queue of threads

核能气质少年 提交于 2019-12-04 05:24:58
问题 For homework we have been given the bathroom synchronization problem. I have been struggling trying to figure out how to start. What I would like to do when a person enter the restroom(personEnterRestrrom function), if they are female and no males are in the restroom they enter,if not they go into a queue for women waiting. I want to do the same for men. I tried to implement a queue that holds thread, but cannot get it to work. Then in personLeavesRestroom function. When a person leaves if no

C semaphores: sem_wait throwing inexplicable error

妖精的绣舞 提交于 2019-12-04 05:04:15
问题 I'm working on a problem which we have to use semaphores to solve. I have an array which contains two semaphores, gsem , and given certain conditions call sem_wait(&(gsem[me])) , which is supposed to waiting until that particular process is woken up. However, for some reason it gives me the error Bad file descriptor . I looked up sem_wait and the Open Group spec says this is not an error sem_wait can cause. This is making my whole program crazy and I have no idea why this is failing. EDIT:

How can I get multiple calls to sem_open working in C?

旧时模样 提交于 2019-12-04 03:37:20
I'm experiencing a lot of difficulty getting Semaphores to work on a Linux based system in C. The process of my application is such: Application starts Application forks into child/parent Each process uses sem_open with a common name to open the semaphore. If I create the semaphore before forking, it works fine. However, requirements prevent me from doing so. When I try to call sem_open for the second time, I get a "Permission Denied" error (via errno ). Is it possible to do this in any way? Or is there any way to open the semaphore in one process and used a shared memory mechanism to share it

Semaphore的使用

江枫思渺然 提交于 2019-12-04 02:28:15
1. Semaphore介绍: Semaphore (信号量)从概念上讲维护了一个许可集. 在获取acquire()到许可之后才能执行任务,每获取一次许可,许可数-1,在线程释放release()之后许可数+1。Semaphore就是根据许可数来控制线程并发执行的。为了更加形象说明Semaphore作用,这里举一个例子如下: 2. Semaphore的简单用法: Run.java package com.test.semaphore; /** * Created by famiover on 16/9/26. */ public class Run { public static void main(String[] args) { Service service = new Service(); for (int i = 0; i < 100; i++) { MyThread myThread = new MyThread(service); myThread.setName("Thread" + i); myThread.start(); } } } Mythread.java package com.test.semaphore; /** * Created by famiover on 16/9/26. */ public class MyThread extends

CountDownLatch、Semaphore等4大并发工具类详解

社会主义新天地 提交于 2019-12-04 02:01:30
Java并发工具包 1.并发工具类 提供了比synchronized更加高级的各种同步结构:包括CountDownLatch、CyclicBarrier、Semaphore等,可以实现更加丰富的多线程操作。 2.并发容器 提供各种线程安全的容器:最常见的ConcurrentHashMap、有序的ConcurrentSkipListMap,实现线程安全的动态数组CopyOnWriteArrayList等。 3.并发队列 各种BlockingQueue的实现:常用的ArrayBlockingQueue、SynchorousQueue或针对特定场景的PriorityBlockingQueue。 4.Executor框架 可以创建各种不同类型的线程池,调度任务运行等,绝大部分情况下,不再需要自己从头实现线程池和任务调度器。 Java常用的并发容器 1.ConcurrentHashMap 经常使用的并发容器,JDK 1.7和1.8的底层数据结构发生了变化(后续文章会详解),这里可以建议学习顺序如下:从Java7 HashMap -> Java7 ConcurrentHashMap -> Java8 HashMap -> Java8 ConcurrentHashMap,这样可以更好的掌握这个并发容器,毕竟都是从HashMap进化而来。 2.ConcurrentSkipListMap 在乎顺序

What are the practical uses of semaphores?

你离开我真会死。 提交于 2019-12-04 00:21:01
Nonbinary ones.. I have never encountered a problem that required me to use a semaphore instead of mutex. So is this mostly theoretical construct, or real sw like Office, Firefox have places where they use it? If so what are the common use patterns for semaphores? I think you already know what a semaphore is and you are just wondering how it is used on "real software". "real sw like Office, Firefox have places where they use it?" Yes, "real software" use semaphores a lot, it is not just theoretical, e.g. Chromium source, windows semaphore handling code and the same code used by Virtual Box .

Django: Simple rate limiting

那年仲夏 提交于 2019-12-03 19:28:24
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 I do this (easily)? Some thoughts : Use django cache Seems to only have 1 second resolution Use a file based semaphore Easy to do locks for concurrency. Not sure how to make sure only 3 fetches happen a second. Use some shared memory state I'd rather not install

Java并发编程中级篇(一):使用Semaphore信号量进行并发控制

我们两清 提交于 2019-12-03 16:28:44
Semaphore是一个二进制信号量,只有0和1两个值。如果线程想要访问一个共享资源,它必须先获得信号量。如果信号量的内部计数器大于0,那么信号量减1,并允许访问这个资源。否则,如果信号量计数器等于0,线程会等待直至计数器大于0。 所以说计数器大于0,说明有资源可用。计数器等于0,说明没有资源可用。 同时Semaphore提供了一个带有boolean参数的构造方法,true代表公平锁,false代表非公平锁,默认实现是非公平锁。 我们使用Semaphore信号量来重写PrintQueue的例子。 首先实现一个PrintQueue打印队列,有一个Semaphore信号量用来并发访问控制。打印之前使用acquire()方法获取信号量,执行完毕后使用release()方法释放信号量。每次打印等待一个随机时间,模拟打印耗时。 public class PrintQueue { private Semaphore semaphore; public PrintQueue() { //semaphore = new Semaphore(1); //非公平的 semaphore = new Semaphore(1, true); //公平的 } public void printJob(Object document) { try { semaphore.acquire(); long

GCD 信号量控制并发 (dispatch_semaphore)

邮差的信 提交于 2019-12-03 16:28:30
当我们在处理一系列线程的时候,当数量达到一定量,在以前我们可能会选择使用NSOperationQueue来处理并发控制,但如何在GCD中快速的控制并发呢?答案就是 dispatch_semaphore ,对经常做unix开发的人来讲,我所介绍的内容可能就显得非常入门级了,信号量在他们的多线程开发中再平常不过了。   信号量是一个整形值并且具有一个初始计数值,并且支持两个操作:信号通知和等待。当一个信号量被信号通知,其计数会被增加。当一个线程在一个信号量上等待时,线程会被阻塞(如果有必要的话),直至计数器大于零,然后线程会减少这个计数。   在GCD中有三个函数是semaphore的操作,分别是:   dispatch_semaphore_create   创建一个semaphore   dispatch_semaphore_signal   发送一个信号   dispatch_semaphore_wait    等待信号    简单的介绍一下这三个函数,第一个函数有一个整形的参数,我们可以理解为信号的总量,dispatch_semaphore_signal是发送一个信 号,自然会让信号总量加1,dispatch_semaphore_wait等待信号,当信号总量少于0的时候就会一直等待,否则就可以正常的执行,并让 信号总量-1,根据这样的原理