semaphore

How do I recover a semaphore when the process that decremented it to zero crashes?

痴心易碎 提交于 2019-11-26 15:43:32
问题 I have multiple apps compiled with g++, running in Ubuntu. I'm using named semaphores to co-ordinate between different processes. All works fine except in the following situation: If one of the processes calls sem_wait() or sem_timedwait() to decrement the semaphore and then crashes or is killed -9 before it gets a chance to call sem_post() , then from that moment on, the named semaphore is "unusable". By "unusable", what I mean is the semaphore count is now zero, and the process that should

Differences between System V and Posix semaphores

谁都会走 提交于 2019-11-26 15:21:23
问题 What are the trade-offs between using a System V and a Posix semaphore? 回答1: From O'Reilly: One marked difference between the System V and POSIX semaphore implementations is that in System V you can control how much the semaphore count can be increased or decreased; whereas in POSIX, the semaphore count is increased and decreased by 1. POSIX semaphores do not allow manipulation of semaphore permissions, whereas System V semaphores allow you to change the permissions of semaphores to a subset

linux命令

荒凉一梦 提交于 2019-11-26 14:58:16
linux文件流 标准输入 0 标准输出 1 标准错误 2 重定向 管道 (pipe): | 管道可以将一个命令的输出导向另一个命令的输入,从而让两个(或者更多命令)像流水线一样连续工作,不断地处理文本流。 进程管理 ps 查看 进程创建:kernel并不提供直接建立新进程的 系统调用 。剩下的所有进程都是init进程通过fork机制建立的。新的进程要通过老的进程复制自身得到,这就是fork。fork通常作为一个函数被调用。这个函数会有两次返回,将子进程的PID返回给父进程,0返回给子进程。所有进程的PPID父进程都是指向init进程。 pstree 显示init进程树 进程关系 进程组 每个进程都会属于一个进程组(process group),每个进程组中可以包含多个进程。进程组会有一个进程组领导进程 (process group leader),领导进程的PID ,成为进程组的ID (process group ID, PGID),以识别进程组。领导进程可以先终结。此时进程组依然存在,并持有相同的PGID,直到进程组中最后一个进程终结。 会话 在shell支持工作控制(job control)的前提下,多个进程组还可以构成一个会话 (session)。bash(Bourne-Again shell)支持工作控制,而sh(Bourne shell)并不支持。

What is a semaphore?

不问归期 提交于 2019-11-26 11:26:15
A semaphore is a programming concept that is frequently used to solve multi-threading problems. My question to the community: What is a semaphore and how do you use it? Patrik Svensson Think of semaphores as bouncers at a nightclub. There are a dedicated number of people that are allowed in the club at once. If the club is full no one is allowed to enter, but as soon as one person leaves another person might enter. It's simply a way to limit the number of consumers for a specific resource. For example, to limit the number of simultaneous calls to a database in an application. Here is a very

Is it possible to use mutex in multiprocessing case on Linux/UNIX ?

折月煮酒 提交于 2019-11-26 08:55:57
问题 This is an interview question. Is it possible to use mutex in multiprocessing case on Linux/UNIX ? My idea: No, different processes have separate memory space. mutex is only used for multithreading. semaphore is used for multiprocessing to do synchronization. right ? Any comments are welcome. thanks 回答1: Mutual exclusion locks (mutexes) prevent multiple threads from simultaneously executing critical sections of code that access shared data (that is, mutexes are used to serialize the execution

CountDownLatch vs. Semaphore

假装没事ソ 提交于 2019-11-26 08:42:41
问题 Is there any advantage of using java.util.concurrent.CountdownLatch instead of java.util.concurrent.Semaphore? As far as I can tell the following fragments are almost equivalent: 1. Semaphore final Semaphore sem = new Semaphore(0); for (int i = 0; i < num_threads; ++ i) { Thread t = new Thread() { public void run() { try { doStuff(); } finally { sem.release(); } } }; t.start(); } sem.acquire(num_threads); 2: CountDownLatch final CountDownLatch latch = new CountDownLatch(num_threads); for (int

Do forked child processes use the same semaphore?

那年仲夏 提交于 2019-11-26 08:28:41
问题 Let\'s say I create a semaphore. If I fork a bunch of child processes, will they all still use that same semaphore? Also, suppose I create a struct with semaphores inside and forked. Do all the child processes still use that same semaphore? If not, would storing that struct+semaphores in shared memory allow the child processes to use the same semaphores? I\'m really confused about how my forked child processes can use the same semaphores. 回答1: Let's say I create a semaphore. If I fork a bunch

How to use POSIX semaphores on forked processes in C?

南笙酒味 提交于 2019-11-26 07:21:19
问题 I want to fork multiple processes and then use a semaphore on them. Here is what I tried: sem_init(&sem, 1, 1); /* semaphore*, pshared, value */ . . . if(pid != 0){ /* parent process */ wait(NULL); /* wait all child processes */ printf(\"\\nParent: All children have exited.\\n\"); . . /* cleanup semaphores */ sem_destroy(&sem); exit(0); } else{ /* child process */ sem_wait(&sem); /* P operation */ printf(\" Child(%d) is in critical section.\\n\",i); sleep(1); *p += i%3; /* increment *p by 0,

What is mutex and semaphore in Java ? What is the main difference?

六眼飞鱼酱① 提交于 2019-11-26 06:53:32
问题 What is mutex and semaphore in Java ? What is the main difference ? 回答1: Semaphore can be counted, while mutex can only count to 1. Suppose you have a thread running which accepts client connections. This thread can handle 10 clients simultaneously. Then each new client sets the semaphore until it reaches 10. When the Semaphore has 10 flags, then your thread won't accept new connections Mutex are usually used for guarding stuff. Suppose your 10 clients can access multiple parts of the system.

Is there a Mutex in Java?

走远了吗. 提交于 2019-11-26 06:16:06
问题 Is there a Mutex object in java or a way to create one? I am asking because a Semaphore object initialized with 1 permit does not help me. Think of this case: try { semaphore.acquire(); //do stuff semaphore.release(); } catch (Exception e) { semaphore.release(); } if an exception happens at the first acquire, the release in the catch block will increase the permits, and the semaphore is no longer a binary semaphore. Will the correct way be? try { semaphore.acquire(); //do stuff } catch