This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Semaphore in Java/Operating Systems  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!!!
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


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 Employeeβ 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 ofcourse) General questions in semaphores:
What's the basic difference between mutex and p,v?
and how should i understand when to use Mutex or not?
 
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question too difficult for this forum: moving to our Threads forum.

And welcome to the Ranch
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Question too difficult for this forum: moving to our Threads forum.

And welcome to the Ranch


where exactly is the "Threads" forum??
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

stathis papapa wrote:. . . where exactly is the "Threads" forum??

Here it is.
 
Saloon Keeper
Posts: 9997
208
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need one Semaphore to signal to B that A is done with the outside operation, and you need one Semaphore to signal to A that B is done with the outside operation.

However, I think in the assignment they forgot something. At the end, both workers are accessing a shared variable, so use of it must be synchronized as well. You can easily do this by using an AtomicInteger for numberOfWorks.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please do yourself a favor and don't call the semaphores p and v.

Instead, name them insidePermitForA and insidePermitForB. After you've written the correct code, you will see that this reads very naturally and makes the code clear to anyone trying to read it.
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Also, please do yourself a favor and don't call the semaphores p and v.

Instead, name them insidePermitForA and insidePermitForB. After you've written the correct code, you will see that this reads very naturally and makes the code clear to anyone trying to read it.



okay,i will call them differently than p and v.
As i said i am studying them now...

I wrote something but i don't know if that is correct...
So,do not yell at me please!!

 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should be something like that??
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's definitely not Java.

Have you tried compiling your code yet?
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That's definitely not Java.

Have you tried compiling your code yet?



I know that it's not Java.
I must make 2 things..
The first one is to write it using semaphores in a simple form (In operation systems) and then write it in Java using classes etc.

The problem is I don't understand them. I knew I had to use 2 semaphores like you said but that's all.. I don't know how to use them.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I consider your code as pseudo-code, but let's establish a naming rule for identifiers. Start variable names with a lower-case letter, and then use an upper-case letter for every other word that the identifier consists of. So use names like sumOfWork, not sumofwork.

Now, get rid of the variables sumOfWorkA and sumOfWorkB. They're not used, and they don't have an initial value. Just increment the shared variable sumOfWork.

You're not using your mutual exclusion lock correctly. Both EmployeeA and EmployeeB are waiting for the lock, and when one of them gets it, they never release it. Even if they released it, it would completely prevent the other employee from doing any work at the same time, so your program would run sequentially, not parallel. The purpose of mutex is to prevent both workers from accessing shared variables at the same time, not prevent them from doing any work at all. Use mutex to protect sumOfWork.

Inside your loops, you're not doing any work at all. You're just signalling something all the time. You should only signal something after you're done doing something. Besides, the variables that you use to signal haven't been declared.

The biggest problem is that you've completely changed the semantics of the original program. I thought that both workers needed to perform two functions, an outside and an inside function. Where have those gone?

I can give you more tips, but you must first tell me what functions are defined on a lock, and what functions are defined on a semaphore. I see you're using wait() on a lock, and you're using signal() on a semaphore. What are their inverse operations?
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possibility is that you're using the same primitive for a mutual exclusion lock and a binary semaphore. In this case I'm assuming that acquiring the primitive occurs with wait(), and releasing the primitive occurs with signal(). In that case, let us rename the semaphores so they make sense in context of your pseudo-code. When you use Java later, rename them back to my initial proposal, because those names with make more sense for Java.

Use the following variables:

You don't need anything else. Use the mutex variable to prevent access to sumOfWork by both workers at the same time. Use aIsDoneOutside to signal from worker A to worker B that worker A is done with the outside function. Use bIsDoneOutside to signal from worker B to worker A that worker B is done with the outside function.
 
Bartender
Posts: 20576
121
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"P" and "V" are conventions that Edsger Djikstra used to denote the two possible operations on a semaphore - One requests the resources guarded by the semaphore and one releases the semaphore for the next sequential requester, First-In/First-out (FIFO). The "P" and "V" names are actually abbreviations of something more meaningful - if you speak Dutch.

So definitely don't name a semaphore "p" or "v". Too confusing.

I rather liked the old IBM terms myself. They had a pair of macros named "ENQ" and "DEQ" to enqueue and dequeue  on a resource, and the process as a whole was called serialization, because it allowed access to that resource serially as though requesters were people forming an orderly queue to go through a turnstile. Regardless of the naming, however, the basic process was essentially the same.

That did get confusing with Java, though, since in Java and Microsoft, "serialization" refers to the process of marshalling and unmarshalling data for the purpose of storage and/or transmission on sequential media.
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am still confused!

