semaphore

strict N process synchronization using 2 semaphores

老子叫甜甜 提交于 2019-12-23 03:58:07
问题 a few years ago I had an Operating Systems seminar. I had been tasked to create an algorithm for process synchronization using as few semaphores as possible. It should have looked like this: P1 -> P2 -> P3 -> P4 -> P5 P(n) - process Only one process running at a time and strict ordering was needed . Last year I came with solution using 3 semaphores (effectively creating a barrier). Here is my algorithm: P S1 S1 S1 S1 4W1 W0 W0 W0 W0 4S0 P S2 S2 S2 3W2 W1 W1 W1 3S1 P S1 S1 2W1 W0 W0 2S0 P S2

How can I control thread count when I use “Task.WhenAll”

感情迁移 提交于 2019-12-23 02:59:04
问题 I am verifying image urls by making an http get request asynchronously. All works fine with the code below but when I have so many Images, our firewall will block my internet access because of so many threads concurrently requesting. Therefore I was looking for a solution how to restrict the count of concurrently running threads. I ended up with this thread telling me to use SemaphoreSlim but I am somehow not able to get the idea and how to implement this? is that SemaphoreSlim wait or

Semaphore的使用及原理

风流意气都作罢 提交于 2019-12-22 17:16:29
前言 Semaphore也是JUC包中一个用于并发控制的工具类,举个常用场景的例子:有三台电脑五个人,每个人都要用电脑注册一个自己的账户,这时最开始只能同时有三个人操作电脑注册账户,这三个人中有人操作完了剩下的两个人才能占用电脑注册自己的账户。这就是Semaphore的经典使用场景,跟并发加锁有点像,只是我们的并发加锁同一时间只让有一个线程执行,而Semaphore的加锁控制是允许同一时间有指定数量的线程同时执行,超过这个数量就加锁控制。 一、使用样例 1 public static void main(String[] args) { 2 Semaphore semaphore = new Semaphore(3); // 对比上面例子中的3台电脑 3 for (int i = 0; i < 5; i++) { // 对比上面例子中的5个人 4 new Thread(() -> { 5 try { 6 semaphore.acquire(1); // 注意acquire中的值可以传任意值>=0的整数 7 } catch (InterruptedException e) { 8 e.printStackTrace(); 9 } 10 System.out.println(Thread.currentThread().getName() + " acquire 1"); 11 try

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

こ雲淡風輕ζ 提交于 2019-12-22 10:17:18
问题 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

dispatch_semaphore_wait does not wait on semaphore

萝らか妹 提交于 2019-12-22 09:49:39
问题 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);

Golang: How to timeout a semaphore?

坚强是说给别人听的谎言 提交于 2019-12-22 07:48:29
问题 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

What is the original meaning of P and V operations in a context of a semaphore?

送分小仙女□ 提交于 2019-12-22 03:42:53
问题 Does anybody know why semaphore operations are called P and V? Every time I read a chapter on semaphores it says something like the following: In order for the thread to obtain a resource it executes a P operation. And in order for the thread to release a resource it executes a V operation. What does P and V stand for? Why they are not called wait and signal? 回答1: Dijkstra, one of the inventors of semaphores, used P and V. The letters come from the Dutch words Probeer (try) and Verhoog

When to call sem_unlink()?

蓝咒 提交于 2019-12-22 02:01:02
问题 I'm a little confused by the Linux API sem_unlink(), mainly when or why to call it. I've used semaphores in Windows for many years. In Windows once you close the last handle of a named semaphore the system removes the underlying kernel object. But it appears in Linux you, the developer, needs to remove the kernel object by calling sem_unlink(). If you don't the kernel object persists in the /dev/shm folder. The problem I'm running into, if process A calls sem_unlink() while process B has the

How to implement a Binary Semaphore Class in Java?

泪湿孤枕 提交于 2019-12-21 23:05:10
问题 I can see how a "standard" Semaphore Class can be implemented in Java. However, I cant see how to implement a Binary Semaphore Class in Java. How does such implementation work? When should I call the wake and notify methods to wake and stop the threads that are on the semaphores? I understand what binary semaphores are, but I have no idea of how to code them. Edit Note: Realize that I said "BINARY" Semaphore class. The standard Semaphore class I already did and I know its correct so the

What happens when two processes are trying to access a critical section with semaphore = 0?

别来无恙 提交于 2019-12-21 21:27:10
问题 In my code I do the following initialization : struct PipeShm myPipe = { .init = 0 , .flag = FALSE , .mutex = NULL , .ptr1 = NULL , .ptr2 = NULL , .status1 = -10 , .status2 = -10 , .semaphoreFlag = FALSE }; int initPipe() { if (!myPipe.init) { myPipe.mutex = mmap (NULL, sizeof *myPipe.mutex, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (!sem_init (myPipe.mutex, 1, 0)) // semaphore is initialized to 0 { myPipe.init = TRUE; } else perror ("initPipe"); } return 1; // always