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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:56:09+00:00 2026-05-28T03:56:09+00:00

I write a linux char device driver to simulate a file. The data is

  • 0

I write a linux char device driver to simulate a file. The data is stored in an array and I want to implement a “read-file”-handler…

static ssize_t data_read(struct file *f, char __user *buf, size_t count, loff_t *f_pos){
char *msg_pointer;
int bytes_read = 0;

if(vault.storage==NULL)
    return -EFAULT;

msg_pointer = vault.storage + *f_pos;

while (count && (*f_pos < vault.size) ) {
    put_user(*(msg_pointer++), buf++);
    count--;
    bytes_read++;
    ++*f_pos;
}

return bytes_read;
}

vault.storage is a pointer to a kmalloc-creation. If I test the code by copying with dd it works as expected, but when I want to open the file with C

if((fp_data = open("/dev/vault0", O_RDWR)) < 0){
    perror("could not open file.\n");
}

err = write(fp_data, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 36);

if (err < 0){
    perror("failed to write to sv \n");
} 

read(fp_data, buffer, 36);
read(fp_data, buffer, 36);

the first read-command returns 4.. the second 0 – how is this possible?

  • 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-28T03:56:09+00:00Added an answer on May 28, 2026 at 3:56 am

    write performed on a file is not guaranteed to write all the bytes requested atomically … that is only reserved for a pipe or FIFO when the requested write-amount is less than PIPE_BUF in size. For instance, write can be interrupted by a signal after writing some bytes, and there will be other instances where write will not output the full number of requested bytes before returning. Therefore you should be testing the number of bytes written before reading back any information into a buffer to make sure you are attempting to read-back the same number of bytes written.

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

Sidebar

Related Questions

I have to write a linux char device, which handles ioctl (without BKL) functions
I'm trying to write a Linux device driver. I've got it to work really
I'm using read/write locks on Linux and I've found that trying to upgrade a
I want to write a long running process (linux daemon) that serves two purposes:
I have several embedded linux systems that I want to write a 'Who's Online?'
i'm writing a char device in linux - xubuntu , and i'm wondering if
I am using the cstdio (stdio.h) to read and write data from binary files.
I want to write a Linux 2.6 netfilter module , which can check the
In my program I read from the serial device (Linux, 8N1) without any problem.
I have the following issue here: I want to write a server streaming data

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.