Open Side Menu Go to the Top

03-02-2016 , 12:23 PM
I will try to look at your code later. This looks like a restatement of the Dining Philosophers Problem but could have just not read closely enough (posting from my phone).
Programming homework and newbie help thread Quote
Programming homework and newbie help thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Programming homework and newbie help thread
03-03-2016 , 12:17 AM
so let me see if I get the idea for what to do with my project thing.

have a wysiwyg editor, save the html/css/images to the database as a file/binary. Since I know all the files are going to be html with embedded css, no reason to give a field for data type, yes?

Once I have the files saved, I can render them with an iframe tag, but only if i have a valid source url to point to, which I guess will be somewhere in the database somehow. And somehow the images will also be pulled in a similar manner. I think.

I don't much care for this project.
Programming homework and newbie help thread Quote
03-03-2016 , 06:59 AM
Quote:
Originally Posted by Mavoor
This is the solution that I came up with
Code:
Initialization of shared variables and semaphores:
	Semaphore sem_condition.init(2);
	Semaphore sem_me.init(1);
	boolean[] occupiedShower = { false, false }
	//
	//sem_condition is responsible for condition synchronization, and only allows two swimmers 
	//  to enter the shower area at the same time. If more than two swimmers try to enter the showers at                
	//  the same time, only the first two can enter, others have to wait in line.
	//sem_me is responsible for mutual exclusion, to make sure that two swimmers in the shower area won't try 
        //  to occupy the same shower simultaneously.
	//occupiedShower knows whether the showers are occupied or not.
	
Swimmer code:
Swimmer {
	enterShowerArea {
		//Swimmer tries to enter shower area, waits if it's full.
		sem_condition.wait(); 
		
		//Swimmer tries to pick a shower, waits if another swimmer is picking a shower.
		sem_me.wait();  
		
		//Swimmer checks if first shower (shower 0) is occupied.
		if(occupiedShower[0]){  
			//If it is occupied, then swimmer occupies second shower (shower 1).
			occupyShower(1);
		}
		else {
			//If first shower isn't occupied, then swimmer occupies it.
			occupyShower(0);
		}
		
		//Swimmer signals that he has picked a shower,if another swimmer was waiting then now he can pick.
		sem_me.signal();
		
		//Swimmer signals that he has left the shower area, if another swimmer was waiting now he can enter.
		sem_condition.signal();
	}
	
	occupyShower(int showerNumber){
		occupiedShower[showerNumber] = true;
		//…Wash whole body thoroughly with soap before big swimming competition….
		occupiedShower[showerNumber] = false;
	}
}
It was simple/obvious enough, just using another semaphore for mutual exclusion. I knew it was pretty simple, just had a brief lapse of thought
Sorry about the big indentation
Quote:
Originally Posted by adios
I will try to look at your code later. This looks like a restatement of the Dining Philosophers Problem but could have just not read closely enough (posting from my phone).
Your code looks fine to me but with the caveat that I am not familiar with the language used. Then the link I posted about Dining Philosophers problem has a solution in Python. The Python solution has a lock for each fork which in your case would be each shower. I like your code better.
Programming homework and newbie help thread Quote
03-03-2016 , 09:49 AM
Thanks for reviewing my code adios. We were asked to provide commented pseudocode, and my code isn't in any real language but rather some kind of mashup between Java and pseudocode.

Looking back at it now I found a small error in the logic.
Assuming the shower area has two swimmers, the first swimmer should pick a shower, occupy it, and then signal the other swimmer that he is now free to pick the one remaining shower. Only after that should the first swimmer finish showering and leave the shower, and shower area.
In the above code the first swimmer picks a shower, occupies it, showers, leaves the shower, and only then signals to the other swimmer that he can now pick a shower.

Code:
//Swimmer tries to pick a shower, waits if another swimmer is picking a shower.
sem_me.wait();  
		
//Swimmer checks if first shower (shower 0) is occupied.
if(occupiedShower[0]){  
	//If it is occupied, then swimmer occupies second shower (shower 1).
	occupyShower(1);
}
else {
	//If first shower isn't occupied, then swimmer occupies it.
	occupyShower(0);
}
		
