Question

1. Monitor Exercise: A file is to be shared among different processes, each of which has a unique priority number. The file c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include <stdio.h>
int n=10;// file id number
int sumOfID=0; / Shared var that contains the sum of the process ids currently accessing the file /
int numOfProcessWaiting=0; / Number of process waiting on the semaphore okToAccess /
semaphore mutex=1; / Semaphore variable ;) /
semaphore okToAccess=0; / The synchronization semaphore /


void get_access(int pid)
{
sem_wait(mutex);
/*all unique numbers associated with all the processes currently accessing the file must be less than n*/
while(sumOfID+pid > n)
{
numOfProcessWaiting++;
sem_signal(mutex);
sem_wait(okToAccess);
sem_wait(mutex);
}
sumOfID += pid;
sem_signal(mutex);
}
void release_access(int pid)
{
int i;
sem_wait(mutex);
sumOfID -= pid;
for (i=0; i < numOfProcessWaiting;++i) {
sem_signal(okToAccess);
}
numOfProcessWaiting = 0;
sem_signal(mutex);
}

void Monitor()
{
int id;
for(id=0;id<20;id++)
{
get_access(id);
}

for(id=0;id<20;id++)
{
release_access(id);
}
}
int main()
{
Monitor();
}

Note: Use "Semaphore class" code with this file to get execution sucess.

As per your requirement following above code is written along with comments for each and important line of codes.

Add a comment
Know the answer?
Add Answer to:
1. Monitor Exercise: A file is to be shared among different processes, each of which has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1 Overview and Background Many of the assignments in this course will introduce you to topics in ...

    1 Overview and Background Many of the assignments in this course will introduce you to topics in computational biology. You do not need to know anything about biology to do these assignments other than what is contained in the description itself. The objective of each assignment is for you to acquire certain particular skills or knowledge, and the choice of topic is independent of that objective. Sometimes the topics will be related to computational problems in biology, chemistry, or physics,...

  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

  • Summary Write a program that demonstrates the skills you’ve learned throughout this quarter. This type of...

    Summary Write a program that demonstrates the skills you’ve learned throughout this quarter. This type of project offers only a few guidelines and requirements, allowing you to invest as much time, effort and imagination as you want.  Submit your java programs (*.java) and any other I/O (*.txt) via Canvas  You’ve worked quite hard to make it this far, so have a bit of fun with this project! Design Brief: Use Case Scenario You are hired to develop a...

  • Below is the information: It is important to understand the different leadership styles employed by nursing...

    Below is the information: It is important to understand the different leadership styles employed by nursing leaders in healthcare organizations and to understand their significance on nursing practice and patient outcomes, for better or for worse. Objective: Read the articles from Nursing Standard (PDF) and Bradley University (PDF). In -250 words, formulate an opinion on the following: 1. Reflect on an occasion where you experienced ineffective leadership (doesn't have to be in the hospital). What behaviors did they display? What...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

  • Using the book, write another paragraph or two: write 170 words: Q: Compare the assumptions of...

    Using the book, write another paragraph or two: write 170 words: Q: Compare the assumptions of physician-centered and collaborative communication. How is the caregiver’s role different in each model? How is the patient’s role different? Answer: Physical-centered communication involves the specialists taking control of the conversation. They decide on the topics of discussion and when to end the process. The patient responds to the issues raised by the caregiver and acts accordingly. On the other hand, Collaborative communication involves a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT