Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4025174
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:48:48+00:00 2026-05-20T10:48:48+00:00

I need to create a file if it does not exist, in a way

  • 0

I need to create a file if it does not exist, in a way that another process trying to create this file would fail. I need the file be considered “created” even before the creating process finished writing the actual data to it.

I read about O_EXCL flag to open(), so it seems that the solution exists, I have a few questions however:

  1. do you have experience with this technique? How good is it? (I guess I can’t have a DB-level atomicity, but but good enough is… well, enough)
  2. should I immediately close the file after open() so that it is considered created, and then reopen it for writing?
  3. are there any subtleties to be aware of?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:48:49+00:00Added an answer on May 20, 2026 at 10:48 am

    The open() man page says your method may fail on NFS.

    From the section on O_EXCL:

    When used with O_CREAT, if the file
    already exists it is an error and the
    open() will fail. In this context, a
    symbolic link exists, regardless of
    where it points to. O_EXCL is broken
    on NFS file systems; programs which
    rely on it for performing locking
    tasks will contain a race condition.

    And it suggests a more general solution:

    The solution for performing atomic
    file locking using a lockfile is to
    create a unique file on the same file
    system (e.g., incorporating hostname
    and pid), use link(2) to make a link
    to the lockfile. If link() returns 0,
    the lock is successful. Otherwise, use
    stat(2) on the unique file to check if
    its link count has increased to 2, in
    which case the lock is also
    successful.

    See the “Using Files as Locks” section of this Web page for more details on the various issues and approaches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need to create a file management interface that integrates with the clients current
I understand that we need to create MXML file to define a view. Suppose
I need to create a log file containing HTML (tables, etc). It seems that
I need to create a batch file that reads a file with one line
I need to create a batch file that changes path to a particular folder
I need to create an application that can be activated with a batch file.
I need to create file sharing and chatting system like Microsoft Groove under .NET
I have a one-time need to create a file from an iOS mutable array.
We need to create an .xls file in ASP .NET which will be imported
I need to create a batch file which will copy web log files from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.