semaphore

Shared memory access control mechanism for processes created by MPI

守給你的承諾、 提交于 2020-01-11 04:15:30
问题 I have a shared memory used by multiple processes, these processes are created using MPI . Now I need a mechanism to control the access of this shared memory. I know that named semaphore and flock mechanisms can be used to do this but just wanted to know if MPI provides any special locking mechanism for shared memory usage ? I am working on C under Linux. 回答1: MPI actually does provide support for shared memory now (as of version 3.0). You might try looking at the One-sided communication

信号量Semaphore,读写锁ReadWriteLock,计数器CountDownLatch,循环栅栏CyclicBarrier运用

不羁的心 提交于 2020-01-10 06:20:53
1.信号量Semaphore: 像synchronized, ReentrantLock等这些对临界区资源进行同步后,所有对临界区资源进行访问的线程都得串行排队,而信号量允许指定的线程数同时进行访问 demo: import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; public class TestSemaphore implements Runnable{ private static Semaphore semaphore = new Semaphore(5); //允许同时5个线程进行访问 不会阻塞 @Override public void run(){ try { semaphore.acquire(); System.out.println(System.currentTimeMillis() +" "+ Thread.currentThread().getName()); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }finally { semaphore.release(

无聊的哲学家进餐问题

不问归期 提交于 2020-01-10 05:47:57
1 描述 一张圆桌上坐着5名哲学家,每两个哲学家之间的桌上摆一根筷子,桌子的中间是一碗米饭,如图所示。哲学家们倾注毕生精力用于思考和进餐,哲学家在思考时,并不影响他人。只有当哲学家饥饿的时候,才试图拿起左、右两根筷子(一根一根地拿起)。如果筷子已在他人手上,则需等待。饥饿的哲学家只有同时拿到了两根筷子才可以开始进餐,当进餐完毕后,放下筷子继续思考。 2 分析 1) 关系分析。 5名哲学家与左右邻居对其中间筷子的访问是互斥关系。 2) 整理思路。 显然这里有五个进程。本题的关键是如何让一个哲学家拿到左右两个筷子而不造成死锁或者饥饿现象。那么解决方法有两个,一个是让他们同时拿两个筷子;二是对每个哲学家的动作制定规则,避免饥饿或者死锁现象的发生。 3) 信号量设置。 定义互斥信号量数组Ch0PstiCk[5] = {l, 1, 1, 1, 1}用于对5个筷子的互斥访问。对哲学家按顺序从0~4编号,哲学家i左边的筷子的编号为i,哲学家右边的筷子的编号为(i+l)%5。 semaphore chopstick[5] = {1,1,1,1,1}; //定义信号量数组chopstick[5],并初始化 Pi(){ //i号哲学家的进程 while(1){ P(chopstick[i]); //取左边筷子 P(chopstick[(i+1)%5]); //取右边篌子 eat; V

线程同步机制的区别与比较及进程通信方法

非 Y 不嫁゛ 提交于 2020-01-07 12:59:21
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 有关多线程的一些技术问题: 1、 何时使用多线程? 2、 线程如何同步? 3、 线程之间如何通讯? 4、 进程之间如何通讯? 先来回答第一个问题,线程实际主要应用于四个主要领域,当然各个领域之间不是绝对孤立的,他们有可能是重叠的,但是每个程序应该都可以归于某个领域: 1、 offloading time-consuming task。由辅助线程来执行耗时计算,而使GUI有更好的反应。我想这应该是我们考虑使用线程最多的一种情况吧。 2、 Scalability。服务器软件最常考虑的问题,在程序中产生多个线程,每个线程做一份小的工作,使每个CPU都忙碌,使CPU(一般是多个)有最佳的使用率,达到负载的均衡,这比较复杂,我想以后再讨论这个问题。 3、 Fair-share resource allocation。当你向一个负荷沉重的服务器发出请求,多少时间才能获得服务。一个服务器不能同时为太多的请求服务,必须有一个请求的最大个数,而且有时候对某些请求要优先处理,这是线程优先级干的活了。 4、 Simulations。线程用于仿真测试。 我把主要的目光放在第一个领域,因为它正是我想要的。第二和第三个领域比较有意思,但是目前不在我的研究时间表中。 线程的同步机制: 1、 Event 用事件(Event

How to explain semaphore processing operation?

主宰稳场 提交于 2020-01-07 04:10:27
问题 Next is a simple semaphore implementation. public class Semaphore { private boolean signal = false; public synchronized void take() { this.signal = true; this.notify(); } public synchronized void release() throws InterruptedException { while (!this.signal) wait(); this.signal = false; } } Is it true, that by calling take() semaphore imitates signal acquisition and wakes up randomly chosen thread (if it actually exists) and by calling release(), if signal was not acquired, semaphore forces

Unexpected Output Running Semaphore [duplicate]

Deadly 提交于 2020-01-06 18:05:26
问题 This question already has an answer here : Unexpected Output Running Semaphore (1 answer) Closed 6 years ago . The first process shouldn't start its (i)th iteration unless the second process has finished its (i-1)th iteration.The output is not what I need.I wonder if it is possible to have the output by only two semaphores? Here is my code. #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <semaphore.h> sem_t semA, semB,sem,m; int

How to revoke SEM_UNDO of latest operation on semaphore?

孤街醉人 提交于 2020-01-06 08:36:32
问题 My problem: A,B - semaphores. Process is loop: waits for A >= 1, decrements A does some computation increments B. back to 1. Goal: After process terminates i need decrements of A be equal to increments of B. So i figure out i will add flag SEM_UNDO in 1. but it undos every DEcrement ever made to A by that process. How to revoke / undo SEM_UNDO from 1. when process reaches 4.? 回答1: ok, i was enlightened when thinking about problem as "undoing" SEM_UNDO rather then "revoking" it. after calling

php multithreading problem [closed]

感情迁移 提交于 2020-01-06 05:22:07
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I am writing a php cron job that reads thousands of feeds / web pages using curl and stores the content in a database. How do I

synchronization between processes using unnamed semaphores

雨燕双飞 提交于 2020-01-04 05:55:44
问题 In process-1 I am trying to write the data into shared memory. At the same time in process-2 I am reading the data from the same shared memory. in this case I need to provide synchronization between these two processes. if I will go through unnamed semaphores (using shm_init(),mmap() ),will it work or not? I have written code like this will it work or not? fd = shm_open("shm_name", O_CREAT| O_RDWR, S_IRUSR | S_IWUSR); sema = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE,MAP_SHARED , fd, 0)

C- Semaphore sem_getvalue not returning what I'm expecting?

此生再无相见时 提交于 2020-01-04 01:52:12
问题 I've been trying to get myself more acquainted with semaphores and was wondering why this code isn't printing the value I expect. #include <semaphore.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { sem_t sem; sem_init(&sem, 0, 1); int value; sem_getvalue(&sem, &value); printf("%d\n",value); return 0; } It prints 0 for the value. But from my understanding it should be getting the value I initialized the semaphore with which is 1? I tried using a semaphore in some