//Swimmer signals that he has picked a shower,if another swimmer was waiting then now he can pick.
sem_me.signal();
The problem is that the occupyShower() function sets the boolean variable occupiedShower[i] to true at the beginning, and then to false again at the end. So the swimmer really enters and leaves the shower during the occupyShower() process, and only after that he signals to the other swimmer.
This results in there never being more than one shower in use at a time, and really makes the second shower obsolete in a sense.

It's a simple enough fix though. Remove the occupiedShower[showerNumber] = false; statement from the occupyShower() function and rather put it in a new function leaveShower() that gets called after signaling that a shower can be picked by another swimmer.

Thanks for linking that Dining Philosopher's problem as well, it looks like an interesting read.

I have more assignments of similar nature coming up, and might post the as well as my solutions (in spoilers ) if anyone finds this to be interesting
Programming homework and newbie help thread Quote
03-03-2016 , 12:05 PM
Quote:
Originally Posted by Mavoor
Thanks for reviewing my code adios. We were asked to provide commented pseudocode, and my code isn't in any real language but rather some kind of mashup between Java and pseudocode.

Looking back at it now I found a small error in the logic.
Assuming the shower area has two swimmers, the first swimmer should pick a shower, occupy it, and then signal the other swimmer that he is now free to pick the one remaining shower. Only after that should the first swimmer finish showering and leave the shower, and shower area.
In the above code the first swimmer picks a shower, occupies it, showers, leaves the shower, and only then signals to the other swimmer that he can now pick a shower.

Code:
//Swimmer tries to pick a shower, waits if another swimmer is picking a shower.
sem_me.wait();  

//Swimmer checks if first shower (shower 0) is occupied.
if(occupiedShower[0]){  
//If it is occupied, then swimmer occupies second shower (shower 1).
occupyShower(1);
}
else {
//If first shower isn't occupied, then swimmer occupies it.
occupyShower(0);
}

//Swimmer signals that he has picked a shower,if another swimmer was waiting then now he can pick.
sem_me.signal();
The problem is that the occupyShower() function sets the boolean variable occupiedShower[i] to true at the beginning, and then to false again at the end. So the swimmer really enters and leaves the shower during the occupyShower() process, and only after that he signals to the other swimmer.
This results in there never being more than one shower in use at a time, and really makes the second shower obsolete in a sense.

It's a simple enough fix though. Remove the occupiedShower[showerNumber] = false; statement from the occupyShower() function and rather put it in a new function leaveShower() that gets called after signaling that a shower can be picked by another swimmer.

Thanks for linking that Dining Philosopher's problem as well, it looks like an interesting read.

I have more assignments of similar nature coming up, and might post the as well as my solutions (in spoilers ) if anyone finds this to be interesting
Should it be the responsibility of the thread occupying the resource to signal the other threads the resource is available?

Wouldn't an independent queue with first in first out logic and some sort of interrupt to jump ahead in the queue be the way to go? Then the shower checks the queue when it's available.

Sorry only understand this from a 30,000 ft perspective so maybe that's what you're doing and I'm just too ignorant to understand the logic you've written here. Novice at this but learning to get better.
Programming homework and newbie help thread Quote
03-05-2016 , 05:16 PM
Quote:
Originally Posted by Mavoor
This is the solution that I came up with
Code:
Initialization of shared variables and semaphores:
	Semaphore sem_condition.init(2);
	Semaphore sem_me.init(1);
	boolean[] occupiedShower = { false, false }
	//
	//sem_condition is responsible for condition synchronization, and only allows two swimmers 
	//  to enter the shower area at the same time. If more than two swimmers try to enter the showers at                
	//  the same time, only the first two can enter, others have to wait in line.
	//sem_me is responsible for mutual exclusion, to make sure that two swimmers in the shower area won't try 
        //  to occupy the same shower simultaneously.
	//occupiedShower knows whether the showers are occupied or not.
	
