7.3 Ring Exploration

Team-FLY

The following exercises test and modify Program 7.1. You can try these either by compiling the ring code or by using the fork-pipe simulator. A link to the simulator appears on the book web page. For each modification, make a new copy of the program. Suggested names for the executables are shown in parentheses.

  1. Run the program shown in Program 7.1 ( ring ).

  2. Create a makefile with descriptions for compiling and linting the program. Use make to compile the program. Add targets for additional parts of this project. (Refer to Section A.3 if you are unfamiliar with the make utility.)

  3. Make any corrections required to eliminate all lint errors and warning messages that reflect problems with the program. (Refer to Section A.4 if you are unfamiliar with the lint utility.)

  4. Run ring for several values of the command-line argument and observe what happens as the number of processes in the ring varies from 1 to 20.

  5. Modify the original ring program by putting a wait call before the final fprintf statement ( ring1 ). How does this affect the output of the program?

  6. Modify the original ring program by putting a wait call after the final fprintf statement ( ring2 ). How does this affect the output of the program?

  7. Replace the fprintf statement in the original ring program with calls to sprintf and prtastr ( ring3 ). Write a prtastr function with the following prototype.

     void prtastr(const char *s, int fd, int n); 

    The prtastr function prints the s string one character at a time to the file specified by descriptor fd using write . After outputting each character, prtastr calls the following function.

    wastesometime.c

     void wastesometime(int n) {    static volatile int dummy = 0;    int i;    for (i=0; i < n; i++)       dummy++; } 

    This just wastes some CPU time. The variable dummy is declared to be volatile so that the action of the for loop is not optimized away. Use prtastr to output the string to standard error. Pass the value of n used by prtastr as an optional command-line argument to ring3 . Use 0 as the default value for this parameter. (The single character at a time gives the ring processes more opportunity to interleave their output.) Run the program with a value of n that causes a small, but barely noticeable, delay between the output of characters .

  8. Compare the results of running the modified ring3 if you do the following.

    1. Insert wait before the call to prtastr ( ring4 ).

    2. Insert wait after the call to prtastr ( ring5 ).

  9. Modify ring1 as follows ( ringtopology ).

    1. Before the wait , each process allocates an array of nprocs elements to hold the IDs of all the processes on the ring. The process puts its own process ID in element zero of the array and sets its variable next_ID to its process ID.

    2. Do the following for k going from 1 to nprocs-1 .

      1. Write next_ID to standard output.

      2. Read next_ID from standard input.

      3. Insert next_ID into position k of the ID array.

    3. Replace the fprintf after the wait with a loop that outputs the contents of the ID array to standard error in a readable single-line format. This output tests the ring connectivity, since the ID array contains the processes in the order in which they appear upstream from a given process.

  10. Modify ringtopology by having the child rather than the parent break out of the loop ( ringchildbreak ). We are now creating a process fan instead of a chain. Determine how this affects the topology. Do we still have a ring? If using the simulator, you can just modify ring since you do not need to send anything around to ring to determine the topology.

  11. Modify ringtopology by having neither process break out of the loop ( ringnobreak ). We are now creating a process tree instead of a chain. Determine how this affects the topology. Do we still have a ring? The number of processes is now greater than nprocs . How does the number of processes depend on nprocs ? You will need to adjust the loop that sends the process IDs around the ring.

  12. Modify ring1 to be a bidirectional ring (information can flow in either direction between neighbors on the ring). Standard input and output are used for the flow in one direction. File descriptors 3 and 4 are used for the flow in the other direction. Test the connections by accumulating ID arrays for each direction ( biring ).

  13. Modify ring1 to create a bidirectional torus of processes. Accumulate ID arrays to test connectivity. A torus has a two-dimensional structure. It is like a mesh except that the processes at the ends are connected together. The n 2 processes are arranged in n rings in each dimension ( torus ). Each process has four connections (North, South, East, and West).

Use the ring simulator that is linked on the book web site to explore various aspects of this problem. Modify the ring simulator example to illustrate the effects of items 4 through 6. Make printing nonatomic to illustrate items 7 and 8. Pass data around the ring as in item 9, and construct a bidirectional ring for item 10.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net