1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Margarita [4]
3 years ago
13

A file is to be shared among different processes, each of which has a unique number. The file can beaccessed simultaneously by s

everal processes, subject to the following constraint: the sum of all uniquenumbers associated with all the processes currently accessing the file must be less than n. Write amonitor to coordinate access to the file.Your solution should use condition variables. Assume abroadcast()operation can be invoked on acondition variable c to resume all processes suspended on c. Hint: Your monitor should contain twofunctions: one function is called before a process accesses a file and the other function is called after aprocess accesses a file.
Computers and Technology
1 answer:
Finger [1]3 years ago
5 0

Answer:

int sumid=0;    /* Shared var that contains the sum of the process ids currently accessing the file */

int waiting=0;   /* Number of process waiting on the semaphore OkToAccess */

semaphore mutex=1;   /* Our good old Semaphore variable ;) */

semaphore OKToAccess=0;    /* The synchronization semaphore */

get_access(int id)

{

    sem_wait(mutex);

    while(sumid+id > n) {

        waiting++;

        sem_signal(mutex);

        sem_wait(OKToAccess);

        sem_wait(mutex);

    }

    sumid += id;

    sem_signal(mutex);

}

release_access(int id)

{

    int i;

    sem_wait(mutex);

    sumid -= id;

    for (i=0; i < waiting;++i) {

        sem_signal(OKToAccess);

    }

    waiting = 0;

    sem_signal(mutex);

}

main()

{

    get_access(id);

    do_stuff();

    release_access(id);

}

Some points to note about the solution:

release_access wakes up all waiting processes. It is NOT ok to wake up the first waiting process in this problem --- that process may have too large a value of id. Also, more than one process may be eligible to go in (if those processes have small ids) when one process releases access.

woken up processes try to see if they can get in. If not, they go back to sleep.

waiting is set to 0 in release_access after the for loop. That avoids unnecessary signals from subsequent release_accesses. Those signals would not make the solution wrong, just less efficient as processes will be woken up unnecessarily.

You might be interested in
Create a method called nicknames that passes an array as a parameter. Inside the method initialize it to hold 5 names of your ch
Gre4nikov [31]
<h2>Answer:</h2>

    //======METHOD DECLARATION=========//          

    //method name: nicknames                                          

    //method return type: void                                            

    //method parameter: an array reference                    

    public static void nicknames(String [] names){      

       //initialize the array with 5 random names              

       names = new String[] {"John", "Doe", "Brian", "Loveth", "Chris"};

       //using an enhanced for loop, print out the elements in the array

       for(String n: names){

           System.out.print(n + " ");

       }

       

    }

<h2>Explanation:</h2>

The program is written in Java. It contains comments explaining important parts of the code. Kindly go through these comments.

A few things to note.

i. Since the method does not return any value, its return type is <em>void</em>

ii. The method is made public so that it can be accessible anywhere in and out of the class the uses it.

iii. The method is made static since it will most probably be called in the static main method (at least for testing in this case)

iv. The method receives an array of type <em>String </em>as parameter since the names to be stored are of type <em>String</em>.

v. The format of initializing an array manually should follow as shown on line 7. The <em>new</em> keyword followed by the array type (String), followed by the square brackets ([]) are all important.

vi. An enhanced for loop (lines 9 - 11) is a shorthand way of writing a for loop. The format is as follows;

=> The keyword <em>for</em>

=> followed by an opening parenthesis

=> followed by the type of each of the elements in the array. Type String in this case.

=> followed by a variable name. This holds an element per cycle of the loop.

=> followed by a column

=> followed by the array

=> followed by the closing parenthesis.

=> followed by a pair of curly parentheses serving as a block containing the  code to be executed in every cycle of the loop. In this case, the array elements, each held in turn by variable n, will be printed followed by a space.

A complete code and sample output for testing purposes are shown as follows:

==================================================

public class Tester{

    //The main method

    public static void main(String []args){

       

       String [] names = new String[5];

       nicknames(names);

    }

   

  //The nicknames method

    public static void nicknames(String [] names){

       names = new String[] {"John", "Doe", "Brian", "Loveth", "Chris"};

       for(String n: names){

           System.out.print(n + " ");

       }

       

    }

}

==================================================

<h2>Output:</h2>

John Doe Brian Loveth Chris

<em>NB: To run this program, copy the complete code, paste in an IDE or editor and save as Tester.java</em>

6 0
3 years ago
Austin has a report due at school tomorrow, but has not started working on it yet. Now he is worried about getting a bad grade.
liq [111]

Answer:

Explanation:

Question One

He could have read other sites with other points of view and summarized that. Most importantly he could have used footnotes that gave credit to every idea that he used.

He could have put the material down and begin to write. The words would have come out in his awkward style. He would still need to footnote the ideas. That is most important.

Question Two

It is both. Plagiarism is a serious crime if it involves many people being affected by what he wrote.

But it is totally unethical at any level. Trying to pass off something that isn't yours is totally unethical. It should never be done. It shames the subject which is shown to be unimportant and it shames the instructor (he knew enough to catch the plagiarism), and it shames the person getting caught.

3 0
3 years ago
Which of the following is not true of both the ACT and the SAT?
nlexa [21]
The answer for that is c
6 0
4 years ago
Read 2 more answers
Meg[] elements = {new Lois(), new Stewie(), new Meg(), new Brian()}; for (int i =0; i
Stells [14]

Complete question is attached as snapshot.

Answer:

This will result in a compilation error.

Here's the inheritance hierarchy:

Meg -> Lois -> Brian -> Stewie

Now Meg has 2 public Fxns, a() and toString().

Lois inherits from Meg and all its public functions as well but, overrides the a() fxn of its super class.

So finally Lois has fxns a(), b() and toString().

Brian extends Lois and inherits all the above listed fxns of Lois, but overrides the b() and toString() fxns.

Stewie extends Brian and inherits all the fxns of Brian, but overrides the a() and toString() fxns.

In the Main driver fxn, we call a() and b() methods of all these classes but, the base class Meg has no b() and it is not extending any class so its not available in its scope.

This results in a compilation error.

Explanation:

7 0
3 years ago
.…………….. Are devices that routepackets of data between two or more networks.
Masja [62]

Answer: Routers

Explanation:

Routers are the device that route packets of data between two or more networks as, core router is that which transmitted data from the other router and route packets based on the network information layers and can forward the other packets based on the data link layers information, the layer on which the bridges are operated.

8 0
4 years ago
Read 2 more answers
Other questions:
  • You are given a network of 10.50.24.0/21, which contains 2,048 addresses. what subnet mask should you use to divide this into fo
    7·1 answer
  • Which of the following allows you to view and access important information about your document all in one location?
    14·1 answer
  • How can styles be used in Word? Check all that apply. to standardize the font size of a title in a Word document to standardize
    9·2 answers
  • An airline company would like to easily locate lost luggage. What technology would make it easier for them to locate the luggage
    6·2 answers
  • A TCP entity opens a connection and uses slow start. Approximately how many round-trip times are required before TCP can send N
    11·1 answer
  • FREE BRAINLIEST!!!
    14·2 answers
  • Carmina works at a fast-food restaurant. During the slow afternoon hours, Carmina always find projects to keep her busy, like wa
    6·1 answer
  • The specific instructions that a computing device needs to boot up and control the hardware
    12·1 answer
  • Government entities may pressure upstream Internet service providers to _____. a. track and monitor the Internet activities of i
    11·1 answer
  • you are tasked with managing multiple servers. you want to manage them all from one server manager interface so you don't have t
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!