Swimmer code:
Swimmer {
	enterShowerArea {
		//Swimmer tries to enter shower area, waits if it's full.
		sem_condition.wait(); 
		
		//Swimmer tries to pick a shower, waits if another swimmer is picking a shower.
		sem_me.wait();  
		
		//Swimmer checks if first shower (shower 0) is occupied.
		if(occupiedShower[0]){  
			//If it is occupied, then swimmer occupies second shower (shower 1).
			occupyShower(1);
		}
		else {
			//If first shower isn't occupied, then swimmer occupies it.
			occupyShower(0);
		}
		
		//Swimmer signals that he has picked a shower,if another swimmer was waiting then now he can pick.
		sem_me.signal();
		
		//Swimmer signals that he has left the shower area, if another swimmer was waiting now he can enter.
		sem_condition.signal();
	}
	
	occupyShower(int showerNumber){
		occupiedShower[showerNumber] = true;
		//…Wash whole body thoroughly with soap before big swimming competition….
		occupiedShower[showerNumber] = false;
	}
}
It was simple/obvious enough, just using another semaphore for mutual exclusion. I knew it was pretty simple, just had a brief lapse of thought
Sorry about the big indentation
Your code allows for two swimmers to simultaneously enter the same shower (shower 0 to be precise).
E.g. after initialisation, the first two swimmers enter the showers simultaneously. Both threads could execute occupyShower(0) at the very same time leading to a race condition over occupiedShower[0].

You should lock / unlock all shared resources (including shower 0 & 1), use the semaphore only to avoid having more than 2 swimmers attempting to access showers.
When swimmers passed the semaphore, make them attempt to access shower 0, then 1. Obviously always in the same order to avoid live locks.
Programming homework and newbie help thread Quote
03-06-2016 , 01:27 PM
Quote:
Originally Posted by just_grindin
Should it be the responsibility of the thread occupying the resource to signal the other threads the resource is available?

Wouldn't an independent queue with first in first out logic and some sort of interrupt to jump ahead in the queue be the way to go? Then the shower checks the queue when it's available.

Sorry only understand this from a 30,000 ft perspective so maybe that's what you're doing and I'm just too ignorant to understand the logic you've written here. Novice at this but learning to get better.
I'm novice at this as well, and not sure if I'm able to answer your questions satisfactory
But, I think it should be the threads responsibility to signal other threads, at least when we're using semaphores, this is their whole point i think. A running thread signals when he's terminating or leaving a critical section, so another thread can wake up and run or enter a critical section.

My solution assumes there's no queue but rather when a thread signals, simply an arbitrary thread from the pool of waiting threads gets woken up. There's no queue or FIFO structure, and i think that is fine because we can assume the swimmers don't care in which order they shower. If we had new swimmers entering continuously then we'd need some sort of queue structure to make sure no swimmer waits indefinitely, but since there are only a constant number of swimmers showering they simply wait until they get randomly picked, and we can still be sure that every swimmer gets to shower.

You probably could have the shower check the queue for waiting swimmers, but then you'd need a new thread for the shower I think? I'm not sure what the benefits of this would be

Quote:
Originally Posted by kazana
Your code allows for two swimmers to simultaneously enter the same shower (shower 0 to be precise).
E.g. after initialisation, the first two swimmers enter the showers simultaneously. Both threads could execute occupyShower(0) at the very same time leading to a race condition over occupiedShower[0].

You should lock / unlock all shared resources (including shower 0 & 1), use the semaphore only to avoid having more than 2 swimmers attempting to access showers.
When swimmers passed the semaphore, make them attempt to access shower 0, then 1. Obviously always in the same order to avoid live locks.
Hmm, are you sure? I was under the impression that the second semaphore takes care of mutual exclusion, ie keeping two swimmers from trying to enter the same shower simultaneously. As you can see if you check the code I have a semaphore sem_me that is initialized with 1, meaning only one process will be allowed to enter it at a time. All swimmers need to pass this semaphore before entering the critical section of trying to check/occupy showers. Could you check my code and see if this makes sense? Or maybe I'm missing something

