locking

Bash scripting: reader writer lock

可紊 提交于 2019-12-02 02:22:49
Imagine a network of several nix machines. A dedicated node stores files and periodically schedules Task A that modifies these files. Each of the other nodes schedules Task B that syncs ( rsync ) those files to local storage. Task A can take considerable amount of time and the file collection needs to be in a consistent state on all nodes. Thus Task B shouldn't run while Task A is running. A possible solution for this is to use a reader-writer lock. Task A and Task B would put a write and a read lock on the resource respectively. I wonder how can we implement such locking mechanism with unix

Using C# is it possible to test if a lock is held on a file

不想你离开。 提交于 2019-12-02 02:10:54
BACKGROUND: I use an offset into a file and the Filestream lock/unlock menthods to control read/write access. I am using the following code to test if a lock is currently held on the file try { fs.Lock( RESERVED_BYTE, 1 ); fs.Unlock( RESERVED_BYTE, 1 ); rc = 1; } catch { rc = 0; } QUESTION: My goal is to eliminate the try/catch block. Is there some better way to see if the lock exists? EDIT: Note: This question is not about if the file exists. I already know it does. It is about synchronizing write access. casperOne You can call the LockFile Windows API function through the P/Invoke layer

why does the following code result in deadlock

时光怂恿深爱的人放手 提交于 2019-12-02 02:01:13
I have the following class public class LockTester implements Runnable{ private static Locker locker = new Locker(); public static void main(String[] args){ for(int i=0;i<10;i++){ Thread t = new Thread(new LockTester()); t.start(); } } public void run(){ for(int i=0;i<1000;i++){ locker.unlockFirst();//note unlocking here locker.lockFirst(); locker.lockSecond(); locker.unlockSecond(); locker.unlockFirst(); } } } and Locker class public class Locker{ private Lock lock1 = new ReentrantLock(); private Lock lock2 = new ReentrantLock(); public void lockFirst(){ lock1.lock(); } public void lockSecond

Should this class use data locking for multi threading?

十年热恋 提交于 2019-12-02 01:45:19
I have a class that contains some data and there are many threads use it: class MyClass { static Dictionary<Key, Value> MyData; static IEnumerable<Data> Data { get { return MyData.Values; } } static void Reset() { MyData = GetMyData(); } } Sometime (say once in a day) the Reset method is called. I don't want to add locking because of performance, but not sure if everything will work without it. The question is: should I use any type of locking in this class? Allon Guralnek I disagree with Richard. You seem to use the Dictionary<,> in an immutable fashion, never changing the content. It is

Default table lock hint on SQL Server 2005/2008

人走茶凉 提交于 2019-12-02 01:20:06
How do you look up a default global table locking hint? -- Questions Are there any DMV/DMF (Dynamic Management View/Function) that return such information? And also, is there a way to change the default lock hint? Currently I am adding nolock hint almost everywhere to prevent locks. I'd like to avoid doing so by changing the default lock hint to nolock so that existing stored procedures do not need to change. I am not aware of any such global setting. IMHO even should that exist there can be little justification for using it. You can however set the isolation levels to control whether

Python: Lock directory

流过昼夜 提交于 2019-12-02 00:34:41
AFAIK this code can be used to lock a directory: class LockDirectory(object): def __init__(self, directory): assert os.path.exists(directory) self.directory = directory def __enter__(self): self.dir_fd = os.open(self.directory, os.O_RDONLY) try: fcntl.flock(self.dir_fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError as ex: if ex.errno != errno.EAGAIN: raise raise Exception('Somebody else is locking %r - quitting.' % self.directory) def __exit__(self, exc_type, exc_val, exc_tb): self.dir_fd.close() But according to the answers of this question locking a directoy is not possible: Python: Lock a

Unordered threads problem

梦想与她 提交于 2019-12-01 23:53:37
I had asked question about lock in here and people responded there is no problem in my lock implementation. But i catched problem. Here is same lock implementation and i am getting weird result. I expect to see numbers starts from 1 but it starts from 5.Example is at below. class Program { static object locker = new object(); static void Main(string[] args) { for (int j = 0; j < 100; j++) { (new Thread(new ParameterizedThreadStart(dostuff))).Start(j); } Console.ReadKey(); } static void dostuff(dynamic input) { lock (locker) { Console.WriteLine(input); } } } Mikael Svenson The code is fine. But

Why are lock hints needed on an atomic statement?

China☆狼群 提交于 2019-12-01 23:09:46
Question What is the benefit of applying locks to the below statement? Similarly, what issue would we see if we didn't include these hints? i.e. Do they prevent a race condition, improve performance, or maybe something else? Asking as perhaps they're included to prevent some issue I've not considered rather than the race condition I'd assumed. NB: This is an overflow from a question asked here: SQL Threadsafe UPDATE TOP 1 for FIFO Queue The Statement In Question WITH nextRecordToProcess AS ( SELECT TOP(1) Id, StatusId FROM DemoQueue WHERE StatusId = 1 --Ready for processing ORDER BY

how to create password to folder using c#

强颜欢笑 提交于 2019-12-01 22:17:40
how to create password to windows folder using c# shall I set password to folders when it is created. You can't set a password on a folder in either FAT32 or NTFS, however you can set up so that only certain user accounts can access it using the DirectorySecurity class. There's some sample code in that link. Something like this cannot be done using C#. The only real reliable way of achieving something like this is to write a kernal-mode File System Filter Driver . If you install the WDK there is an example called minispy Minifilter Sample which would be a good starting point for you. 来源: https

boost interprocess file_lock does not work with multiple processes

荒凉一梦 提交于 2019-12-01 22:17:26
I seem to be having an issue with boost::interprocess::file_lock I have process 1 that is essentially boost::interprocess::file_lock test_lock("testfile.csv"); test_lock.lock(); sleep(1000); test_lock.unlock(); When I run a second process while the first process is sleeping, I find that I am still able to read testfile.csv. What's worse, I can even overwrite it. Am I misinterpreting how file_lock works? I was under the impression that calling .lock() gives it exclusive lock over the file and prevents any other process from read/modifying the file. Joe file_lock is not for locking a file. It is