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
BaLLatris [955]
3 years ago
11

Use semaphore(s) to solve the following problem. There are three processes: P1, P2, and P3. Each process Pi has a segment of cod

es Ci, i=1, 2, 3. These three processes are executed only once, i.e., no repeat or loop at all, and their executions can start at any time. Your goal is to ensure that the execution of C1, C2, and C3 must satisfy the following conditions:
a. If C1 is executed ahead of C2 and C3, C2 must be executed ahead of C3.
b. Otherwise, C1 must be executed after both C2 and C3 are executed. In this case, the order of C2 and C3's execution doesn't matter. One of the possible execution orders is demonstrated below. Obviously, two other possible sequences in time are C2, C3, C1 and C3, C2, C1.
Please write your algorithm level code for semaphore initialization and usage in each code segment. [Hint: no if statements should ever be used. All you need is some semaphore function calls surrounding C1, C2, and C3 and their initial values.]
Computers and Technology
1 answer:
Lisa [10]3 years ago
4 0

Answer:

See explaination

Explanation:

Here we will use two semaphore variables to satisfy our goal

We will initialize s1=1 and s2=1 globally and they are accessed by all 3 processes and use up and down operations in following way

Code:-

s1,s2=1

P1 P2 P3

P(s1)

P(s2)

C1

V(s2) .

P(s2). .

. C2

V(s1) .

P(s1)

. . C3

V(s2)

Explanation:-

The P(s1) stands for down operation for semaphore s1 and V(s1) stands for Up operation for semaphore s1.

The Down operation on s1=1 will make it s1=0 and our process will execute ,and down on s1=0 will block the process

The Up operation on s1=0 will unblock the process and on s1=1 will be normal execution of process

Now in the above code:

1)If C1 is executed first then it means down on s1,s2 will make it zero and up on s2 will make it 1, so in that case C3 cannot execute because P3 has down operation on s1 before C3 ,so C2 will execute by performing down on s2 and after that Up on s1 will be done by P2 and then C3 can execute

So our first condition gets satisfied

2)If C1 is not executed earlier means:-

a)If C2 is executed by performing down on S2 then s2=0,so definitely C3 will be executed because down(s2) in case of C1 will block the process P1 and after C3 execute Up operation on s2 ,C1 can execute because P1 gets unblocked .

b)If C3 is executed by performing down on s1 then s1=0 ,so definitely C2 will be executed now ,because down on s1 will block the process P1 and after that P2 will perform up on s1 ,so P1 gets unblocked

So C1 will be executed after C2 and C3 ,hence our 2nd condition satisfied.

You might be interested in
A marketing firm has been hired to help a client understand how their online brand is perceived. The firm has set up a system to
KiRa [710]

Answer:

Automated Reasoning

Explanation:

Automated reasoning is a type of Natural language understanding(NLP) in computer science that makes logical inferences based on information or data previously gathered. Automated reasoning allows computers make intelligent decisions and reason logically based on data gathered. For example, a system is based to scan online reviews and conclude on the ones that are negative reactions based on previous data.

3 0
3 years ago
Assuming that a user enters 15 as input, what is the output of the following code snippet? Scanner in = new Scanner(System.in);
snow_tiger [21]

Answer:

The output of the following code is "The number is SMALL!"

Explanation:

When the following java code is executed. Firstly the program will ask for "Please enter a number" after input the number 15 as per the question, than it check the condition which is "number > 20", if the number is greater than 20 it shows "The number is LARGE!", but the given number is smaller than 20.

3 0
3 years ago
Question 1
Alexxandr [17]

Explanation:

so, what do you think ?

I am sure you have used a computer or a smart phone yourself.

how many clicks or taps do you want to do before you you get what you were looking for ?

hmmmm ?

as few a possible, right ?

ideally, of course, this is one (1) click or tap.

7 0
3 years ago
I need help with doing a debate for ela. The debate topic is "do you think we are to dependable on our laptops/cellphones. My te
Mama L [17]

Answer:

I think we are so this is hard lol

Explanation:

we are not too dependable

things that show this is library are still open, school sports teams still flourish(if we were too depending on them they wouldnt) they help us plan ways to interact in person if we were too dependent businesses would struggle more and only be online

this is hard if I can think of any others I'll come back I'm sorry if this isn't any help

7 0
3 years ago
Read 2 more answers
The most important source of income on the Galápagos Islands is also a potential threat to diversity. What is it?
Lapatulllka [165]
How would the War Powers Act have impacted President Truman's initial ability to deploy US soldiers into the Korean War?

A. It would have granted him more power to use US forces.
B. It would have allowed US forces to fight only under the United Nations.
C. It would have forced him to seek Congress's approval for US involvement.
D. It would have prevented him from approving the Bay of Pigs invasion.
4 0
4 years ago
Other questions:
  • What is a nonlinear presentation
    9·2 answers
  • How to write a program that prompts the user to input two POSITIVE numbers — a dividend (numerator) and a divisor (denominator).
    13·1 answer
  • The overall purpose of a food guide is________.
    11·1 answer
  • The Earth's _______ is ductile overall, tending to flow very slowly and deform in a _______ manner.
    14·2 answers
  • Eugene wants to indent a paragraph, but the ruler is not present. Which tabs could Eugene use in order to accomplish his goal?
    14·1 answer
  • A customer survey asked respondents to indicate their highest levels of education. The only three choices in the survey were hig
    15·1 answer
  • Provides images of weather systems, and helps to track storms at different altitudes
    7·1 answer
  • 10 sentences about computer parts.
    5·1 answer
  • 8. Which of the following is an output device
    14·1 answer
  • write a program that computes an integer's checksum. to compute the checksum, break the integer into its constituent
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!