semaphore

How to share semaphores between processes using shared memory

若如初见. 提交于 2019-11-26 06:06:53
问题 I have to synchronize N client processes with one server. These processes are forked by a main function in which I declared 3 semaphores. I decided to use POSIX semaphores but I don\'t know how to share them between these processes. I thought that shared memory should work correctly, but I have some questions: How can I allocate the right space of memory in my segment? Can I use sizeof(sem_t) in size_t field of shmget in order to allocate exactly the space I need? Does anyone have some

sem_init on OS X

假如想象 提交于 2019-11-26 05:36:25
问题 I am working on some code which uses the pthread and semaphore libraries. The sem_init function works fine on my Ubuntu machine, but on OS X the sem_init function has absolutely no effect. Is there something wrong with the library or is there a different way of doing it? This is the code I am using to test. sem_t sem1; sem_t sem2; sem_t sem3; sem_t sem4; sem_t sem5; sem_t sem6; sem_init(&sem1, 1, 1); sem_init(&sem2, 1, 2); sem_init(&sem3, 1, 3); sem_init(&sem4, 1, 4); sem_init(&sem5, 1, 5);

When should we use mutex and when should we use semaphore

二次信任 提交于 2019-11-26 04:58:12
问题 When should we use mutex and when should we use semaphore ? 回答1: Here is how I remember when to use what - Semaphore: Use a semaphore when you (thread) want to sleep till some other thread tells you to wake up. Semaphore 'down' happens in one thread (producer) and semaphore 'up' (for same semaphore) happens in another thread (consumer) e.g.: In producer-consumer problem, producer wants to sleep till at least one buffer slot is empty - only the consumer thread can tell when a buffer slot is

How can I tell if another instance of my program is already running?

荒凉一梦 提交于 2019-11-26 03:57:41
问题 How do i tell if one instance of my program is running? I thought I could do this with a data file but it would just be messy :( I want to do this as I only want 1 instance to ever be open at one point. 回答1: You can create a Semaphore and stop execution (put the code into your *.dpr file) and bring you running application to the screen. var Semafor: THandle; begin { Don't start twice ... if already running bring this instance to front } Semafor := CreateSemaphore(nil, 0, 1, 'MY_APPLICATION_IS

What is a semaphore?

我怕爱的太早我们不能终老 提交于 2019-11-26 02:26:17
问题 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? 回答1: 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,

Difference between binary semaphore and mutex

对着背影说爱祢 提交于 2019-11-26 01:43:30
问题 Is there any difference between a binary semaphore and mutex or are they essentially the same? 回答1: They are NOT the same thing. They are used for different purposes! While both types of semaphores have a full/empty state and use the same API, their usage is very different. Mutual Exclusion Semaphores Mutual Exclusion semaphores are used to protect shared resources (data structure, file, etc..). A Mutex semaphore is "owned" by the task that takes it. If Task B attempts to semGive a mutex

Throttling asynchronous tasks

隐身守侯 提交于 2019-11-26 00:22:25
问题 I would like to run a bunch of async tasks, with a limit on how many tasks may be pending completion at any given time. Say you have 1000 URLs, and you only want to have 50 requests open at a time; but as soon as one request completes, you open up a connection to the next URL in the list. That way, there are always exactly 50 connections open at a time, until the URL list is exhausted. I also want to utilize a given number of threads if possible. I came up with an extension method,

Difference between binary semaphore and mutex

岁酱吖の 提交于 2019-11-25 23:00:29
Is there any difference between a binary semaphore and mutex or are they essentially the same? Benoit They are NOT the same thing. They are used for different purposes! While both types of semaphores have a full/empty state and use the same API, their usage is very different. Mutual Exclusion Semaphores Mutual Exclusion semaphores are used to protect shared resources (data structure, file, etc..). A Mutex semaphore is "owned" by the task that takes it. If Task B attempts to semGive a mutex currently held by Task A, Task B's call will return an error and fail. Mutexes always use the following