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 7690199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:20:28+00:00 2026-05-31T20:20:28+00:00

I wrote a UNIX daemon (targeting Debian, but it shouldn’t matter) and I wanted

  • 0

I wrote a UNIX daemon (targeting Debian, but it shouldn’t matter) and I wanted to provide some way of creating a “.pid” file, (a file which contains the process identifier of the daemon).

I searched for a way of opening a file only if it doesn’t exist, but couldn’t find one.

Basically, I could do something like:

if (fileexists())
{
  //fail...
}
else
{
  //create it with fopen() or similar
}

But as it stands, this code does not perform the task in a atomic fashion, and doing so would be dangerous, because another process might create the file during my test, and the file creation.

Do you guys have any idea on how to do that?

Thank you.

P.S: Bonus point for a solution which only involves std::streams.

  • 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-31T20:20:29+00:00Added an answer on May 31, 2026 at 8:20 pm

    man 2 open:

    O_EXCL Ensure that this call creates the file: if this flag is specified in conjunction with O_CREAT, and pathname already exists, then open()
    will fail. The behavior of O_EXCL is undefined if O_CREAT is not specified.

    so, you could call fd = open(name, O_CREAT | O_EXCL, 0644); /* Open() is atomic. (for a reason) */

    UPDATE: and you should of course OR one of the O_RDONLY, O_WRONLY, or O_RDWR flags into the flags argument.

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

Sidebar

Related Questions

What is the proper way to create a well-behaved Unix or Linux daemon in
I wrote a basic UNIX program that uses some low-level I/O. Nothing special, here's
I'll write a program for Interactive UNIX ( http://en.wikipedia.org/wiki/INTERACTIVE_UNIX ). But in a year
I wrote a program that forks some processes with fork(). I want to kill
I wrote a server based on Twisted, and I encountered a problem, some of
So I wrote a Python script which does some simple stuff. It was originally
My webapp is hosted on a unix server using MySQL as database. I wrote
Is there a way to periodically run a Unix command ( ps in my
I am exploring parent process & child processes concept in UNIX. I wrote this
I'm trying to port a piece of software I wrote with Unix sockets to

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.