Last edited by Mavoor; 03-06-2016 at 01:32 PM.
Programming homework and newbie help thread Quote
03-06-2016 , 03:43 PM
Quote:
Originally Posted by Mavoor
I'm novice at this as well, and not sure if I'm able to answer your questions satisfactory
But, I think it should be the threads responsibility to signal other threads, at least when we're using semaphores, this is their whole point i think. A running thread signals when he's terminating or leaving a critical section, so another thread can wake up and run or enter a critical section.
Thanks for the response. Just going to get some of my thoughts out below for you and others to comment on (or not ).

My impression was that the semaphore is a separate entity from the thread and is controlled by the OS. Threads interact with the semaphore by incrementing/decrementing the semaphore and other threads use that as as the indication that resources are available. From my understanding the threads don't interact with each other at all - they share information through the semaphore which is separate entity controlled by the OS.

Quote:
Originally Posted by Mavoor
My solution assumes there's no queue but rather when a thread signals, simply an arbitrary thread from the pool of waiting threads gets woken up. There's no queue or FIFO structure, and i think that is fine because we can assume the swimmers don't care in which order they shower. If we had new swimmers entering continuously then we'd need some sort of queue structure to make sure no swimmer waits indefinitely, but since there are only a constant number of swimmers showering they simply wait until they get randomly picked, and we can still be sure that every swimmer gets to shower.
That makes sense to me. I was thinking continuous swimmers.

Quote:
Originally Posted by Mavoor
You probably could have the shower check the queue for waiting swimmers, but then you'd need a new thread for the shower I think? I'm not sure what the benefits of this would be
Sorry my thoughts weren't well articulated on that. Some how I thought that the shower or the queue would need to handle the signaling to move swimmers from the queue as they are the resources shared by all swimmers. My impression of the philosophers problem was that there's no direct communication between threads/philosophers. The signaling is handled by resources they shared which would be managed by the OS.

I think now my understanding would be the semaphore acts as the signal. If the semaphore is >0 then resources are available and swimmers should move into the shower.

After that, it would require some sort of data structure that handles locking/unlocking showers to prevent multiple swimmers from entering the shower. To me this would be more like a switch than a semaphore and it should be associated with the shower not with the swimmer.

Again just my thoughts and would be interested in any feedback on ly logic.
Programming homework and newbie help thread Quote
03-06-2016 , 04:26 PM
Quote:
Originally Posted by Mavoor
Hmm, are you sure? I was under the impression that the second semaphore takes care of mutual exclusion, ie keeping two swimmers from trying to enter the same shower simultaneously. As you can see if you check the code I have a semaphore sem_me that is initialized with 1, meaning only one process will be allowed to enter it at a time. All swimmers need to pass this semaphore before entering the critical section of trying to check/occupy showers. Could you check my code and see if this makes sense? Or maybe I'm missing something
Yes, you are absolutely right. My bad.
Just not used to seeing semaphores being used for a single lock or locking critical sections.
Programming homework and newbie help thread Quote
03-06-2016 , 04:43 PM
Hey everyone, I'm new and trying to learn to code for the first time. Here I'm trying some basic line in https://jsfiddle.net/ Be cause when I drag my notepad.html into the browser he's not converting the code to a text page. Still have to figure this out.

Ok while writing this post I found the error in my code, I already like that new hobby. So can someone just explain me how can I work only with my notepad and not use confusing 3rd party like jsfiddle that separate HTML, CSS and JS.

Thank you.
Programming homework and newbie help thread Quote
03-06-2016 , 05:25 PM
Quote:
Originally Posted by LonelyBox
Hey everyone, I'm new and trying to learn to code for the first time. Here I'm trying some basic line in https://jsfiddle.net/ Be cause when I drag my notepad.html into the browser he's not converting the code to a text page. Still have to figure this out.

Ok while writing this post I found the error in my code, I already like that new hobby. So can someone just explain me how can I work only with my notepad and not use confusing 3rd party like jsfiddle that separate HTML, CSS and JS.

Thank you.
I wish I could edit/deleta that post
Programming homework and newbie help thread Quote
03-06-2016 , 06:11 PM
Quote:
Originally Posted by just_grindin
Thanks for the response. Just going to get some of my thoughts out below for you and others to comment on (or not ).

