semaphore

How do I terminate a thread that is waiting for a semaphore operation

 ̄綄美尐妖づ 提交于 2019-12-10 22:43:08
问题 I am writing a program that uses shared memory and semaphores for ipc. There is one main server process that creates the shared memory and semaphores. Any number of client processes can attach to the shared memory and read and write to it when allowed. The semaphores provide the blocking mechanism to control reads and writes. Everything works fine except when a I try to terminate a client. The semaphore block to access the shared memory is in a thread and on process termination I have no way

Object-oriented semaphore usage in C++

删除回忆录丶 提交于 2019-12-10 22:28:53
问题 I know how to use Unix semaphores in C. Before using them I must call a constructor-ish function named sem_init and after using them I have to call a destructor-like function named sem_destroy . I know I can keep doing this in C++ because of its backwards compatibility with C, but does C++ have a real object-oriented way to use semaphores? 回答1: If you really insist on using POSIX semaphores and not Boost, you can of course wrap sem_t in a class: class Semaphore { sem_t sem; public: Semaphore

Why does sem_open work with fork() without shared memory?

喜欢而已 提交于 2019-12-10 21:54:01
问题 This program works (I tested it), even though the semaphore is not in shared memory. Note how I create the variable once - before the fork(). On the other hand, a semaphore created with sem_init() needs to be in shared memory to work. But it's still a sem_t structure, so why doesn't it require shared memory? Are the contents of the sem_t structure somehow different? sem_t *s = sem_open("mysemaphore1", O_CREAT, 0600, 0); if (fork()) { sleep(3); sem_post(s); } else { sem_wait(s); printf("Woke\n

Solving Readers/Writers using java Semaphores

落爺英雄遲暮 提交于 2019-12-10 21:19:32
问题 So, it's a classical concurrency problem we're (Me and my colleague) facing here. We weren't lazy, We brought some relevant code in order for you to help us properly. We have two classes defining Readers and Writers, they both extend Thread class, and of course override the run method like so: while(!isInterrupted()) { try{ Thread.sleep(for some time) }catch(InterruptedException e) {} database.readLock(); readersWorking++; //for debugging purposes database.readUnlock(); } Writer's run method

sem_init() causing SEGV

北战南征 提交于 2019-12-10 21:02:43
问题 I have the following code and it is being killed by a SEGV signal. Using the debugger shows that it is being killed by the first sem_init() in main(). If I comment out the first sem_init() the second causes the same problem. I have tried figuring out what would cause this sys call to cause a SEGV. The else is not being run, so the error is happening before it can return a value. Any help would be greatly appreciated, Thank you. I removed the rest of the code that isnt being run before this

Can someone explain producer and consumer in P V form?

大兔子大兔子 提交于 2019-12-10 20:58:13
问题 Wikipedia has a sample code that everyone uses. I honestly don't get the P,V thing. It first said The consumer must wait for the producer to produce something if the queue is empty. But then it said Example. A single consumer enters its critical section. Since fullCount is 0, the consumer blocks. I assume blocking means waiting? My homework requires me to understand the use of such binary semaphore and then implement a solution for a different kind of producer-consumer problem. But I don't

Threads and semaphores in Ada95

自闭症网瘾萝莉.ら 提交于 2019-12-10 20:34:46
问题 How can I use threads in Ada95? What functions can I use to create, destroy, stop and start them? How can I use semaphores in this language? 回答1: Ada's terminology for a thread is a "task". Ada doesn't have semaphores (as such) built directly into the language, but Googling for something like "Ada semaphore" should turn up a fair number of hits. AdaPower.com, in particular, has quite a bit about concurrent programming in Ada (and, for that matter, almost all kinds of programming in Ada). 回答2:

what is Counting Semaphore?

社会主义新天地 提交于 2019-12-10 20:22:21
问题 Hi I did get how the Counting Semaphore works? Please help me in understanding. As per my understanding if we set count as 3, then process can use 3 threads to access the resource. so, here just 3 threads have access on the resource. When 1 thread leaves the other waiting thread comes in. If my understanding is correct, these 3 thread can corrupt shared data too. Then what is use of it? 回答1: Your observations are correct; typically a resource either needs to be restricted to one thread (e.g.

Semaphore源码解读,若要转载请注明出处

白昼怎懂夜的黑 提交于 2019-12-10 19:50:20
/* * Semaphore源码解读,转载请注明出处,赵泉伟 */ package java.util.concurrent; import java.util.Collection; import java.util.concurrent.locks.AbstractQueuedSynchronizer; /** * A counting semaphore. Conceptually, a semaphore maintains a set of * permits. Each {@link #acquire} blocks if necessary until a permit is * available, and then takes it. Each {@link #release} adds a permit, * potentially releasing a blocking acquirer. * However, no actual permit objects are used; the {@code Semaphore} just * keeps a count of the number available and acts accordingly. * * <p>Semaphores are often used to restrict the

Why it isn't advised to call the release() method of a binary semaphore from inside a finally-clause?

℡╲_俬逩灬. 提交于 2019-12-10 18:55:42
问题 To make sure that a Lock is unlocked, it is adviced to call the unlock() method from inside a finally-clause: lock.lock(); try{ // critical section which may throw exceptions } finally { lock.unlock(); } This is to avoid a possible deadlock, in case an exception is thrown from the code in the critical section. Why isn't the same practice adviced for binary semaphores in equivalent scenarios? mutex.acquire(); try{ // critical section which may throw exceptions } finally { mutex.release(); }