Illustrate semaphore awakening behavior

advertisement
UNIVERSITY OF NEBRASKA AT OMAHA
Computer Science 8530
Advanced Operating Systems
Fall 2015
Programming Assignment 2
Introduction The purpose of this assignment is to demonstrate the effects of a policy change on the
behavior of processes.
Recall that Xinu uses a First-Come-First-Served policy when selecting a process to remove from
a semaphore’s queue of waiting processes. The text indicates that selecting the highest-priority
process would violate the principle of fairness. But the POSIX standards require that with FIFO
and Round-Robin scheduling “the highest priority waiting thread shall be unblocked, and if there
is more than one highest priority thread blocked waiting for the semaphore, then the highest
priority thread that has been waiting the longest shall be unblocked.”1 (Although the statement
uses the word “thread,” you can also apply it to processes.)
Part 1 Construct a shell application named psort with two parameters, N and SEED, that does the
following:
(1) Use SEED as the seed for the linear congruential random number generator given in the
Xinu file lib/rand.c. This is accomplished by passing SEED to the srand function.2
(2) Allocate storage (using Xinu getmem) for an array that can hold N int32 values.
(3) Fill the array allocated in (2) with N random numbers generated using the rand function.
(4) Allocate another array like that created in (2), and copy the random numbers from the
previous array to the new array.
(5) Create a semaphore with an initial count of 1.
At this point you should have two identical arrays, each containing N random numbers, and a
semaphore.
(6) Create a function called bsort that has as its arguments a uint32 n, a pointer to an
array a of n int32 values, and a semaphore ID s. This function should perform a
bubble sort on the array. However, before it can access any element in the array, it must
do a wait operation on the semaphore s, and immediately after accessing the element, it
must do a signal operation. Thus to compare the values in array locations i and i+1,
the following code (or something similar to it) will be required (assume x1 and x2 are
local int32 variables):
wait(s); x1 = a[i]; signal(s);
wait(s); x2 = a[i+1]; signal(s);
if (x1 > x2) … /* or whatever comparison is needed */
1
2
From page 1275 of the POSIX System Calls document on the class web page
There are numerous references for rand and srand on the web.
Computer Science 8530
Programming Assignment 2
Page 1
This approach must also be used to store into the array; there must be a wait preceding
assignment to the array element, and a signal after storing. When the sorting has been
completed, the function is to display its process ID, a message indicating it completed
sorting, and return 0. (You may, in early versions of you work, wish to verify that the
array is actually sorted. It would be embarrassing for you, as a graduate student, to be
unable to write correct code for a bubble sort. It would also affect your grade…)
(7) Create two processes with different priorities. Each process is to execute the function
bsort, each on one of the arrays created in steps 2-3 and 4.
(8) Resume the two processes created in step 7.
(9) Wait for both processes to complete execution, then terminate the process running the
psort program.
Execute psort with various parameters – different random number seeds and significantly
different array sizes. Of course the sorting will be even slower than expected with a bubble sort,
since the wait/signal operations on the semaphore will encumber the activity. This will affect the
size of the arrays you can reasonably sort. Take note of the order in which the processes
terminate. Does the higher-priority process always finish first? How much time elapses between
termination of the processes? How are your answers affected by the size of the arrays?
Part 2 The work you do in this part of the assignment is to create a new version of the signal system
call (and anything else that needs to be changed to implement the new semaphore policy). The
change, of course, is to implement the previously-noted POSIX policy for awakening processes
that have been waiting on a semaphore. Clearly you will need to modify the code in
system/signal.c, but you may also find it appropriate to modify the code in
system/wait.c (where a process is enqueued if it must wait).
Then repeat the tests you performed in part 1 and note any differences in the results.
Submission To submit your work, create a directory called $HOME/csci8530-153-prog2 on
Loki. ($HOME is the name of your Loki home directory, which is something like
/home/astudent).
In that directory put your source code for psort, bsort, the modified signal, and any other
files you modified. Also prepare a document (e.g. Microsoft Word or PDF format) that describes
your findings from execution of your psort program with the “old” and the “new” policies for
removing processes from the semaphore’s waiting queue.
Make certain your name appears in a comment at the top of each source code file, and
prominently in your Word or PDF document. If you are working in a group, include the name of
each member of the group in the comment, and only create the submission directory in one
member’s home directory.
Due Date This assignment is due no later than 11:59 PM on Friday, November 6, 2015. At that time,
the contents of the submission directories will be collected by the instructor.
Computer Science 8530
Programming Assignment 2
Page 2
Download