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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:51:10+00:00 2026-06-04T02:51:10+00:00

Apparently POSIX states that Either a file descriptor or a stream is called a

  • 0

Apparently POSIX states that

Either a file descriptor or a stream is called a “handle” on the
open file description to which it refers; an open file description
may have several handles. […] All activity by the application
affecting the file offset on the first handle shall be suspended
until it again becomes the active file handle. […] The handles need
not be in the same process for these rules to apply.
— POSIX.1-2008

and

If two threads each call [the write() function], each call shall
either see all of the specified effects of the other call, or none
of them.
— POSIX.1-2008

My understanding of this is that when the first process issues a
write(handle, data1, size1) and the second process issues
write(handle, data2, size2), the writes can occur in any order but
the data1 and data2 must be both pristine and contiguous.

But running the following code gives me unexpected results.

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
die(char *s)
{
  perror(s);
  abort();
}

main()
{
  unsigned char buffer[3];
  char *filename = "/tmp/atomic-write.log";
  int fd, i, j;
  pid_t pid;
  unlink(filename);
  /* XXX Adding O_APPEND to the flags cures it. Why? */
  fd = open(filename, O_CREAT|O_WRONLY/*|O_APPEND*/, 0644);
  if (fd < 0)
    die("open failed");
  for (i = 0; i < 10; i++) {
    pid = fork();
    if (pid < 0)
      die("fork failed");
    else if (! pid) {
      j = 3 + i % (sizeof(buffer) - 2);
      memset(buffer, i % 26 + 'A', sizeof(buffer));
      buffer[0] = '-';
      buffer[j - 1] = '\n';
      for (i = 0; i < 1000; i++)
        if (write(fd, buffer, j) != j)
          die("write failed");
      exit(0);
    }
  }
  while (wait(NULL) != -1)
    /* NOOP */;
  exit(0);
}

I tried running this on Linux and Mac OS X 10.7.4 and using grep -a
'^[^-]\|^..*-' /tmp/atomic-write.log
shows that some writes are not
contiguous or overlap (Linux) or plain corrupted (Mac OS X).

Adding the flag O_APPEND in the open(2) call fixes this
problem. Nice, but I do not understand why. POSIX says

O_APPEND
If set, the file offset shall be set to the end of the file prior to each write.

but this is not the problem here. My sample program never does
lseek(2) but share the same file description and thus same file
offset.

I have already read similar questions on Stackoverflow but they still
do not fully answer my question.

Atomic write on file from two process does not specifically
address the case where the processes share the same file description
(as opposed to the same file).

How does one programmatically determine if “write” system call is atomic on a particular file? says that

The write call as defined in POSIX has no atomicity guarantee at all.

But as cited above it does have some. And what’s more,
O_APPEND seems to trigger this atomicity guarantee although it seems
to me that this guarantee should be present even without O_APPEND.

Can you explain further this behaviour ?

  • 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-06-04T02:51:12+00:00Added an answer on June 4, 2026 at 2:51 am

    man 2 write on my system sums it up nicely:

    Note that not all file systems are POSIX conforming.

    Here is a quote from a recent discussion on the ext4 mailing list:

    Currently concurrent reads/writes are atomic only wrt individual pages,
    however are not on the system call. This may cause read() to return data
    mixed from several different writes, which I do not think it is good
    approach. We might argue that application doing this is broken, but
    actually this is something we can easily do on filesystem level without
    significant performance issues, so we can be consistent. Also POSIX
    mentions this as well and XFS filesystem already has this feature.

    This is a clear indication that ext4 — to name just one modern filesystem — doesn’t conform to POSIX.1-2008 in this respect.

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

Sidebar

Related Questions

Apparently Matlab load loads the data from a .mat file into the variable that
Apparently there is a database postgres that is created by default on each postgresql
Apparently the speed of the C++ linker in Visual Studio 2010 hasn't improved that
Apparently libigraph and python-igraph are the only packages on earth that can't be installed
Okay, mkstemp is the preferred way to create a temp file in POSIX. But
Apparently I can't move files on different volumes using Directory.Move. I have read that
Apparently some vendors (like Telerik) are working on versions of their controls that do
Apparently there is a method that takes a char and returns a char: http://download.oracle.com/javase/6/docs/api/java/lang/Character.html#toLowerCase(char
Apparently there's a problem with the nagivateToUrl function in Flex , namely that it
Apparently Linkedin is funny about urlencoding the ~ in https://api.linkedin.com/v1/people/~ my problem is that

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.