Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
Why does write continuously leave 4K bytes in the buffer?

I have essentially the following code:

int fileWrite(int file, void * pBuffer, size_t size)
{
    size_t bytesWritten = (size_t)write( file, pBuffer, size ) ;
    if (bytesWritten != size)
    {
       return -1;
    }
    return 0;
}

It works if the size is 1GB, but when the size is ~2GB, it get 4K bytes left consistently. I can fix this by wrapping write in a loop and moving the buffer up but I'm curious as to why it is always failing.

For example if size is 2147483648, write only writes 2147479552, leaving 4096 unwritten. Why would this happen and is it correct to always wrap write in a loop?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can find the answer in man 2 write:

It is not an error if this number is smaller than the number of bytes requested; this may happen for example because the disk device was filled.


And from the write() man page description:

ssize_t write(int fd, const void *buf, size_t count);

According to POSIX.1, if count is greater than SSIZE_MAX, the result is implementation-defined; see NOTES for the upper limit on Linux.

NOTES

On Linux, write() (and similar system calls) will transfer at most 0x7ffff000 (2,147,479,552) bytes, returning the number of bytes actually transferred. (This is true on both 32-bit and 64-bit systems.)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!