P r o b l e m 0.
P r o b l e m 1. Consider the following program. ( F o r k ( ) is a wrapper that calls f o r k ( ) and exits in case of error, i.e., always returns a nonnegative number).
i n t m a i n ( ) { p i d _ t p i d ; i n t i ; f o r ( i = 0 ; i < 2 ; i + + ) i f ( ( p i d = F o r k ( ) ) == 0 ) { p r i n t f ( " H e l l o ! n " ) ; f f l u s h ( s t d o u t ) ; F o r k ( ) ; p r i n t f ( " H e l l o ! n " ) ; f f l u s h ( s t d o u t ) ; } p r i n t f ( " H e l l o ! n " ) ; f f l u s h ( s t d o u t ) ; r e t u r n 0 ; } a. How many times does the above program print Hello!"? b. Draw a process graph showing the processes created and when each process prints Hello". To save space, do not label the events; just use a small circle to represent a Fork", and a triangle to represent printing Hello!" (you may want to use dierent colors to represent the dierent calls to printf ). c. (Optional { Extra Credit.) If the f f l u s h ( ) calls in the above are omitted, and you pipe the output of the program to wc (which counts lines, words, and characters in its input), you get a dierent number than without the f f l u s h ( ) statements. Why is that? P r o b l e m 2. System calls are the way programs invoke the services of the operating system kernel. Th e standard C library has a function that is a wrapper" for each system call. # i n c l u d e < s t d i o . h >
# i n c l u d e < u n i s t d . h >
i n t m a i n ( ) { i n t p i p e f d s [ 2 ] ; i f ( p i p e ( p i p e f d s ) < 0 ) f p r i n t f ( s t d e r r , " c a l l t o p i p e ( ) f a i l e d " ) ; e l s e p r i n t f ( " r e a d end = %d, w r i t e end = % d . n " , p i p e f d s [ 0 ] , p i p e f d s [ 1 ] ) ; r e t u r n 0 ; } Compile the above code with -Og. Ru n gdb on it, setting a breakpoint in main, and then giving the run command. When the program stops, dis ass em bl e the p i p e function. What sequence of instructions invokes the system call? What is the system call number of pip e? P r o b l e m 3. P r o b l e m 4. Modify the code in Figure 10.3 so that the program takes an optional argument. If the argument is present ( a r g c = 2), the program copies the named le to its standard output, and otherwise it copies the standard input to standard output. T h e catch is that you must n o t modify the main loop of the program, nor are you allowed to redene STDI N_FILENO or STDOUT_FILENO. ( H i n t : use du p 2( ) .) P r o b l e m 5. Th i s problem deals with the semantics of Unix I/O. a. Suppose a process calls open with ags O_WRONLY|O_CREAT and then forks. T h e child process writes fo o". Th e parent writes bar". Both processes then close the le. What will be the nal contents of the le? Is the answer nondeterministic, or will it be the same every time? b. Suppose two separate processes concurrently open an existing le for writing with ags O_APPEND|O_WRONLY Th e initial content of the le is X X X " . Process A writes the 10 bytes A A A A A A A A A A " , while Process B writes the 10 bytes B B B B B B B B B B " . Assume the two w r i t e system calls occur very close to each oth er|that is, their machine instructions are interleaved at the lowest level. What will be the results? c. Suppose a process calls open on a le with ags O_RDONLY and then forks. Th e le contains the A S C I I string A B C D E F G H I J K " . Th e child process reads 5 bytes into a buer, calls s l e e p ( 5 ) , then exits. Th e parent process calls s l e e p ( 4 ) and then reads 5 bytes into a buer. What does the parents r e a d ( ) call put in the buer? Is the answer the same every time? d. What can you say, if anything, about where the kernel stores the read and write pointers for a le that is opened by more than one process? In particular, are they kept in the per-process open le table, or the system-wide open le table? Or neither?
