semaphore

fcntl() for thread or process synchronization?

一个人想着一个人 提交于 2019-12-12 14:07:43
问题 Is it possible to use fcntl() system call on a file to achieve thread/process synchronization (instead of semaphoress)? 回答1: Yes. Unix fcntl locks (and filesystem resources in general) are system-wide, so any two threads of execution (be they separate processes or not) can use them. Whether that's a good idea or not is context-dependent. 回答2: That's one way of synchronizing between processes, but if you don't want to use semaphores, you could use process shared mutexes, such as mutexes and

javascript critical sections or semaphore problem

扶醉桌前 提交于 2019-12-12 13:05:11
问题 function myobj(){ var gup=this; this.lastindex=-1; this.criticalSectionInTimer=0; this.updateTimer; this.start = function(l){ if((typeof this.updateTimer)=="number"){ clearInterval ( this.updateTimer ); } this.updateTimer=setInterval(function() {gup.getMessages();} , 30); } this.stop= function(){ if((typeof this.updateTimer)=="number"){ clearInterval ( this.updateTimer ); } } this.addUpdate(i){ //some code } this.rrrrnr=0; this.getMessages = function (){ if(this.criticalSection==0){ this

System-wide global variable / semaphore / mutex in C++/Linux?

Deadly 提交于 2019-12-12 10:37:32
问题 Is it possible to create a system-wide global variable / semaphore / mutex in C++ on Linux? Here's the reason: I've got a system that often runs multiple copies of the same software on unrelated data. It's common to have 4 jobs, each running the same software. The software has a small section where it creates a huge graph that takes a lot of memory; outside that section memory usage is moderate. It so happens sometimes that 2 jobs simultaneously hit the same memory-hungry section and the

C# - Parallelizing While Loop with StreamReader causing High CPU

限于喜欢 提交于 2019-12-12 10:02:42
问题 SemaphoreSlim sm = new SemaphoreSlim(10); using (FileStream fileStream = File.OpenRead("...")) using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8, true, 4096)) { String line; while ((line = streamReader.ReadLine()) != null) { sm.Wait(); new Thread(() => { doSomething(line); sm.Release(); }).Start(); } } MessageBox.Show("This should only show once doSomething() has done its LAST line."); So, I have an extremely large file that I want to execute code on every single

java Semaphore north south que

ε祈祈猫儿з 提交于 2019-12-12 09:28:16
问题 There are two queues of children waiting to use a roundabout in a playground – one is facing it from the north, one from the south. Children may only enter the roundabout from the front of either queue and may only enter if there is a space available (only one child may use each segment at a time). Once on the roundabout they use it for a random period of time, then leave, either to the east or west, at random. They then play elsewhere for a random period and, after that, re-enter a north

Java read & write lock requirement, with lock and release from different threads

独自空忆成欢 提交于 2019-12-12 08:14:57
问题 I'm trying to find a less clunky solution to a Java concurrency problem. The gist of the problem is that I need a shutdown call to block while there are still worker threads active, but the crucial aspect is that the worker tasks are each spawned and completed asynchronously so the hold and release must be done by different threads. I need them to somehow send a signal to the shutdown thread once their work has completed. Just to make things more interesting, the worker threads cannot block

Semaphores and concurrent programming

喜夏-厌秋 提交于 2019-12-12 07:15:31
问题 For a homework assignment i need to program the following scenario. This is going to be done using semaphores using BACI (which is C--) There are 2 unisex restrooms that can hold 4 people each. Since it is unisex only people of the same sex can be in the restroom at the same time and FIFO is not important. I have the basic "algorithm" in my head to handle 4 men and 4 woman for 1 restroom. But i don't know how to code this. Any help would be greatly appreciated. Here is what I have. Woman:

Pthread Synchronization: Back & Forth Reading of Two Text Files

∥☆過路亽.° 提交于 2019-12-12 04:41:29
问题 I'm writing a program that creates two threads. Each thread is responsible for reading through one text file, with one char on each line. The first is formatted like: h 0 h 0 ... The second is formatted like: 0 i 0 i 0 i Sometimes there can be multiple letters after each other, or multiple zeros after each other. However, the one certainty is that if there is a letter on one line of one file, the corresponding line of the second file will have a 0, and vice versa. The threads are supposed to

protect a shared memory segment with semaphore does not work

我与影子孤独终老i 提交于 2019-12-12 04:37:25
问题 I have a programm which creates 1000 child processes. Each process should access to a int variable, which is stored in a shared memory segment. To protect the int variable I have created a semaphore: #define _XOPEN_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/sem.h> #include <sys/ipc.h> #include <sys/shm.h> #define KEY 1000 #define LOCK -1 #define UNLOCK 1 int *ptr; int pid; int shm_id; int sem

POSIX semaphore and pthread issue

折月煮酒 提交于 2019-12-12 04:09:08
问题 I'm practicing semaphores using the POSIX library. I am trying to pass threads (representing customers) through one semaphore (representing a server) that sits 8 people down at two tables (each controlled by sempahores). I think the culprit is the order of unlocking and locking multiple semaphores, but I can't seem to target where the Illegal instruction (core dumped) error is coming from. EDITED - inversed order of mutex init and creating threads loop - added return NULL to end of eat() :