Let's take it from the beginning right?

i have this exercise:



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 Employeeβ the "Β_works_inside()" runs only when Α_works_outside() is finished
3.Variable numberofworks must be the sum of numberofworksA+numberofworksB


So in case there is no while loop and the 1 & 2 parts should be somrthing like this?



Now i must find out how to write it in while loop and what to do with the shared variable numberOfworks


verybody says that semaphores are very simple but i still don't get them.Perhaps i am stupid
 
Tim Holloway
Bartender
Posts: 20576
121
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I present my good friend, the Wikipedia?

https://en.wikipedia.org/wiki/Semaphore_(programming)

Actually, this isn't the simplest explanation I've seen, but it's relatively short.

One thing that may be confusing you is that in Java, we have the concept of "synchronized" code. Java manages synchronized code with semaphores, but those semaphores are hidden inside the Java synchronization mechanism, so that you don't really see the "P" and "V" methods at work - or even the actual semaphore.

Java 5 did add an explicit Semaphore class, but the original synchronization features are sufficient for most people.

There's also something called a "spin lock" (and locks in general), but they are used for situations where the overhead of placing a thread on a waiting queue and pulling it off takes too much time. You use a spin lock when you need the locked resource within a few microseconds at most, and you're willing to simply spin around in a loop testing until you can get it instead of doing a thread-wait. That's mostly used by low-level OS code.
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:May I present my good friend, the Wikipedia?

https://en.wikipedia.org/wiki/Semaphore_(programming)

Actually, this isn't the simplest explanation I've seen, but it's relatively short.

One thing that may be confusing you is that in Java, we have the concept of "synchronized" code. Java manages synchronized code with semaphores, but those semaphores are hidden inside the Java synchronization mechanism, so that you don't really see the "P" and "V" methods at work - or even the actual semaphore.

Java 5 did add an explicit Semaphore class, but the original synchronization features are sufficient for most people.

There's also something called a "spin lock" (and locks in general), but they are used for situations where the overhead of placing a thread on a waiting queue and pulling it off takes too much time. You use a spin lock when you need the locked resource within a few microseconds at most, and you're willing to simply spin around in a loop testing until you can get it instead of doing a thread-wait. That's mostly used by low-level OS code.



i have already read all these and everything i could find in google and still i do not understand anything.Perhpas i should quit solving this exercise.

Stephan van Hulst wrote what i need to do (and i thank him for that) but i don't get it.
and if i can't write it in pseudocode for operation system..probably i won't be able to write it in java
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your own words, tell us what a Mutex is and what operations you can perform on it and what those operations do. Do the same thing for Semaphore.
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:In your own words, tell us what a Mutex is and what operations you can perform on it and what those operations do. Do the same thing for Semaphore.



you want the truth??
i don't understand..That's why i am trying to solve this exercise..in case i understand their purposes..

only through exercises i understand theories.......
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know whether this Wikipedia article is any good, but many of them are good.
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I don't know whether this Wikipedia article is any good, but many of them are good.



i read it and some others too.
still i don't get how to use them in my exercise.

Between, i have never seen semaphores in a java program.So,why we learn them??
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's important to know these primitives because they form the basis of more complex and abstract synchronization mechanisms that ARE used in Java more regularly.

Mutexes and binary semaphores are very similar and may in fact be implemented the same way, or even BE the same underlying type. We name them differently because they solve different problems. Let's describe those problems first:

When two threads access the same piece of memory, they may actually see two very different states stored in that piece of memory. For instance, if thread A overwrites a variable x that has an initial value 5 with a new value 7, it may still appear to thread B that the value of x is 5. This is just a consequence of the way modern computers function, and the reason why is very complex. For now, just accept the fact that two threads may see different versions of the same piece of memory.

To make sure that two threads agree with each other on the value that a shared variable is supposed to have, we need to tell both of them when they read from or write to the variable that they need to make sure that they have the most recent value of that variable, and that no other thread is writing to the variable at the same time.

This is what a mutex (mutual exclusion lock) does. When a thread reaches a part of the code that is protected by a mutex, it waits until it is sure that no other thread is in a piece of code that is also protected by the same mutex. When it's sure, the thread continues into the protected code, and we say that "the thread has acquired the lock". Any other thread that is waiting on the same mutex must stay waiting until the thread that has acquired the lock releases that lock again.

If thread A writes 7 to variable x in a piece of code protected by mutex m, and thread B tries to read variable x in a different piece of code that is also protected by mutex m, then thread B MUST wait until AFTER thread A has released mutex m before it can execute the code that reads x, thereby ensuring that it reads the value 7 that thread A wrote.

