首页 > 试题广场 >

Assume that a finite number of

[问答题]
Assume that a finite number of resources of a single resource type must be managed.Processes may ask for a number of these resources and—once finished—will return them.As an example,many commercial software packages provide a given number of licenses,indicating the number of applications that may run concurrently.When the application is started,the license count is decremented.When the application is terminated,the license count is incremented.If all licenses are in use,requests to start the application are denied.Such requests will only be granted when an existing license holder terminates the application and a license is returned.
The following program segment is used to manage a finite number of instances of an available resource.The maximum number of resources and the number of available resources are declared as follows:
   #define MAX_RESOURCES 5
   int available_resources = MAX_RESOURCES;
When a process wishes to obtain a number of resources,it invokes the decrease_count() function:
/* decrease available_resources by count resources */
/* return 0 if sufficient resources available, */
/* otherwise return-1 */
int decrease_count(int count) {
    if (available_resources < count)
        return-1;
    else {
        available_resources -= count;

        return 0;
    }
}
When a process wants to return a number of resources,it calls the decrease_count() function:
   /* increase available_resources by count */
   int increase_count (intcount)  {
    available_resources += count;

        return 0;
      }
The preceding program segment produces a race condition.Do the following:
a.Identify the data involved in the race condition.
b.Identify the location (or locations) in the code where the race condition occurs.
c.Using a semaphore,fix the race condition.

推荐
•Identify the data involved in the race condition:The variable available_resources.
•Identify thel ocation (or locations) in the code where the race condition occurs:The code that decrements available_resources and the code that increments available_resources are the statements that could be involved in race conditions.
•Using a semaphore,fix the race condition:Use a semaphore to represent the available_resources variable and replace increment and decrement operations by semaphore increment and semaphore decrement operations.

发表于 2018-05-05 22:16:46 回复(0)