January 2003
Intermediate to advanced
480 pages
13h 22m
English
The pread() and pwrite() system calls combine the effects of lseek() and read() (or write()) into a single system call. This provides some improvement in performance although the net effect will only really be visible in an application that has a very I/O intensive workload. However, both interfaces are supported by the Single UNIX Specification and should be accessible in most UNIX environments. The definition of these interfaces is as follows:
#include <unistd.h>
ssize_t pread(int fildes, void buf, size_t nbyte, off_t offset);
ssize_t pwrite(int fildes, const void buf, size_t nbyte,
off t offset);
The example below continues on from the dd program described earlier and shows the use of combining the lseek() with read() and write() calls:
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 main(int argc, char argv)
7 {
8 char *buf;
9 int ifd, ofd, nread;
10 off_t inoffset, outoffset;
11 size_t insize, outsize;
12
13 if (argc != 7) {
14 printf(“usage: mydd infilename in_offset”
15 “ in_size outfilename out_offset”
16 “ out_size\n”);
17 }
18 ifd = open(argv[1], O_RDONLY);
19 if (ifd < 0) {
20 printf(“unable to open %s\n”, argv[1]);
21 exit(1);
22 }
23 ofd = open(argv[4], O_WRONLY);
24 if (ofd < 0) {
25 printf(“unable to open %s\n”, argv[4]);
26 exit(1);
27 }
28 inoffset = (off_t)atol(argv[2]);
29 insize = (size_t)atol(argv[3]); 30 outoffset = (off_t)atol(argv[5]); 31 outsize = (size_t)atol(argv[6]); 32 buf = ...