In your case, the shared variable is sumOfWork, and whenever you read from it or write to it, you must protect it with a mutex. Let's assume that the operations on a mutex are called acquire() and release(), and that the mutex variable is called lock. Protected access then looks like this:

That's how you protect shared variables. However, sometimes the goal of synchronization between threads is not to access the same variable, but to notify another thread that some event has occurred. This is where semaphores come in. A semaphore is similar to a mutex in that a thread needs to wait until the lock is free, but the difference is that the thread that releases the lock is not inside a protected piece of code itself. It just releases a lock it doesn't own as a way of notifying a different thread that something has happened.

Let's assume that the operations on a semaphore are called await() and signal(). await() makes the thread wait until it can acquire the lock and signal() releases the lock. If one of your semaphores is called aIsDoneOutside, it would look like this:
and:

Thread B waits before it executes bWorksInside() until thread A has signalled it that it is done executing aWorksOutside().

Now it's up to you to combine the use of a mutex and semaphores into a complete program.
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It's important to know these primitives . . . combine the use of a mutex and semaphores into a complete program.



thanks i will try to solve it in the 4 hours and i will show you my solution.Let's hope i succeed!
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys I would like to thank you for the time you spend in this thread.
But still I can't solve the exercise so I decided to stop.
Perhaps I will look it again later but I have 2 more days for this one.
I don't understand anything. I have read the theory many times and still reading it and it's like i am looking at some  alien language.
Sorry for the trouble I caused you.

Perhaps I am so stupid and I can't understand anything. 😭😭😭
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We'd like to help you, but you have to be specific in what you don't understand. What is so alien to you?
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:We'd like to help you, but you have to be specific in what you don't understand. What is so alien to you?


Well, everything.
Stephan you told me what I must do. You either wrote some of what I must use!
And I still can't combine them.
It's very confusing.
Even if it's the simplest exersice ever (perhaps it is, because its grade is 12/100 in my paper) I can't solve it.
I do not know what's wrong with me but this moment I can't solve it.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me write a bit of Java code, and then you can explain to me what the application does. Forget about mutexes and semaphores and such for now.
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan I think you make it more complicated. This moment I want to understand and write the exercise  in pseudocode.
If I can't understand and write it in pseudocode like the faulty one some posts before and I won't write any Java.
As for the code you wrote I don't get what .sleep is.!

You have a class worker and instances and some methods
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The pseudo-code is not helping you, because there are no well-defined operations on mutexes and semaphores, and I think it's time that you saw some of this stuff in action, because I think you have trouble visualizing what is happening.

The sleep() method just causes the current thread to pause for a few milliseconds, to simulate that it is working on a time-consuming job.

Here's an example output from the above program:

As you can see, the program consists of two workers who both repeatedly do something, and then they do something else. Worker A is faster than Worker B, as you can see by the fact that Worker A has already finished doing something else the first time, before Worker B has even started doing something else.

Can you relate this output to the code I presented earlier? Why is Worker A faster than Worker B?
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worker A has 500
Worker B has 1000
If that's what you are asking

But the teacher want to write pseudocode.
Change the code in the first post using semaphores  to do the things they want...

Why you say pseudocode isn't helping?
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm saying pseudo-code isn't helping you right now because in pseudo-code we keep ending up with confusing definitions such as whether mutex and semaphore are different types and what operations are valid for them. Furthermore, trying to explain what these operations do so far has not been clear to you, so I think it's good that you see their results in action. Pseudo-code is good, but then you first have to understand the underlying concepts. When you understand how mutexes and semaphores influence the application, you can try to write the pseudo-code that will make your teacher happy. Otherwise you'll just be guessing.

Now, you're correct in that A works faster than B, because it sleeps less long. We'll modify the application so that now, A and B count the number of times they've looped, and it's printed out in the end:



This prints:

Do you understand so far? If you do, we can move on to semaphores.
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't get the result but i got what you are saying
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What don't you understand about the result?
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What don't you understand about the result?


why 7 and 4?

anyway,i get what this code does(i think)
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deadline is over.
i just send the rest exercises.
thanks for the help anyway..
in 2 weeks i will study the solution of this exercise....
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that the deadline is over can someone solve the exercise so i can compare with the one teacher will give us i n 2 weeks???
If you know,how to solve it of course!
 
stathis papapa
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
noone???
hahaha!
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I know how to solve the solution, but I don't think you'll benefit from knowing the solution without obtaining it by yourself, with guidance if need be. Do you want to continue where we left?
 
Die Fledermaus does not fear such a tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!