semaphore

How does a single thread does the work of multiple threads?

一笑奈何 提交于 2020-01-03 05:13:11
问题 I have a program that is representing a car making factory. A queue holds the car parts to be made, each number represents a different part of the car, ex. 4 is window, we need 7 windows: queue(^: front, *: rear): [0^, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 7*] num_workers represent the number of workers who can work parallely(threads). This is the part of the main where I call the thread function. int num_work = num_cars * NUM_PARTS; int k=0; while(k<num_work){ for(int i=0;i<num

Multiple threads can wait on a semaphore at same time

风格不统一 提交于 2020-01-03 03:32:10
问题 Can multiple threads wait on a single semaphore ? If yes, upon semaphore down which one will be resumed ? 回答1: As the document for sem_post suggests, If the value of the semaphore resulting from this operation is zero, then one of the threads blocked waiting for the semaphore will be allowed to return successfully from its call to sem_wait() . If the symbol _POSIX_PRIORITY_SCHEDULING is defined, the thread to be unblocked will be chosen in a manner appropriate to the scheduling policies and

操作系统-同步互斥

£可爱£侵袭症+ 提交于 2020-01-03 01:15:59
并发性:互斥和同步 基本概念 原子操作: 一个函数或动作由一个或多个指令的序列实现,对外是不可见的;保证指令的序列要么作为一个组执行, 要么都不执行,对系统状态没有可见的影响。 保证了并发的隔离。 临界区:一段代码,在这段代码中进程将访问共享资源,当另一个进程已经在这段代码中运行时,这个进程就不能在这段代码中运行。 临界资源:虽然多个进程可以共享系统中的各种资源,但其中许多资源一次只能为一个进程所使用,我们把一次只允许一个进程使用的资源成为临界资源。包括许多的物理设备如打印机,以及许多的变量和数据。 死锁:两个或两个以上的进程因其中的每个进程都在等待其他进程做完某些事情而不能继续执行。 活锁:两个或两个以上进程为了响应其他进程中的变化而持续改变自己的状态但不做有用的工作。 互斥:当一个进程在临界区访问共享资源的时候,其他进程不能进入该临界区访问任何共享资源。 同步:为完成某种任务而建立的两个或多个进程,这些进程因为需要在某些位置上协调它们的工作次序而等待、传递消息所产生的制约关系。进程间的直接制约关系就是源于他们之间的相互合作。 竞争条件:多个线程或者进程在读写一个共享资源时,结果依赖于它们执行的相对时间,这种情形称为竞争条件 饥饿: 一个可运行的进程尽管能继续执行,但被调度程序无限期地忽视,而不能被调度执行的情形。 忙等待/自旋等待: 进程在得到临界区访问权之前

What is the difference between semaphore and mutex in implementation?

点点圈 提交于 2020-01-02 05:45:08
问题 I read that mutex and binary semaphore are different in only one aspect, in the case of mutex the locking thread has to unlock, but in semaphore the locking and unlocking thread can be different? Which one is more efficient? 回答1: Assuming you know the basic differences between a sempahore and mutex : For fast, simple synchronization, use a critical section. To synchronize threads across process boundaries, use mutexes. To synchronize access to limited resources, use a semaphore. Apart from

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

若如初见. 提交于 2020-01-02 05:04:42
问题 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

possibility of starvation of Dining Philosophers

本小妞迷上赌 提交于 2019-12-31 10:42:12
问题 I need to check my algorithm of solving the dining philosopher problem if it guarantees that all of the following are satisfied or not: No possibility of deadlock. No possibility of starvation. I am using the semaphore on the chopsticks to solve the problem. Here is my code (the algorithm): while(true) { // He is Hungry pickup_chopsticks(i); // He is Eating... drop_chopsticks(i); // He is thinking } // ... void pickup_chopsticks(int i) { if(i % 2 == 0) /* Even number: Left, then right */ {

possibility of starvation of Dining Philosophers

。_饼干妹妹 提交于 2019-12-31 10:42:03
问题 I need to check my algorithm of solving the dining philosopher problem if it guarantees that all of the following are satisfied or not: No possibility of deadlock. No possibility of starvation. I am using the semaphore on the chopsticks to solve the problem. Here is my code (the algorithm): while(true) { // He is Hungry pickup_chopsticks(i); // He is Eating... drop_chopsticks(i); // He is thinking } // ... void pickup_chopsticks(int i) { if(i % 2 == 0) /* Even number: Left, then right */ {

How can I get the UI thread to wait on a semaphore, but process additional dispatcher requests? (like what MessageBox.Show does natively)

天大地大妈咪最大 提交于 2019-12-31 05:42:12
问题 Normally, when the UI thread calls something like MessageBox.Show() , the current code execution doesn't continue until the user clicks OK, but the program will continue to run other code dispatched on the UI thread. In this question, I had a problem with too many delegates dispatched on the UI Thread being called at once. I wanted to pause at certain points before continuing execution. In my new Error handler, I use semaphores to make sure no more than one error is being handled at once. I

java 并发工具类Semaphore用法

与世无争的帅哥 提交于 2019-12-30 23:13:51
Semaphore(int permits,boolean fair):构造方法,当fair等于true时,创建具有给定许可数的计数信号量并设置为公平信号量。 int availablePermits():返回此信号量中当前可用的许可数。 void acquire():表示从此信号量获取一个许可,并将可用许可数减1。如果当前信号量没有许可可获取,线程将被阻塞。 void release():用于释放一个许可,并将其返回给信号量,同时将可用许可数加1。 import java.util.Random; import java.util.concurrent.Semaphore; /** * Semaphore的使用 * 场景:模拟一个厕所只有2个坑位,有5个人(编号1-5)先后来上厕所,采用先来先上的策略。 */ public class UseSemaphore { private static class UseRunner implements Runnable{ private String name; private Semaphore semaphore; public UseRunner(String name, Semaphore semaphore){ this.name = name; this.semaphore = semaphore; } @Override

Undefined Reference issues using Semaphores

好久不见. 提交于 2019-12-30 02:07:04
问题 I am playing around with using Semaphores, but I keep encountering Undefined Reference warnings, thus causing my code not to work. I pulled example code from a text, but was having issues with some of their syntax, so I went to POSIX's semaphore tutorial and changed things around to their syntax and as a result am now getting these reference errors. I may simply be overlooking something, but I cannot find it. Errors: Producers_Consumers.c:52: warning: return type of ‘main’ is not ‘int’ /tmp