My impression was that the semaphore is a separate entity from the thread and is controlled by the OS. Threads interact with the semaphore by incrementing/decrementing the semaphore and other threads use that as as the indication that resources are available. From my understanding the threads don't interact with each other at all - they share information through the semaphore which is separate entity controlled by the OS.



That makes sense to me. I was thinking continuous swimmers.



Sorry my thoughts weren't well articulated on that. Some how I thought that the shower or the queue would need to handle the signaling to move swimmers from the queue as they are the resources shared by all swimmers. My impression of the philosophers problem was that there's no direct communication between threads/philosophers. The signaling is handled by resources they shared which would be managed by the OS.

I think now my understanding would be the semaphore acts as the signal. If the semaphore is >0 then resources are available and swimmers should move into the shower.

After that, it would require some sort of data structure that handles locking/unlocking showers to prevent multiple swimmers from entering the shower. To me this would be more like a switch than a semaphore and it should be associated with the shower not with the swimmer.

Again just my thoughts and would be interested in any feedback on ly logic.
First of all it is probably better to refer to the synchronization resource as a lock or mutex but yeah semaphores can be used for mutual exclusion. Not sure if the following answers your question but when a thread tries to acquire the lock and it isn't available, the thread will block and the OS will maintain a queue of its own thus synchronizing access to the resource which in this case is the shower.
Programming homework and newbie help thread Quote
03-06-2016 , 07:09 PM
Quote:
Originally Posted by adios
First of all it is probably better to refer to the synchronization resource as a lock or mutex but yeah semaphores can be used for mutual exclusion. Not sure if the following answers your question but when a thread tries to acquire the lock and it isn't available, the thread will block and the OS will maintain a queue of its own thus synchronizing access to the resource which in this case is the shower.
Thanks for the response.
Programming homework and newbie help thread Quote
03-06-2016 , 10:55 PM
Quote:
Originally Posted by LonelyBox
Hey everyone, I'm new and trying to learn to code for the first time. Here I'm trying some basic line in https://jsfiddle.net/ Be cause when I drag my notepad.html into the browser he's not converting the code to a text page. Still have to figure this out.

Ok while writing this post I found the error in my code, I already like that new hobby. So can someone just explain me how can I work only with my notepad and not use confusing 3rd party like jsfiddle that separate HTML, CSS and JS.

Thank you.
Are you making webpages or just practicing JavaScript code?

If the former, just throw all your code in a folder and use any editor like notepad++ or sublime 2.

If the latter, just go to codecademy labs website and cut out the middleman.
Programming homework and newbie help thread Quote
03-06-2016 , 11:12 PM
i created a carousel in a rails project using boot strap and was confused why i do not have to call the function with something like:

$('.carousel').carousel()

??

http://getbootstrap.com/javascript/#carousel

It just knows what to do because of special bootstrap classes? or is it because i have jquery-rails and bootstrap gems?
Programming homework and newbie help thread Quote
03-06-2016 , 11:15 PM
Bootstrap is a css framework. No functions in css that I know of. So yeah, just based off the classes afaik.
Programming homework and newbie help thread Quote
03-07-2016 , 12:11 AM
He linked to the JavaScript page on bootstrap.com. Scroll up and it appears to use jQuery.
Programming homework and newbie help thread Quote
03-07-2016 , 07:53 AM
Quote:
Originally Posted by just_grindin
Thanks for the response.
You are welcome.

Bonus question.

A caveat, when we consider these multi threaded problems where synchronization of shared resources is required to prevent things such as deadlock and race conditions typically we think of them in terms of single processor environments. However, multi core systems where OSs such as Linux and Windows do symmetric multiprocessing which basically means that simultaneous requests for resources can and do happen. How does this complicate the problem Mavoor presented?
Programming homework and newbie help thread Quote
03-07-2016 , 01:43 PM
Quote:
Originally Posted by adios
You are welcome.

Bonus question.

