World Library  
Flag as Inappropriate
Email this Article

Read (system call)

Article Id: WHEBN0032030979
Reproduction Date:

Title: Read (system call)  
Author: World Heritage Encyclopedia
Language: English
Subject: Linux kernel, Read, C POSIX library, Computer file, System call
Collection: C Posix Library, System Calls
Publisher: World Heritage Encyclopedia

Read (system call)

In modern POSIX compliant operating systems, a program that needs to access data from a file stored in a file system uses the read system call. The file is identified by a file descriptor that is normally obtained from a previous call to open. This system call reads in data in bytes, the number of which is specified by the caller, from the file and stores then into a buffer supplied by the calling process.

The read system call takes three arguments:

  1. The file descriptor of the file,
  2. the buffer where the read data is to be stored and
  3. the number of bytes to be read from the file.

POSIX usage

The read system call interface is standardized by the POSIX specification. Data from a file is read by calling the read function:

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

The value returned is the number of bytes read (zero indicates end of file) and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because the system call was interrupted by a signal.

Alternatively, -1 is returned when an error occurs, in such a case errno is set appropriately and further it is left unspecified whether the file position (if any) changes.


POSIX read

This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.