mutual-exclusion

Critical Section in JavaScript or jQuery

ⅰ亾dé卋堺 提交于 2020-01-14 07:51:08
问题 I have a webpage, in which a certain Ajax event is triggered asynchronously. This Ajax section could be called once or more than once. I do not have control over the number of times this event is triggered, nor the timing. Also, there is a certain code in that Ajax section that should run as a critical section, meaning, when it is running, no other copy of that code should be running. Here is a pseudo code: Run JavaScript or jQuery code Enter critical section that is Ajax (when a certain

Mutual exclusion algorithm using only atomic reads and writes for unknown number of processes and robust to process abortion

百般思念 提交于 2020-01-03 17:05:36
问题 I am trying to come up with a mutual exclusion algorithm that is based only on atomic reads and atomic writes of shared memory (i.e. no compare-and-swap or similar). Apart from mutual exclusion and deadlock freedom, it needs to fulfill the following properties: It needs to be robust in the face of processes dying at any point It needs to handle an ex-ante unknown number of processes competing for the lock Every few seconds I need one of the processes to enter the critical section (I don't

Mutually exclusive random sampling from a list

假装没事ソ 提交于 2019-12-30 11:19:09
问题 input = ['beleriand','mordor','hithlum','eol','morgoth','melian','thingol'] I'm having trouble creating X number of lists of size Y without repeating any elements. What I have been doing is using: x = 3 y = 2 import random output = random.sample(input, y) # ['mordor', 'thingol'] but if I repeat this then I will have repeats. I would like the output to be something like [['mordor', 'thingol'], ['melian', 'hithlum'], ['beleriand', 'eol']] since I chose x = 3 (3 lists) of size y = 2 (2 elements

Implementing a stock exchange using monitors

落爺英雄遲暮 提交于 2019-12-24 14:07:06
问题 I am trying to implement a stock exchange using Hoare's monitors. It has two functions buy() and sell() as follow: buy(procid, ticker, nshares, limit) sell(procid, ticker, nshares, limit) And should print information on buyer id, seller id, ticker, number of shares, and price. And fairness is always satisfied. The pseudo-code of my solution is as follows, but it's not complete. It basically uses a condition variable queue for each ticker. A seller process is put to sleep on this queue when it

lock file between C and php

空扰寡人 提交于 2019-12-23 01:57:08
问题 Although the title mentions file, it doesn't have to be a file. Any locking mechanism would do. Here is the situation: I have a daemon process written in C, and a web page in php. I'd like to have a way of mutual locking so that under certain situation, the C daemon would lock a file and php detects the situation and tells the client side that the system is busy. Is there an easy way of doing this? Thanks, 回答1: flock does it properly. In your PHP script, use a non-blocking lock : $fd = fopen(

Implementing Mutual Exclusion Algorithm by Burns and Lynch in Java: Could there be issues due to instruction reordering?

人盡茶涼 提交于 2019-12-20 07:24:05
问题 I found a fairly simple n-process mutual exclusion algorithm on page 4 (836) in the following paper: "Mutual Exclusion Using Indivisible Reads and Writes" by Burns and Lynch program Process_i; type flag = (down, up); shared var F : array [1..N] of flag; var j : 1..N; begin while true do begin 1: F[i] := down; 2: remainder; (* remainder region *) 3: F[i] := down; 4: for j := 1 to i-1 do if F[j] = up then goto 3; 5: F[i] := up; 6: for j := 1 to i-1 do if F[j] = up then goto 3; 7: for j := i+1

How to provide a sequence of interleaving threads to show that a code breaks and doesn't provide perfect synchronization?

送分小仙女□ 提交于 2019-12-20 06:17:40
问题 I know what the following code does and I know why it is a broken code for synchronization as it has only one conditional variable while we need two but I don't know how to provide a sequence of interleaving threads for showing it doesn't work. Can you show why this code doesn't work with an example? 1 cond_t cond = PTHREAD_COND_INITIALIZER; 2 mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;; 3 4 void *producer(void *arg) { 5 int i; 6 for (i = 0; i < loops; i++) { 7 Pthread_mutex_lock(&mutex); 8

Is my spin lock implementation correct and optimal?

ぃ、小莉子 提交于 2019-12-17 23:21:00
问题 I'm using a spin lock to protect a very small critical section. Contention happens very rarely so a spin lock is more appropriate than a regular mutex. My current code is as follows, and assumes x86 and GCC: volatile int exclusion = 0; void lock() { while (__sync_lock_test_and_set(&exclusion, 1)) { // Do nothing. This GCC builtin instruction // ensures memory barrier. } } void unlock() { __sync_synchronize(); // Memory barrier. exclusion = 0; } So I'm wondering: Is this code correct? Does it