Do You Want to Read One Less Than the Size of the Buffer C Posix
In traditional POSIX uniform operating systems, to get information from a document independent in a file organization, a programme used the read system call. A document descriptor that is usually accessed from a prior call to open up is defined by the file. This read system phone call reads out the information in bytes and the integer of which the caller specifies from the document, and then saves it in a buffer provided by the calling mechanism.
Part Definition
Before defining the read role in your code, y'all have to include some required packages.
Hither is how you define the POSIX read office:
>> ssize_t pread( int fildes, void *buf, size_t nbyte, off_t commencement) ;
>> ssize_t read( int fd, void *buf, size_t nbytes) ;
Three parameter arguments tin be taken from the read method call:
int fd: The file descriptor of the file from where the information is to be read. We could either be using a file descriptor acquired via an open system phone call, or we could just utilise 0, ane, or 2 referring to typical input, regular output, or regular error, respectively.
Void *buf: The buffer or character assortment in which the read data should be saved and kept.
Size_t nbyte: The number of bytes that needed to be read from the document before truncating. All information tin can be stored in the buffer if the information to exist read is shorter than nbytes.
Clarification
The read() method tries to read 'nbyte' bytes into the buffer cache referred to by 'buf' from either the file connected with the open up document descriptor 'Fildes' or 'fd'. It does not ascertain the nature of several simultaneous reads on the aforementioned stream, FIFO, or last unit.
On documents that enable the reading, the reading process begins at the first of the document, and the get-go is increased by the number of bytes read. If the document kickoff is at or across the file's edge, at that place are no bytes read, and read() yields none.
When the count is 0, read() will recognize the errors mentioned below. If there are no mistakes, or if read() is not accounted for with Errors, a read() yields naught with a count of 0 and therefore has no other repercussions.
If the count is higher than SSIZE_MAX, as per POSIX.1, so the result is adamant by the implementation.
Return Value
The numeral of bytes 'read' and 'pread' reverted upon achievement must be a non-negative integer while zippo points to the end of the file. The certificate position is progressed past this number, or else, to signify an fault, the methods return -1 and assign 'errno'. When this effigy is less than the number of bytes requested, it is not a fault byte. Information technology could exist possible that fewer bytes are available for now.
Errors
The pread and read office will be unsuccessful if these errors occur:
EAGAIN:
The document or file descriptor 'fd' belongs to a non-socket file that has been labeled as not-blocking (O NONBLOCK) and will block the reading.
EWOULDBLOCK:
The descriptor 'fd' belongs to a socket that has been labeled as non-blocking (O_NONBLOCK) and will block the reading.
EBADF:
The 'fd' may non exist a usable descriptor, or information technology may non exist open for reading.
EFAULT:
This happens when your 'buf' is outside your reachable address space.
EINTR:
Before the reading of data information, the call may take broken upward by a signal.
EINVAL:
This error occurs when your 'fd' descriptor is involved in an object, which is non appropriate for reading, or the document was untied with the O_DIRECT flag, and one or the other address stated in 'buf', the value indicated in 'count', or the document offset is non appropriately associated.
EINVAL:
The descriptor 'fd' may take been formed using a call to timerfd_create(2), and the incorrect size buffer has been given to read.
EIO:
It is an input/output fault. Information technology occurs when the background procedure group attempts to read from its regulatory last, and ane or the other is overlooking or blocking SIGTTIN, or its process group is bereaved. Another reason for this error could be depression-level input/output fault meanwhile reading from a hd or tape. Another potential cause of EIO on networked data files is the removal of advisory locking on the file descriptor and the failure of that lock.
EISDIR:
The file descriptor 'fd' belongs to a directory.
Notes:
Many other errors may as well occur, contingent on the object linked to descriptor 'fd'. Both size_t and ssize_t forms are unmarked and marked numerical information types defined past POSIX.ane. On Linux, at most 0x7ffff000 (2,147,479,552) bytes can be transmitted by reading part (and equivalent system calls), returning the number of bytes originally transmitted (on both 32-bit and 64-chip platforms). With NFS filesystems, just the outset moment the timestamp is changed by reading tiny streams of information, subsequent calls wouldn't exercise and so. Information technology is triggered by caching of client-side attributes since, although not all, NFS clients quit updating to the server via st_atime (last file admission fourth dimension) and customer-side reads fulfilled from the buffer of the client would not trigger changes to st-atime on the server as no server-side readings are available. By removing client-side attribute caching, UNIX metadata may exist accessed, merely this would significantly increase the load on the server and affect productivity in most cases.
Example 01:
Here is a C plan to demonstrate the read role call on the Linux Organisation. Write the beneath-command as it is in a new file. Add libraries, and in the main part, initialize a descriptor and size. The descriptor is opening the file, and size is used to read file information.
The output for the above-code would be as shown in the below image.
Example 02:
Some other example to illustrate the working of the read function is given beneath.
Create another file and write down the code below equally it is in it. Here are 2 descriptors, fd1 & fd2, that both take their own open tabular array file access. And so for foobar.txt, every descriptor does take its file location. The very outset byte of foobar.txt is translated from fd2, and the upshot is c = f, not c = o.
Conclusion
We have read the POSIX read part in C programming efficiently. Hopefully, there are no doubts left.
Source: https://linuxhint.com/posix-read-functioning-in-c-programming/
0 Response to "Do You Want to Read One Less Than the Size of the Buffer C Posix"
Post a Comment