A caveat, when we consider these multi threaded problems where synchronization of shared resources is required to prevent things such as deadlock and race conditions typically we think of them in terms of single processor environments. However, multi core systems where OSs such as Linux and Windows do symmetric multiprocessing which basically means that simultaneous requests for resources can and do happen. How does this complicate the problem Mavoor presented?
I fell into a Wikipedia hole this morning and actually read all the links you have here prior to seeing your post, so I feel like I should be able to answer this, but I feel like I'm struggling to satisfy myself that I have the correct answer.

So my best guess right now is the additional issue of data coherence across multiple processors that share main memory. Thanks for pushing me to think about these things.
Programming homework and newbie help thread Quote
03-07-2016 , 04:56 PM
I got the following piece of code

Code:
public void remove(String titel){
		int i = find(titel);
		if (i >=0)
			{
			for (int j=i;j<anzAufnahmen-1;j++)
					arrayAufnahme[j]=arrayAufnahme[j+1];
					arrayAufnahme[anzAufnahmen-1] = null;
					anzAufnahmen--;
			}else
				throw new RuntimeException(msgTitel);
	}
It's a piece of code from one of my exercises in university, when I looked at it I was like "This does not work", but when I was testing it, it was working and really confused me.

Why does the for loop stop at the second line of code? This surely can't be proper codeetiquette right?

I for sure thought it would have to be written like this :

Code:
public void remove(String titel)
{
		int i = find(titel);
		if (i >=0)
			{
			for (int j=i;j<anzAufnahmen-1;j++)
                              {
					arrayAufnahme[j]=arrayAufnahme[j+1];
                               }
                               arrayAufnahme[anzAufnahmen-1] = null;
                               anzAufnahmen--;
			}else
				throw new RuntimeException(msgTitel);
}
edit:

For some reason it's all alligned don't know why.

Last edited by NiSash1337; 03-07-2016 at 05:18 PM.
Programming homework and newbie help thread Quote
03-07-2016 , 05:10 PM
Gotta use code tags to keep spacing
Programming homework and newbie help thread Quote
03-07-2016 , 05:17 PM
When an if statement, or a loop, etc, only has one line of code as it's body, the brackets are optional. It's generally considered bad style to do this. One reason is that you can fall prone to indentation throwing you off. Consider

Code:
for (int i=i; i<10; i++)
    doThing();
And then someone comes along and adds a line, without looking closely enough


Code:
for (int i=i; i<10; i++)
    doThing();
    doOtherThing();

That'll run doThing() 10 times, and doOtherThing() 1 time, because there are no curly braces.

When a 3rd guy comes along, he has no way of knowing whether the writer intended this and fudged the indent, or didn't intend this and fudged the braces.

Hint: a lot of times, programmers 1, 2 and 3 are all the same person.
Programming homework and newbie help thread Quote
03-07-2016 , 05:24 PM
Quote:
Originally Posted by Noodle Wazlib
Gotta use code tags to keep spacing
Ahh yeah thx totally forgot about this.

Thx Rusty.

Is this only a Java thing or is this common for other languages ?
Programming homework and newbie help thread Quote
03-07-2016 , 05:29 PM
Quote:
Originally Posted by NiSash1337
Ahh yeah thx totally forgot about this.

Thx Rusty.

Is this only a Java thing or is this common for other languages ?
C/C++ both do it. Perl also I think. Python uses indentation for flow control. Other languages vary a little.

Partly it's to let you do stuff like

if (true) doSomething();
instead of
if (true) { doSomething; }

which is a more reasonable use of leaving the braces off.
Programming homework and newbie help thread Quote
03-08-2016 , 12:10 AM
Quote:
Originally Posted by RustyBrooks
When an if statement, or a loop, etc, only has one line of code as it's body, the brackets are optional. It's generally considered bad style to do this.
You're right, it is generally considered bad style, but I think common wisdom has it backwards here. I prefer this rule: never use brackets for an if or for. And now you are forced to refactor so that their bodies are a single statement.

And once you're comfortable with that rule, it's just a small step to an even better one: never use if or for.
Programming homework and newbie help thread Quote
Programming homework and newbie help thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Programming homework and newbie help thread

      
m