Question: Write a program, analogous to the Unix/Linux cat command, that uses mmap() and write() system calls (instead of read() or write() ) to copy a
Write a program, analogous to the Unix/Linux cat command, that uses mmap() and write() system calls (instead of read() or write() ) to copy a source file to the screen. Use fstat() to obtain the size of the input file which can be used to size the required memory mapping.
Programming Language: C
Programming Environment: puTTy
I posted the codes we have below, if possible, help us to revise it. If our codes are completely wrong, then help us to redo it.
Make sure your code works before you post the answer, thank you!
The statement to write the file contents to STDOUT is
write (STDOUT_FILENO, addr, sb.st_size);
where
addr is the start of the mapping, gotten from mmap() and,
sb.st_size is the size of the mapping returned by fstat() in the
sb struct.
--------------
The codes we have now:
#include
int main (int argc, char * argv[]) { char addr; int fd; struct stat sb;
if (argc != 2 || strcmp(argv[1], help) == 0) printf(Usage: %s file , argv[0]);
fd = open(argv[1], O_RDONLY); if (fd == -1){ printf(File open fdailed. ); exit(EXIT_FAILURE); } / Obtain size of the file and use it to specify the size of the buffer to be written */ if fstat(fd, &sb) == -1) { printf (fstat error ); exit(EXIT_FAILURE); } addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (addr == MAP_FAILED) { printf (mmap failed ); exit (EXIT_FAILURE); } if (write(STDOUT_FILENO, addr, sb.st_size)!= sb.st_size) printf (Failed write );
exit(EXIT_SUCCESS); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
