Vous êtes sur la page 1sur 3

Lab #2 Introduction to File Descriptors Villanova University Fall 2013 CSC1600: Operating Systems

LEARNING OUTCOMES Once you have completed this exercise you will have done the following: Gained an understanding of file descriptors and the underlying data structures

DIRECTIONS This is a pair programming exercise. Work on this exercise with your programming partner following the guidelines in the pair programming introductory document. You and your partner need to submit one completed lab, including the path name for your completed code and a word-processed document with answers to the lab questions. EXERCISES Complete the following exercises once, and answer the questions in a separate word-processed document. If you do not have time to complete this lab in class, you and your partner must complete it on your own and upload it to Blackboard to receive credit. 1. Create a lab2 directory in the module1 directory you created in your first lab. 2. Copy both the myFork.c program and the primes.c program into your lab2 directory. Do the following: o Remove the sleep from your primes.c program. o Add the fflush(stdout); function call to your primes.c program after you print your good output. You will need this later. What does the fflush function do? o Add a programming error to the end of the primes.c program in your lab2 directory that generates a trap. Make certain you generate the trap after you display your output to the screen. o Compile both programs. o Execute your myFork.c program. What is a trap? How did you generate this trap? What displays on your terminal screen? What files are now in your current directory? o Modify your myFork.c program to only display return codes other than zero. Use fprintf to display the return code to stderr. o Execute your myFork.c program. What displays on your terminal screen? Does anything appear to have changed?

3. Modify your myFork.c program in the following manner. o Close standard output (fd1) and standard error (fd2) before spawning your child process. o Open two new files, $HOME/csc1600/module1/lab2/output and $HOME/csc1600/module1/lab2/error, also before spawning your child process. o Compile and execute your code. Describe how the contents of your file descriptor, kernel file and vnode tables change as you close and open these files, and spawn your process. You may draw this in a separate sheet of paper if you wish. What file descriptor is associated with each file in each process when you finally spawn your child process? What does each of your process write to each file? Notes: The code in your text on page 24 will not execute unless the two files you are opening already exist. To create and open a new file use the following flags in your code. You must include fcntl.h and sys/stat.h to eliminate warnings. If you find your open failing see me and we will discuss debugging. if (open("/mnt/a/nbercich/csc1600/module1/lab2/output", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) { exit(0); } 4. Modify your myFork.c program again, this time in the following manner. Replace your second open to $HOME/csc1600/module1/lab2/error with another open $HOME/csc1600/module1/lab2/output. Delete your earlier output files. Compile and execute your code. o Describe how the contents of your file descriptor, kernel file and vnode tables change as you close and open these files, and spawn your process. You may draw this in a separate sheet of paper if you wish. o What file descriptor is associated with each file in each process when you finally spawn your child process? o What does each of your process write to each file? 5. Modify your myFork.c program once more, this time in the following manner. Replace the second open of $HOME/csc1600/module1/lab2/output with dup(1). Compile and execute your code.

o o

Describe how the contents of your file descriptor, kernel file and vnode tables change as you close and open these files, and spawn your process. You may draw this in a separate sheet of paper if you wish. What file descriptor is associated with each file in each process when you finally spawn your child process? What does each of your process write to each file?

Vous aimerez peut-être aussi