0

I am new in the forum and in programming in general.At the moment i study "Operation Systems" and Java. This week we started the topic "Semaphores" and it seems to me that it's a bit confusing. I have this exercise:

shared variable numberofworks = 0


    EmployeeA 
    {
        while (True) {
            A_works_outside();
            A_works_inside();
            numberofworks =numberofworks + 1;
        }
    }

    EmployeeB
    {
        while (True) {
            B_works_outside();
            B_works_inside();
            numberofworks =numberofworks + 1;
        }
    }

well,the exercise wants to use semaphores (P and V) so

  1. In i repetition of EmployeeA the A_works_inside() runs only when B_works_outside() is finished
  2. In i repetition of EmployeeB the Β_works_inside() runs only when Α_works_outside() is finished

  3. Variable numberofworks must be the sum of numberofworksA + numberofworksB

I must write the code in Java and in "operation system" before helping me in this exercise (if you can of course) General questions in semaphores: What's the basic difference between mutex and a sempahore? and how should i understand when to use mutex or not?

Ali Ben Zarrouk
  • 1,891
  • 16
  • 24
  • `P=Semaphore.acquire` and `V=Semaphore.release` but that is probably entirely clear. Where Dijkstra coined P = Dutch passeren = pass, V = vrij geven = give free = release. – Joop Eggen Mar 26 '19 at 16:08

1 Answers1

0

Trying to use a resource protected by a Mutex:

- Hi! Can I ?
*If free*
- Yup go on.
*Else*
- No, go away!

Trying to use a resource protected by a Semaphore:

- Hi! Can I ?
*If free*
- Yup go on.
*Else*
- No, wait in the queue!
Ali Ben Zarrouk
  • 1,891
  • 16
  • 24
  • See this: https://stackoverflow.com/questions/62814/difference-between-binary-semaphore-and-mutex – NormR Mar 26 '19 at 15:05