Exclusive file open
source link: https://lispblog.xach.com/post/189664324263/exclusive-file-open
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
The Planet Lisp twitter fix involved tracking post status with a file. Although it’s not 100% bulletproof, there’s a trick when using files to reduce races.
Here’s a naive approach:
- Assign a Planet Lisp post a unique pathname
- If that pathname exists, do nothing
- Otherwise, send a tweet for the post and save its details to the pathname
The problem is between 2 and 3 - another process could post the tweet first.
There’s another option:
- Assign a Planet Lisp post a unique pathname
- Attempt to open the file with
O_EXCL|O_CREAT
mode - If the attempt succeeds, send a tweet for the post and save its details to the pathname
The key bit is O_EXCL|O_CREAT
mode, a Unix option that means open
will fail if the file exists, and succeed atomically
otherwise. Specifically:
The check for the existence of the file and the creation of the file if it does not exist shall be atomic with respect to other threads executing open() naming the same filename in the same directory with O_EXCL and O_CREAT set.
In SBCL, you can
getO_EXCL|O_CREAT
semantics by specifing :direction :output
and
NIL
, :error
, or :new-version
as the :if-exists
argument to
CL:OPEN. For NIL
, you can check to see if
the returned value is NIL, instead of a stream, to know if you have
successfully exclusively opened the file and can proceed. I used the
NIL
approach
in the Planet Lisp twitter gateway.
Recommend
-
14
Quick Tip - Open A File In Default Program When you double click a file in Explorer.exe, it automatically opens in its default program if it has one associated with its type. But did you know you can do the same thing us...
-
8
Open File Dialog Box In PowerShell Here’s a neat little PowerShell function you can throw into your scripts. Lots of times I want to specify a CSV or TXT or some other file in a script. It’s easy to do this:
-
8
Increasing Linux Open File Limits Mar 2, 2017 So Winston found this log message in our logs today: 2017-03-02T18:18:31.561Z 32313 TID-osjfdq3bg...
-
3
Open folder or file with Sublime Text from Finder toolbar Post author:herrkaefer Post published:01/19/2020...
-
10
Android Studio Failed to open zip file问题 – Android开发中文站你的位置:Android开发中文站 > Android开发 >
-
6
What Is a CSV File, and How Do I Open It?A Comma Separated Values (CSV) file is a plain text file that contains a list of data. These files are often used for exchanging data between different applications. For example, databases and contact...
-
4
Python is one of the most popular programming languages, and unlike some other languages, it does not r...
-
4
Open the file in the Android File Manager advertisements I saw many similar question, but no one help me! So, I want to get list of availabl...
-
9
How to Safely Open a Windows REG File By M. Fahad Khawaja Published 8 hours ago A malicious REG file can do serious harm to yo...
-
4
Home News Tata Group to open 100 exclusive Apple stores
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK