semaphore

How to sync processes using semaphore

对着背影说爱祢 提交于 2019-12-12 03:45:13
问题 let's say I have 3 processes including a parent process I have to execute I program in sequence of P3,P1,P2. Guys please help me how I can start the computation from process P3. I need the out as {0,1,2,3,4,5,.. max} For the reference my code snapshot is :- #define SEM_NAME "//test.mutex" //#define SEM_NAME2 "//test2.mutex" int main(int argc, char const *argv[]) { int max = 0, i =0; sem_t *sem; sem_t *sem2; pid_t pid, pid2; sem = sem_open(SEM_NAME, O_CREAT, O_RDWR, 1); sem_unlink(SEM_NAME);

How to use semaphore to lock table?

一世执手 提交于 2019-12-12 02:33:02
问题 I would like to lock an MDB table from reads while a transaction executes. I would use dbDenyRead but apparently this is unreliable and doesn't always the lock the table: http://www.office-archive.com/32-ms-access/c2bd1a2553e79c60.htm How can I use a semaphore solution to achieve a virtual lock on a table? If I store the semaphore in another table with a row holding the table name and a process/workstation ID which will be cleared at the end of the transaction, how can I prevent the following

How to read from /write to anonymous shared mapping?

最后都变了- 提交于 2019-12-12 02:27:01
问题 Attempting to write a message to anonymous shared memory with a child process, terminate it. Then have the message read by the parent. I have seen examples for mapping input & output files using file descriptors obtained through read & write calls. But do not know how to approach this correctly. int main(void) { char *shared; int status; pid_t child; shared = mmap(0, sizeof(int) , PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (shared == MAP_FAILED) { perror("mmap"); return 1; }

How do I allow only 1 named Semaphore to be created on a server?

天涯浪子 提交于 2019-12-12 00:26:55
问题 I would like to create a semaphore in my app, where the creation will fail (with a clear exception), if another instance of the app is running and has already created the semaphore. So only one per server. I'd like the limit of only one to hold across the system, not just the CLR. But I do not want it to hold across multiple servers (or VMs). i.e. I want the app able to run on 2 distinct servers. Is this possible? If so, how? thanks - dave 回答1: You can use System.Threading.Mutex for this.

predefined preemption points among processes

烈酒焚心 提交于 2019-12-11 19:36:50
问题 I have forked many child processes and assigned priority and core to each of them. Porcess A executes at period of 3 sec and process B at a period of 6 sec. I want them to execute in such a way that the higher priority processes should preempt lower priority ones only at predefined points and tried to acheive it with semaphores. I have used this same code snippets within the 2 processes with different array values in both. 'bubblesort_desc()' sorts the array in descending order and prints it.

C/C++ - Single semaphore of type sem_t to print numbers in order

蹲街弑〆低调 提交于 2019-12-11 19:33:59
问题 Problem: Let's say we have n threads where each thread receives a random unique number between 1 and n. And we want the threads to print the numbers in sorted order. Trivial Solution (using n semaphore/mutex): We can use n mutex locks (or similarly semaphores) where thread i waits to acquire mutex lock number i and unlocks number i + 1. Also, thread 1 has no wait. However, I'm wondering if it's possible to simulate a similar logic using a single semaphore (of type sem_t) to implement the

Throttling asynchronous tasks

寵の児 提交于 2019-12-11 18:49:10
问题 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,

asyncio.Semaphore RuntimeError: Task got Future attached to a different loop

守給你的承諾、 提交于 2019-12-11 18:26:00
问题 When I run this code in Python 3.7: import asyncio sem = asyncio.Semaphore(2) async def work(): async with sem: print('working') await asyncio.sleep(1) async def main(): await asyncio.gather(work(), work(), work()) asyncio.run(main()) It fails with RuntimeError: $ python3 demo.py working working Traceback (most recent call last): File "demo.py", line 13, in <module> asyncio.run(main()) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 43

Question about semaphore

半世苍凉 提交于 2019-12-11 16:07:12
问题 Given the following code, can you figure out what caused "You input 7 characters" showed up 3 times especially the last time? #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <semaphore.h> void *thread_function(void *arg); sem_t bin_sem; #define WORK_SIZE 1024 char work_area[WORK_SIZE]; int main(){ int res; pthread_t a_thread; void *thread_result; res = sem_init(&bin_sem,0,0); if (res!=0){ perror("Semaphore initialization failed");

Synchronize threads on per-item base

廉价感情. 提交于 2019-12-11 12:29:14
问题 While this question is about the MemoryCache class, I can imagine the same need with a Dictionary or ConcurrentDictionary.GetOrAdd where the valueFactory -lambda is also a lengthy operation. In essence I want to synchronize/lock threads on a per-item base. I know MemoryCache is thread safe, but still, checking if an item exists and add the item when it doesn't exist, still needs to be synchronized. Consider this sample code: public class MyCache { private static readonly MemoryCache cache =