Your Perfect Assignment is Just a Click Away

We Write Custom Academic Papers

100% Original, Plagiarism Free, Customized to your instructions!

glass
pen
clip
papers
heaphones

C Programming HW

C Programming HW

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 parent’s 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?

Order Solution Now

Our Service Charter

1. Professional & Expert Writers: Blackboard Experts only hires the best. Our writers are specially selected and recruited, after which they undergo further training to perfect their skills for specialization purposes. Moreover, our writers are holders of masters and Ph.D. degrees. They have impressive academic records, besides being native English speakers.

2. Top Quality Papers: Our customers are always guaranteed of papers that exceed their expectations. All our writers have +5 years of experience. This implies that all papers are written by individuals who are experts in their fields. In addition, the quality team reviews all the papers before sending them to the customers.

3. Plagiarism-Free Papers: All papers provided by Blackboard Experts are written from scratch. Appropriate referencing and citation of key information are followed. Plagiarism checkers are used by the Quality assurance team and our editors just to double-check that there are no instances of plagiarism.

4. Timely Delivery: Time wasted is equivalent to a failed dedication and commitment. Blackboard Experts is known for timely delivery of any pending customer orders. Customers are well informed of the progress of their papers to ensure they keep track of what the writer is providing before the final draft is sent for grading.

5. Affordable Prices: Our prices are fairly structured to fit in all groups. Any customer willing to place their assignments with us can do so at very affordable prices. In addition, our customers enjoy regular discounts and bonuses.

6. 24/7 Customer Support: At Blackboard Experts, we have put in place a team of experts who answer to all customer inquiries promptly. The best part is the ever-availability of the team. Customers can make inquiries anytime.