LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-02-2013, 04:39 PM   #16
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,377

Rep: Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108

I solved a problem identical to that one (never-mind how many years ago) when I was still in high school and had consumed an entire (very illicit) pitcher of beer. And you have to ask the Internet?!
 
Old 05-02-2013, 05:05 PM   #17
Miranden
Member
 
Registered: May 2012
Posts: 188

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by sundialsvcs View Post
I solved a problem identical to that one (never-mind how many years ago) when I was still in high school and had consumed an entire (very illicit) pitcher of beer. And you have to ask the Internet?!
Oh really? I didn't realize people knew what bathrooms were in SE Tennessee.

Oh, and also, I skipped high school. I just wasn't born knowing what synchronization was and thought maybe someone here would be kind enough to help. Thankfully, I've seen one page full of decent people and only one jerk. Not bad, I'd say!
 
Old 05-02-2013, 10:21 PM   #18
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,454

Rep: Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842
Quote:
Originally Posted by Miranden View Post
Yes, you are right about that. Once they are in the queue, they can all go in together as soon as someone of the opposite gender comes out. I was talking about a situation where the women would walk up to the queue together. The way I was thinking of it, only one person of either gender is generated on each cycle.
Maybe only there are only k stalls in total?

Quote:
Yes, I see what you mean. That's another reason I just think there is something here I'm not getting. I'm thinking that if I read enough about threads it will make sense, but so far it hasn't happened.
It seems like the problem description is (still) missing something. It certainly looks like a metaphor for thread synchronization, where each person is a thread and a bathroom stall is a resource that needs to have access to it synchronized. However, if your program is in charge of the cycles that makes it a cooperative threading problem which really makes synchronizing so much easier that it feels like cheating...

And is your program also in charge of deciding what people want to use the bathroom? I would expect that you have to at least take some kind input indicating who wants the bathroom next, but nothing you've posted so far mentions anything like that. If there is no input you could just print a fixed string and the problem is "solved".
 
Old 05-03-2013, 12:15 AM   #19
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 556
Blog Entries: 2

Rep: Reputation: 71
Quote:
Originally Posted by Miranden View Post
This is not for homework - it is for entry to a computer science club.
Maybe you could post the original problem here.
 
Old 05-03-2013, 10:33 AM   #20
Miranden
Member
 
Registered: May 2012
Posts: 188

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by psionl0 View Post
Maybe you could post the original problem here.
The original I have is on a pdf file which contains names and private information of members. But here is the entire segment relating to the problem, copied and pasted exactly:

Quote:
Your two-part solution is due by email on Friday, May 03 by 11:59 pm. Here is the problem you must solve:

A dorm in a university wants to show off how progressive it is and ends its long standing practice of gender-segregated bathrooms on campus. However, as a concession to tradition, it makes a policy that when a woman is in the bathroom only other women may enter, but not men, and vice versa. On the door of every bathroom there will be a sign with a sliding marker that will indicate one of three possible states it is currently in:

-Empty
-Women Present
-Men Present

The project will have two parts:

1. Using pseudocode, outline the following procedures: woman_wants_to_enter, man_wants_to_enter, woman_leaves, man_leaves.

2. In C, write an application that implements the pseudocode and uses the above procedures.

You may use whatever counters and synchronization techniques you like.

**This problem is adapted from the book, Modern Operating Systems, by Tanenbaum.

Mandatory Guidelines:

-The program must have a queue where patrons will wait if they are unable to enter the bathroom. Everyone will stay in the bathroom for a set period of time.

-The program will execute for 15 or more cycles. (A cycle is defined as one iteration of a person entering a queue if unable to enter the bathroom, and/or entering the bathroom if eligible and/or exiting the bathroom.)

The program must display the following during execution:

-The number of the cycle
-the state of the bathroom (empty, or occupied by n men or n women)
-how many people are in the queue, and what their genders are.

The program can be fully automated (executes for a set number of cycles without intervention), or manually operated (it request input from the user through the console).

**This problem is designed to highlight the use of synchronization techniques in C.
That is all I have.
 
Old 05-03-2013, 11:23 AM   #21
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,454

Rep: Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842
Quote:
Originally Posted by psionl0 View Post
I got the impression from the problem that once the toilet was emptied, the entire queue could enter (since everybody is in the queue is the same sex). So it would just be a different cycle counter for each occupant depending on their sex.
...
Come to think of it, I don't see why you have to break it down into woman_wants_to_enter, man_wants_to_enter, woman_leaves, man_leaves procedures. The occupants of the toilet will be the opposite sex to those in the queue so all you need is a simple test like:
Code:
IF (arrival.sex = toilet.sex) OR (toilet.sex = nil) THEN
    enterToilet(arrival)
ELSE
    enterQueue(arrival)
ENDIF
The problem with this strategy is that a person could be stuck indefinitely in the queue as long as people of the opposite sex are going into the bathroom. See Resource starvation. A good solution would be first-come-first-serve.
 
Old 05-03-2013, 12:09 PM   #22
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 556
Blog Entries: 2

Rep: Reputation: 71
Quote:
Originally Posted by ntubski View Post
The problem with this strategy is that a person could be stuck indefinitely in the queue as long as people of the opposite sex are going into the bathroom.
Clearly not a very good policy and one a crowded nightclub would be ill advised to adopt.

It does highlight however the importance of the consultation phase of any programming project. A good programming consultant will spend time with the customer clarifying his needs and pointing out consequences like the one you mentioned. Prototyping so the customer can get a feel for what he is asking for is also a good idea.

Unfortunately, in most professions, the customer is regarded as a complete idiot and his input is not valued.
 
Old 05-03-2013, 12:57 PM   #23
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 1,527

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Personally I think there should be a high priority queue.
 
Old 05-03-2013, 08:34 PM   #24
Miranden
Member
 
Registered: May 2012
Posts: 188

Original Poster
Rep: Reputation: 19
Well, I got it finished and learned how to use mutexes. All my threads are now accessing the bathroom in a very orderly fashion. Thanks for the help.
 
Old 05-04-2013, 11:51 PM   #25
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 556
Blog Entries: 2

Rep: Reputation: 71
Quote:
Originally Posted by Miranden View Post
Well, I got it finished and learned how to use mutexes. All my threads are now accessing the bathroom in a very orderly fashion. Thanks for the help.
The way the problem is worded, I wouldn't have used anything more sophisticated than the sleep() function. I still don't see why you needed to model the toilet patrons with threads and mutexes but glad you learned so much.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to solve solve broken shell problem prasanth.george Red Hat 1 01-21-2011 09:48 AM
does anyone knows how to solve this problem nikhidet Linux - Software 1 10-14-2010 02:08 AM
could u solve my problem chandu.bezawada Linux - General 3 07-26-2007 06:11 AM
Who can help me to solve this problem? Annie0716 Programming 2 08-09-2004 07:59 PM
No one can solve my problem Sundance Linux - Newbie 5 12-08-2003 01:13 PM


All times are GMT -5. The time now is 11:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration