"The C Programming Language", 2nd edition, Kernighan and Ritchie

Answers to Exercises

Chapter 8 - The UNIX System Interface


Please note: these solutions use the Unix system interface, so they're not portable. Nevertheless, they're in K&R, so they appear here despite their inherent non-portability. In any case, they will become portable as soon as everyone's using Unix, as God intended.


Ex No Pg No Exercise Solution Solved by
1 174 Rewrite the program cat from Chapter 7 using read , write , open and close instead of their standard library equivalents. Perform experiments to determine the relative speeds of the two versions. Listing krx801 Andrew Tesker
2 178 Rewrite fopen and _fillbuf with fields instead of explicit bit operations. Compare code size and execution speed. No solution yet posted. ----
3 179 Design and write _flushbuf , fflush and fclose . Listing krx803 Gregory Pietsch
4 179 The standard library function
int fseek(FILE *fp, long offset, int origin)
is identical to lseek except that fp is a file pointer instead of a file descriptor and the return value is an int status, not a position. Write fseek . Make sure that your fseek coordinates properly with the buffering done for the other functions of the library.
Listing krx804 Gregory Pietsch
5 184 Modify the fsize program to print the other information contained in the inode entry. No solution yet posted. ----
6 189 The standard library function calloc(n,size) returns a pointer to n objects of size size , with the storage initialized to zero. Write calloc , by calling malloc or by modifying it. Listing krx806 Bryan Williams
7 189 malloc accepts a size request without checking its plausibility; free believes that the block it is asked to free contains a valid size field. Improve these routines so they take more pains with error checking. No solution yet posted. ----
8 189 Write a routine bfree(p,n) that will free an arbitrary block p of n characters into the free list maintained by malloc and free . By using bfree , a user can add a static or external array to the free list at any time. No solution yet posted. ----


Back
Home




You are visitor number - call again soon!