Comp111: Operating Systems
Classroom Exercise 15
File I/O
Fall 2017

group member 1: ____________________________ login: ______________
group member 2: ____________________________ login: ______________
group member 3: ____________________________ login: ______________
group member 4: ____________________________ login: ______________
group member 5: ____________________________ login: ______________
group member 6: ____________________________ login: ______________
group member 7: ____________________________ login: ______________
group member 8: ____________________________ login: ______________

Consider the following simple programs:
// program 1
main() { 
    FILE *f = fopen("foo", "w"); 
    fprintf(f,"hi!\n");
    fclose(f); 
}
// program 2
main() { 
    FILE *g = fopen("foo", "a"); 
    fprintf(g,"ho!\n");
    fprintf(g,"yo!\n");
    fclose(g); 
}

  1. Suppose that there is a shared kernel file descriptor for both open calls (as in linux), and that formatted I/O to disk files is block-buffered, as in linux. If we repeatedly execute the two processes asynchronously, how many different files "foo" can be produced? What are they, and how are they created? Hint:
    1. whenever it is called, fopen("foo", "w") implicitly erases the file contents and sets the file pointer to the beginning of the file.
    2. whenever it is called, fopen("foo", "a") does not erase file contents and sets the file pointer to the end of the file ("append mode").








  2. Answer question (1) again but instead suppose that there is a different kernel descriptor for each process (not linux).
  3. What is the answer to (1) if the same kernel descriptor describes the two open files (as in linux), and the file f is forced to be line-buffered rather than block-buffered, (e.g., by calling setlinebuf(f) just after fopen in both programs)?









  4. What is the answer to (1) if the file f is line-buffered rather than block-buffered, and there is a different kernel descriptor for each open call?









  5. (Advanced) What are the disadvantages of having one shared file descriptor for each file that is opened? What kinds of file updates are not supported?









  6. (Advanced) It is typical to store a database as a binary file of structures, e.g., literal copies of
     
    struct table { 
        int id; 
        char name[20]; 
        double salary; 
    }; 
    
    are written to predetermined offsets in the file. What issues of portability arise if structures are written literally to disk in this manner? Can I transfer the file to another machine and be assured of reading back the same structures? Why or why not?